summaryrefslogtreecommitdiff
path: root/jstests/sharding/sharding_balance1.js
blob: 22a8aaa621030b2c0224918db37ff06820c52f20 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
(function() {
    'use strict';

    var s = new ShardingTest({shards: 2, mongos: 1, other: {chunkSize: 1, enableBalancer: true}});

    assert.commandWorked(s.s0.adminCommand({enablesharding: "test"}));
    s.ensurePrimaryShard('test', 'shard0001');

    var db = s.getDB("test");

    var bigString = "";
    while (bigString.length < 10000) {
        bigString += "asdasdasdasdadasdasdasdasdasdasdasdasda";
    }

    var inserted = 0;
    var num = 0;
    var bulk = db.foo.initializeUnorderedBulkOp();
    while (inserted < (20 * 1024 * 1024)) {
        bulk.insert({_id: num++, s: bigString});
        inserted += bigString.length;
    }
    assert.writeOK(bulk.execute());

    assert.commandWorked(s.s0.adminCommand({shardcollection: "test.foo", key: {_id: 1}}));
    assert.lt(20, s.config.chunks.count(), "setup2");

    function diff1() {
        var x = s.chunkCounts("foo");
        printjson(x);
        return Math.max(x.shard0000, x.shard0001) - Math.min(x.shard0000, x.shard0001);
    }

    function sum() {
        var x = s.chunkCounts("foo");
        return x.shard0000 + x.shard0001;
    }

    assert.lt(20, diff1(), "big differential here");
    print(diff1());

    assert.soon(function() {
        var d = diff1();
        return d < 5;
        // Make sure there's enough time here, since balancing can sleep for 15s or so between
        // balances.
    }, "balance didn't happen", 1000 * 60 * 5, 5000);

    s.stop();
})();