英文:
How to wait while replicas are caught up master
问题
有一个MongoDB集群(1个主节点和2个副本节点)
在更新大量记录时,使用了BulkWrite,需要在副本节点追赶上主节点后调用下一个BulkWrite,确保副本节点已经追赶上主节点以完成此请求。使用了go.mongodb.org/mongo-driver/mongo。
英文:
There is a mongodb cluster (1 master 2 replicas)
Updating records in a larger number and for this used BulkWrite, need to call next BulkWrite after replicas caught up master, need to make sure that the replicas have already caught up with the master for this request. Used go.mongodb.org/mongo-driver/mongo
答案1
得分: 3
写入传播可以通过writeconcern.WriteConcern
进行“控制”。
可以使用不同的writeconcern.Option
创建WriteConcern
。可以使用writeconcern.W()
函数创建一个writeconcern.Option
,该选项控制写入操作必须传播到的实例数量(或更具体地说,写入操作将等待来自给定数量实例的确认):
> func W(w int) Option
> W请求确认写入操作传播到指定数量的mongod实例。
因此,首先需要创建一个正确配置的writeconcern.WriteConcern
,以等待来自2个节点的确认(在您的情况下,您有1个主节点和2个副本):
wc := writeconcern.New(writeconcern.W(2))
现在,您必须选择此写入关注的“范围”:它可以应用于整个mongo.Client
,也可以应用于mongo.Database
或仅应用于mongo.Collection
。
创建客户端、获取数据库或只是集合,都有选项可以指定写入关注,所以这取决于您希望将其应用在哪里。
如果您只希望此写入关注对某些更新生效,只需将其应用于用于执行更新的mongo.Collection
即可:
var client *mongo.Client // 初始化/连接客户端
wc := writeconcern.New(writeconcern.W(2))
c := client.Database("<您的数据库名称>").
Collection("<您的集合名称>",
options.Collection().SetWriteConcern(wc))
现在,如果您使用此c
集合执行写入(更新),则将使用wc
写入关注,也就是说,每个写入操作将等待传播到2个实例的写入确认。
如果您将wc
写入关注应用于mongo.Client
,那么它将成为您使用该客户端进行的所有操作的默认写入关注;如果您将其应用于mongo.Database
,那么它将成为您使用该数据库进行的所有操作的默认写入关注。当然,默认值可以被覆盖,就像我们在上面的示例中将wc
应用于c
集合一样。
英文:
Write propagation can be "controlled" with a writeconcern.WriteConcern
.
WriteConcern
can be created using different writeconcern.Option
s. The writeconcern.W()
function can be used to create a writeconcern.Option
that controls how many instances writes must be progatated to (or more specifically the write operation will wait for acknowledgements from the given number of instances):
> func W(w int) Option
> W requests acknowledgement that write operations propagate to the specified number of mongod instances.
So first you need to create a writeconcern.WriteConcern
that is properly configured to wait for acknowledgements from 2 nodes in your case (you have 1 master and 2 replicas):
wc := writeconcern.New(writeconcern.W(2))
You now have to choose the scope of this write concern: it may be applied on the entire mongo.Client
, it may be applied on a mongo.Database
or just applied on a mongo.Collection
.
Creating a client, obtaining a database or just a collection all have options which allow specifying the write concern, so this is really up to you where you want it to be applied.
If you just want this write concern to have effect on certain updates, it's enough to apply it on mongo.Collection
which you use to perform the updates:
var client *mongo.Client // Initialize / connect client
wc := writeconcern.New(writeconcern.W(2))
c := client.Database("<your db name>").
Collection("<your collection name>",
options.Collection().SetWriteConcern(wc))
Now if you use this c
collection to perform the writes (updates), the wc
write concern will be used / applied, that is, each write operation will wait for acknowledgements of the writes propagated to 2 instances.
If you would apply the wc
write concert on the mongo.Client
, then that would be the default write concern for everything you do with that client, if you'd apply it on a mongo.Database
, then it would be the default for everything you do with that database. Of course the default can be overridden, just how we applied wc
on the c
collection in the above example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论