2014-02-26 17:22:07 -08:00
|
|
|
// Test that setting maxSyncSourceLagSecs causes the set to change sync target
|
2015-11-19 14:01:59 -05:00
|
|
|
//
|
|
|
|
|
// This test requires the fsync command to ensure members experience a delay.
|
|
|
|
|
// @tags: [requires_fsync]
|
2024-08-20 17:54:15 -04:00
|
|
|
import {ReplSetTest} from "jstests/libs/replsettest.js";
|
2017-03-08 17:14:26 -05:00
|
|
|
import {syncFrom} from "jstests/replsets/rslib.js";
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2025-08-28 15:11:44 -04:00
|
|
|
let name = "maxSyncSourceLagSecs";
|
|
|
|
|
let replTest = new ReplSetTest({
|
2016-03-09 12:17:50 -05:00
|
|
|
name: name,
|
2017-01-24 12:19:33 -05:00
|
|
|
nodes: [
|
|
|
|
|
{rsConfig: {priority: 3}},
|
|
|
|
|
{rsConfig: {priority: 0}},
|
2025-08-21 10:17:44 -07:00
|
|
|
{rsConfig: {priority: 0}, setParameter: "maxSyncSourceLagSecs=3"},
|
2017-01-24 12:19:33 -05:00
|
|
|
],
|
2016-03-09 12:17:50 -05:00
|
|
|
oplogSize: 5,
|
|
|
|
|
});
|
2025-08-28 15:11:44 -04:00
|
|
|
let nodes = replTest.nodeList();
|
2014-11-14 06:27:02 -05:00
|
|
|
replTest.startSet();
|
2017-01-24 12:19:33 -05:00
|
|
|
replTest.initiate();
|
2016-05-24 10:20:44 -04:00
|
|
|
replTest.awaitNodesAgreeOnPrimary();
|
2025-08-28 15:11:44 -04:00
|
|
|
let primary = replTest.getPrimary();
|
|
|
|
|
let secondaries = replTest.getSecondaries();
|
2021-04-29 01:19:06 +00:00
|
|
|
|
|
|
|
|
// The default WC is majority and stopServerReplication could prevent satisfying any majority
|
|
|
|
|
// writes.
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.commandWorked(
|
|
|
|
|
primary.adminCommand({setDefaultRWConcern: 1, defaultWriteConcern: {w: 1}, writeConcern: {w: "majority"}}),
|
|
|
|
|
);
|
2021-04-29 01:19:06 +00:00
|
|
|
|
|
|
|
|
replTest.awaitReplication();
|
|
|
|
|
|
2020-08-26 11:28:13 -04:00
|
|
|
syncFrom(secondaries[0], primary, replTest);
|
|
|
|
|
syncFrom(secondaries[1], primary, replTest);
|
|
|
|
|
primary.getDB("foo").bar.save({a: 1});
|
2014-11-14 06:27:02 -05:00
|
|
|
replTest.awaitReplication();
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2020-08-26 11:28:13 -04:00
|
|
|
jsTestLog("Setting sync target of secondary 2 to secondary 1");
|
|
|
|
|
syncFrom(secondaries[1], secondaries[0], replTest);
|
2014-11-14 06:27:02 -05:00
|
|
|
printjson(replTest.status());
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2017-04-14 16:44:39 -04:00
|
|
|
// need to put at least maxSyncSourceLagSecs b/w first op and subsequent ops
|
|
|
|
|
// so that the shouldChangeSyncSource logic goes into effect
|
|
|
|
|
sleep(4000);
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2025-08-21 10:17:44 -07:00
|
|
|
jsTestLog("Lock secondary 1 and add some docs. Force sync target for secondary 2 to change to primary");
|
2020-08-26 11:28:13 -04:00
|
|
|
assert.commandWorked(secondaries[0].getDB("admin").runCommand({fsync: 1, lock: 1}));
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.soon(
|
|
|
|
|
function () {
|
|
|
|
|
primary.getDB("foo").bar.insert({a: 2});
|
2025-08-28 15:11:44 -04:00
|
|
|
let res = secondaries[1].getDB("admin").runCommand({"replSetGetStatus": 1});
|
2025-08-21 10:17:44 -07:00
|
|
|
return res.syncSourceHost === primary.name;
|
|
|
|
|
},
|
|
|
|
|
"sync target not changed back to primary",
|
|
|
|
|
100 * 1000,
|
|
|
|
|
2 * 1000,
|
|
|
|
|
);
|
2014-11-14 06:27:02 -05:00
|
|
|
printjson(replTest.status());
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.soon(function () {
|
|
|
|
|
return secondaries[1].getDB("foo").bar.count({a: 1}) > 0 && secondaries[1].getDB("foo").bar.count({a: 2}) > 0;
|
2020-08-26 11:28:13 -04:00
|
|
|
}, "secondary should have caught up after syncing to primary.");
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2020-08-26 11:28:13 -04:00
|
|
|
assert.commandWorked(secondaries[0].getDB("admin").fsyncUnlock());
|
2025-08-21 10:17:44 -07:00
|
|
|
replTest.stopSet();
|