summaryrefslogtreecommitdiff
path: root/jstests/replsets/newly_added_with_user_reconfig.js
blob: b5530812a4d0e22511cc8e29c65daabbedd6c649 (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
/**
 * Tests that additions or removals of 'newlyAdded' fields do not interfere with user initiated
 * reconfigs.
 *
 * @tags: [
 *   requires_fcv_46,
 * ]
 */

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

const testName = jsTestName();
const dbName = "testdb";
const collName = "testcoll";

const rst = new ReplSetTest(
    {name: testName, nodes: 1, settings: {chainingAllowed: false}, useBridge: true});
rst.startSet();
rst.initiateWithHighElectionTimeout();

const primary = rst.getPrimary();
const primaryDb = primary.getDB(dbName);
const primaryColl = primaryDb.getCollection(collName);

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

jsTestLog("Adding a new node to the replica set");
const secondary = rst.add({
    rsConfig: {priority: 0},
    setParameter: {
        'failpoint.initialSyncHangBeforeFinish': tojson({mode: 'alwaysOn'}),
        'numInitialSyncAttempts': 1,
    }
});
rst.reInitiate();
assert.commandWorked(secondary.adminCommand({
    waitForFailPoint: "initialSyncHangBeforeFinish",
    timesEntered: 1,
    maxTimeMS: kDefaultWaitForFailPointTimeout
}));

jsTestLog("Checking that the 'newlyAdded' field is set on the new node");
assert(isMemberNewlyAdded(primary, 1));

jsTestLog("Checking vote counts while the secondary is in initial sync");
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 2
});

jsTestLog("Checking that user reconfigs still succeed, during initial sync");
let config = rst.getReplSetConfigFromNode();

// First try making a change without modifying the member set.
jsTestLog("[1] Change in config.settings, during initial sync");
const baseElectionTimeoutMillis = config.settings.electionTimeoutMillis;
config.settings.electionTimeoutMillis++;
reconfig(rst, config, false /* force */, true /* doNotWaitForMembers */);

config = rst.getReplSetConfigFromNode();
assert.eq(baseElectionTimeoutMillis + 1, config.settings.electionTimeoutMillis);

// Check 'newlyAdded' and vote counts.
assert(isMemberNewlyAdded(primary, 1));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 2
});

// Now try adding a member.
jsTestLog("[2] Member addition, during initial sync");
rst.add({rsConfig: {priority: 0, votes: 0}});
rst.reInitiate();

// Check 'newlyAdded' and vote counts.
assert(isMemberNewlyAdded(primary, 1));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 3
});

// Do not let 'newlyAdded' be removed yet.
jsTestLog("Allowing member to complete initial sync");

let doNotRemoveNewlyAddedFP = configureFailPoint(primaryDb, "doNotRemoveNewlyAddedOnHeartbeats");
assert.commandWorked(
    secondary.adminCommand({configureFailPoint: "initialSyncHangBeforeFinish", mode: "off"}));
rst.waitForState(secondary, ReplSetTest.State.SECONDARY);

jsTestLog("Checking that the 'newlyAdded' field is still set");
assert(isMemberNewlyAdded(primary, 1));

jsTestLog("Checking behavior with 'newlyAdded' field set, after initial sync");
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 3,
});

jsTestLog("Checking that user reconfigs still succeed, after initial sync");
config = rst.getReplSetConfigFromNode();

jsTestLog("[3] Change in config.settings, during initial sync");
config.settings.electionTimeoutMillis++;
reconfig(rst, config, false /* force */, true /* doNotWaitForMembers */);

config = rst.getReplSetConfigFromNode();
assert.eq(baseElectionTimeoutMillis + 2, config.settings.electionTimeoutMillis);

// Check 'newlyAdded' and vote counts.
assert(isMemberNewlyAdded(primary, 1));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 3
});

// Now try removing the member we added above.
jsTestLog("[4] Member removal, after initial sync");
rst.remove(2);
config = rst.getReplSetConfigFromNode();
const twoNodeConfig = Object.assign({}, config);
twoNodeConfig.members = twoNodeConfig.members.slice(0, 2);  // Remove the last node.
reconfig(rst, twoNodeConfig);

// Check 'newlyAdded' and vote counts.
assert(isMemberNewlyAdded(primary, 1));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 2
});

jsTestLog("Waiting for 'newlyAdded' field to be removed");
doNotRemoveNewlyAddedFP.off();
waitForNewlyAddedRemovalForNodeToBeCommitted(primary, 1);
assertVoteCount(primary, {
    votingMembersCount: 2,
    majorityVoteCount: 2,
    writableVotingMembersCount: 2,
    writeMajorityCount: 2,
    totalMembersCount: 2,
});

jsTestLog("Making sure set can accept w:2 writes");
assert.commandWorked(primaryColl.insert({"steady": "state"}, {writeConcern: {w: 2}}));

rst.awaitReplication();
rst.stopSet();
})();