Skip to content

Instantly share code, notes, and snippets.

@Alex-Ikanow
Last active August 29, 2015 13:57
Show Gist options
  • Save Alex-Ikanow/9508101 to your computer and use it in GitHub Desktop.
Save Alex-Ikanow/9508101 to your computer and use it in GitHub Desktop.
Fixed version of getSplitKeysForChunks
DBCollection.prototype.getSplitKeysForChunks = function ( keyPattern, chunkSize ){
var stats = this.stats()
if( ! stats.sharded ){
print( "Collection " + this + " is not sharded." )
return
}
var config = this.getMongo().getDB("config")
if( ! chunkSize ){
chunkSize = config.settings.findOne({ _id : "chunksize" }).value
print( "Chunk size not set, using default of " + chunkSize + "MB" )
}
else{
print( "Using chunk size of " + chunkSize + "MB" )
}
var shardDocs = config.shards.find().toArray()
var allSplitPoints = {}
var numSplits = 0
for( var i = 0; i < shardDocs.length; i++ ){
var shardDoc = shardDocs[i]
var shard = shardDoc._id
var host = shardDoc.host
var sconn = new Mongo( host )
var chunks = config.chunks.find({ _id : sh._collRE( this ), shard : shard }).toArray()
print( "\nGetting split points for chunks on shard " + shard + " at " + host )
var splitPoints = []
for( var j = 0; j < chunks.length; j++ ){
var chunk = chunks[j]
var result = sconn.getDB("admin").runCommand({ splitVector : this + "", min : chunk.min, max : chunk.max, maxChunkSize : chunkSize, keyPattern:keyPattern })
if( ! result.ok ){
print( " Had trouble getting split keys for chunk " + sh._pchunk( chunk ) + " :\n" )
printjson( result )
}
else{
splitPoints = splitPoints.concat( result.splitKeys )
if( result.splitKeys.length > 0 )
print( " Added " + result.splitKeys.length + " split points for chunk " + sh._pchunk( chunk ) )
}
}
print( "Total splits for shard " + shard + " : " + splitPoints.length )
numSplits += splitPoints.length
allSplitPoints[ shard ] = splitPoints
}
var migration = config.changelog.find({ what : /^move.*/ }).sort({ time : -1 }).limit( 1 ).toArray()
if( migration.length == 0 )
print( "\nNo migrations found in changelog." )
else {
migration = migration[0]
print( "\nMost recent migration activity was on " + migration.ns + " at " + migration.time )
}
var admin = this.getMongo().getDB("admin")
var coll = this
var splitFunction = function(){
print( "Turning off balancer..." )
config.settings.update({ _id : "balancer" }, { $set : { stopped : true } }, true )
print( "Sleeping for 30s to allow balancers to detect change. To be extra safe, check config.changelog" +
" for recent migrations." )
sleep( 30000 )
for( shard in allSplitPoints ){
for( var i = 0; i < allSplitPoints[ shard ].length; i++ ){
var splitKey = allSplitPoints[ shard ][i]
print( "Splitting at " + tojson( splitKey ) )
printjson( admin.runCommand({ split : coll + "", middle : splitKey }) )
}
}
print( "Turning the balancer back on." )
config.settings.update({ _id : "balancer" }, { $set : { stopped : false } } )
sleep( 1 )
}
splitFunction.getSplitPoints = function(){ return allSplitPoints; }
print( "\nGenerated " + numSplits + " split keys, run output function to perform splits.\n" +
" ex : \n" +
" > var splitter = <collection>.getSplitKeysForChunks()\n" +
" > splitter() // Execute splits on cluster !\n" )
return splitFunction
}
splitter = db['COLLECTION_NAME'].getSplitKeysForChunks({_id:1})
splitter();
@Alex-Ikanow
Copy link
Author

The version in mongo shell (2.4.9) seems to be broken (it doesn't pass keyPattern into splitVector, which is a mandatory field). Otherwise it seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment