【发布时间】:2017-12-25 10:10:14
【问题描述】:
问题处理此类错误的最佳方法是什么?说后重试,最多 30 秒。 5倍?
设置 我在 AWS ECS 上有一个 PRIMARY + 两个 SECONDARY 设置,并且没有一个盒子被关闭。
从异常消息来看,Mongo 似乎在说“我没有 PRIMARY,我试图从 SECONDARY 获取数据,但它们也已关闭”。但是,没有一个 Mongo 服务器出现故障。我有另一个进程正在运行,它的默认 read_preference 为 SECONDARY,并且一直运行良好。
例外
is_auth = db.authenticate("myid","mypass")
File "/usr/local/lib/python2.7/dist-packages/pymongo/database.py", line
720, in authenticate
read_preference=read_pref)["nonce"]
File "/usr/local/lib/python2.7/dist-packages/pymongo/database.py", line
390, in command
result = self["$cmd"].find_one(command, **extra_opts)
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py",
line 598, in find_one
for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line
814, in next
if len(self.__data) or self._refresh():
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line
763, in _refresh
self.__uuid_subtype))
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line
700, in __send_message
**kwargs)
File
"/usr/local/lib/python2.7/dist-packages/pymongo/mongo_replica_set_client.py
", line 1210, in _send_message_with_response
raise AutoReconnect(msg, errors)
AutoReconnect: No replica set members available for query with
ReadPreference PRIMARY_PREFERRED
代码
connection = MongoReplicaSetClient(, replicaSet='myReplica-2', use_greenlets = True)
db = "my_real_db"
db.read_preference = ReadPreference.PRIMARY
is_auth = db.authenticate("myid", "mypass") # ERROR at this line
版本 MongoDB:2.2.0 和 PyMongo:2.4.2
【问题讨论】:
-
您对消息的含义是正确的。
MongoReplicaSetClient实际上对authenticate()使用PRIMARY_PREFERRED模式,无论您在其上配置了什么阅读偏好,因此配置应该无关紧要。我看到你设置了use_greenlets--你也打电话给gevent.monkey.patch_socket()吗? -
@A.JesseJiryuDavis 感谢您的来信。是的,我在调用上面的代码之前调用了 gevent.monkey.patch_socket()。
-
这可能只是一个网络故障,在 EC2 中并非闻所未闻,它位于您的 Python 程序和与调用 authenticate() 的副本集之间。但在我尝试回答您的问题之前,我想确保没有错误。你能复制这种行为吗?
-
@A.JesseJiryuDavis 这个问题每天至少发生一次......虽然我不知道是什么原因造成的。正如您在那台机器上指出的那样,它可能只是 EC2 的一个小问题,但没有明确的知道方法。
-
总是在 authenticate() 中吗?您的应用程序是否经常调用 authenticate()?除了 authenticate() 之外的其他函数是否也会引发 AutoReconnect?除了 read_preference 之外,显示问题的应用程序和不显示问题的应用程序之间还有什么不同?
标签: mongodb python-2.7 pymongo