def test_secondary_connection(self): self.c = ReplicaSetConnection( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) self.assertTrue(bool(len(self.c.secondaries))) db = self.c.pymongo_test db.test.remove({}, safe=True, w=len(self.c.secondaries)) # Force replication... w = len(self.c.secondaries) + 1 db.test.insert({'foo': 'bar'}, safe=True, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) self.assertTrue(Connection( primary_host, primary_port, use_greenlets=use_greenlets).is_primary) self.assertTrue(Connection( primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.PRIMARY_PREFERRED).is_primary) self.assertTrue(Connection( primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.SECONDARY_PREFERRED).is_primary) self.assertTrue(Connection( primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.NEAREST).is_primary) self.assertTrue(Connection( primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.SECONDARY).is_primary) for kwargs in [ {'read_preference': ReadPreference.PRIMARY_PREFERRED}, {'read_preference': ReadPreference.SECONDARY}, {'read_preference': ReadPreference.SECONDARY_PREFERRED}, {'read_preference': ReadPreference.NEAREST}, {'slave_okay': True}, ]: conn = Connection(secondary_host, secondary_port, use_greenlets=use_greenlets, **kwargs) self.assertEqual(secondary_host, conn.host) self.assertEqual(secondary_port, conn.port) self.assertFalse(conn.is_primary) self.assert_(conn.pymongo_test.test.find_one()) # Test direct connection to an arbiter secondary_host = ha_tools.get_arbiters()[0] host, port = ha_tools.get_arbiters()[0].split(':') port = int(port) conn = Connection(host, port) self.assertEqual(host, conn.host) self.assertEqual(port, conn.port)
def test_secondary_connection(self): self.c = yield motor.MotorReplicaSetClient( self.seed, replicaSet=self.name).open() self.assertTrue(bool(len(self.c.secondaries))) db = self.c.motor_test yield db.test.remove({}, w=len(self.c.secondaries)) # Wait for replication... w = len(self.c.secondaries) + 1 yield db.test.insert({'foo': 'bar'}, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':') arbiter_port = int(arbiter_port) # A connection succeeds no matter the read preference for kwargs in [ {'read_preference': PRIMARY}, {'read_preference': PRIMARY_PREFERRED}, {'read_preference': SECONDARY}, {'read_preference': SECONDARY_PREFERRED}, {'read_preference': NEAREST}, ]: client = yield motor.MotorClient( primary_host, primary_port, **kwargs).open() self.assertEqual(primary_host, client.host) self.assertEqual(primary_port, client.port) self.assertTrue(client.is_primary) # Direct connection to primary can be queried with any read pref self.assertTrue((yield client.motor_test.test.find_one())) client = yield motor.MotorClient( secondary_host, secondary_port, **kwargs).open() self.assertEqual(secondary_host, client.host) self.assertEqual(secondary_port, client.port) self.assertFalse(client.is_primary) # Direct connection to secondary can be queried with any read pref # but PRIMARY if kwargs.get('read_preference') != PRIMARY: self.assertTrue(( yield client.motor_test.test.find_one())) else: with assert_raises(AutoReconnect): yield client.motor_test.test.find_one() # Since an attempt at an acknowledged write to a secondary from a # direct connection raises AutoReconnect('not master'), MotorClient # should do the same for unacknowledged writes. try: yield client.motor_test.test.insert({}, w=0) except AutoReconnect, e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into secondary client %s should' 'have raised exception' % client) # Test direct connection to an arbiter client = yield motor.MotorClient( arbiter_host, arbiter_port, **kwargs).open() self.assertEqual(arbiter_host, client.host) self.assertEqual(arbiter_port, client.port) self.assertFalse(client.is_primary) # See explanation above try: yield client.motor_test.test.insert({}, w=0) except AutoReconnect, e: self.assertEqual('not master', e.message)
def test_secondary_connection(self): self.c = yield motor.MotorReplicaSetClient( self.seed, replicaSet=self.name).open() self.assertTrue(bool(len(self.c.secondaries))) db = self.c.motor_test yield db.test.remove({}, w=len(self.c.secondaries)) # Wait for replication... w = len(self.c.secondaries) + 1 yield db.test.insert({'foo': 'bar'}, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':') arbiter_port = int(arbiter_port) # A connection succeeds no matter the read preference for kwargs in [ { 'read_preference': PRIMARY }, { 'read_preference': PRIMARY_PREFERRED }, { 'read_preference': SECONDARY }, { 'read_preference': SECONDARY_PREFERRED }, { 'read_preference': NEAREST }, ]: client = yield motor.MotorClient(primary_host, primary_port, **kwargs).open() self.assertEqual(primary_host, client.host) self.assertEqual(primary_port, client.port) self.assertTrue(client.is_primary) # Direct connection to primary can be queried with any read pref self.assertTrue((yield client.motor_test.test.find_one())) client = yield motor.MotorClient(secondary_host, secondary_port, **kwargs).open() self.assertEqual(secondary_host, client.host) self.assertEqual(secondary_port, client.port) self.assertFalse(client.is_primary) # Direct connection to secondary can be queried with any read pref # but PRIMARY if kwargs.get('read_preference') != PRIMARY: self.assertTrue((yield client.motor_test.test.find_one())) else: with assert_raises(AutoReconnect): yield client.motor_test.test.find_one() # Since an attempt at an acknowledged write to a secondary from a # direct connection raises AutoReconnect('not master'), MotorClient # should do the same for unacknowledged writes. try: yield client.motor_test.test.insert({}, w=0) except AutoReconnect, e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into secondary client %s should' 'have raised exception' % client) # Test direct connection to an arbiter client = yield motor.MotorClient(arbiter_host, arbiter_port, **kwargs).open() self.assertEqual(arbiter_host, client.host) self.assertEqual(arbiter_port, client.port) self.assertFalse(client.is_primary) # See explanation above try: yield client.motor_test.test.insert({}, w=0) except AutoReconnect, e: self.assertEqual('not master', e.message)
def test_secondary_connection(self): self.c = ReplicaSetConnection( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) self.assertTrue(bool(len(self.c.secondaries))) db = self.c.pymongo_test db.test.remove({}, safe=True, w=len(self.c.secondaries)) # Wait for replication... w = len(self.c.secondaries) + 1 db.test.insert({'foo': 'bar'}, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':') arbiter_port = int(arbiter_port) # A Connection succeeds no matter the read preference for kwargs in [ {'read_preference': PRIMARY}, {'read_preference': PRIMARY_PREFERRED}, {'read_preference': SECONDARY}, {'read_preference': SECONDARY_PREFERRED}, {'read_preference': NEAREST}, {'slave_okay': True} ]: conn = Connection(primary_host, primary_port, use_greenlets=use_greenlets, **kwargs) self.assertEqual(primary_host, conn.host) self.assertEqual(primary_port, conn.port) self.assertTrue(conn.is_primary) # Direct connection to primary can be queried with any read pref self.assertTrue(conn.pymongo_test.test.find_one()) conn = Connection(secondary_host, secondary_port, use_greenlets=use_greenlets, **kwargs) self.assertEqual(secondary_host, conn.host) self.assertEqual(secondary_port, conn.port) self.assertFalse(conn.is_primary) # Direct connection to secondary can be queried with any read pref # but PRIMARY if kwargs.get('read_preference') != PRIMARY: self.assertTrue(conn.pymongo_test.test.find_one()) else: self.assertRaises( AutoReconnect, conn.pymongo_test.test.find_one) # Since an attempt at an acknowledged write to a secondary from a # direct connection raises AutoReconnect('not master'), Connection # should do the same for unacknowledged writes. try: conn.pymongo_test.test.insert({}, safe=False) except AutoReconnect, e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into secondary connection %s should' 'have raised exception' % conn) # Test direct connection to an arbiter conn = Connection(arbiter_host, arbiter_port, **kwargs) self.assertEqual(arbiter_host, conn.host) self.assertEqual(arbiter_port, conn.port) self.assertFalse(conn.is_primary) # See explanation above try: conn.pymongo_test.test.insert({}, safe=False) except AutoReconnect, e: self.assertEqual('not master', e.args[0])
def test_secondary_connection(self): self.c = ReplicaSetConnection(self.seed, replicaSet=self.name, use_greenlets=use_greenlets) self.assertTrue(bool(len(self.c.secondaries))) db = self.c.pymongo_test db.test.remove({}, safe=True, w=len(self.c.secondaries)) # Force replication... w = len(self.c.secondaries) + 1 db.test.insert({'foo': 'bar'}, safe=True, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) self.assertTrue( Connection(primary_host, primary_port, use_greenlets=use_greenlets).is_primary) self.assertTrue( Connection( primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.PRIMARY_PREFERRED).is_primary) self.assertTrue( Connection( primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.SECONDARY_PREFERRED).is_primary) self.assertTrue( Connection(primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.NEAREST).is_primary) self.assertTrue( Connection(primary_host, primary_port, use_greenlets=use_greenlets, read_preference=ReadPreference.SECONDARY).is_primary) for kwargs in [ { 'read_preference': ReadPreference.PRIMARY_PREFERRED }, { 'read_preference': ReadPreference.SECONDARY }, { 'read_preference': ReadPreference.SECONDARY_PREFERRED }, { 'read_preference': ReadPreference.NEAREST }, { 'slave_okay': True }, ]: conn = Connection(secondary_host, secondary_port, use_greenlets=use_greenlets, **kwargs) self.assertEqual(secondary_host, conn.host) self.assertEqual(secondary_port, conn.port) self.assertFalse(conn.is_primary) self.assert_(conn.pymongo_test.test.find_one()) # Test direct connection to an arbiter host, port = ha_tools.get_arbiters()[0].split(':') port = int(port) conn = Connection(host, port) self.assertEqual(host, conn.host) self.assertEqual(port, conn.port)
def test_secondary_connection(self): self.c = ReplicaSetConnection( self.seed, replicaSet=self.name, use_greenlets=use_greenlets) self.assertTrue(bool(len(self.c.secondaries))) db = self.c.pymongo_test db.test.remove({}, safe=True, w=len(self.c.secondaries)) # Wait for replication... w = len(self.c.secondaries) + 1 db.test.insert({'foo': 'bar'}, w=w) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':') arbiter_port = int(arbiter_port) # A Connection succeeds no matter the read preference for kwargs in [ {'read_preference': ReadPreference.PRIMARY}, {'read_preference': ReadPreference.PRIMARY_PREFERRED}, {'read_preference': ReadPreference.SECONDARY}, {'read_preference': ReadPreference.SECONDARY_PREFERRED}, {'read_preference': ReadPreference.NEAREST}, {'slave_okay': True} ]: conn = Connection(primary_host, primary_port, use_greenlets=use_greenlets, **kwargs) self.assertEqual(primary_host, conn.host) self.assertEqual(primary_port, conn.port) self.assertTrue(conn.is_primary) # Direct connection to primary can be queried with any read pref self.assertTrue(conn.pymongo_test.test.find_one()) conn = Connection(secondary_host, secondary_port, use_greenlets=use_greenlets, **kwargs) self.assertEqual(secondary_host, conn.host) self.assertEqual(secondary_port, conn.port) self.assertFalse(conn.is_primary) # Direct connection to secondary can be queried with any read pref # but PRIMARY if kwargs.get('read_preference') != ReadPreference.PRIMARY: self.assertTrue(conn.pymongo_test.test.find_one()) else: self.assertRaises( AutoReconnect, conn.pymongo_test.test.find_one) # Since an attempt at an acknowledged write to a secondary from a # direct connection raises AutoReconnect('not master'), Connection # should do the same for unacknowledged writes. try: conn.pymongo_test.test.insert({}, safe=False) except AutoReconnect, e: self.assertEqual('not master', e.message) else: self.fail( 'Unacknowledged insert into secondary connection %s should' 'have raised exception' % conn) # Test direct connection to an arbiter conn = Connection(arbiter_host, arbiter_port, **kwargs) self.assertEqual(arbiter_host, conn.host) self.assertEqual(arbiter_port, conn.port) self.assertFalse(conn.is_primary) # See explanation above try: conn.pymongo_test.test.insert({}, safe=False) except AutoReconnect, e: self.assertEqual('not master', e.message)
def test_secondary_connection(self): self.c = MongoClient( self.seed, replicaSet=self.name, serverSelectionTimeoutMS=self.server_selection_timeout) wait_until(lambda: len(self.c.secondaries), "discover secondary") # Wait for replication... w = len(self.c.secondaries) + 1 db = self.c.get_database("pymongo_test", write_concern=WriteConcern(w=w)) db.test.delete_many({}) db.test.insert_one({'foo': 'bar'}) # Test direct connection to a primary or secondary primary_host, primary_port = ha_tools.get_primary().split(':') primary_port = int(primary_port) (secondary_host, secondary_port) = ha_tools.get_secondaries()[0].split(':') secondary_port = int(secondary_port) arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':') arbiter_port = int(arbiter_port) # MongoClient succeeds no matter the read preference for kwargs in [ {'read_preference': PRIMARY}, {'read_preference': PRIMARY_PREFERRED}, {'read_preference': SECONDARY}, {'read_preference': SECONDARY_PREFERRED}, {'read_preference': NEAREST}, ]: client = MongoClient( primary_host, primary_port, serverSelectionTimeoutMS=self.server_selection_timeout, **kwargs) wait_until(lambda: primary_host == client.host, "connect to primary") self.assertEqual(primary_port, client.port) self.assertTrue(client.is_primary) # Direct connection to primary can be queried with any read pref self.assertTrue(client.pymongo_test.test.find_one()) client = MongoClient( secondary_host, secondary_port, serverSelectionTimeoutMS=self.server_selection_timeout, **kwargs) wait_until(lambda: secondary_host == client.host, "connect to secondary") self.assertEqual(secondary_port, client.port) self.assertFalse(client.is_primary) # Direct connection to secondary can be queried with any read pref # but PRIMARY if kwargs.get('read_preference') != PRIMARY: self.assertTrue(client.pymongo_test.test.find_one()) else: self.assertRaises( AutoReconnect, client.pymongo_test.test.find_one) # Since an attempt at an acknowledged write to a secondary from a # direct connection raises AutoReconnect('not master'), MongoClient # should do the same for unacknowledged writes. try: client.get_database( "pymongo_test", write_concern=WriteConcern(w=0)).test.insert_one({}) except AutoReconnect as e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into secondary client %s should' 'have raised exception' % (client,)) # Test direct connection to an arbiter client = MongoClient( arbiter_host, arbiter_port, serverSelectionTimeoutMS=self.server_selection_timeout, **kwargs) wait_until(lambda: arbiter_host == client.host, "connect to arbiter") self.assertEqual(arbiter_port, client.port) self.assertFalse(client.is_primary) # See explanation above try: client.get_database( "pymongo_test", write_concern=WriteConcern(w=0)).test.insert_one({}) except AutoReconnect as e: self.assertEqual('not master', e.args[0]) else: self.fail( 'Unacknowledged insert into arbiter client %s should' 'have raised exception' % (client,))
def post(self, op): """ Respond to a POST """ # Start RS if op == "start": request = self._parse_json(self.request.body) members = request["members"] try: # start RS and get its' params res = ha_tools.start_replica_set(members) except: raise tornado.httpserver._BadRequestException("Couldn't start RS!") rs_uri, rs_name = res rs_id = uuid.uuid4() self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_uri=rs_uri, rs_name=rs_name)) # Stop rs elif op == "stop": try: ha_tools.kill_all_members() except: raise tornado.httpserver._BadRequestException("Couldn't stop RS!") # Get primary elif op == "get_primary": request = self._parse_json(self.request.body) rs_id = request["rs"]["id"] rs_primary_uri = ha_tools.get_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"] rs_secondaries_uris = ha_tools.get_secondaries() self.write( self._template.load(op + self._ext).generate(rs_id=rs_id, rs_secondaries_uris=rs_secondaries_uris) ) # Get arbiters elif op == "get_arbiters": request = self._parse_json(self.request.body) rs_id = request["rs"]["id"] rs_arbiters_uris = ha_tools.get_arbiters() self.write(self._template.load(op + self._ext).generate(rs_id=rs_id, rs_arbiters_uris=rs_arbiters_uris)) # Kill primary elif op == "kill_primary": request = self._parse_json(self.request.body) rs_id = request["rs"]["id"] rs_killed_primary_uri = ha_tools.kill_primary() self.write( self._template.load(op + self._ext).generate(rs_id=rs_id, rs_killed_primary_uri=rs_killed_primary_uri) ) # Kill secondary elif op == "kill_secondary": request = self._parse_json(self.request.body) rs_id = request["rs"]["id"] rs_killed_secondary_uri = ha_tools.kill_secondary() self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_killed_secondary_uri=rs_killed_secondary_uri ) ) # Kill all secondaries elif op == "kill_all_secondaries": request = self._parse_json(self.request.body) rs_id = request["rs"]["id"] rs_killed_secondaries_uris = ha_tools.kill_all_secondaries() self.write( self._template.load(op + self._ext).generate( rs_id=rs_id, rs_killed_secondaries_uris=rs_killed_secondaries_uris ) )