summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/sharding/sharding_balance1.js62
-rw-r--r--src/mongo/shell/shardingtest.js2
2 files changed, 27 insertions, 37 deletions
diff --git a/jstests/sharding/sharding_balance1.js b/jstests/sharding/sharding_balance1.js
index d6e0384d7f7..18a86e8b76d 100644
--- a/jstests/sharding/sharding_balance1.js
+++ b/jstests/sharding/sharding_balance1.js
@@ -1,51 +1,41 @@
(function() {
'use strict';
-var s = new ShardingTest({shards: 2, mongos: 1, other: {chunkSize: 1, enableBalancer: true}});
+var st = new ShardingTest({shards: 2, mongos: 1, other: {chunkSize: 1, enableBalancer: true}});
-assert.commandWorked(s.s0.adminCommand({enablesharding: "test"}));
-s.ensurePrimaryShard('test', s.shard1.shardName);
+const dbName = 'ShardingBalanceTest';
+const collName = 'foo';
+const coll = st.getDB(dbName).getCollection(collName);
+const minChunkNum = 20;
-var db = s.getDB("test");
+assert.commandWorked(st.s.adminCommand({enablesharding: dbName}));
+st.ensurePrimaryShard(dbName, st.shard1.shardName);
-var bigString = "";
-while (bigString.length < 10000) {
- bigString += "asdasdasdasdadasdasdasdasdasdasdasdasda";
-}
+const bigStringSize = 10000;
+const bigString = "X=".repeat(bigStringSize / 2);
-var inserted = 0;
+jsTest.log("Inserting documents that will account for at least 20MB");
+var insertedChars = 0;
var num = 0;
-var bulk = db.foo.initializeUnorderedBulkOp();
-while (inserted < (20 * 1024 * 1024)) {
+var bulk = coll.initializeUnorderedBulkOp();
+while (insertedChars < (minChunkNum * 1024 * 1024)) {
bulk.insert({_id: num++, s: bigString});
- inserted += bigString.length;
+ insertedChars += bigStringSize;
}
assert.commandWorked(bulk.execute());
-assert.commandWorked(s.s0.adminCommand({shardcollection: "test.foo", key: {_id: 1}}));
-assert.lt(20, s.config.chunks.count({"ns": "test.foo"}), "setup2");
-
-function diff1() {
- var x = s.chunkCounts("foo");
- printjson(x);
- return Math.max(x[s.shard0.shardName], x[s.shard1.shardName]) -
- Math.min(x[s.shard0.shardName], x[s.shard1.shardName]);
-}
-
-function sum() {
- var x = s.chunkCounts("foo");
- return x[s.shard0.shardName] + x[s.shard1.shardName];
-}
-
-assert.lt(20, diff1(), "big differential here");
-print(diff1());
+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");
-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);
+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 */);
-s.stop();
+st.stop();
})();
diff --git a/src/mongo/shell/shardingtest.js b/src/mongo/shell/shardingtest.js
index fa3bf67b546..8e7e721cd5c 100644
--- a/src/mongo/shell/shardingtest.js
+++ b/src/mongo/shell/shardingtest.js
@@ -560,7 +560,7 @@ var ShardingTest = function(params) {
this.chunkDiff = function(collName, dbName) {
var c = this.chunkCounts(collName, dbName);
- var min = 100000000;
+ var min = Number.MAX_VALUE;
var max = 0;
for (var s in c) {
if (c[s] < min)