Permalink
Browse files

Visual Studio -- update shell/mongo_vstudio.cpp

  • Loading branch information...
1 parent f9828af commit 4ada6dfbc2aa8e3f6702243c5b29d08949ce7ef9 @tadmarshall tadmarshall committed Jun 20, 2012
Showing with 206 additions and 1 deletion.
  1. +206 −1 shell/mongo_vstudio.cpp
View
@@ -744,7 +744,8 @@ const StringData _jscode_raw_utils =
"\"jstests/notablescan.js\",\n"
"\"jstests/drop2.js\",\n"
"\"jstests/dropdb_race.js\",\n"
-"\"jstests/bench_test1.js\"] );\n"
+"\"jstests/bench_test1.js\",\n"
+"\"jstests/queryoptimizera.js\"] );\n"
"\n"
"// some tests can't be run in parallel with each other\n"
"var serialTestsArr = [ \"jstests/fsync.js\",\n"
@@ -1725,6 +1726,22 @@ const StringData _jscode_raw_utils_sh =
"return res;\n"
"}\n"
"\n"
+"\n"
+"sh._dataFormat = function( bytes ){\n"
+"if( bytes < 1024 ) return Math.floor( bytes ) + \"b\"\n"
+"if( bytes < 1024 * 1024 ) return Math.floor( bytes / 1024 ) + \"kb\"\n"
+"if( bytes < 1024 * 1024 * 1024 ) return Math.floor( ( Math.floor( bytes / 1024 ) / 1024 ) * 100 ) / 100 + \"Mb\"\n"
+"return Math.floor( ( Math.floor( bytes / ( 1024 * 1024 ) ) / 1024 ) * 100 ) / 100 + \"Gb\"\n"
+"}\n"
+"\n"
+"sh._collRE = function( coll ){\n"
+"return RegExp( \"^\" + (coll + \"\").replace(/\\./g, \"\\\\.\") + \"-.*\" )\n"
+"}\n"
+"\n"
+"sh._pchunk = function( chunk ){\n"
+"return \"[\" + tojson( chunk.min ) + \" -> \" + tojson( chunk.max ) + \"]\"\n"
+"}\n"
+"\n"
"sh.help = function() {\n"
"print( \"\\tsh.addShard( host ) server:port OR setname/server:port\" )\n"
"print( \"\\tsh.enableSharding(dbname) enables sharding on the database dbname\" )\n"
@@ -3216,6 +3233,7 @@ const StringData _jscode_raw_collection =
"print(\"\\tdb.\" + shortName + \".update(query, object[, upsert_bool, multi_bool])\");\n"
"print(\"\\tdb.\" + shortName + \".validate( <full> ) - SLOW\");;\n"
"print(\"\\tdb.\" + shortName + \".getShardVersion() - only for use with sharding\");\n"
+"print(\"\\tdb.\" + shortName + \".getShardDistribution() - prints statistics about data distribution in the cluster\");\n"
"return __magicNoPrint;\n"
"}\n"
"\n"
@@ -3808,6 +3826,193 @@ const StringData _jscode_raw_collection =
"}\n"
"return ret;\n"
"}\n"
+"\n"
+"\n"
+"// Sharding additions\n"
+"\n"
+"/*\n"
+"Usage :\n"
+"\n"
+"mongo <mongos>\n"
+"> load('path-to-file/shardingAdditions.js')\n"
+"Loading custom sharding extensions...\n"
+"true\n"
+"\n"
+"> var collection = db.getMongo().getCollection(\"foo.bar\")\n"
+"> collection.getShardDistribution() // prints statistics related to the collection's data distribution\n"
+"\n"
+"> collection.getSplitKeysForChunks() // generates split points for all chunks in the collection, based on the\n"
+"// default maxChunkSize or alternately a specified chunk size\n"
+"> collection.getSplitKeysForChunks( 10 ) // Mb\n"
+"\n"
+"> var splitter = collection.getSplitKeysForChunks() // by default, the chunks are not split, the keys are just\n"
+"// found. A splitter function is returned which will actually\n"
+"// do the splits.\n"
+"\n"
+"> splitter() // ! Actually executes the splits on the cluster !\n"
+"\n"
+"*/\n"
+"\n"
+"DBCollection.prototype.getShardDistribution = function(){\n"
+"\n"
+"var stats = this.stats()\n"
+"\n"
+"if( ! stats.sharded ){\n"
+"print( \"Collection \" + this + \" is not sharded.\" )\n"
+"return\n"
+"}\n"
+"\n"
+"var config = this.getMongo().getDB(\"config\")\n"
+"\n"
+"var numChunks = 0\n"
+"\n"
+"for( var shard in stats.shards ){\n"
+"\n"
+"var shardDoc = config.shards.findOne({ _id : shard })\n"
+"\n"
+"print( \"\\nShard \" + shard + \" at \" + shardDoc.host )\n"
+"\n"
+"var shardStats = stats.shards[ shard ]\n"
+"\n"
+"var chunks = config.chunks.find({ _id : sh._collRE( this ), shard : shard }).toArray()\n"
+"\n"
+"numChunks += chunks.length\n"
+"\n"
+"var estChunkData = shardStats.size / chunks.length\n"
+"var estChunkCount = Math.floor( shardStats.count / chunks.length )\n"
+"\n"
+"print( \" data : \" + sh._dataFormat( shardStats.size ) +\n"
+"\" docs : \" + shardStats.count +\n"
+"\" chunks : \" + chunks.length )\n"
+"print( \" estimated data per chunk : \" + sh._dataFormat( estChunkData ) )\n"
+"print( \" estimated docs per chunk : \" + estChunkCount )\n"
+"\n"
+"}\n"
+"\n"
+"print( \"\\nTotals\" )\n"
+"print( \" data : \" + sh._dataFormat( stats.size ) +\n"
+"\" docs : \" + stats.count +\n"
+"\" chunks : \" + numChunks )\n"
+"for( var shard in stats.shards ){\n"
+"\n"
+"var shardStats = stats.shards[ shard ]\n"
+"\n"
+"var estDataPercent = Math.floor( shardStats.size / stats.size * 10000 ) / 100\n"
+"var estDocPercent = Math.floor( shardStats.count / stats.count * 10000 ) / 100\n"
+"\n"
+"print( \" Shard \" + shard + \" contains \" + estDataPercent + \"% data, \" + estDocPercent + \"% docs in cluster, \" +\n"
+"\"avg obj size on shard : \" + sh._dataFormat( stats.shards[ shard ].avgObjSize ) )\n"
+"}\n"
+"\n"
+"print( \"\\n\" )\n"
+"\n"
+"}\n"
+"\n"
+"// In testing phase, use with caution\n"
+"DBCollection.prototype._getSplitKeysForChunks = function( chunkSize ){\n"
+"\n"
+"var stats = this.stats()\n"
+"\n"
+"if( ! stats.sharded ){\n"
+"print( \"Collection \" + this + \" is not sharded.\" )\n"
+"return\n"
+"}\n"
+"\n"
+"var config = this.getMongo().getDB(\"config\")\n"
+"\n"
+"if( ! chunkSize ){\n"
+"chunkSize = config.settings.findOne({ _id : \"chunksize\" }).value\n"
+"print( \"Chunk size not set, using default of \" + chunkSize + \"Mb\" )\n"
+"}\n"
+"else{\n"
+"print( \"Using chunk size of \" + chunkSize + \"Mb\" )\n"
+"}\n"
+"\n"
+"var shardDocs = config.shards.find().toArray()\n"
+"\n"
+"var allSplitPoints = {}\n"
+"var numSplits = 0\n"
+"\n"
+"for( var i = 0; i < shardDocs.length; i++ ){\n"
+"\n"
+"var shardDoc = shardDocs[i]\n"
+"var shard = shardDoc._id\n"
+"var host = shardDoc.host\n"
+"var sconn = new Mongo( host )\n"
+"\n"
+"var chunks = config.chunks.find({ _id : sh._collRE( this ), shard : shard }).toArray()\n"
+"\n"
+"print( \"\\nGetting split points for chunks on shard \" + shard + \" at \" + host )\n"
+"\n"
+"var splitPoints = []\n"
+"\n"
+"for( var j = 0; j < chunks.length; j++ ){\n"
+"var chunk = chunks[j]\n"
+"var result = sconn.getDB(\"admin\").runCommand({ splitVector : this + \"\", min : chunk.min, max : chunk.max, maxChunkSize : chunkSize })\n"
+"if( ! result.ok ){\n"
+"print( \" Had trouble getting split keys for chunk \" + sh._pchunk( chunk ) + \" :\\n\" )\n"
+"printjson( result )\n"
+"}\n"
+"else{\n"
+"splitPoints = splitPoints.concat( result.splitKeys )\n"
+"\n"
+"if( result.splitKeys.length > 0 )\n"
+"print( \" Added \" + result.splitKeys.length + \" split points for chunk \" + sh._pchunk( chunk ) )\n"
+"}\n"
+"}\n"
+"\n"
+"print( \"Total splits for shard \" + shard + \" : \" + splitPoints.length )\n"
+"\n"
+"numSplits += splitPoints.length\n"
+"allSplitPoints[ shard ] = splitPoints\n"
+"\n"
+"}\n"
+"\n"
+"// Get most recent migration\n"
+"var migration = config.changelog.find({ what : /^move.*/ }).sort({ time : -1 }).limit( 1 ).toArray()\n"
+"if( migration.length == 0 )\n"
+"print( \"\\nNo migrations found in changelog.\" )\n"
+"else {\n"
+"migration = migration[0]\n"
+"print( \"\\nMost recent migration activity was on \" + migration.ns + \" at \" + migration.time )\n"
+"}\n"
+"\n"
+"var admin = this.getMongo().getDB(\"admin\")\n"
+"var coll = this\n"
+"var splitFunction = function(){\n"
+"\n"
+"// Turn off the balancer, just to be safe\n"
+"print( \"Turning off balancer...\" )\n"
+"config.settings.update({ _id : \"balancer\" }, { $set : { stopped : true } }, true )\n"
+"print( \"Sleeping for 30s to allow balancers to detect change. To be extra safe, check config.changelog\" +\n"
+"\" for recent migrations.\" )\n"
+"sleep( 30000 )\n"
+"\n"
+"for( shard in allSplitPoints ){\n"
+"for( var i = 0; i < allSplitPoints[ shard ].length; i++ ){\n"
+"var splitKey = allSplitPoints[ shard ][i]\n"
+"print( \"Splitting at \" + tojson( splitKey ) )\n"
+"printjson( admin.runCommand({ split : coll + \"\", middle : splitKey }) )\n"
+"}\n"
+"}\n"
+"\n"
+"print( \"Turning the balancer back on.\" )\n"
+"config.settings.update({ _id : \"balancer\" }, { $set : { stopped : false } } )\n"
+"sleep( 1 )\n"
+"}\n"
+"\n"
+"print( \"\\nGenerated \" + numSplits + \" split keys, run output function to perform splits.\\n\" +\n"
+"\" ex : \\n\" +\n"
+"\" > var splitter = <collection>.getSplitKeysForChunks()\\n\" +\n"
+"\" > splitter() // Execute splits on cluster !\\n\" )\n"
+"\n"
+"return splitFunction\n"
+"\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"\n"
;
extern const JSFile collection;
const JSFile collection = { "shell/collection.js" , _jscode_raw_collection };

0 comments on commit 4ada6df

Please sign in to comment.