Skip to content

Instantly share code, notes, and snippets.

@idning
Created April 25, 2014 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idning/11279924 to your computer and use it in GitHub Desktop.
Save idning/11279924 to your computer and use it in GitHub Desktop.
mongodb-pre-split
#3. Pre-Splitting
# export_size > 64K(records) and no data in dest collection
chunksize = self.args.chunksize # default 64K items (for 64MBytes chunksize)
maxshardkey = self.args.maxshardkey
if not(self.pre_src_count > chunksize and self.pre_to_count == 0):
logging.info('we will not pre-split !')
return
logging.info('we will pre-split by chunksize: %d, maxshardkey is: %d' % (chunksize, maxshardkey))
splits = self.pre_src_count/chunksize
shardkey_chunk_size = maxshardkey/splits
#split
for i in range(1, splits):
split = i * shardkey_chunk_size
toshard = random_shard()
try:
logging.info('split chunk %s @ %s' % (ns, str({shardkey:split})) )
self.dest_conn.admin.command('split', ns, middle={shardkey:split})
except Exception as e:
logging.warn('exception: ' + str(e))
#move
for i in range(1, splits):
split = i * shardkey_chunk_size
toshard = random_shard()
try:
logging.info('move chunk %s [find:%s] [to:%s]' % (ns, str({shardkey:split+1}), toshard ))
self.dest_conn.admin.command('moveChunk', ns, find={shardkey:split+1}, to=toshard)
except Exception as e:
logging.warn('exception: ' + str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment