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 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题