douye4051 2019-04-16 10:44
浏览 528
已采纳

将与Mongo Go官方驱动程序mongo-go-driver(mgo中的session.SetMode)的会话的一致性规则设置为单调

We're switching to the official driver, away from the deprecated mgov2

In mgo we set the session mode to be monotonic like this:

myMongoSession.SetMode(mgo.Monotonic, true)

My understanding of doing so is to ensure: "If a process reads the value of a data item x, any successive read operation on x by that process will always return that same value or a more recent value."

This is important for us

How would I do this using the official driver? I can't find a reference to doing so

  • 写回答

1条回答 默认 最新

  • du8442 2019-04-17 08:05
    关注

    My understanding of doing so is to ensure: "If a process reads the value of a data item x, any successive read operation on x by that process will always return that same value or a more recent value."

    Yes, but not always. Let's look into the code comment for Monotonic mode session.go#L73:

    // Monotonic mode is specific to mgo, and is same as SecondaryPreferred before first write. 
    // Same as Primary after first write.
    Monotonic Mode = 1
    

    According to godoc.org/github.com/globalsign/mgo for SetMode():

    In the Monotonic consistency mode reads may not be entirely up-to-date, but they will always see the history of changes moving forward, the data read will be consistent across sequential queries in the same session, and modifications made within the session will be observed in following queries (read-your-writes).

    In practice, the Monotonic mode is obtained by performing initial reads on a unique connection to an arbitrary secondary, if one is available, and once the first write happens, the session connection is switched over to the primary server. This manages to distribute some of the reading load with secondaries, while maintaining some useful guarantees.

    For most of the cases this is true, however there could be instances where SecondaryPreferred may choose different secondaries that could have different Oplog times.

    How would I do this using the official driver?

    Monotonic mode is a term specific for mgo, and does not exist for MongoDB Go driver. This has been superseded by new features such as causal consistency and/or multi-document transactions.

    mongo-go-driver is more verbose, but also to provide more control over read preferences, read concern, and write concern

    Example of causal consistency:

    opts := options.Client().ApplyURI(mongoURI).SetReadPreference(readpref.Secondary()).SetReadConcern(readconcern.Majority()).SetWriteConcern(writeconcern.New(writeconcern.WMajority()))
    client, err := mongo.NewClient(opts)
    if err != nil {
        panic(err)
    }
    

    For transactions example see Transactions in Applications (Choose Go language on the tab)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料