summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_aware_init_secondaries.js
blob: 59a8542f44b025af3612c2f3805673f2982c3b96 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * Tests for shard aware initialization on secondaries during startup and shard
 * identity document creation.
 * @tags: [requires_persistence]
 */

(function() {
"use strict";

var st = new ShardingTest({shards: 1});

var replTest = new ReplSetTest({nodes: 2});
replTest.startSet({shardsvr: ''});
var nodeList = replTest.nodeList();
replTest.initiate({
    _id: replTest.name,
    members: [{_id: 0, host: nodeList[0], priority: 1}, {_id: 1, host: nodeList[1], priority: 0}]
});

var priConn = replTest.getPrimary();

var configConnStr = st.configRS.getURL();

var shardIdentityDoc = {
    _id: 'shardIdentity',
    configsvrConnectionString: configConnStr,
    shardName: 'newShard',
    clusterId: ObjectId()
};

// Simulate the upsert that is performed by a config server on addShard.
var shardIdentityQuery = {
    _id: shardIdentityDoc._id,
    shardName: shardIdentityDoc.shardName,
    clusterId: shardIdentityDoc.clusterId
};
var shardIdentityUpdate = {
    $set: {configsvrConnectionString: shardIdentityDoc.configsvrConnectionString}
};
assert.commandWorked(priConn.getDB('admin').system.version.update(
    shardIdentityQuery, shardIdentityUpdate, {upsert: true, writeConcern: {w: 2}}));

var secConn = replTest.getSecondary();
secConn.setSlaveOk(true);

var res = secConn.getDB('admin').runCommand({shardingState: 1});

assert(res.enabled, tojson(res));
assert.eq(shardIdentityDoc.configsvrConnectionString, res.configServer);
assert.eq(shardIdentityDoc.shardName, res.shardName);
assert.eq(shardIdentityDoc.clusterId, res.clusterId);

replTest.restart(replTest.getNodeId(secConn));
replTest.waitForPrimary();
replTest.awaitSecondaryNodes();

secConn = replTest.getSecondary();
secConn.setSlaveOk(true);

res = secConn.getDB('admin').runCommand({shardingState: 1});

assert(res.enabled, tojson(res));
assert.eq(shardIdentityDoc.configsvrConnectionString, res.configServer);
assert.eq(shardIdentityDoc.shardName, res.shardName);
assert.eq(shardIdentityDoc.clusterId, res.clusterId);

replTest.stopSet();

st.stop();
})();