def start(num_shard): """Starts MongoDB cluster. @param num_shard the number of shard servers. """ print(yellow("Starting MongoDB instance..")) num_shard = int(num_shard) download_tarball(URL) if os.path.exists(env.config_dir): shutil.rmtree(env.config_dir) os.makedirs(env.data_dir) execute(start_config_server) execute(start_shared_server, hosts=env.workers[:num_shard]) local("sleep 5") print(yellow("Initialize MongoDB")) conn = pymongo.Connection("%s:27018" % env.head) print(yellow("MongoDB connected")) admin = conn.admin for shard in env.workers[:num_shard]: retry = 10 while retry: try: print(yellow("Adding %s:27017 to shared" % shard)) admin.command("addshard", "%s:27017" % shard) break except pymongo.errors.OperationFailure: retry -= 1 if retry == 0: raise time.sleep(2) db = conn.vsfs collection = db["test"] collection.create_index([("file", pymongo.ASCENDING)]) collection.ensure_index("file") admin.command("enableSharding", "vsfs") admin.command("shardCollection", "vsfs.test", key={"file": "hashed"}) for i in range(0, 61): collection.create_index([("index" + str(i), pymongo.ASCENDING)])
def download(): download_tarball(URL) download_tarball(CXX_DRIVER_URL, output="mongo-cxx-driver-v2.5.tgz") with lcd("mongo-cxx-driver-v2.5"): local("scons")