summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_existing.js
blob: 754d2a8bea8ae5793631c5be9ae69f125c491605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(function() {
    'use strict';

    var s = new ShardingTest({name: "shard_existing", shards: 2, mongos: 1, other: {chunkSize: 1}});
    var db = s.getDB("test");

    var stringSize = 10000;
    var numDocs = 2000;

    // we want a lot of data, so lets make a string to cheat :)
    var bigString = new Array(stringSize).toString();
    var docSize = Object.bsonsize({_id: numDocs, s: bigString});
    var totalSize = docSize * numDocs;
    print("NumDocs: " + numDocs + " DocSize: " + docSize + " TotalSize: " + totalSize);

    var bulk = db.data.initializeUnorderedBulkOp();
    for (var i = 0; i < numDocs; i++) {
        bulk.insert({_id: i, s: bigString});
    }
    assert.writeOK(bulk.execute());

    var avgObjSize = db.data.stats().avgObjSize;
    var dataSize = db.data.stats().size;
    assert.lte(totalSize, dataSize);

    s.adminCommand({enablesharding: "test"});
    s.ensurePrimaryShard('test', 'shard0001');
    var res = s.adminCommand({shardcollection: "test.data", key: {_id: 1}});
    printjson(res);

    // number of chunks should be approx equal to the total data size / half the chunk size
    var numChunks = s.config.chunks.find({ns: 'test.data'}).itcount();
    var guess = Math.ceil(dataSize / (512 * 1024 + avgObjSize));
    assert(Math.abs(numChunks - guess) < 2, "not right number of chunks");

    s.stop();
})();