Skip to content

Instantly share code, notes, and snippets.

@sindbach
Created June 6, 2016 06:40
Show Gist options
  • Save sindbach/1efb8628d5bf2652aca204031fe24888 to your computer and use it in GitHub Desktop.
Save sindbach/1efb8628d5bf2652aca204031fe24888 to your computer and use it in GitHub Desktop.
Shard existing collection JS test
var st = new ShardingTest({shards: 2, mongos: 1, other: { chunkSize: 1 }});
var db = st.s.getDB('test');
var coll = db.world;
/* Create ashard key length (approximate) */
var keyval = "";
var keylength = 512;
/* Create a document of approximately 1MB (minus shard key) */
var padding = 'i';
var paddingsize = 1000000 - keylength;
for ( i = 0; i < paddingsize; i++ ) {padding = padding + 'i'};
/* Number of documents - which decides the size of the collection .*/
var docsnumber = 35000;
var bulk = coll.initializeUnorderedBulkOp();
for ( var n = 0; n < docsnumber; n++ ) {
var numStr = n + '';
var keyPadLen = keylength - numStr.length - 8; // minus some extra (objectId)
var keyPadStr = '!';
for (var i = 1; i < keyPadLen; i++) {
keyPadStr = keyPadStr + '!';
}
/* Inserting 1Kb document
Creating :
{ _id: ObjectId('5743758b3c8d024caa9731c8'),
p: "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...",
k: "9596!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!..." }
*/
keyVal = numStr + keyPadStr;
var doc = { p: padding, k: keyVal };
bulk.insert(doc);
}
assert.writeOK(bulk.execute());
db.runCommand({ getLastError: 1 });
print('____________________________DEBUG___________________________');
var shardKeySize = Object.bsonsize({k:keyVal});
print('Shard key size : '+ shardKeySize);
gc();
var res = coll.ensureIndex({ k: 1 });
res = coll.getIndexes();
print('____________________________DEBUG___________________________');
print('Indexes: ' + tojson(res));
print('____________________________DEBUG___________________________');
print('collection stats: ' + tojson(coll.stats()));
print('____________________________DEBUG___________________________');
var avgObjSize = coll.stats().avgObjSize;
var dataSize = coll.stats().size;
print('Average Object Size: '+ avgObjSize);
print('Total data size: '+ dataSize);
db.adminCommand({ enableSharding: 'test' });
res = db.adminCommand({ shardCollection: 'test.world', key: { k: 1 }});
print('____________________________DEBUG___________________________');
printjson('shard collection command result: ' + tojson(res));
assert(res.ok);
print('____________________________DEBUG___________________________');
// sleep to give time for balancing
print('Sleep for x seconds');
var sleeping = sleep(60 * 1000 * 10);
res = db.printShardingStatus();
print('____________________________DEBUG___________________________');
printjson('Sharding status :'+ tojson(res));
st.stop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment