summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_aware_primary_failover.js
blob: 5ae7dadd5ae7b18bd787f091f93dd3405e8e8675 (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
/**
 * Test that a new primary that gets elected will properly perform shard initialization.
 */
(function() {
"use strict";

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

// Setting CWWC for addShard to work, as implicitDefaultWC is set to w:1.
assert.commandWorked(st.s.adminCommand(
    {setDefaultRWConcern: 1, defaultWriteConcern: {w: 1}, writeConcern: {w: "majority"}}));

var replTest = new ReplSetTest({nodes: 3});
replTest.startSet({shardsvr: ''});

var nodes = replTest.nodeList();
replTest.initiate({
    _id: replTest.name,
    members: [
        {_id: 0, host: nodes[0]},
        {_id: 1, host: nodes[1]},
        {_id: 2, host: nodes[2], arbiterOnly: true}
    ]
});

var primaryConn = replTest.getPrimary();

var shardIdentityDoc = {
    _id: 'shardIdentity',
    configsvrConnectionString: st.configRS.getURL(),
    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(primaryConn.getDB('admin').system.version.update(
    shardIdentityQuery, shardIdentityUpdate, {upsert: true, writeConcern: {w: 'majority'}}));

replTest.stopPrimary();
replTest.waitForPrimary(30000);

primaryConn = replTest.getPrimary();

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

assert(res.enabled);
assert.eq(shardIdentityDoc.shardName, res.shardName);
assert.eq(shardIdentityDoc.clusterId, res.clusterId);
assert.soon(() => shardIdentityDoc.configsvrConnectionString ==
                primaryConn.adminCommand({shardingState: 1}).configServer);

replTest.stopSet();

st.stop();
})();