summaryrefslogtreecommitdiff
path: root/jstests/replsets/remove_newly_added_field_votes_zero.js
blob: f883ff190f953eb50cbf8c0bf59c02e11a3355a8 (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
/**
 * Test that the 'newlyAdded' field of a MemberConfig is not added for nodes configured with
 * 'votes:0', but is also removed if a node ends up with 'votes:0' and 'newlyAdded'.
 *
 * @tags: [
 * ]
 */

(function() {
"use strict";

load('jstests/replsets/rslib.js');
load("jstests/libs/fail_point_util.js");

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

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

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

assert.commandWorked(primaryColl.insert({a: 1}));

jsTestLog("Adding a new non-voting node to the replica set");
const secondary0 = rst.add({
    rsConfig: {priority: 0, votes: 0},
    setParameter: {
        'failpoint.initialSyncHangBeforeFinish': tojson({mode: 'alwaysOn'}),
        'numInitialSyncAttempts': 1,
    }
});
rst.reInitiate();

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

jsTestLog("Checking that 'newlyAdded' field is not set");
assert(!isMemberNewlyAdded(primary, 1));
assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[1].votes);
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 2,
});

jsTestLog("Waiting for initial sync to complete");
assert.commandWorked(
    secondary0.adminCommand({configureFailPoint: "initialSyncHangBeforeFinish", mode: "off"}));
rst.waitForState(secondary0, ReplSetTest.State.SECONDARY);

jsTestLog("Checking that 'newlyAdded' field is still not set");
assert(!isMemberNewlyAdded(primary, 1));
assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[1].votes);
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 2,
});

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

jsTestLog("Adding a new voting node to the replica set");
const secondary1 = rst.add({
    rsConfig: {priority: 0},
    setParameter: {
        'failpoint.initialSyncHangBeforeFinish': tojson({mode: 'alwaysOn'}),
        'numInitialSyncAttempts': 1,
    }
});
rst.reInitiate();

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

jsTestLog("Checking that 'newlyAdded' field is set");
assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[1].votes);
assert.eq(1, rst.getReplSetConfigFromNode(primary.nodeId).members[2].votes);
assert(!isMemberNewlyAdded(primary, 1));
assert(isMemberNewlyAdded(primary, 2));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 3,
});

jsTestLog("Reconfiguring new node to have 0 votes");
let cfg = rst.getReplSetConfigFromNode(primary.nodeId);
cfg.version += 1;
cfg.members[2].votes = 0;
assert.commandWorked(
    primary.adminCommand({replSetReconfig: cfg, maxTimeMS: ReplSetTest.kDefaultTimeoutMS}));

assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[1].votes);
assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[2].votes);
assert(!isMemberNewlyAdded(primary, 1));
assert(isMemberNewlyAdded(primary, 2));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 3,
});

jsTestLog("Waiting for second initial sync to complete");
assert.commandWorked(
    secondary1.adminCommand({configureFailPoint: "initialSyncHangBeforeFinish", mode: "off"}));
rst.waitForState(secondary1, ReplSetTest.State.SECONDARY);

jsTestLog("Checking that 'newlyAdded' field was removed");
waitForNewlyAddedRemovalForNodeToBeCommitted(primary, 2);
assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[1].votes);
assert.eq(0, rst.getReplSetConfigFromNode(primary.nodeId).members[2].votes);
assert(!isMemberNewlyAdded(primary, 1));
assertVoteCount(primary, {
    votingMembersCount: 1,
    majorityVoteCount: 1,
    writableVotingMembersCount: 1,
    writeMajorityCount: 1,
    totalMembersCount: 3,
});

jsTestLog("Making sure the set can accept w:3 writes");
assert.commandWorked(primaryColl.insert({a: 3}, {writeConcern: {w: 3}}));

rst.stopSet();
})();