简体   繁体   English

pymongo:使用MongoReplicaSetClient的优势?

[英]pymongo: Advantage of using MongoReplicaSetClient?

It seems that both MongoClient and MongoReplicaSetClient can connect to mongo replica sets. 似乎MongoClientMongoReplicaSetClient都可以连接到mongo副本集。 In fact, their documentation pages are nearly identical - same options, same methods, etc - except that the latter's constructor requires me to specify a replicaSet. 实际上,他们的文档页面几乎相同 - 相同的选项,相同的方法等 - 除了后者的构造函数要求我指定replicaSet。

In both cases, we may specify a read preference. 在这两种情况下,我们都可以指定读取首选项。 In both cases, we must handle the AutoReconnect exception if a stepdown occurs. 在这两种情况下,如果发生降级,我们必须处理AutoReconnect异常。

So my questions are: 所以我的问题是:

  1. Why would one use one versus the other, since one can perform the exact same operations with both? 为什么一个人使用一个而不是另一个,因为一个人可以执行完全相同的操作?

  2. Both can perform secondary reads, correct? 两者都可以执行二次读取,对吗? The documentation says that the advantage of a ReplicaSetClient is that we can do secondary reads, but clearly they are supported in both. 文档说ReplicaSetClient的优点是我们可以进行二次读取,但显然它们都受支持。

  3. The documentation says that the ReplicaSetClient features "replica set health monitoring." 文档说ReplicaSetClient具有“副本集健康监控”功能。 What exactly does that mean? 这到底是什么意思呢? Are there new methods I can invoke which tell me about a replset's health that I cannot otherwise do with MongoClient? 是否有我可以调用的新方法告诉我有关我不能用MongoClient做的replset的健康状况?

  4. In theory a MongoReplicaSetClient will connect to all members of the replset, rather than just one. 理论上 ,MongoReplicaSetClient将连接到replset的所有成员,而不仅仅是一个。 This is false: you may munge or omit any of the servers in the connection string, and both MongoClient and MongoReplicaSetClient are still able to connect. 这是错误的:您可能会删除连接字符串中的任何服务器,MongoClient和MongoReplicaSetClient仍然可以连接。 Am I missing something? 我错过了什么吗?

This is was a confusing API choice that we regret in PyMongo 2.x. 这是一个令人困惑的API选择,我们在PyMongo 2.x中感到遗憾。 We will merge all the client classes into MongoClient in PyMongo 3, in April 2015: 我们将在2015年4月将所有客户端类合并到PyMongo 3中的MongoClient:

http://emptysqua.re/blog/good-idea-at-the-time-pymongo-mongoreplicasetclient/ http://emptysqua.re/blog/good-idea-at-the-time-pymongo-mongoreplicasetclient/

Meanwhile: 与此同时:

  1. Use MongoReplicaSetClient when you plan to connect to a whole replica set. 计划连接到整个副本集时,请使用MongoReplicaSetClient。 MongoClient only connects to one member. MongoClient仅连接到一个成员。
  2. A single MongoReplicaSetClient can be used to perform primary or secondary reads, as well as more sophisticated decision-making with read preferences, see my blog post on the subject . 单个MongoReplicaSetClient可用于执行主要或次要读取,以及使用读取首选项的更复杂的决策,请参阅我关于该主题的博客文章 A MongoClient will connect to one member of the replica set (the primary) and always read from it, unless you make a direct connection to a secondary using MongoClient, in which case it will always read from that secondary. MongoClient将连接到副本集的一个成员(主要)并始终从中读取,除非您使用MongoClient直接连接到辅助节点,在这种情况下,它将始终从该辅助节点读取。
  3. MongoReplicaSetClient monitors the set's health with a background thread that periodically checks on all the members. MongoReplicaSetClient使用后台线程监视集合的运行状况,后台线程定期检查所有成员。 The client tracks whether members are up, it tracks their ping times, and it notices when a member is added. 客户端跟踪成员是否已启动,它是否跟踪其ping时间,并在添加成员时发出通知。 This will reduce the number of exceptions you see on a flaky network or when the replica set's configuration changes, and it allows the client to correctly implement read preferences. 这将减少您在片状网络上看到的异常数量,或者副本集的配置发生变化时,它允许客户端正确实现读取首选项。
  4. A MongoReplicaSetClient does in fact connect to all members, whereas a MongoClient only connects to one member. MongoReplicaSetClient实际上连接到所有成员,而MongoClient只连接到一个成员。 MongoReplicaSetClient tries to connect to each member listed in the connection string; MongoReplicaSetClient尝试连接到连接字符串中列出的每个成员; as soon as it connects to one it asks that member for a list of all other members. 一旦连接到一个,它就会要求该成员提供所有其他成员的列表。 From this point forward it ignores your connection string and uses the list it got from the member it connected to. 从这一点开始,它会忽略您的连接字符串并使用它从连接到的成员获得的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM