def test_auth_during_failover(self, done): loop = IOLoop.instance() c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) c.open_sync() db = c.pymongo_ha_auth res = yield motor.Op(db.authenticate, 'user', 'userpass') self.assertTrue(res) yield motor.Op(db.foo.insert, {'foo': 'bar'}, w=3, wtimeout=1000) yield motor.Op(db.logout) yield AssertRaises(OperationFailure, db.foo.find_one) primary = '%s:%d' % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone yield gen.Task(loop.add_timeout, time.time() + 2 * MONITOR_INTERVAL) # Make sure we can still authenticate res = yield motor.Op(db.authenticate, 'user', 'userpass') self.assertTrue(res) # And still query. db.read_preference = PRIMARY_PREFERRED res = yield motor.Op(db.foo.find_one) self.assertEqual('bar', res['foo']) c.close() done()
def test_alive(self, done): primary = ha_tools.get_primary() secondary = ha_tools.get_random_secondary() primary_cx = motor.MotorClient(primary).open_sync() secondary_cx = motor.MotorClient(secondary).open_sync() rsc = motor.MotorReplicaSetClient( self.seed, replicaSet=self.name).open_sync() loop = IOLoop.instance() try: yield AssertTrue(primary_cx.alive) yield AssertTrue(secondary_cx.alive) yield AssertTrue(rsc.alive) ha_tools.kill_primary() yield gen.Task(loop.add_timeout, time.time() + 0.5) yield AssertFalse(primary_cx.alive) yield AssertTrue(secondary_cx.alive) # Sometimes KeyError: https://jira.mongodb.org/browse/PYTHON-467 yield AssertFalse(rsc.alive) ha_tools.kill_members([secondary], 2) yield gen.Task(loop.add_timeout, time.time() + 0.5) yield AssertFalse(primary_cx.alive) yield AssertFalse(secondary_cx.alive) yield AssertFalse(rsc.alive) finally: rsc.close() done()
def test_passive_and_hidden(self): self.c = ReplicaSetConnection( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) db = self.c.pymongo_test w = len(self.c.secondaries) + 1 db.test.remove({}, safe=True, w=w) db.test.insert({'foo': 'bar'}, safe=True, w=w) passives = ha_tools.get_passives() passives = [_partition_node(member) for member in passives] hidden = ha_tools.get_hidden_members() hidden = [_partition_node(member) for member in hidden] self.assertEqual(self.c.secondaries, set(passives)) for mode in ( ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED ): db.read_preference = mode for _ in xrange(10): cursor = db.test.find() cursor.next() self.assertTrue(cursor._Cursor__connection_id in passives) self.assertTrue(cursor._Cursor__connection_id not in hidden) ha_tools.kill_members(ha_tools.get_passives(), 2) sleep(2 * MONITOR_INTERVAL) db.read_preference = ReadPreference.SECONDARY_PREFERRED for _ in xrange(10): cursor = db.test.find() cursor.next() self.assertEqual(cursor._Cursor__connection_id, self.c.primary)
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(0.5) 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(0.5) self.assertFalse((yield primary_cx.alive())) self.assertFalse((yield secondary_cx.alive())) self.assertFalse((yield rsc.alive())) finally: rsc.close()
def test_alive(self): primary = ha_tools.get_primary() secondary = ha_tools.get_random_secondary() primary_cx = MongoClient(primary, use_greenlets=use_greenlets) secondary_cx = MongoClient(secondary, use_greenlets=use_greenlets) rsc = MongoReplicaSetClient(self.seed, replicaSet=self.name, use_greenlets=use_greenlets) try: self.assertTrue(primary_cx.alive()) self.assertTrue(secondary_cx.alive()) self.assertTrue(rsc.alive()) ha_tools.kill_primary() time.sleep(0.5) self.assertFalse(primary_cx.alive()) self.assertTrue(secondary_cx.alive()) self.assertFalse(rsc.alive()) ha_tools.kill_members([secondary], 2) time.sleep(0.5) self.assertFalse(primary_cx.alive()) self.assertFalse(secondary_cx.alive()) self.assertFalse(rsc.alive()) finally: rsc.close()
def test_auth_during_failover(self): c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield c.open() db = c.pymongo_ha_auth res = yield db.authenticate('user', 'userpass') self.assertTrue(res) yield db.foo.insert({'foo': 'bar'}, w=3, wtimeout=1000) yield db.logout() with assert_raises(OperationFailure): yield db.foo.find_one() primary = '%s:%d' % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone yield self.pause(2 * MONITOR_INTERVAL) # Make sure we can still authenticate res = yield db.authenticate('user', 'userpass') self.assertTrue(res) # And still query. db.read_preference = PRIMARY_PREFERRED res = yield db.foo.find_one() self.assertEqual('bar', res['foo']) c.close()
def test_alive(self): primary = ha_tools.get_primary() secondary = ha_tools.get_random_secondary() primary_cx = connected( MongoClient( primary, serverSelectionTimeoutMS=self.server_selection_timeout)), secondary_cx = connected( MongoClient( secondary, serverSelectionTimeoutMS=self.server_selection_timeout)) rsc = connected( MongoClient( self.seed, replicaSet=self.name, serverSelectionTimeoutMS=self.server_selection_timeout)) self.assertTrue(primary_cx.alive()) self.assertTrue(secondary_cx.alive()) self.assertTrue(rsc.alive()) ha_tools.kill_primary() time.sleep(0.5) self.assertFalse(primary_cx.alive()) self.assertTrue(secondary_cx.alive()) self.assertFalse(rsc.alive()) ha_tools.kill_members([secondary], 2) time.sleep(0.5) self.assertFalse(primary_cx.alive()) self.assertFalse(secondary_cx.alive()) self.assertFalse(rsc.alive())
def test_passive_and_hidden(self): self.c = ReplicaSetConnection(self.seed, replicaSet=self.name, use_greenlets=use_greenlets) db = self.c.pymongo_test w = len(self.c.secondaries) + 1 db.test.remove({}, safe=True, w=w) db.test.insert({'foo': 'bar'}, safe=True, w=w) passives = ha_tools.get_passives() passives = [_partition_node(member) for member in passives] hidden = ha_tools.get_hidden_members() hidden = [_partition_node(member) for member in hidden] self.assertEqual(self.c.secondaries, set(passives)) for mode in (ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED): db.read_preference = mode for _ in xrange(10): cursor = db.test.find() cursor.next() self.assertTrue(cursor._Cursor__connection_id in passives) self.assertTrue(cursor._Cursor__connection_id not in hidden) ha_tools.kill_members(ha_tools.get_passives(), 2) sleep(2 * MONITOR_INTERVAL) db.read_preference = ReadPreference.SECONDARY_PREFERRED for _ in xrange(10): cursor = db.test.find() cursor.next() self.assertEqual(cursor._Cursor__connection_id, self.c.primary)
def test_alive(self): primary = ha_tools.get_primary() secondary = ha_tools.get_random_secondary() primary_cx = MongoClient(primary, use_greenlets=use_greenlets) secondary_cx = MongoClient(secondary, use_greenlets=use_greenlets) rsc = MongoReplicaSetClient( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) try: self.assertTrue(primary_cx.alive()) self.assertTrue(secondary_cx.alive()) self.assertTrue(rsc.alive()) ha_tools.kill_primary() time.sleep(0.5) self.assertFalse(primary_cx.alive()) self.assertTrue(secondary_cx.alive()) self.assertFalse(rsc.alive()) ha_tools.kill_members([secondary], 2) time.sleep(0.5) self.assertFalse(primary_cx.alive()) self.assertFalse(secondary_cx.alive()) self.assertFalse(rsc.alive()) finally: rsc.close()
def test_ship_of_theseus(self): c = MongoReplicaSetClient( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) db = c.pymongo_test db.test.insert({}, w=len(c.secondaries) + 1) find_one = db.test.find_one primary = ha_tools.get_primary() secondary1 = ha_tools.get_random_secondary() new_hosts = [ha_tools.add_member() for _ in range(3)] # Wait for new members to join. for _ in xrange(120): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4: break sleep(1) else: self.fail("New secondaries didn't join") ha_tools.kill_members([primary, secondary1], 9) # Wait for primary. for _ in xrange(30): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2: break sleep(1) else: self.fail("No failover") sleep(2 * MONITOR_INTERVAL) # No error. find_one() find_one(read_preference=SECONDARY) # All members down. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members(new_hosts) # Should be able to reconnect to set even though original seed # list is useless. Use SECONDARY so we don't have to wait for # the election, merely for the client to detect members are up. sleep(2 * MONITOR_INTERVAL) find_one(read_preference=SECONDARY)
def test_passive_and_hidden(self): self.c = MongoReplicaSetClient( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) passives = ha_tools.get_passives() passives = partition_nodes(passives) self.assertEqual(self.c.secondaries, set(passives)) for mode in SECONDARY, SECONDARY_PREFERRED: utils.assertReadFromAll(self, self.c, passives, mode) ha_tools.kill_members(ha_tools.get_passives(), 2) sleep(2 * MONITOR_INTERVAL) utils.assertReadFrom(self, self.c, self.c.primary, SECONDARY_PREFERRED)
def test_ship_of_theseus(self, done): loop = IOLoop.instance() c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) c.open_sync() db = c.pymongo_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 xrange(120): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4: break yield gen.Task(loop.add_timeout, time.time() + 1) else: self.fail("New secondaries didn't join") ha_tools.kill_members([primary, secondary1], 9) # Wait for primary for _ in xrange(30): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2: break yield gen.Task(loop.add_timeout, time.time() + 1) else: self.fail("No failover") # Ensure monitor picks up new members yield gen.Task(loop.add_timeout, time.time() + 2 * MONITOR_INTERVAL) try: yield motor.Op(db.test.find_one) except AutoReconnect: # Might take one try to reconnect yield gen.Task(loop.add_timeout, time.time() + 1) # No error yield motor.Op(db.test.find_one) yield motor.Op(db.test.find_one, read_preference=SECONDARY) done()
def test_passive_and_hidden(self): self.c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield self.c.open() passives = ha_tools.get_passives() passives = [_partition_node(member) for member in passives] self.assertEqual(self.c.secondaries, set(passives)) for mode in SECONDARY, SECONDARY_PREFERRED: yield assert_read_from_all(self, self.c, passives, mode) ha_tools.kill_members(ha_tools.get_passives(), 2) yield self.pause(2 * MONITOR_INTERVAL) yield assert_read_from(self, self.c, self.c.primary, SECONDARY_PREFERRED)
def test_passive_and_hidden(self): self.c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield self.c.open() passives = ha_tools.get_passives() passives = [_partition_node(member) for member in passives] self.assertEqual(self.c.secondaries, set(passives)) for mode in SECONDARY, SECONDARY_PREFERRED: yield assert_read_from_all(self, self.c, passives, mode) ha_tools.kill_members(ha_tools.get_passives(), 2) yield self.pause(2 * MONITOR_INTERVAL) yield assert_read_from( self, self.c, self.c.primary, SECONDARY_PREFERRED)
def test_passive_and_hidden(self): self.c = MongoClient( self.seed, replicaSet=self.name, serverSelectionTimeoutMS=self.server_selection_timeout) passives = ha_tools.get_passives() passives = partition_nodes(passives) self.assertEqual(self.c.secondaries, set(passives)) for mode in SECONDARY, SECONDARY_PREFERRED: utils.assertReadFromAll(self, self.c, passives, mode) ha_tools.kill_members(ha_tools.get_passives(), 2) time.sleep(2 * self.heartbeat_frequency) utils.assertReadFrom(self, self.c, self.c.primary, SECONDARY_PREFERRED)
def test_passive_and_hidden(self): loop = IOLoop.instance() self.c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) self.c passives = ha_tools.get_passives() passives = [_partition_node(member) for member in passives] self.assertEqual(self.c.secondaries, set(passives)) for mode in SECONDARY, SECONDARY_PREFERRED: yield motor.Op(assertReadFromAll, self, self.c, passives, mode) ha_tools.kill_members(ha_tools.get_passives(), 2) yield gen.Task(loop.add_timeout, time.time() + 2 * MONITOR_INTERVAL) yield motor.Op(assertReadFrom, self, self.c, self.c.primary, SECONDARY_PREFERRED)
def test_auth_during_failover(self): self.assertTrue(self.db.authenticate("user", "userpass")) self.assertTrue(self.db.foo.insert({"foo": "bar"}, safe=True, w=3, wtimeout=1000)) self.db.logout() self.assertRaises(OperationFailure, self.db.foo.find_one) primary = "%s:%d" % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone sleep(2 * MONITOR_INTERVAL) # Make sure we can still authenticate self.assertTrue(self.db.authenticate("user", "userpass")) # And still query. self.db.read_preference = ReadPreference.PRIMARY_PREFERRED self.assertEqual("bar", self.db.foo.find_one()["foo"])
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 xrange(120): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4: break yield self.pause(1) else: self.fail("New secondaries didn't join") ha_tools.kill_members([primary, secondary1], 9) # Wait for primary for _ in xrange(30): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2: break yield self.pause(1) else: self.fail("No failover") # Ensure monitor picks up new members yield self.pause(2 * MONITOR_INTERVAL) try: yield db.test.find_one() except AutoReconnect: # Might take one try to reconnect yield self.pause(1) # No error yield db.test.find_one() yield db.test.find_one(read_preference=SECONDARY)
def test_auth_during_failover(self): self.assertTrue(self.db.authenticate('user', 'userpass')) self.assertTrue( self.db.foo.insert({'foo': 'bar'}, safe=True, w=3, wtimeout=1000)) self.db.logout() self.assertRaises(OperationFailure, self.db.foo.find_one) primary = '%s:%d' % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone sleep(2 * MONITOR_INTERVAL) # Make sure we can still authenticate self.assertTrue(self.db.authenticate('user', 'userpass')) # And still query. self.db.read_preference = PRIMARY_PREFERRED self.assertEqual('bar', self.db.foo.find_one()['foo'])
def test_auth_during_failover(self): self.assertTrue(self.db.authenticate('user', 'userpass')) self.assertTrue(self.db.foo.insert({'foo': 'bar'}, safe=True, w=3, wtimeout=3000)) self.db.logout() self.assertRaises(OperationFailure, self.db.foo.find_one) primary = '%s:%d' % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone sleep(2 * MONITOR_INTERVAL) # Make sure we can still authenticate self.assertTrue(self.db.authenticate('user', 'userpass')) # And still query. self.db.read_preference = PRIMARY_PREFERRED self.assertEqual('bar', self.db.foo.find_one()['foo'])
def test_auth_during_failover(self): self.assertTrue(self.db.authenticate('user', 'userpass')) db = self.db.client.get_database( self.db.name, write_concern=WriteConcern(w=3, wtimeout=3000)) self.assertTrue(db.foo.insert_one({'foo': 'bar'})) self.db.logout() self.assertRaises(OperationFailure, self.db.foo.find_one) primary = self.c.primary ha_tools.kill_members(['%s:%d' % primary], 2) # Let monitor notice primary's gone time.sleep(2 * self.heartbeat_frequency) self.assertFalse(primary == self.c.primary) # Make sure we can still authenticate self.assertTrue(self.db.authenticate('user', 'userpass')) # And still query. self.db.read_preference = PRIMARY_PREFERRED self.assertEqual('bar', self.db.foo.find_one()['foo'])
def test_read_preference(self): # This is long, but we put all the tests in one function to save time # on setUp, which takes about 30 seconds to bring up a replica set. # We pass through four states: # # 1. A primary and two secondaries # 2. Primary down # 3. Primary up, one secondary down # 4. Primary up, all secondaries down # # For each state, we verify the behavior of PRIMARY, # PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, and NEAREST c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield c.open() @gen.coroutine def read_from_which_host( rsc, mode, tag_sets=None, latency=15, ): db = rsc.motor_test db.read_preference = mode if isinstance(tag_sets, dict): tag_sets = [tag_sets] db.tag_sets = tag_sets or [{}] db.secondary_acceptable_latency_ms = latency cursor = db.test.find() try: yield cursor.fetch_next raise gen.Return(cursor.delegate._Cursor__connection_id) except AutoReconnect: raise gen.Return(None) @gen.coroutine def assert_read_from(member, *args, **kwargs): for _ in range(10): used = yield read_from_which_host(c, *args, **kwargs) self.assertEqual(member, used) @gen.coroutine def assert_read_from_all(members, *args, **kwargs): members = set(members) all_used = set() for _ in range(100): used = yield read_from_which_host(c, *args, **kwargs) all_used.add(used) if members == all_used: raise gen.Return() # Success # This will fail self.assertEqual(members, all_used) def unpartition_node(node): host, port = node return '%s:%s' % (host, port) primary = self.primary secondary = self.secondary other_secondary = self.other_secondary bad_tag = {'bad': 'tag'} # 1. THREE MEMBERS UP ------------------------------------------------- # PRIMARY yield assert_read_from(primary, PRIMARY) # PRIMARY_PREFERRED # Trivial: mode and tags both match yield assert_read_from(primary, PRIMARY_PREFERRED, self.primary_dc) # Secondary matches but not primary, choose primary yield assert_read_from(primary, PRIMARY_PREFERRED, self.secondary_dc) # Chooses primary, ignoring tag sets yield assert_read_from(primary, PRIMARY_PREFERRED, self.primary_dc) # Chooses primary, ignoring tag sets yield assert_read_from(primary, PRIMARY_PREFERRED, bad_tag) yield assert_read_from(primary, PRIMARY_PREFERRED, [bad_tag, {}]) # SECONDARY yield assert_read_from_all( [secondary, other_secondary], SECONDARY, latency=9999999) # SECONDARY_PREFERRED yield assert_read_from_all( [secondary, other_secondary], SECONDARY_PREFERRED, latency=9999999) # Multiple tags yield assert_read_from( secondary, SECONDARY_PREFERRED, self.secondary_tags) # Fall back to primary if it's the only one matching the tags yield assert_read_from( primary, SECONDARY_PREFERRED, {'name': 'primary'}) # No matching secondaries yield assert_read_from(primary, SECONDARY_PREFERRED, bad_tag) # Fall back from non-matching tag set to matching set yield assert_read_from_all( [secondary, other_secondary], SECONDARY_PREFERRED, [bad_tag, {}], latency=9999999) yield assert_read_from( other_secondary, SECONDARY_PREFERRED, [bad_tag, {'dc': 'ny'}]) # NEAREST self.clear_ping_times() yield assert_read_from_all( [primary, secondary, other_secondary], NEAREST, latency=9999999) yield assert_read_from_all( [primary, other_secondary], NEAREST, [bad_tag, {'dc': 'ny'}], latency=9999999) self.set_ping_time(primary, 0) self.set_ping_time(secondary, .03) # 30 milliseconds. self.set_ping_time(other_secondary, 10) # Nearest member, no tags yield assert_read_from(primary, NEAREST) # Tags override nearness yield assert_read_from(primary, NEAREST, {'name': 'primary'}) yield assert_read_from(secondary, NEAREST, self.secondary_dc) # Make secondary fast self.set_ping_time(primary, .03) # 30 milliseconds. self.set_ping_time(secondary, 0) yield assert_read_from(secondary, NEAREST) # Other secondary fast self.set_ping_time(secondary, 10) self.set_ping_time(other_secondary, 0) yield assert_read_from(other_secondary, NEAREST) # High secondaryAcceptableLatencyMS, should read from all members yield assert_read_from_all( [primary, secondary, other_secondary], NEAREST, latency=9999999) self.clear_ping_times() yield assert_read_from_all( [primary, other_secondary], NEAREST, [{'dc': 'ny'}], latency=9999999) # 2. PRIMARY DOWN ----------------------------------------------------- killed = ha_tools.kill_primary() # Let monitor notice primary's gone yield self.pause(2 * MONITOR_INTERVAL) # PRIMARY yield assert_read_from(None, PRIMARY) # PRIMARY_PREFERRED # No primary, choose matching secondary yield assert_read_from_all( [secondary, other_secondary], PRIMARY_PREFERRED, latency=9999999) yield assert_read_from( secondary, PRIMARY_PREFERRED, {'name': 'secondary'}) # No primary or matching secondary yield assert_read_from(None, PRIMARY_PREFERRED, bad_tag) # SECONDARY yield assert_read_from_all( [secondary, other_secondary], SECONDARY, latency=9999999) # Only primary matches yield assert_read_from(None, SECONDARY, {'name': 'primary'}) # No matching secondaries yield assert_read_from(None, SECONDARY, bad_tag) # SECONDARY_PREFERRED yield assert_read_from_all( [secondary, other_secondary], SECONDARY_PREFERRED, latency=9999999) # Mode and tags both match yield assert_read_from( secondary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST self.clear_ping_times() yield assert_read_from_all( [secondary, other_secondary], NEAREST, latency=9999999) # 3. PRIMARY UP, ONE SECONDARY DOWN ----------------------------------- ha_tools.restart_members([killed]) for _ in range(30): if ha_tools.get_primary(): break yield self.pause(1) else: self.fail("Primary didn't come back up") ha_tools.kill_members([unpartition_node(secondary)], 2) self.assertTrue(pymongo.MongoClient( unpartition_node(primary), slave_okay=True ).admin.command('ismaster')['ismaster']) yield self.pause(2 * MONITOR_INTERVAL) # PRIMARY yield assert_read_from(primary, PRIMARY) # PRIMARY_PREFERRED yield assert_read_from(primary, PRIMARY_PREFERRED) # SECONDARY yield assert_read_from(other_secondary, SECONDARY) yield assert_read_from( other_secondary, SECONDARY, self.other_secondary_dc) # Only the down secondary matches yield assert_read_from(None, SECONDARY, {'name': 'secondary'}) # SECONDARY_PREFERRED yield assert_read_from(other_secondary, SECONDARY_PREFERRED) yield assert_read_from( other_secondary, SECONDARY_PREFERRED, self.other_secondary_dc) # The secondary matching the tag is down, use primary yield assert_read_from( primary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST yield assert_read_from_all( [primary, other_secondary], NEAREST, latency=9999999) yield assert_read_from( other_secondary, NEAREST, {'name': 'other_secondary'}) yield assert_read_from(primary, NEAREST, {'name': 'primary'}) # 4. PRIMARY UP, ALL SECONDARIES DOWN --------------------------------- ha_tools.kill_members([unpartition_node(other_secondary)], 2) self.assertTrue(pymongo.MongoClient( unpartition_node(primary), slave_okay=True ).admin.command('ismaster')['ismaster']) # PRIMARY yield assert_read_from(primary, PRIMARY) # PRIMARY_PREFERRED yield assert_read_from(primary, PRIMARY_PREFERRED) yield assert_read_from(primary, PRIMARY_PREFERRED, self.secondary_dc) # SECONDARY yield assert_read_from(None, SECONDARY) yield assert_read_from(None, SECONDARY, self.other_secondary_dc) yield assert_read_from(None, SECONDARY, {'dc': 'ny'}) # SECONDARY_PREFERRED yield assert_read_from(primary, SECONDARY_PREFERRED) yield assert_read_from(primary, SECONDARY_PREFERRED, self.secondary_dc) yield assert_read_from( primary, SECONDARY_PREFERRED, {'name': 'secondary'}) yield assert_read_from(primary, SECONDARY_PREFERRED, {'dc': 'ny'}) # NEAREST yield assert_read_from(primary, NEAREST) yield assert_read_from(None, NEAREST, self.secondary_dc) yield assert_read_from(None, NEAREST, {'name': 'secondary'}) # Even if primary's slow, still read from it self.set_ping_time(primary, 100) yield assert_read_from(primary, NEAREST) yield assert_read_from(None, NEAREST, self.secondary_dc) self.clear_ping_times()
def test_ship_of_theseus(self): c = MongoClient( self.seed, replicaSet=self.name, serverSelectionTimeoutMS=self.server_selection_timeout) db = c.get_database( "pymongo_test", write_concern=WriteConcern(w=len(c.secondaries) + 1)) db.test.insert_one({}) find_one = db.test.find_one primary = ha_tools.get_primary() secondary1 = ha_tools.get_random_secondary() new_hosts = [] for i in range(3): new_hosts.append(ha_tools.add_member()) # RS closes all connections after reconfig. for j in xrange(30): try: if ha_tools.get_primary(): break except (ConnectionFailure, OperationFailure): pass time.sleep(1) else: self.fail("Couldn't recover from reconfig") # Wait for new members to join. for _ in xrange(120): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4: break time.sleep(1) else: self.fail("New secondaries didn't join") ha_tools.kill_members([primary, secondary1], 9) time.sleep(5) wait_until(lambda: (ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2), "fail over", timeout=30) time.sleep(2 * self.heartbeat_frequency) # No error. find_one() find_one(read_preference=SECONDARY) # All members down. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members(new_hosts) # Should be able to reconnect to set even though original seed # list is useless. Use SECONDARY so we don't have to wait for # the election, merely for the client to detect members are up. time.sleep(2 * self.heartbeat_frequency) find_one(read_preference=SECONDARY) # Kill new members and switch back to original two members. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members([primary, secondary1]) # Wait for members to figure out they're secondaries. wait_until(lambda: len(ha_tools.get_secondaries()) == 2, "detect two secondaries", timeout=30) # Should be able to reconnect to set again. time.sleep(2 * self.heartbeat_frequency) find_one(read_preference=SECONDARY)
def test_read_preference(self): # We pass through four states: # # 1. A primary and two secondaries # 2. Primary down # 3. Primary up, one secondary down # 4. Primary up, all secondaries down # # For each state, we verify the behavior of PRIMARY, # PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, and NEAREST c = MongoClient( self.seed, replicaSet=self.name, serverSelectionTimeoutMS=self.server_selection_timeout) wait_until(lambda: c.primary, "discover primary") wait_until(lambda: len(c.secondaries) == 2, "discover secondaries") def assertReadFrom(member, *args, **kwargs): utils.assertReadFrom(self, c, member, *args, **kwargs) def assertReadFromAll(members, *args, **kwargs): utils.assertReadFromAll(self, c, members, *args, **kwargs) def unpartition_node(node): host, port = node return '%s:%s' % (host, port) # To make the code terser, copy hosts into local scope primary = self.primary secondary = self.secondary other_secondary = self.other_secondary bad_tag = {'bad': 'tag'} # 1. THREE MEMBERS UP ------------------------------------------------- # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED # Trivial: mode and tags both match assertReadFrom(primary, PRIMARY_PREFERRED, self.primary_dc) # Secondary matches but not primary, choose primary assertReadFrom(primary, PRIMARY_PREFERRED, self.secondary_dc) # Chooses primary, ignoring tag sets assertReadFrom(primary, PRIMARY_PREFERRED, self.primary_dc) # Chooses primary, ignoring tag sets assertReadFrom(primary, PRIMARY_PREFERRED, bad_tag) assertReadFrom(primary, PRIMARY_PREFERRED, [bad_tag, {}]) # SECONDARY assertReadFromAll([secondary, other_secondary], SECONDARY) # SECONDARY_PREFERRED assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED) # Multiple tags assertReadFrom(secondary, SECONDARY_PREFERRED, self.secondary_tags) # Fall back to primary if it's the only one matching the tags assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'primary'}) # No matching secondaries assertReadFrom(primary, SECONDARY_PREFERRED, bad_tag) # Fall back from non-matching tag set to matching set assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED, [bad_tag, {}]) assertReadFrom(other_secondary, SECONDARY_PREFERRED, [bad_tag, {'dc': 'ny'}]) # NEAREST self.clear_ping_times() assertReadFromAll([primary, secondary, other_secondary], NEAREST) assertReadFromAll([primary, other_secondary], NEAREST, [bad_tag, {'dc': 'ny'}]) self.set_ping_time(primary, 0) self.set_ping_time(secondary, .03) # 30 ms self.set_ping_time(other_secondary, 10) # Nearest member, no tags assertReadFrom(primary, NEAREST) # Tags override nearness assertReadFrom(primary, NEAREST, {'name': 'primary'}) assertReadFrom(secondary, NEAREST, self.secondary_dc) # Make secondary fast self.set_ping_time(primary, .03) # 30 ms self.set_ping_time(secondary, 0) assertReadFrom(secondary, NEAREST) # Other secondary fast self.set_ping_time(secondary, 10) self.set_ping_time(other_secondary, 0) assertReadFrom(other_secondary, NEAREST) self.clear_ping_times() assertReadFromAll([primary, other_secondary], NEAREST, [{'dc': 'ny'}]) # 2. PRIMARY DOWN ----------------------------------------------------- killed = ha_tools.kill_primary() # Let monitor notice primary's gone time.sleep(2 * self.heartbeat_frequency) # PRIMARY assertReadFrom(None, PRIMARY) # PRIMARY_PREFERRED # No primary, choose matching secondary assertReadFromAll([secondary, other_secondary], PRIMARY_PREFERRED) assertReadFrom(secondary, PRIMARY_PREFERRED, {'name': 'secondary'}) # No primary or matching secondary assertReadFrom(None, PRIMARY_PREFERRED, bad_tag) # SECONDARY assertReadFromAll([secondary, other_secondary], SECONDARY) # Only primary matches assertReadFrom(None, SECONDARY, {'name': 'primary'}) # No matching secondaries assertReadFrom(None, SECONDARY, bad_tag) # SECONDARY_PREFERRED assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED) # Mode and tags both match assertReadFrom(secondary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST self.clear_ping_times() assertReadFromAll([secondary, other_secondary], NEAREST) # 3. PRIMARY UP, ONE SECONDARY DOWN ----------------------------------- ha_tools.restart_members([killed]) ha_tools.wait_for_primary() ha_tools.kill_members([unpartition_node(secondary)], 2) time.sleep(5) ha_tools.wait_for_primary() time.sleep(2 * self.heartbeat_frequency) # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED assertReadFrom(primary, PRIMARY_PREFERRED) # SECONDARY assertReadFrom(other_secondary, SECONDARY) assertReadFrom(other_secondary, SECONDARY, self.other_secondary_dc) # Only the down secondary matches assertReadFrom(None, SECONDARY, {'name': 'secondary'}) # SECONDARY_PREFERRED assertReadFrom(other_secondary, SECONDARY_PREFERRED) assertReadFrom( other_secondary, SECONDARY_PREFERRED, self.other_secondary_dc) # The secondary matching the tag is down, use primary assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST assertReadFromAll([primary, other_secondary], NEAREST) assertReadFrom(other_secondary, NEAREST, {'name': 'other_secondary'}) assertReadFrom(primary, NEAREST, {'name': 'primary'}) # 4. PRIMARY UP, ALL SECONDARIES DOWN --------------------------------- ha_tools.kill_members([unpartition_node(other_secondary)], 2) # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED assertReadFrom(primary, PRIMARY_PREFERRED) assertReadFrom(primary, PRIMARY_PREFERRED, self.secondary_dc) # SECONDARY assertReadFrom(None, SECONDARY) assertReadFrom(None, SECONDARY, self.other_secondary_dc) assertReadFrom(None, SECONDARY, {'dc': 'ny'}) # SECONDARY_PREFERRED assertReadFrom(primary, SECONDARY_PREFERRED) assertReadFrom(primary, SECONDARY_PREFERRED, self.secondary_dc) assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) assertReadFrom(primary, SECONDARY_PREFERRED, {'dc': 'ny'}) # NEAREST assertReadFrom(primary, NEAREST) assertReadFrom(None, NEAREST, self.secondary_dc) assertReadFrom(None, NEAREST, {'name': 'secondary'}) # Even if primary's slow, still read from it self.set_ping_time(primary, 100) assertReadFrom(primary, NEAREST) assertReadFrom(None, NEAREST, self.secondary_dc) self.clear_ping_times()
def test_ship_of_theseus(self): c = MongoReplicaSetClient( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) db = c.pymongo_test db.test.insert({}, w=len(c.secondaries) + 1) find_one = db.test.find_one primary = ha_tools.get_primary() secondary1 = ha_tools.get_random_secondary() new_hosts = [ha_tools.add_member() for _ in range(3)] # Wait for new members to join. for _ in xrange(120): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4: break sleep(1) else: self.fail("New secondaries didn't join") ha_tools.kill_members([primary, secondary1], 9) # Wait for primary. for _ in xrange(30): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2: break sleep(1) else: self.fail("No failover") sleep(2 * MONITOR_INTERVAL) # No error. find_one() find_one(read_preference=SECONDARY) # All members down. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members(new_hosts) # Should be able to reconnect to set even though original seed # list is useless. Use SECONDARY so we don't have to wait for # the election, merely for the client to detect members are up. sleep(2 * MONITOR_INTERVAL) find_one(read_preference=SECONDARY) # Kill new members and switch back to original two members. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members([primary, secondary1]) # Should be able to reconnect to set again. sleep(2 * MONITOR_INTERVAL) find_one(read_preference=SECONDARY)
def test_read_preference(self): # This is long, but we put all the tests in one function to save time # on setUp, which takes about 30 seconds to bring up a replica set. # We pass through four states: # # 1. A primary and two secondaries # 2. Primary down # 3. Primary up, one secondary down # 4. Primary up, all secondaries down # # For each state, we verify the behavior of PRIMARY, # PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, and NEAREST c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield c.open() @gen.coroutine def read_from_which_host( rsc, mode, tag_sets=None, latency=15, ): db = rsc.motor_test db.read_preference = mode if isinstance(tag_sets, dict): tag_sets = [tag_sets] db.tag_sets = tag_sets or [{}] db.secondary_acceptable_latency_ms = latency cursor = db.test.find() try: yield cursor.fetch_next raise gen.Return(cursor.delegate._Cursor__connection_id) except AutoReconnect: raise gen.Return(None) @gen.coroutine def assert_read_from(member, *args, **kwargs): for _ in range(10): used = yield read_from_which_host(c, *args, **kwargs) self.assertEqual(member, used) @gen.coroutine def assert_read_from_all(members, *args, **kwargs): members = set(members) all_used = set() for _ in range(100): used = yield read_from_which_host(c, *args, **kwargs) all_used.add(used) if members == all_used: raise gen.Return() # Success # This will fail self.assertEqual(members, all_used) def unpartition_node(node): host, port = node return '%s:%s' % (host, port) primary = self.primary secondary = self.secondary other_secondary = self.other_secondary bad_tag = {'bad': 'tag'} # 1. THREE MEMBERS UP ------------------------------------------------- # PRIMARY yield assert_read_from(primary, PRIMARY) # PRIMARY_PREFERRED # Trivial: mode and tags both match yield assert_read_from(primary, PRIMARY_PREFERRED, self.primary_dc) # Secondary matches but not primary, choose primary yield assert_read_from(primary, PRIMARY_PREFERRED, self.secondary_dc) # Chooses primary, ignoring tag sets yield assert_read_from(primary, PRIMARY_PREFERRED, self.primary_dc) # Chooses primary, ignoring tag sets yield assert_read_from(primary, PRIMARY_PREFERRED, bad_tag) yield assert_read_from(primary, PRIMARY_PREFERRED, [bad_tag, {}]) # SECONDARY yield assert_read_from_all([secondary, other_secondary], SECONDARY, latency=9999999) # SECONDARY_PREFERRED yield assert_read_from_all([secondary, other_secondary], SECONDARY_PREFERRED, latency=9999999) # Multiple tags yield assert_read_from(secondary, SECONDARY_PREFERRED, self.secondary_tags) # Fall back to primary if it's the only one matching the tags yield assert_read_from(primary, SECONDARY_PREFERRED, {'name': 'primary'}) # No matching secondaries yield assert_read_from(primary, SECONDARY_PREFERRED, bad_tag) # Fall back from non-matching tag set to matching set yield assert_read_from_all([secondary, other_secondary], SECONDARY_PREFERRED, [bad_tag, {}], latency=9999999) yield assert_read_from(other_secondary, SECONDARY_PREFERRED, [bad_tag, { 'dc': 'ny' }]) # NEAREST self.clear_ping_times() yield assert_read_from_all([primary, secondary, other_secondary], NEAREST, latency=9999999) yield assert_read_from_all([primary, other_secondary], NEAREST, [bad_tag, { 'dc': 'ny' }], latency=9999999) self.set_ping_time(primary, 0) self.set_ping_time(secondary, .03) # 30 milliseconds. self.set_ping_time(other_secondary, 10) # Nearest member, no tags yield assert_read_from(primary, NEAREST) # Tags override nearness yield assert_read_from(primary, NEAREST, {'name': 'primary'}) yield assert_read_from(secondary, NEAREST, self.secondary_dc) # Make secondary fast self.set_ping_time(primary, .03) # 30 milliseconds. self.set_ping_time(secondary, 0) yield assert_read_from(secondary, NEAREST) # Other secondary fast self.set_ping_time(secondary, 10) self.set_ping_time(other_secondary, 0) yield assert_read_from(other_secondary, NEAREST) # High secondaryAcceptableLatencyMS, should read from all members yield assert_read_from_all([primary, secondary, other_secondary], NEAREST, latency=9999999) self.clear_ping_times() yield assert_read_from_all([primary, other_secondary], NEAREST, [{ 'dc': 'ny' }], latency=9999999) # 2. PRIMARY DOWN ----------------------------------------------------- killed = ha_tools.kill_primary() # Let monitor notice primary's gone yield self.pause(2 * MONITOR_INTERVAL) # PRIMARY yield assert_read_from(None, PRIMARY) # PRIMARY_PREFERRED # No primary, choose matching secondary yield assert_read_from_all([secondary, other_secondary], PRIMARY_PREFERRED, latency=9999999) yield assert_read_from(secondary, PRIMARY_PREFERRED, {'name': 'secondary'}) # No primary or matching secondary yield assert_read_from(None, PRIMARY_PREFERRED, bad_tag) # SECONDARY yield assert_read_from_all([secondary, other_secondary], SECONDARY, latency=9999999) # Only primary matches yield assert_read_from(None, SECONDARY, {'name': 'primary'}) # No matching secondaries yield assert_read_from(None, SECONDARY, bad_tag) # SECONDARY_PREFERRED yield assert_read_from_all([secondary, other_secondary], SECONDARY_PREFERRED, latency=9999999) # Mode and tags both match yield assert_read_from(secondary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST self.clear_ping_times() yield assert_read_from_all([secondary, other_secondary], NEAREST, latency=9999999) # 3. PRIMARY UP, ONE SECONDARY DOWN ----------------------------------- ha_tools.restart_members([killed]) for _ in range(30): if ha_tools.get_primary(): break yield self.pause(1) else: self.fail("Primary didn't come back up") ha_tools.kill_members([unpartition_node(secondary)], 2) self.assertTrue( pymongo.MongoClient( unpartition_node(primary), slave_okay=True).admin.command('ismaster')['ismaster']) yield self.pause(2 * MONITOR_INTERVAL) # PRIMARY yield assert_read_from(primary, PRIMARY) # PRIMARY_PREFERRED yield assert_read_from(primary, PRIMARY_PREFERRED) # SECONDARY yield assert_read_from(other_secondary, SECONDARY) yield assert_read_from(other_secondary, SECONDARY, self.other_secondary_dc) # Only the down secondary matches yield assert_read_from(None, SECONDARY, {'name': 'secondary'}) # SECONDARY_PREFERRED yield assert_read_from(other_secondary, SECONDARY_PREFERRED) yield assert_read_from(other_secondary, SECONDARY_PREFERRED, self.other_secondary_dc) # The secondary matching the tag is down, use primary yield assert_read_from(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST yield assert_read_from_all([primary, other_secondary], NEAREST, latency=9999999) yield assert_read_from(other_secondary, NEAREST, {'name': 'other_secondary'}) yield assert_read_from(primary, NEAREST, {'name': 'primary'}) # 4. PRIMARY UP, ALL SECONDARIES DOWN --------------------------------- ha_tools.kill_members([unpartition_node(other_secondary)], 2) self.assertTrue( pymongo.MongoClient( unpartition_node(primary), slave_okay=True).admin.command('ismaster')['ismaster']) # PRIMARY yield assert_read_from(primary, PRIMARY) # PRIMARY_PREFERRED yield assert_read_from(primary, PRIMARY_PREFERRED) yield assert_read_from(primary, PRIMARY_PREFERRED, self.secondary_dc) # SECONDARY yield assert_read_from(None, SECONDARY) yield assert_read_from(None, SECONDARY, self.other_secondary_dc) yield assert_read_from(None, SECONDARY, {'dc': 'ny'}) # SECONDARY_PREFERRED yield assert_read_from(primary, SECONDARY_PREFERRED) yield assert_read_from(primary, SECONDARY_PREFERRED, self.secondary_dc) yield assert_read_from(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) yield assert_read_from(primary, SECONDARY_PREFERRED, {'dc': 'ny'}) # NEAREST yield assert_read_from(primary, NEAREST) yield assert_read_from(None, NEAREST, self.secondary_dc) yield assert_read_from(None, NEAREST, {'name': 'secondary'}) # Even if primary's slow, still read from it self.set_ping_time(primary, 100) yield assert_read_from(primary, NEAREST) yield assert_read_from(None, NEAREST, self.secondary_dc) self.clear_ping_times()
def post(self, op): """ Respond to a POST """ # Start RS if op == 'start': request = self._parse_json(self.request.body) members = request['members'] rs_id = uuid.uuid4() rs_name = 'repl' + str(get_new_repl_id()) try: # start RS and get its' params res = ha_tools.start_replica_set(members, rs_name=rs_name) except: self.error("Couldn't start RS!") # Let's gather all the info about RS and save it rs_uri, rs_name, nodes, arbiters = res # Try to connect to new RS try: for _ in range(1 * 60 / 2): time.sleep(2) try: c = ReplicaSetConnection(rs_uri, replicaSet=rs_name) except: pass except: self.error("Couldn't connect to the new RS: " + rs_uri + ", " + rs_name) secondaries_uris = [] for secondary in c.secondaries: secondaries_uris.append('%s:%d' % (secondary)) rs_t = {} rs_t['id'] = str(rs_id) rs_t['name'] = rs_name rs_t['primary'] = rs_uri rs_t['secondaries'] = secondaries_uris rs_t['nodes'] = nodes rs_t['arbiters'] = arbiters rs.append(rs_t) self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_uri=rs_uri, rs_name=rs_name)) # FIXME: Some checks should be implemented # Stop rs elif op == 'stop': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) try: ha_tools.kill_members(rs[found_index]['nodes'].keys(), 2, rs[found_index]['nodes']) rs_t = rs.pop(found_index) except: self.error("Couldn't stop RS!") rs_id = rs_t['id'] self.write(self._template.load(op + self._ext).generate(rs_id=rs_id)) # Get primary elif op == 'get_primary': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) rs_primary_uri = rs[found_index]['primary'] self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_primary_uri=rs_primary_uri)) # Get secondaries elif op == 'get_secondaries': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) rs_secondaries_uris = rs[found_index]['secondaries'] self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_secondaries_uris=rs_secondaries_uris)) # Get the arbiters elif op == 'get_arbiters': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) rs_arbiters_uris = rs[found_index]['arbiters'] self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_arbiters_uris=rs_arbiters_uris)) # Kill the primary elif op == 'kill_primary': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) try: rs_primary_uri = rs[found_index]['primary'] ha_tools.kill_members([rs_primary_uri], 2, rs[found_index]['nodes']) del rs[found_index]['primary'] except: self.error("No primary found for this RS!") self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_primary_uri=rs_primary_uri)) # Kill the secondary elif op == 'kill_secondary': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] try: found_index = self._get_index(rs, 'id', rs_id) except: self.error("No secondaries found for this RS!") rs_secondaries_uris = rs[found_index]['secondaries'] try: # Get a random secondary for this RS # Get a random index first rs_secondary_uri_index = random.randrange( len(rs_secondaries_uris) ) # Pop out the element by this random index rs_secondary_uri = rs[found_index]['secondaries'].pop(rs_secondary_uri_index) ha_tools.kill_members([rs_secondary_uri], 2, rs[found_index]['nodes']) del rs[found_index]['nodes'][rs_secondary_uri] except: self.error("No secondaries found for this RS!") self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_secondary_uri=rs_secondary_uri)) # Kill all the secondaries elif op == 'kill_all_secondaries': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) try: rs_secondaries_uris = rs[found_index]['secondaries'] ha_tools.kill_members(rs_secondaries_uris, 2, rs[found_index]['nodes']) del rs[found_index]['secondaries'] except: self.error("No secondaries found for this RS!") self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_secondaries_uris=rs_secondaries_uris))
def post(self, op): """ Respond to a POST """ # Start RS if op == 'start': request = self._parse_json(self.request.body) members = request['members'] rs_id = uuid.uuid4() rs_name = 'repl' + str(get_new_repl_id()) try: # start RS and get its' params res = ha_tools.start_replica_set(members, rs_name=rs_name) except: self.error("Couldn't start RS!") # Let's gather all the info about RS and save it rs_uri, rs_name, nodes, arbiters = res # Try to connect to new RS try: for _ in range(1 * 60 / 2): time.sleep(2) try: c = ReplicaSetConnection(rs_uri, replicaSet=rs_name) except: pass except: self.error("Couldn't connect to the new RS: " + rs_uri + ", " + rs_name) secondaries_uris = [] for secondary in c.secondaries: secondaries_uris.append('%s:%d' % (secondary)) rs_t = {} rs_t['id'] = str(rs_id) rs_t['name'] = rs_name rs_t['primary'] = rs_uri rs_t['secondaries'] = secondaries_uris rs_t['nodes'] = nodes rs_t['arbiters'] = arbiters rs.append(rs_t) self.write( self._template.load(op + self._ext).generate(rs_id=rs_id, rs_uri=rs_uri, rs_name=rs_name)) # FIXME: Some checks should be implemented # Stop rs elif op == 'stop': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) try: ha_tools.kill_members(rs[found_index]['nodes'].keys(), 2, rs[found_index]['nodes']) rs_t = rs.pop(found_index) except: self.error("Couldn't stop RS!") rs_id = rs_t['id'] self.write( self._template.load(op + self._ext).generate(rs_id=rs_id)) # Get primary elif op == 'get_primary': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) rs_primary_uri = rs[found_index]['primary'] self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_primary_uri=rs_primary_uri)) # Get secondaries elif op == 'get_secondaries': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) rs_secondaries_uris = rs[found_index]['secondaries'] self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_secondaries_uris=rs_secondaries_uris)) # Get the arbiters elif op == 'get_arbiters': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) rs_arbiters_uris = rs[found_index]['arbiters'] self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_arbiters_uris=rs_arbiters_uris)) # Kill the primary elif op == 'kill_primary': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) try: rs_primary_uri = rs[found_index]['primary'] ha_tools.kill_members([rs_primary_uri], 2, rs[found_index]['nodes']) del rs[found_index]['primary'] except: self.error("No primary found for this RS!") self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_killed_primary_uri=rs_primary_uri)) # Kill the secondary elif op == 'kill_secondary': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] try: found_index = self._get_index(rs, 'id', rs_id) except: self.error("No secondaries found for this RS!") rs_secondaries_uris = rs[found_index]['secondaries'] try: # Get a random secondary for this RS # Get a random index first rs_secondary_uri_index = random.randrange( len(rs_secondaries_uris)) # Pop out the element by this random index rs_secondary_uri = rs[found_index]['secondaries'].pop( rs_secondary_uri_index) ha_tools.kill_members([rs_secondary_uri], 2, rs[found_index]['nodes']) del rs[found_index]['nodes'][rs_secondary_uri] except: self.error("No secondaries found for this RS!") self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_killed_secondary_uri=rs_secondary_uri)) # Kill all the secondaries elif op == 'kill_all_secondaries': request = self._parse_json(self.request.body) rs_id = request['rs']['id'] found_index = self._get_index(rs, 'id', rs_id) try: rs_secondaries_uris = rs[found_index]['secondaries'] ha_tools.kill_members(rs_secondaries_uris, 2, rs[found_index]['nodes']) del rs[found_index]['secondaries'] except: self.error("No secondaries found for this RS!") self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_killed_secondaries_uris=rs_secondaries_uris))
def test_ship_of_theseus(self): c = MongoReplicaSetClient( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) db = c.pymongo_test db.test.insert({}, w=len(c.secondaries) + 1) find_one = db.test.find_one primary = ha_tools.get_primary() secondary1 = ha_tools.get_random_secondary() new_hosts = [] for i in range(3): new_hosts.append(ha_tools.add_member()) # RS closes all connections after reconfig. for j in xrange(30): try: if ha_tools.get_primary(): break except (ConnectionFailure, OperationFailure): pass sleep(1) else: self.fail("Couldn't recover from reconfig") # Wait for new members to join. for _ in xrange(120): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4: break sleep(1) else: self.fail("New secondaries didn't join") ha_tools.kill_members([primary, secondary1], 9) sleep(5) # Wait for primary. for _ in xrange(30): if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 2: break sleep(1) else: self.fail("No failover") sleep(2 * MONITOR_INTERVAL) # No error. find_one() find_one(read_preference=SECONDARY) # All members down. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members(new_hosts) # Should be able to reconnect to set even though original seed # list is useless. Use SECONDARY so we don't have to wait for # the election, merely for the client to detect members are up. sleep(2 * MONITOR_INTERVAL) find_one(read_preference=SECONDARY) # Kill new members and switch back to original two members. ha_tools.kill_members(new_hosts, 9) self.assertRaises( ConnectionFailure, find_one, read_preference=SECONDARY) ha_tools.restart_members([primary, secondary1]) # Wait for members to figure out they're secondaries. for _ in xrange(30): try: if len(ha_tools.get_secondaries()) == 2: break except ConnectionFailure: pass sleep(1) else: self.fail("Original members didn't become secondaries") # Should be able to reconnect to set again. sleep(2 * MONITOR_INTERVAL) find_one(read_preference=SECONDARY)
def test_read_preference(self): # This is long, but we put all the tests in one function to save time # on setUp, which takes about 30 seconds to bring up a replica set. # We pass through four states: # # 1. A primary and two secondaries # 2. Primary down # 3. Primary up, one secondary down # 4. Primary up, all secondaries down # # For each state, we verify the behavior of PRIMARY, # PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, and NEAREST c = ReplicaSetConnection( self.seed, replicaSet=self.name, use_greenlets=use_greenlets, auto_start_request=False) def assertReadFrom(member, *args, **kwargs): utils.assertReadFrom(self, c, member, *args, **kwargs) def assertReadFromAll(members, *args, **kwargs): utils.assertReadFromAll(self, c, members, *args, **kwargs) def unpartition_node(node): host, port = node return '%s:%s' % (host, port) # To make the code terser, copy modes and hosts into local scope PRIMARY = ReadPreference.PRIMARY PRIMARY_PREFERRED = ReadPreference.PRIMARY_PREFERRED SECONDARY = ReadPreference.SECONDARY SECONDARY_PREFERRED = ReadPreference.SECONDARY_PREFERRED NEAREST = ReadPreference.NEAREST primary = self.primary secondary = self.secondary other_secondary = self.other_secondary bad_tag = {'bad': 'tag'} # 1. THREE MEMBERS UP ------------------------------------------------- # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED # Trivial: mode and tags both match assertReadFrom(primary, PRIMARY_PREFERRED, self.primary_dc) # Secondary matches but not primary, choose primary assertReadFrom(primary, PRIMARY_PREFERRED, self.secondary_dc) # Chooses primary, ignoring tag sets assertReadFrom(primary, PRIMARY_PREFERRED, self.primary_dc) # Chooses primary, ignoring tag sets assertReadFrom(primary, PRIMARY_PREFERRED, bad_tag) assertReadFrom(primary, PRIMARY_PREFERRED, [bad_tag, {}]) # SECONDARY assertReadFromAll([secondary, other_secondary], SECONDARY) # SECONDARY_PREFERRED assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED) # Multiple tags assertReadFrom(secondary, SECONDARY_PREFERRED, self.secondary_tags) # Fall back to primary if it's the only one matching the tags assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'primary'}) # No matching secondaries assertReadFrom(primary, SECONDARY_PREFERRED, bad_tag) # Fall back from non-matching tag set to matching set assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED, [bad_tag, {}]) assertReadFrom(other_secondary, SECONDARY_PREFERRED, [bad_tag, {'dc': 'ny'}]) # NEAREST self.clear_ping_times() assertReadFromAll([primary, secondary, other_secondary], NEAREST) assertReadFromAll([primary, other_secondary], NEAREST, [bad_tag, {'dc': 'ny'}]) self.set_ping_time(primary, 0) self.set_ping_time(secondary, .03) # 30 ms self.set_ping_time(other_secondary, 10) # Nearest member, no tags assertReadFrom(primary, NEAREST) # Tags override nearness assertReadFrom(primary, NEAREST, {'name': 'primary'}) assertReadFrom(secondary, NEAREST, self.secondary_dc) # Make secondary fast self.set_ping_time(primary, .03) # 30 ms self.set_ping_time(secondary, 0) assertReadFrom(secondary, NEAREST) # Other secondary fast self.set_ping_time(secondary, 10) self.set_ping_time(other_secondary, 0) assertReadFrom(other_secondary, NEAREST) # High secondaryAcceptableLatencyMS, should read from all members assertReadFromAll( [primary, secondary, other_secondary], NEAREST, secondary_acceptable_latency_ms=1000*1000) self.clear_ping_times() assertReadFromAll([primary, other_secondary], NEAREST, [{'dc': 'ny'}]) # 2. PRIMARY DOWN ----------------------------------------------------- killed = ha_tools.kill_primary() # Let monitor notice primary's gone sleep(2 * MONITOR_INTERVAL) # PRIMARY assertReadFrom(None, PRIMARY) # PRIMARY_PREFERRED # No primary, choose matching secondary assertReadFromAll([secondary, other_secondary], PRIMARY_PREFERRED) assertReadFrom(secondary, PRIMARY_PREFERRED, {'name': 'secondary'}) # No primary or matching secondary assertReadFrom(None, PRIMARY_PREFERRED, bad_tag) # SECONDARY assertReadFromAll([secondary, other_secondary], SECONDARY) # Only primary matches assertReadFrom(None, SECONDARY, {'name': 'primary'}) # No matching secondaries assertReadFrom(None, SECONDARY, bad_tag) # SECONDARY_PREFERRED assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED) # Mode and tags both match assertReadFrom(secondary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST self.clear_ping_times() assertReadFromAll([secondary, other_secondary], NEAREST) # 3. PRIMARY UP, ONE SECONDARY DOWN ----------------------------------- ha_tools.restart_members([killed]) for _ in range(30): if ha_tools.get_primary(): break sleep(1) else: self.fail("Primary didn't come back up") ha_tools.kill_members([unpartition_node(secondary)], 2) self.assertTrue(Connection( unpartition_node(primary), use_greenlets=use_greenlets, slave_okay=True ).admin.command('ismaster')['ismaster']) sleep(2 * MONITOR_INTERVAL) # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED assertReadFrom(primary, PRIMARY_PREFERRED) # SECONDARY assertReadFrom(other_secondary, SECONDARY) assertReadFrom(other_secondary, SECONDARY, self.other_secondary_dc) # Only the down secondary matches assertReadFrom(None, SECONDARY, {'name': 'secondary'}) # SECONDARY_PREFERRED assertReadFrom(other_secondary, SECONDARY_PREFERRED) assertReadFrom( other_secondary, SECONDARY_PREFERRED, self.other_secondary_dc) # The secondary matching the tag is down, use primary assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST assertReadFromAll([primary, other_secondary], NEAREST) assertReadFrom(other_secondary, NEAREST, {'name': 'other_secondary'}) assertReadFrom(primary, NEAREST, {'name': 'primary'}) # 4. PRIMARY UP, ALL SECONDARIES DOWN --------------------------------- ha_tools.kill_members([unpartition_node(other_secondary)], 2) self.assertTrue(Connection( unpartition_node(primary), use_greenlets=use_greenlets, slave_okay=True ).admin.command('ismaster')['ismaster']) # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED assertReadFrom(primary, PRIMARY_PREFERRED) assertReadFrom(primary, PRIMARY_PREFERRED, self.secondary_dc) # SECONDARY assertReadFrom(None, SECONDARY) assertReadFrom(None, SECONDARY, self.other_secondary_dc) assertReadFrom(None, SECONDARY, {'dc': 'ny'}) # SECONDARY_PREFERRED assertReadFrom(primary, SECONDARY_PREFERRED) assertReadFrom(primary, SECONDARY_PREFERRED, self.secondary_dc) assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) assertReadFrom(primary, SECONDARY_PREFERRED, {'dc': 'ny'}) # NEAREST assertReadFrom(primary, NEAREST) assertReadFrom(None, NEAREST, self.secondary_dc) assertReadFrom(None, NEAREST, {'name': 'secondary'}) # Even if primary's slow, still read from it self.set_ping_time(primary, 100) assertReadFrom(primary, NEAREST) assertReadFrom(None, NEAREST, self.secondary_dc) self.clear_ping_times()
def test_read_preference(self): # We pass through four states: # # 1. A primary and two secondaries # 2. Primary down # 3. Primary up, one secondary down # 4. Primary up, all secondaries down # # For each state, we verify the behavior of PRIMARY, # PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, and NEAREST c = MongoReplicaSetClient(self.seed, replicaSet=self.name, use_greenlets=use_greenlets) def assertReadFrom(member, *args, **kwargs): utils.assertReadFrom(self, c, member, *args, **kwargs) def assertReadFromAll(members, *args, **kwargs): utils.assertReadFromAll(self, c, members, *args, **kwargs) def unpartition_node(node): host, port = node return '%s:%s' % (host, port) # To make the code terser, copy hosts into local scope primary = self.primary secondary = self.secondary other_secondary = self.other_secondary bad_tag = {'bad': 'tag'} # 1. THREE MEMBERS UP ------------------------------------------------- # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED # Trivial: mode and tags both match assertReadFrom(primary, PRIMARY_PREFERRED, self.primary_dc) # Secondary matches but not primary, choose primary assertReadFrom(primary, PRIMARY_PREFERRED, self.secondary_dc) # Chooses primary, ignoring tag sets assertReadFrom(primary, PRIMARY_PREFERRED, self.primary_dc) # Chooses primary, ignoring tag sets assertReadFrom(primary, PRIMARY_PREFERRED, bad_tag) assertReadFrom(primary, PRIMARY_PREFERRED, [bad_tag, {}]) # SECONDARY assertReadFromAll([secondary, other_secondary], SECONDARY) # SECONDARY_PREFERRED assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED) # Multiple tags assertReadFrom(secondary, SECONDARY_PREFERRED, self.secondary_tags) # Fall back to primary if it's the only one matching the tags assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'primary'}) # No matching secondaries assertReadFrom(primary, SECONDARY_PREFERRED, bad_tag) # Fall back from non-matching tag set to matching set assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED, [bad_tag, {}]) assertReadFrom(other_secondary, SECONDARY_PREFERRED, [bad_tag, { 'dc': 'ny' }]) # NEAREST self.clear_ping_times() assertReadFromAll([primary, secondary, other_secondary], NEAREST) assertReadFromAll([primary, other_secondary], NEAREST, [bad_tag, { 'dc': 'ny' }]) self.set_ping_time(primary, 0) self.set_ping_time(secondary, .03) # 30 ms self.set_ping_time(other_secondary, 10) # Nearest member, no tags assertReadFrom(primary, NEAREST) # Tags override nearness assertReadFrom(primary, NEAREST, {'name': 'primary'}) assertReadFrom(secondary, NEAREST, self.secondary_dc) # Make secondary fast self.set_ping_time(primary, .03) # 30 ms self.set_ping_time(secondary, 0) assertReadFrom(secondary, NEAREST) # Other secondary fast self.set_ping_time(secondary, 10) self.set_ping_time(other_secondary, 0) assertReadFrom(other_secondary, NEAREST) # High secondaryAcceptableLatencyMS, should read from all members assertReadFromAll([primary, secondary, other_secondary], NEAREST, secondary_acceptable_latency_ms=1000 * 1000) self.clear_ping_times() assertReadFromAll([primary, other_secondary], NEAREST, [{'dc': 'ny'}]) # 2. PRIMARY DOWN ----------------------------------------------------- killed = ha_tools.kill_primary() # Let monitor notice primary's gone sleep(2 * MONITOR_INTERVAL) # PRIMARY assertReadFrom(None, PRIMARY) # PRIMARY_PREFERRED # No primary, choose matching secondary assertReadFromAll([secondary, other_secondary], PRIMARY_PREFERRED) assertReadFrom(secondary, PRIMARY_PREFERRED, {'name': 'secondary'}) # No primary or matching secondary assertReadFrom(None, PRIMARY_PREFERRED, bad_tag) # SECONDARY assertReadFromAll([secondary, other_secondary], SECONDARY) # Only primary matches assertReadFrom(None, SECONDARY, {'name': 'primary'}) # No matching secondaries assertReadFrom(None, SECONDARY, bad_tag) # SECONDARY_PREFERRED assertReadFromAll([secondary, other_secondary], SECONDARY_PREFERRED) # Mode and tags both match assertReadFrom(secondary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST self.clear_ping_times() assertReadFromAll([secondary, other_secondary], NEAREST) # 3. PRIMARY UP, ONE SECONDARY DOWN ----------------------------------- ha_tools.restart_members([killed]) for _ in range(30): if ha_tools.get_primary(): break sleep(1) else: self.fail("Primary didn't come back up") ha_tools.kill_members([unpartition_node(secondary)], 2) self.assertTrue( MongoClient(unpartition_node(primary), use_greenlets=use_greenlets, read_preference=PRIMARY_PREFERRED).admin.command( 'ismaster')['ismaster']) sleep(2 * MONITOR_INTERVAL) # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED assertReadFrom(primary, PRIMARY_PREFERRED) # SECONDARY assertReadFrom(other_secondary, SECONDARY) assertReadFrom(other_secondary, SECONDARY, self.other_secondary_dc) # Only the down secondary matches assertReadFrom(None, SECONDARY, {'name': 'secondary'}) # SECONDARY_PREFERRED assertReadFrom(other_secondary, SECONDARY_PREFERRED) assertReadFrom(other_secondary, SECONDARY_PREFERRED, self.other_secondary_dc) # The secondary matching the tag is down, use primary assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) # NEAREST assertReadFromAll([primary, other_secondary], NEAREST) assertReadFrom(other_secondary, NEAREST, {'name': 'other_secondary'}) assertReadFrom(primary, NEAREST, {'name': 'primary'}) # 4. PRIMARY UP, ALL SECONDARIES DOWN --------------------------------- ha_tools.kill_members([unpartition_node(other_secondary)], 2) self.assertTrue( MongoClient(unpartition_node(primary), use_greenlets=use_greenlets, read_preference=PRIMARY_PREFERRED).admin.command( 'ismaster')['ismaster']) # PRIMARY assertReadFrom(primary, PRIMARY) # PRIMARY_PREFERRED assertReadFrom(primary, PRIMARY_PREFERRED) assertReadFrom(primary, PRIMARY_PREFERRED, self.secondary_dc) # SECONDARY assertReadFrom(None, SECONDARY) assertReadFrom(None, SECONDARY, self.other_secondary_dc) assertReadFrom(None, SECONDARY, {'dc': 'ny'}) # SECONDARY_PREFERRED assertReadFrom(primary, SECONDARY_PREFERRED) assertReadFrom(primary, SECONDARY_PREFERRED, self.secondary_dc) assertReadFrom(primary, SECONDARY_PREFERRED, {'name': 'secondary'}) assertReadFrom(primary, SECONDARY_PREFERRED, {'dc': 'ny'}) # NEAREST assertReadFrom(primary, NEAREST) assertReadFrom(None, NEAREST, self.secondary_dc) assertReadFrom(None, NEAREST, {'name': 'secondary'}) # Even if primary's slow, still read from it self.set_ping_time(primary, 100) assertReadFrom(primary, NEAREST) assertReadFrom(None, NEAREST, self.secondary_dc) self.clear_ping_times()