summaryrefslogtreecommitdiff
path: root/jstests/replsets/reconfig_waits_for_oplog_commitment_condition_when_leaving_force.js
blob: 32658b9fe58c9cf2cbcf350965f1825a6c5d24a7 (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
/**
 * Verify that a non force replica set reconfig waits for all oplog entries committed in the
 * previous config to be committed in the current config even if we are exiting a config that was
 * installed via a 'force' reconfig.
 */
(function() {
"use strict";
load("jstests/libs/write_concern_util.js");
load("jstests/replsets/rslib.js");

const dbName = "test";
const collName = "coll";
// Make the secondary unelectable.
let rst = new ReplSetTest({nodes: [{}, {rsConfig: {priority: 0}}]});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();
const secondary = rst.getSecondary();
const coll = primary.getDB(dbName)[collName];

// The default WC is majority and stopServerReplication will prevent satisfying any majority writes.
assert.commandWorked(primary.adminCommand(
    {setDefaultRWConcern: 1, defaultWriteConcern: {w: 1}, writeConcern: {w: "majority"}}));

// This makes the test run faster.
assert.commandWorked(secondary.adminCommand(
    {configureFailPoint: 'setSmallOplogGetMoreMaxTimeMS', mode: 'alwaysOn'}));

// Create collection.
assert.commandWorked(coll.insert({}));
rst.awaitReplication();

// Stop replication on the secondary.
stopServerReplication(secondary);

// Force reconfig down to a 1 node replica set.
let twoNodeConfig = rst.getReplSetConfigFromNode();
let singleNodeConfig = Object.assign({}, twoNodeConfig);
singleNodeConfig.members = singleNodeConfig.members.slice(0, 1);  // Remove the second node.
singleNodeConfig.version++;

jsTestLog("Force reconfig down to a single node.");
assert.commandWorked(primary.adminCommand({replSetReconfig: singleNodeConfig, force: true}));
assert(isConfigCommitted(primary));

jsTestLog("Do a write on primary and commit it in the current config.");
assert.commandWorked(coll.insert({x: 1}, {writeConcern: {w: "majority"}}));

jsTestLog("Force reconfig to add the secondary back in.");
// We expect this to succeed but the last committed op from the previous config
// isn't committed in the current config yet.
twoNodeConfig.version = rst.getReplSetConfigFromNode().version + 1;
assert.commandWorked(primary.adminCommand({replSetReconfig: twoNodeConfig, force: true}));

// Wait until the config has propagated to the secondary and the primary has learned of it, so that
// the config replication check is satisfied.
rst.waitForConfigReplication(primary);

// Reconfig should succeed even if we have not committed the last committed op in the current
// config because the current config is from a force reconfig.
assert.eq(isConfigCommitted(primary), false);
twoNodeConfig.version = rst.getReplSetConfigFromNode().version + 1;
assert.commandWorked(primary.adminCommand({replSetReconfig: twoNodeConfig}));

// Make sure we can connect to the secondary after it was REMOVED.
reconnect(secondary);

// Let the last committed op from the original 1 node config become committed in the current config.
restartServerReplication(secondary);
rst.awaitReplication();
assert.soon(() => isConfigCommitted(primary));

// Now that we can commit the op in the new config, reconfig should succeed.
twoNodeConfig.version = rst.getReplSetConfigFromNode().version + 1;
assert.commandWorked(primary.adminCommand({replSetReconfig: twoNodeConfig}));
assert(isConfigCommitted(primary));
rst.awaitReplication();

rst.stopSet();
}());