Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_primary_failure(self):
c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name)
yield c.open()
self.assertTrue(c.secondaries)
primary = c.primary
secondaries = c.secondaries
killed = ha_tools.kill_primary()
self.assertTrue(bool(len(killed)))
yield self.pause(1)
# Wait for new primary to step up, and for MotorReplicaSetClient
# to detect it.
for _ in range(60):
if c.primary != primary and c.secondaries != secondaries:
break
yield self.pause(1)
else:
self.fail("New primary not detected")
def test_stepdown_triggers_refresh(self):
c_find_one = yield motor.MotorReplicaSetClient(
self.seed, replicaSet=self.name).open()
# We've started the primary and one secondary
primary = ha_tools.get_primary()
secondary = ha_tools.get_secondaries()[0]
self.assertEqual(
one(c_find_one.secondaries), _partition_node(secondary))
ha_tools.stepdown_primary()
# Make sure the stepdown completes
yield self.pause(1)
# Trigger a refresh
with self.assertRaises(AutoReconnect):
yield c_find_one.test.test.find_one()
def test_alive(self):
primary = ha_tools.get_primary()
secondary = ha_tools.get_random_secondary()
primary_cx = yield motor.MotorClient(primary).open()
secondary_cx = yield motor.MotorClient(secondary).open()
rsc = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name)
yield rsc.open()
try:
self.assertTrue((yield primary_cx.alive()))
self.assertTrue((yield secondary_cx.alive()))
self.assertTrue((yield rsc.alive()))
ha_tools.kill_primary()
yield self.pause(2)
self.assertFalse((yield primary_cx.alive()))
self.assertTrue((yield secondary_cx.alive()))
self.assertFalse((yield rsc.alive()))
ha_tools.kill_members([secondary], 2)
yield self.pause(2)
def test_read_with_failover(self):
c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name)
yield c.open()
self.assertTrue(c.secondaries)
db = c.motor_test
w = len(c.secondaries) + 1
db.test.remove({}, w=w)
# Force replication
yield db.test.insert([{'foo': i} for i in range(10)], w=w)
self.assertEqual(10, (yield db.test.count()))
db.read_preference = SECONDARY
cursor = db.test.find().batch_size(5)
yield cursor.fetch_next
self.assertEqual(5, cursor.delegate._Cursor__retrieved)
for i in range(5):
cursor.next_object()
def test_monitor_removes_recovering_member(self):
self.c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name)
yield self.c.open()
secondaries = ha_tools.get_secondaries()
for mode in SECONDARY, SECONDARY_PREFERRED:
partitioned_secondaries = [_partition_node(s) for s in secondaries]
yield assert_read_from_all(
self, self.c, partitioned_secondaries, mode)
secondary, recovering_secondary = secondaries
ha_tools.set_maintenance(recovering_secondary, True)
yield self.pause(2 * MONITOR_INTERVAL)
for mode in SECONDARY, SECONDARY_PREFERRED:
# Don't read from recovering member
yield assert_read_from(
self, self.c, _partition_node(secondary), mode)
def test_ship_of_theseus(self):
c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name)
yield c.open()
db = c.motor_test
w = len(c.secondaries) + 1
db.test.insert({}, w=w)
primary = ha_tools.get_primary()
secondary1 = ha_tools.get_random_secondary()
ha_tools.add_member()
ha_tools.add_member()
ha_tools.add_member()
# Wait for new members to join
for _ in range(120):
if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4:
break
def test_recovering_member_triggers_refresh(self):
# To test that find_one() and count() trigger immediate refreshes,
# we'll create a separate client for each
self.c_find_one, self.c_count = yield [
motor.MotorReplicaSetClient(
self.seed, replicaSet=self.name, read_preference=SECONDARY
).open() for _ in range(2)]
# We've started the primary and one secondary
primary = ha_tools.get_primary()
secondary = ha_tools.get_secondaries()[0]
# Pre-condition: just make sure they all connected OK
for c in self.c_find_one, self.c_count:
self.assertEqual(one(c.secondaries), _partition_node(secondary))
ha_tools.set_maintenance(secondary, True)
# Trigger a refresh in various ways
with self.assertRaises(AutoReconnect):
yield self.c_find_one.test.test.find_one()
- `replica_set(optional)`: If true it will use
:class:`~motor.MotorReplicaSetClient` instead of
:class:`~motor.MotorClient` to create a new connection.
'''
if db_name is not None and not isinstance(db_name, str):
raise TypeError("Argument 'db_name' should be str type.")
if connection_name is None:
connection_name = cls.DEFAULT_CONNECTION_NAME
if not isinstance(connection_name, str):
raise TypeError("Argument 'connection_name' should be str type.")
if replica_set:
motor_client = motor.MotorReplicaSetClient
pymongo_client = pymongo.MongoReplicaSetClient
else:
motor_client = motor.MotorClient
pymongo_client = pymongo.MongoClient
motor_connection = motor_client(*args, **kwargs)
pymongo_connection = pymongo_client(*args, **kwargs)
cls.disconnect(connection_name)
cls._connections.insert(
0, {connection_name: (motor_connection, pymongo_connection)})
cls._default_connection = connection_name
cls._default_db = db_name
def get_connection(alias=DEFAULT_CONNECTION_NAME, db=None):
global _connections
global _default_dbs
if alias not in _connections:
conn_settings = _connection_settings[alias].copy()
db = conn_settings.pop('name', None)
connection_class = MotorClient
if 'replicaSet' in conn_settings:
connection_class = MotorReplicaSetClient
conn_settings['hosts_or_uri'] = conn_settings.pop('host', None)
# Discard port since it can't be used on MongoReplicaSetClient
conn_settings.pop('port', None)
# Discard replicaSet if not base string
if not isinstance(conn_settings['replicaSet'], six.string_types):
conn_settings.pop('replicaSet', None)
try:
_connections[alias] = connection_class(**conn_settings)
except Exception:
exc_info = sys.exc_info()
err = ConnectionError("Cannot connect to database %s :\n%s" % (alias, exc_info[1]))
raise six.reraise(ConnectionError, err, exc_info[2])
def open_db():
global db, fs
if config.database_rs:
client = motor.MotorReplicaSetClient(config.database_uri,read_preference=ReadPreference.NEAREST, replicaSet=config.database_rs)
else:
client = motor.MotorClient(config.database_uri,read_preference=ReadPreference.NEAREST)
IOLoop.current().run_sync(client.open)
db = client[config.database]
fs = client[config.database_fs]