summaryrefslogtreecommitdiff
path: root/jstests/sharding/sharding_balance1.js
blob: 18a86e8b76d4b9f743b737a5caf7be25e1f326c2 (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
(function() {
'use strict';

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

const dbName = 'ShardingBalanceTest';
const collName = 'foo';
const coll = st.getDB(dbName).getCollection(collName);
const minChunkNum = 20;

assert.commandWorked(st.s.adminCommand({enablesharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard1.shardName);

const bigStringSize = 10000;
const bigString = "X=".repeat(bigStringSize / 2);

jsTest.log("Inserting documents that will account for at least 20MB");
var insertedChars = 0;
var num = 0;
var bulk = coll.initializeUnorderedBulkOp();
while (insertedChars < (minChunkNum * 1024 * 1024)) {
    bulk.insert({_id: num++, s: bigString});
    insertedChars += bigStringSize;
}
assert.commandWorked(bulk.execute());

assert.commandWorked(st.s.adminCommand({shardcollection: coll.getFullName(), key: {_id: 1}}));
jsTest.log("Checking initial chunk distribution: " + st.chunkCounts(collName, dbName));
assert.lt(minChunkNum,
          st.config.chunks.count({ns: coll.getFullName()}),
          "Number of initial chunks is less then expected");
assert.lt(minChunkNum,
          st.chunkDiff(collName, dbName),
          "The initial chunks difference between the shards is less then expected");

jsTest.log("Await for the balancer to reduce the chunk imbalance between the shards");
// Make sure there's enough time here, since balancing can sleep for 15s or so between balances.
st.awaitBalance(collName, dbName, 1000 * 60 * 5 /* 5 min */);

st.stop();
})();