updates for MasterSlaveConnection
This commit is contained in:
parent
244ada075f
commit
e1e5252e6d
@ -37,11 +37,6 @@ class MasterSlaveConnection(object):
|
||||
connection pooling, etc. 'Connection' instances used as slaves should
|
||||
be created with the slave_okay option set to True.
|
||||
|
||||
If connection pooling is being used the connections should be created
|
||||
with "auto_start_request" mode set to False. All request functionality
|
||||
that is needed should be initiated by calling `start_request` on the
|
||||
`MasterSlaveConnection` instance.
|
||||
|
||||
Raises TypeError if `master` is not an instance of `Connection` or
|
||||
slaves is not a list of at least one `Connection` instances.
|
||||
|
||||
@ -93,7 +88,7 @@ class MasterSlaveConnection(object):
|
||||
# _connection_to_use is a hack that we need to include to make sure
|
||||
# that killcursor operations can be sent to the same instance on which
|
||||
# the cursor actually resides...
|
||||
def _send_message(self, operation, data, safe=False, _connection_to_use=None):
|
||||
def _send_message(self, message, safe=False, _connection_to_use=None):
|
||||
"""Say something to Mongo.
|
||||
|
||||
Sends a message on the Master connection. This is used for inserts,
|
||||
@ -108,13 +103,13 @@ class MasterSlaveConnection(object):
|
||||
- `safe`: perform a getLastError after sending the message
|
||||
"""
|
||||
if _connection_to_use is None or _connection_to_use == -1:
|
||||
return self.__master._send_message(operation, data, safe)
|
||||
return self.__slaves[_connection_to_use]._send_message(operation, data, safe)
|
||||
return self.__master._send_message(message, safe)
|
||||
return self.__slaves[_connection_to_use]._send_message(message, safe)
|
||||
|
||||
# _connection_to_use is a hack that we need to include to make sure
|
||||
# that getmore operations can be sent to the same instance on which
|
||||
# the cursor actually resides...
|
||||
def _send_message_with_response(self, operation, data,
|
||||
def _send_message_with_response(self, message,
|
||||
_sock=None, _connection_to_use=None,
|
||||
_must_use_master=False):
|
||||
"""Receive a message from Mongo.
|
||||
@ -127,13 +122,12 @@ class MasterSlaveConnection(object):
|
||||
"""
|
||||
if _connection_to_use is not None:
|
||||
if _connection_to_use == -1:
|
||||
return (-1, self.__master._send_message_with_response(operation,
|
||||
data,
|
||||
return (-1, self.__master._send_message_with_response(message,
|
||||
_sock))
|
||||
else:
|
||||
return (_connection_to_use,
|
||||
self.__slaves[_connection_to_use]
|
||||
._send_message_with_response(operation, data, _sock))
|
||||
._send_message_with_response(message, _sock))
|
||||
|
||||
# for now just load-balance randomly among slaves only...
|
||||
connection_id = random.randrange(0, len(self.__slaves))
|
||||
@ -142,23 +136,21 @@ class MasterSlaveConnection(object):
|
||||
# master instance. any queries in a request must be sent to the
|
||||
# master since that is where writes go.
|
||||
if _must_use_master or self.__in_request or connection_id == -1:
|
||||
return (-1, self.__master._send_message_with_response(operation,
|
||||
data, _sock))
|
||||
return (-1, self.__master._send_message_with_response(message,
|
||||
_sock))
|
||||
|
||||
return (connection_id,
|
||||
self.__slaves[connection_id]._send_message_with_response(operation,
|
||||
data,
|
||||
self.__slaves[connection_id]._send_message_with_response(message,
|
||||
_sock))
|
||||
|
||||
def start_request(self):
|
||||
"""Start a "request".
|
||||
|
||||
See documentation for `Connection.start_request`. Note that all
|
||||
operations performed within a request will be sent using the Master
|
||||
connection.
|
||||
Start a sequence of operations in which order matters. Note
|
||||
that all operations performed within a request will be sent
|
||||
using the Master connection.
|
||||
"""
|
||||
self.__in_request = True
|
||||
self.__master.start_request()
|
||||
|
||||
def end_request(self):
|
||||
"""End the current "request".
|
||||
|
||||
@ -133,20 +133,6 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
self.connection.end_request()
|
||||
self.failIf(count)
|
||||
|
||||
def test_insert_find_one_no_slaves(self):
|
||||
if self.slaves:
|
||||
raise SkipTest()
|
||||
count = 0
|
||||
for i in range(100):
|
||||
self.db.test.remove({})
|
||||
self.db.test.insert({"x": i})
|
||||
try:
|
||||
if i != self.db.test.find_one()["x"]:
|
||||
count += 1
|
||||
except:
|
||||
count += 1
|
||||
self.failIf(count)
|
||||
|
||||
# This was failing because commands were being sent to the slaves
|
||||
def test_create_collection(self):
|
||||
self.connection.drop_database('pymongo_test')
|
||||
@ -167,8 +153,6 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
# NOTE this test is non-deterministic, but I expect
|
||||
# some failures unless the db is pulling instantaneously...
|
||||
def test_insert_find_one_with_slaves(self):
|
||||
if not self.slaves:
|
||||
raise SkipTest()
|
||||
count = 0
|
||||
for i in range(100):
|
||||
self.db.test.remove({})
|
||||
@ -183,8 +167,6 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
# NOTE this test is non-deterministic, but hopefully we pause long enough
|
||||
# for the slaves to pull...
|
||||
def test_insert_find_one_with_pause(self):
|
||||
if not self.slaves:
|
||||
raise SkipTest()
|
||||
count = 0
|
||||
|
||||
self.db.test.remove({})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user