summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_aware_on_add_shard.js
blob: 0b9260c42d391a2c1521801abb7da59a70f1e9cf (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
/**
 * Tests that the addShard process initializes sharding awareness on an added standalone or
 * replica set shard that was started with --shardsvr.
 */

(function() {
"use strict";

const checkShardingStateInitialized = function(conn, configConnStr, shardName, clusterId) {
    const res = conn.getDB('admin').runCommand({shardingState: 1});
    assert.commandWorked(res);
    assert(res.enabled);
    assert.eq(shardName, res.shardName);
    assert(clusterId.equals(res.clusterId),
           'cluster id: ' + tojson(clusterId) + ' != ' + tojson(res.clusterId));
    assert.soon(() => configConnStr == conn.adminCommand({shardingState: 1}).configServer);
};

const checkShardMarkedAsShardAware = function(mongosConn, shardName) {
    const res = mongosConn.getDB('config').getCollection('shards').findOne({_id: shardName});
    assert.neq(null, res, "Could not find new shard " + shardName + " in config.shards");
    assert.eq(1, res.state);
};

// Create the cluster to test adding shards to.
const st = new ShardingTest({shards: 1});
const clusterId = st.s.getDB('config').getCollection('version').findOne().clusterId;
const newShardName = "newShard";

// Add a shard and ensure awareness.
const replTest = new ReplSetTest({nodes: 1});
replTest.startSet({shardsvr: ''});
replTest.initiate();

jsTest.log("Going to add replica set as shard: " + tojson(replTest));
assert.commandWorked(st.s.adminCommand({addShard: replTest.getURL(), name: newShardName}));
checkShardingStateInitialized(replTest.getPrimary(), st.configRS.getURL(), newShardName, clusterId);
checkShardMarkedAsShardAware(st.s, newShardName);

replTest.stopSet();

st.stop();
})();