summaryrefslogtreecommitdiff
path: root/jstests/replsets/force_reconfig_sets_newly_added_field_correctly.js
blob: 1c5eca5622709e0ac038aed1a3c12d8e1df3c052 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * Verify that force reconfigs overwrite the 'newlyAdded' field correctly in a replica set.
 *
 * @tags: [
 * ]
 */

(function() {
"use strict";
load("jstests/replsets/rslib.js");
load('jstests/libs/fail_point_util.js');

const rst = new ReplSetTest({name: jsTestName(), nodes: 1});
rst.startSet();
rst.initiateWithHighElectionTimeout();

const primary = rst.getPrimary();

const dbName = "testdb";
const collName = "testcoll";
const primaryDb = primary.getDB(dbName);
const primaryColl = primaryDb.getCollection(collName);

assert.commandWorked(primaryColl.insert({"starting": "doc"}));

assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 1,
});

const addNode = (id, {newlyAdded, force, shouldSucceed, failureCode} = {}) => {
    jsTestLog(`Adding node ${id}, newlyAdded: ${newlyAdded}, force: ${force}, shouldSucceed: ${
        shouldSucceed}, failureCode: ${failureCode}`);

    const newNode = rst.add({
        rsConfig: {priority: 0},
        setParameter: {
            'failpoint.forceSyncSourceCandidate':
                tojson({mode: 'alwaysOn', data: {"hostAndPort": primary.host}}),
            'failpoint.initialSyncHangBeforeFinish': tojson({mode: 'alwaysOn'}),
            'numInitialSyncAttempts': 1,
        }
    });

    let newNodeObj = {
        _id: id,
        host: newNode.host,
        priority: 0,
    };

    if (newlyAdded) {
        newNodeObj.newlyAdded = true;
    }

    let config = assert.commandWorked(primary.adminCommand({replSetGetConfig: 1})).config;
    config.version++;
    config.members.push(newNodeObj);

    if (!shouldSucceed) {
        jsTestLog("Running reconfig with bad config " + tojsononeline(config));

        assert.commandFailedWithCode(primary.adminCommand({replSetReconfig: config, force: force}),
                                     failureCode);
        rst.remove(newNode);
        return null;
    }

    jsTestLog("Running reconfig with valid config " + tojsononeline(config));
    assert(!failureCode);
    assert.commandWorked(primary.adminCommand({replSetReconfig: config, force: force}));
    rst.waitForConfigReplication(primary, rst.nodes);

    assert.commandWorked(newNode.adminCommand({
        waitForFailPoint: "initialSyncHangBeforeFinish",
        timesEntered: 1,
        maxTimeMS: kDefaultWaitForFailPointTimeout,
    }));

    return newNode;
};

jsTestLog("Fail adding a new node with 'newlyAdded' with force reconfig");
addNode(2, {
    newlyAdded: true,
    force: true,
    shouldSucceed: false,
    failureCode: ErrorCodes.InvalidReplicaSetConfig
});
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 1,
});

jsTestLog("Fail adding a new node with 'newlyAdded' with safe reconfig");
addNode(2, {
    newlyAdded: true,
    force: false,
    shouldSucceed: false,
    failureCode: ErrorCodes.InvalidReplicaSetConfig
});
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 1,
});

jsTestLog("Add a new node without 'newlyAdded' with force reconfig");
const firstNewNode = addNode(2, {newlyAdded: false, force: true, shouldSucceed: true});
assert.eq(false, isMemberNewlyAdded(primary, 1, true /* force */));
assertVoteCount(primary, {
    votingMembersCount: 2,
    majorityVoteCount: 2,
    writableVotingMembersCount: 2,
    writeMajorityCount: 2,
    totalMembersCount: 2,
});

jsTestLog("Add a new node without 'newlyAdded' with safe reconfig");
const secondNewNode = addNode(3, {newlyAdded: false, force: false, shouldSucceed: true});
assert.eq(false, isMemberNewlyAdded(primary, 1, false /* force */));
assert.eq(true, isMemberNewlyAdded(primary, 2, false /* force */));
assertVoteCount(primary, {
    votingMembersCount: 2,
    majorityVoteCount: 2,
    writableVotingMembersCount: 2,
    writeMajorityCount: 2,
    totalMembersCount: 3,
});

jsTestLog(
    "Add a new node without 'newlyAdded' with force reconfig, squashing old 'newlyAdded' fields");
const thirdNewNode = addNode(4, {newlyAdded: false, force: true, shouldSucceed: true});
assert.eq(false, isMemberNewlyAdded(primary, 1, true /* force */));
assert.eq(false, isMemberNewlyAdded(primary, 2, true /* force */));
assert.eq(false, isMemberNewlyAdded(primary, 3, true /* force */));
assertVoteCount(primary, {
    votingMembersCount: 4,
    majorityVoteCount: 3,
    writableVotingMembersCount: 4,
    writeMajorityCount: 3,
    totalMembersCount: 4,
});

assert.commandWorked(
    firstNewNode.adminCommand({configureFailPoint: "initialSyncHangBeforeFinish", mode: "off"}));
assert.commandWorked(
    secondNewNode.adminCommand({configureFailPoint: "initialSyncHangBeforeFinish", mode: "off"}));
assert.commandWorked(
    thirdNewNode.adminCommand({configureFailPoint: "initialSyncHangBeforeFinish", mode: "off"}));

rst.waitForState(firstNewNode, ReplSetTest.State.SECONDARY);
rst.waitForState(secondNewNode, ReplSetTest.State.SECONDARY);
rst.waitForState(thirdNewNode, ReplSetTest.State.SECONDARY);

jsTestLog("Making sure the set can accept writes with write concerns");
assert.commandWorked(primaryColl.insert({"steady": "state"}, {writeConcern: {w: 4}}));
assert.commandWorked(
    primaryColl.insert({"steady": "state_majority"}, {writeConcern: {w: 'majority'}}));

assertVoteCount(primary, {
    votingMembersCount: 4,
    majorityVoteCount: 3,
    writableVotingMembersCount: 4,
    writeMajorityCount: 3,
    totalMembersCount: 4,
});

rst.stopSet();
})();