Add replicaset support #58

Merged
merged 1 commit into from Apr 3, 2013

Projects

None yet

4 participants

@manover

ReplicaSet support

@manover

@jehiah @mreiferson @ploxiln
Basic RS support implemented, no working tests yet

@jehiah jehiah commented on an outdated diff Mar 15, 2013
asyncmongo/connection.py
- `dbuser`: db user to connect with
- `dbpass`: db password
- `autoreconnect` (optional): auto reconnect on interface errors
+ - `rs` (optional): replica set name (required when replica sets are used)
+ - `seed` (optional): seed list to connect to a replica set (required when replica sets are used)
@jehiah
jehiah Mar 15, 2013

It would be nice to either a) make this a more structured [(host, port)...] list, or at least ['host:port', ...] or switch this to code that handles mongo uri's and have ['mongo://host:port',...]. In either case since it's a list of spots to connect to, let's make it a list instead of a comma separated string.

@jehiah
Bitly member

@manover this looks like a very good way to structure the authentication/replica set discovery flow. nice work so far.

@SeanOC SeanOC commented on the diff Mar 26, 2013
test/test_replica_set.py
+ ('--port', '27020', '--replSet', 'rs0'),
+ ]
+
+ def wait_master(self, port):
+ while True:
+ pipe = subprocess.Popen("echo -n 'db.isMaster();\n' |mongo --port %d --host 127.0.0.1" % port,
+ stdout=subprocess.PIPE, shell=True)
+ repl = pipe.communicate()[0]
+ if repl.find('"ismaster" : true') >= 0:
+ logging.info("%d is a master", port)
+ break
+ else:
+ logging.info("Waiting for %d to become master", port)
+ time.sleep(5)
+
+ def setUp(self):
@SeanOC
SeanOC Mar 26, 2013

You might want to try using a setUpClass class method to do the expensive mongo setup instead of the setUp method. By using setUpClass your setup should only be run once for all of tests.

@manover
manover Mar 26, 2013

Yeah, I could do that or even use a module-wide setup() function, the only problems are:

  1. It would still take a lot of time, even running once, so Travis would fail
  2. I would really hate to duplicate the existing structure used by other tests (with MongoTest as a super class)

but thanks for the tip

@manover manover was assigned Mar 26, 2013
@ploxiln ploxiln commented on an outdated diff Mar 27, 2013
test/test_replica_set.py
+import asyncmongo
+import asyncmongo.connection
+import asyncmongo.errors
+
+TEST_TIMESTAMP = int(time.time())
+
+class ReplicaSetTest(test_shunt.MongoTest):
+ mongod_options = [
+ ('--port', '27018', '--replSet', 'rs0'),
+ ('--port', '27019', '--replSet', 'rs0'),
+ ('--port', '27020', '--replSet', 'rs0'),
+ ]
+
+ def mongo_cmd(self, cmd, port=27018, res='"ok" : 1'):
+ pipe = subprocess.Popen("echo '%s' | mongo --port %d" % (cmd, port), shell=True, stdout=subprocess.PIPE)
+ repl = pipe.communicate()[0]
@ploxiln
ploxiln Mar 27, 2013

could be:

mongo_cli = subprocess.Popen(["mongo", "--port", str(port)], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
reply, _ = mongo_cli.communicate(cmd)

which I arbitrarily consider to be more elegant :)

@jehiah
Bitly member

@manover changes look great. go ahead and squash this change.

@jehiah jehiah merged commit 810e11b into bitly:master Apr 3, 2013

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment