summaryrefslogtreecommitdiff
path: root/jstests/sharding/auto_rebalance.js
blob: 826e979d68b465e77541c7c1e1686e3aa3f88b83 (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
// Validates that auto rebalancing works and reaches quiescent state when replica sets are used as
// shards
(function() {

    'use strict';

    var st = new ShardingTest(
        {name: 'auto_rebalance_rs', mongos: 1, shards: 2, chunksize: 1, rs: {nodes: 3}});

    assert.writeOK(st.getDB("config").settings.update(
        {_id: "balancer"}, {$set: {"_secondaryThrottle": false}}, {upsert: true}));

    st.getDB("admin").runCommand({enableSharding: "TestDB_auto_rebalance_rs"});
    st.getDB("admin").runCommand({shardCollection: "TestDB_auto_rebalance_rs.foo", key: {x: 1}});

    var dbTest = st.getDB("TestDB_auto_rebalance_rs");

    var num = 100000;
    var bulk = dbTest.foo.initializeUnorderedBulkOp();
    for (var i = 0; i < num; i++) {
        bulk.insert(
            {_id: i, x: i, abc: "defg", date: new Date(), str: "all the talk on the market"});
    }
    assert.writeOK(bulk.execute());

    // Wait for the rebalancing to kick in
    st.startBalancer(60000);

    assert.soon(function() {
        var s1Chunks = st.getDB("config").chunks.count({shard: "auto_rebalance_rs-rs0"});
        var s2Chunks = st.getDB("config").chunks.count({shard: "auto_rebalance_rs-rs1"});
        var total = st.getDB("config").chunks.count({ns: "TestDB_auto_rebalance_rs.foo"});

        print("chunks: " + s1Chunks + " " + s2Chunks + " " + total);

        return s1Chunks > 0 && s2Chunks > 0 && (s1Chunks + s2Chunks == total);
    }, "Chunks failed to balance", 60000, 5000);

    // Ensure the range deleter quiesces
    st.rs0.awaitReplication(120000);
    st.rs1.awaitReplication(120000);

    // TODO: mongod only exits with MongoRunner.EXIT_ABRUPT in sharding_legacy_op_query_WT
    // this should be fixed by SERVER-22176
    st.stop({allowedExitCodes: [MongoRunner.EXIT_ABRUPT]});

})();