summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_collection_verify_initial_chunks.js
blob: 38e3d4d67a691fba14caef0e5f66b83918ed7a9e (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
/**
 * Verify initial chunks are properly created and distributed in various combinations of shard key
 * and empty/non-empty collections.
 */
(function() {
'use strict';

let st = new ShardingTest({mongos: 1, shards: 2});
let mongos = st.s0;

let config = mongos.getDB("config");
let db = mongos.getDB('TestDB');

assert.commandWorked(mongos.adminCommand({enableSharding: 'TestDB'}));
st.ensurePrimaryShard('TestDB', st.shard1.shardName);

function checkChunkCounts(collName, chunksOnShard0, chunksOnShard1) {
    let counts = st.chunkCounts(collName, 'TestDB');
    assert.eq(
        chunksOnShard0, counts[st.shard0.shardName], 'Count mismatch on shard0: ' + tojson(counts));
    assert.eq(
        chunksOnShard1, counts[st.shard1.shardName], 'Count mismatch on shard1: ' + tojson(counts));
}

// Unsupported: Range sharding + numInitialChunks
assert.commandFailed(mongos.adminCommand(
    {shardCollection: 'TestDB.RangeCollEmpty', key: {aKey: 1}, numInitialChunks: 6}));

// Unsupported: Hashed sharding + numInitialChunks + non-empty collection
assert.commandWorked(db.HashedCollNotEmpty.insert({aKey: 1}));
assert.commandWorked(db.HashedCollNotEmpty.createIndex({aKey: "hashed"}));
assert.commandFailed(mongos.adminCommand(
    {shardCollection: 'TestDB.HashedCollNotEmpty', key: {aKey: "hashed"}, numInitialChunks: 6}));

// Supported: Hashed sharding + numInitialChunks + empty collection
// Expected: Even chunk distribution
assert.commandWorked(db.HashedCollEmpty.createIndex({aKey: "hashed"}));
assert.commandWorked(mongos.adminCommand(
    {shardCollection: 'TestDB.HashedCollEmpty', key: {aKey: "hashed"}, numInitialChunks: 6}));
checkChunkCounts('HashedCollEmpty', 3, 3);

// Supported: Hashed sharding + numInitialChunks + non-existent collection
// Expected: Even chunk distribution
assert.commandWorked(mongos.adminCommand(
    {shardCollection: 'TestDB.HashedCollNonExistent', key: {aKey: "hashed"}, numInitialChunks: 6}));
checkChunkCounts('HashedCollNonExistent', 3, 3);

st.stop();
})();