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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题