summaryrefslogtreecommitdiff
path: root/jstests/sharding/sharding_balance3.js
blob: 01383286b8855eb8ef32c48be4ebb8a5bc3ff0e8 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Waits for the balancer to run once, then stops it and checks that it is no longer running.

(function() {

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

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

    s.config.settings.find().forEach(printjson);

    db = s.getDB("test");

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

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

    s.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);
    }

    assert.lt(10, diff1());

    // Wait for balancer to kick in.
    var initialDiff = diff1();
    assert.soon(function() {
        return diff1() != initialDiff;
    }, "Balancer did not kick in");

    print("* A");
    print("disabling the balancer");
    s.config.settings.update({_id: "balancer"}, {$set: {stopped: true}}, true);
    s.config.settings.find().forEach(printjson);
    print("* B");

    print(diff1());

    var currDiff = diff1();
    var waitTime = 0;
    var startTime = Date.now();
    while (waitTime < (1000 * 60)) {
        // Wait for 60 seconds to ensure balancer did not run
        assert.eq(currDiff, diff1(), "balance with stopped flag should not have happened");
        sleep(5000);
        waitTime = Date.now() - startTime;
    }

    s.stop();

})();