Created
March 4, 2013 10:43
-
-
Save dariosky/5081432 to your computer and use it in GitHub Desktop.
Script for creating a complex mongodb shard for testing purpose on the local machine.
Wrote for the Mongodb 102 course (week6) to facilitate the setup. Change the settings on the top of the script and run it.
All commands are passed to mongo via subprocess, no pyMongo required.
I keep it as mongo shell reference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# @author: Dario Varotto | |
""" | |
Script for creating a complex mongodb shard for testing purpose on the local machine. | |
Wrote for the Mongodb 102 course (week6) to facilitate the setup. | |
Change the settings on the top of the script and run it. | |
All commands are passed to mongo via subprocess, no pyMongo required. | |
I keep it as mongo shell reference. | |
""" | |
# SETTINGS ******************************************************************* | |
dataDir = "data" | |
logDir = "log" | |
mongo_start_port = 27000 # the mongod will be there (grouped by replica set) | |
# there will be replica*shards mongod shard server | |
# then the config servers and then the mongos | |
replicaPrefix = "bigR" | |
n_replica = 3 | |
n_shards = 4 | |
n_config = 3 | |
n_mongos = 4 | |
hostname = "localhost" # to pass as mongos config | |
# **************************************************************************** | |
import os | |
import sys | |
import subprocess | |
from subprocess import Popen, PIPE | |
import time | |
def doRun(cmd): | |
print cmd | |
subprocess.Popen(cmd) | |
time.sleep(1) | |
def startDaemons(): | |
# lets start with config servers | |
try: | |
os.makedirs(logDir) | |
except: | |
pass | |
for i in range(n_config): | |
settings = { | |
"port":mongo_start_port + n_replica * n_shards + i, | |
"db":os.path.join(dataDir, "config%d" % (i + 1)), | |
"log":os.path.join(logDir, "config%d" % (i + 1)), | |
} | |
try: | |
os.makedirs(settings["db"]) | |
except: | |
pass | |
cmd = "mongod --configsvr --port %(port)d --smallfiles --oplogSize 50 --dbpath %(db)s --logappend --logpath %(log)s" % settings | |
doRun(cmd) | |
# now the mongoD processes | |
for i in range(n_shards): | |
for j in range(n_replica): | |
settings = { | |
"port":mongo_start_port + (i * 3) + j, | |
"db":os.path.join(dataDir, "mongod%d.%d" % ((i + 1), (j + 1))), | |
"log":os.path.join(logDir, "mongod%d.%d" % ((i + 1), (j + 1))), | |
"replicaName":"%s%d" % (replicaPrefix, i + 1), | |
} | |
try: | |
os.makedirs(settings["db"]) | |
except: | |
pass | |
cmd = "mongod --shardsvr --replSet %(replicaName)s --port %(port)d --smallfiles --oplogSize 50 --dbpath %(db)s --logappend --logpath %(log)s" % settings | |
doRun(cmd) | |
# finally the mongos | |
for i in range(n_mongos): | |
settings = { | |
"port":mongo_start_port + n_replica * n_shards + n_config + i, | |
"log":os.path.join(logDir, "mongos%d" % (i)), | |
"configServers":",".join(["%s:%d" % (hostname, port) for port in range(mongo_start_port + n_replica * n_shards, mongo_start_port + n_replica * n_shards + n_config,)]) | |
} | |
cmd = "mongos --port %(port)d --logappend -logpath %(log)s --configdb %(configServers)s" % settings | |
doRun(cmd) | |
# now we can initiate the replicaset | |
def initializeReplicas(): | |
for i in range(n_shards): | |
hostlist = [] | |
for j in range(n_replica): | |
memberhost = "%s:%d" % (hostname, mongo_start_port + (i * 3) + j) | |
hostlist.append(memberhost) | |
settings = { | |
"replicaName":"%s%d" % (replicaPrefix, i + 1), | |
"hostlist": ", ".join(['{_id:%d, host:"%s"}' % (j, memberhost) for j, memberhost in enumerate(hostlist) ]) | |
} | |
pipe = Popen("mongo --port %d " % (mongo_start_port + (i * 3)), stdin=PIPE) | |
print "Initiate on %s%d" % (replicaPrefix, i + 1) | |
command = """ | |
rs.initiate( | |
{ | |
_id:"%(replicaName)s", | |
members: [%(hostlist)s] | |
} | |
); | |
'' | |
""" % settings | |
print command | |
pipe.stdin.write(command) | |
time.sleep(1) | |
# for j in range(n_replica - 1): # add other hosts to the replica | |
# print " adding host on port %d" % (mongo_start_port + (i * 3) + 1 + j) | |
# command = 'rs.add("%s:%d")\n' % (hostname, mongo_start_port + (i * 3) + 1 + j) | |
# pipe.stdin.write(command) | |
pipe.stdin.close() | |
# now create shards, the name will be the name of the replicaset | |
def createShards(): | |
# let's connect to the first mongos | |
pipe = Popen("mongo --port %d " % (mongo_start_port + n_replica * n_shards + n_config), stdin=PIPE) | |
for i in range(n_shards): | |
settings = { | |
"replicaName":"%s%d" % (replicaPrefix, i + 1), | |
'firstHost':"%s:%d" % (hostname, mongo_start_port + (i * 3)), | |
} | |
print "Initiating shard %s" % settings["replicaName"] | |
command = 'sh.addShard("%(replicaName)s/%(firstHost)s");\n' % settings | |
print command | |
pipe.stdin.write(command) | |
time.sleep(1) | |
startDaemons() | |
initializeReplicas() | |
print "The end." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment