2012-02-26 70 views
3

看起來好像與pymongo,連接總是嘗試讀小學,當它下跌時,提出了一個套接字錯誤,直到新的選舉過程完成。replicaset(+ pymongo)從奴隸讀取時的主跌

鑑於replicset的目的之一是平衡讀取負載,這似乎是一個主要缺陷,除非我在這裏錯過了一個關鍵概念。

我已經提供了slave_ok thingy,但只要沒有主可用,沒有讀取,更不用說寫入了。

我已經在端口8910,8911和8912上啓動了3個mongod實例,並且一次又一次地把它們取下來,當最後一個留下時,無法讀取它,即使你讓mongo(cli)讓它讀取。

版本在使用中:

mongodb: 2.0.2 
pymongo: 2.1.1 

pymongo控制檯輸出

>>> collection = Connection("localhost:8910, localhost:8911, localhost:8912", slave_okay=True).testdb['TEST'] 
>>> len(list(collection.find())) 
0 
>>> collection.insert({"a": 1}) 
ObjectId('4f4a491bb9efb72ec8000045') 
>>> len(list(collection.find())) 
1 

取下來1 3個實例(主之一),然後的...

>>> len(list(collection.find())) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 703, in next 
    if len(self.__data) or self._refresh(): 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 666, in _refresh 
    self.__uuid_subtype)) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 616, in __send_message 
    **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 880, in _send_message_with_response 
    sock = self.__socket() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 686, in __socket 
    sock, from_pool = self.__pool.get_socket(host, port) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 165, in get_socket 
    self.sock = (pid, self.connect(host, port)) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 127, in connect 
    s.connect((host, port)) 
    File "/usr/lib/python2.7/socket.py", line 224, in meth 
    return getattr(self._sock,name)(*args) 
socket.error: [Errno 111] Connection refused 
>>> 

一個新的主已當選,然後...

>>> len(list(collection.find())) 
1 

取下二審開出3,然後...

>>> collection.insert({"c": 3}) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/collection.py", line 312, in insert 
    continue_on_error, self.__uuid_subtype), safe) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 811, in _send_message 
    sock = self.__socket() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 674, in __socket 
    host, port = self.__find_node() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 659, in __find_node 
    raise AutoReconnect(', '.join(errors)) 
pymongo.errors.AutoReconnect: could not connect to localhost:8911: [Errno -5] No address associated with hostname, could not connect to localhost:8911: [Errno 111] Connection refused, could not connect to localhost:8912: [Errno 111] Connection refused, could not connect to localhost:8912: [Errno -5] No address associated with hostname, localhost:8910 is not primary or master 
>>> 
>>> 
>>> collection.insert({"c": 3}) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/collection.py", line 312, in insert 
    continue_on_error, self.__uuid_subtype), safe) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 811, in _send_message 
    sock = self.__socket() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 674, in __socket 
    host, port = self.__find_node() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 659, in __find_node 
    raise AutoReconnect(', '.join(errors)) 
pymongo.errors.AutoReconnect: could not connect to localhost:8911: [Errno -5] No address associated with hostname, could not connect to localhost:8911: [Errno 111] Connection refused, could not connect to localhost:8912: [Errno 111] Connection refused, could not connect to localhost:8912: [Errno -5] No address associated with hostname, localhost:8910 is not primary or master 
>>> 
>>> 
>>> len(list(collection.find())) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 703, in next 
    if len(self.__data) or self._refresh(): 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 666, in _refresh 
    self.__uuid_subtype)) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/cursor.py", line 616, in __send_message 
    **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 880, in _send_message_with_response 
    sock = self.__socket() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 674, in __socket 
    host, port = self.__find_node() 
    File "/usr/local/lib/python2.7/dist-packages/pymongo-2.1.1-py2.7-linux-x86_64.egg/pymongo/connection.py", line 659, in __find_node 
    raise AutoReconnect(', '.join(errors)) 
pymongo.errors.AutoReconnect: could not connect to localhost:8911: [Errno -5] No 
address associated with hostname, could not connect to localhost:8911: [Errno 111] 
Connection refused, could not connect to localhost:8912: [Errno 111] Connection refused, 
could not connect to localhost:8912: [Errno -5] No address associated with hostname, 
localhost:8910 is not primary or master 
>>> 

蒙戈(CLI)輸出

SECONDARY> rs.status() 
{ 
     "set" : "myset", 
     "date" : ISODate("2012-02-26T15:09:49Z"), 
     "myState" : 2, 
     "members" : [ 
       { 
         "_id" : 0, 
         "name" : "localhost:8910", 
         "health" : 0, 
         "state" : 8, 
         "stateStr" : "(not reachable/healthy)", 
         "uptime" : 0, 
         "optime" : { 
           "t" : 1330268443000, 
           "i" : 1 
         }, 
         "optimeDate" : ISODate("2012-02-26T15:00:43Z"), 
         "lastHeartbeat" : ISODate("2012-02-26T15:09:37Z"), 
         "pingMs" : 0, 
         "errmsg" : "socket exception" 
       }, 
       { 
         "_id" : 1, 
         "name" : "localhost:8911", 
         "health" : 1, 
         "state" : 2, 
         "stateStr" : "SECONDARY", 
         "optime" : { 
           "t" : 1330268443000, 
           "i" : 1 
         }, 
         "optimeDate" : ISODate("2012-02-26T15:00:43Z"), 
         "self" : true 
       }, 
       { 
         "_id" : 2, 
         "name" : "localhost:8912", 
         "health" : 0, 
         "state" : 8, 
         "stateStr" : "(not reachable/healthy)", 
         "uptime" : 0, 
         "optime" : { 
           "t" : 1330268443000, 
           "i" : 1 
         }, 
         "optimeDate" : ISODate("2012-02-26T15:00:43Z"), 
         "lastHeartbeat" : ISODate("2012-02-26T15:09:37Z"), 
         "pingMs" : 0, 
         "errmsg" : "socket exception" 
       } 
     ], 
     "ok" : 1 
} 
SECONDARY> 
SECONDARY> 
SECONDARY> db.TEST.find().count() 
54 
SECONDARY> db.TEST.insert({eeee:23232323}) 
not master 
SECONDARY> 
SECONDARY> db.TEST.find().count() 
54 
+0

您是否嘗試過使用ReplicaSetConnection類型,而不是普通連接類型的?它出現在最近的PyMongo發行版中,似乎修復了將PyMongo驅動程序與複製集合一起使用的一些(儘管不是全部)麻煩。 – mattbornski 2012-02-26 18:53:14

+0

感謝您的提示,但我觀察到與ReplicaSetConnection非常相同的問題。 – 2012-02-27 03:49:17

回答

4

有兩個因素來解決這個問題,ReplicaSetConnectionReadPreference.SECONDARY

ReplicaSetConnection("localhost:8910,localhost:8911,localhost8912", 
     replicaSet='myset', read_prefererence=ReadPreference.SECONDARY) 
+0

但是,如果可用,此連接將不會使用主連接。 看來PyMongo不支持[primaryPrefered讀取偏好模式](http://docs.mongodb.org/manual/applications/replication/#primaryPrefered) – 2012-08-14 08:44:54