Read Concern 读取关注"linearizable"

"linearizable"

The query returns data that reflects all successful majority-acknowledged writes that completed prior to the start of the read operation. 查询返回的数据反映了在读取操作开始之前完成的所有成功的多数确认写入。The query may wait for concurrently executing writes to propagate to a majority of replica set members before returning results.查询可以在返回结果之前等待并发执行的写入传播到大多数副本集成员。

If a majority of your replica set members crash and restart after the read operation, documents returned by the read operation are durable if writeConcernMajorityJournalDefault is set to the default state of true.如果大多数副本集成员在读取操作后崩溃并重新启动,则如果writeConcernMajorityJournalDefault设置为默认状态true,则读取操作返回的文档是持久的。

With writeConcernMajorityJournalDefault set to false, MongoDB does not wait for w: "majority" writes to be written to the on-disk journal before acknowledging the writes. writeConcernMajorityJournalDefault设置为false时,MongoDB在确认写入之前不会等待w: "majority"写入写入到磁盘日志。As such, "majority" write operations could possibly roll back in the event of a transient loss (e.g. crash and restart) of a majority of nodes in a given replica set.因此,"majority"写入操作可能会在给定副本集中的大多数节点发生瞬时丢失(如崩溃和重新启动)时回滚。

You can specify linearizable read concern for read operations on the primary only.您可以仅为primary设备上的读取操作指定可线性化的读取关注点。

Linearizable read concern guarantees only apply if read operations specify a query filter that uniquely identifies a single document.仅当读取操作指定了唯一标识单个文档的查询筛选器时,线性化读取关注点保证才适用。

Tip提示

Always use maxTimeMS with linearizable read concern in case a majority of data bearing members are unavailable. 在大多数数据承载成员不可用的情况下,始终使用具有线性化读取关注点的maxTimeMS。maxTimeMS ensures that the operation does not block indefinitely and instead ensures that the operation returns an error if the read concern cannot be fulfilled.确保操作不会无限期阻塞,相反,如果无法满足读取要求,则确保操作返回错误。

Causally Consistent Sessions因果一致会话

Read concern "linearizable" is unavailable for use with causally consistent sessions.读关注点"linearizable"不可用于因果一致的会话。

Aggregation Restriction聚合限制

You cannot use the $out or the $merge stage in conjunction with read concern "linearizable". 不能将$out$merge阶段与读取关注点"linearizable"结合使用。That is, if you specify "linearizable" read concern for db.collection.aggregate(), you cannot include either stages in the pipeline.也就是说,如果为db.collection.aggregate()指定"linearizable"读取关注点,则不能在管道中包含任何一个阶段。

Real Time Order实时订单

Combined with "majority" write concern, "linearizable" read concern enables multiple threads to perform reads and writes on a single document as if a single thread performed these operations in real time; that is, the corresponding schedule for these reads and writes is considered linearizable."majority"写关注点相结合,"linearizable"读关注点使多个线程能够对单个文档执行读写操作,就像单个线程实时执行这些操作一样;也就是说,这些读写的相应调度被认为是可线性化的。

Read Your Own Writes读你自己写的

Changed in version 3.6.在版本3.6中更改

Starting in MongoDB 3.6, you can use causally consistent sessions to read your own writes, if the writes request acknowledgement.从MongoDB 3.6开始,如果写入请求确认,您可以使用因果一致的会话来读取自己的写入。

Prior to MongoDB 3.6, in order to read your own writes you must issue your write operation with { w: "majority" } write concern, and then issue your read operation with primary read preference, and either "majority" or "linearizable" read concern.在MongoDB 3.6之前,为了读取您自己的写入操作,您必须使用{ w: "majority" }写入关注点发出写入操作,然后使用primary读取首选项发出读取操作,以及"majority""linearizable"读取关注点。

Performance Comparisons性能比较

Unlike "majority", "linearizable" read concern confirms with secondary members that the read operation is reading from a primary that is capable of confirming writes with { w: "majority" } write concern. "majority"不同,"linearizable"”读关注点通过二级成员确认读操作是从能够通过{ w: "majority" }写关注点确认写的主成员读取。[1] As such, reads with linearizable read concern may be significantly slower than reads with "majority" or "local" read concerns.因此,具有线性化读取关注点的读取可能比具有"majority""local"读取关注点时的读取慢得多。

Always use maxTimeMS with linearizable read concern in case a majority of data bearing members are unavailable. 在大多数数据承载成员不可用的情况下,始终使用具有线性化读取关注点的maxTimeMSmaxTimeMS ensures that the operation does not block indefinitely and instead ensures that the operation returns an error if the read concern cannot be fulfilled.确保操作不会无限期阻塞,相反,如果无法满足读取要求,则确保操作返回错误。

For example:例如:

db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000)
db.runCommand( {
     find: "restaurants",
     filter: { _id: 5 },
     readConcern: { level: "linearizable" },
     maxTimeMS: 10000
} )
[1]In some circumstances, two nodes in a replica set may transiently believe that they are the primary, but at most, one of them will be able to complete writes with { w: "majority" } write concern. 某些情况下,副本集中的两个节点可能会暂时认为它们是主节点,但最多其中一个节点将能够完成{ w: "majority" }写操作。The node that can complete { w: "majority" } writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to a network partition. 能够完成{ w: "majority" }写入的节点是当前主节点,而另一个节点是尚未识别其降级的前主节点,通常是由于网络分区When this occurs, clients that connect to the former primary may observe stale data despite having requested read preference primary, and new writes to the former primary will eventually roll back.发生这种情况时,连接到前primary的客户端可能会观察到陈旧数据,尽管已请求primary的读取首选项,而对前primary进行的新写入最终将回滚。
←  Read Concern "majority"Read Concern "snapshot" →