From c4b918f7335495a8d5fc7f70ed14a9d674a4f289 Mon Sep 17 00:00:00 2001 From: Ali Mir Date: Sat, 22 Feb 2020 04:12:19 +0000 Subject: SERVER-45089 Assert force reconfig with missing term field results in default -1 term --- jstests/replsets/reconfig_ignores_term_field.js | 15 +++++++++++++++ src/mongo/db/repl/replication_coordinator_impl.cpp | 6 ++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/jstests/replsets/reconfig_ignores_term_field.js b/jstests/replsets/reconfig_ignores_term_field.js index 2cc48ac950d..82971834432 100644 --- a/jstests/replsets/reconfig_ignores_term_field.js +++ b/jstests/replsets/reconfig_ignores_term_field.js @@ -26,5 +26,20 @@ replTest.awaitReplication(); config = primary.getDB("local").system.replset.findOne(); assert.eq(config.term, 1); + +jsTestLog("Force reconfig ignores user provided term"); +config.term = 55; +config.version++; +assert.commandWorked(primary.getDB("admin").runCommand({replSetReconfig: config, force: true})); +config = primary.getDB("local").system.replset.findOne(); +assert.eq(config.term, -1); + +jsTestLog("Force reconfig with missing term results in term -1"); +delete config.term; +config.version++; +assert.commandWorked(primary.getDB("admin").runCommand({replSetReconfig: config, force: true})); +config = primary.getDB("local").system.replset.findOne(); +assert.eq(config.term, -1); + replTest.stopSet(); }()); \ No newline at end of file diff --git a/src/mongo/db/repl/replication_coordinator_impl.cpp b/src/mongo/db/repl/replication_coordinator_impl.cpp index 751f971a648..290223fc358 100644 --- a/src/mongo/db/repl/replication_coordinator_impl.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl.cpp @@ -2922,7 +2922,6 @@ Status ReplicationCoordinatorImpl::processReplSetReconfig(OperationContext* opCt makeGuard([&] { lockAndCall(&lk, [=] { _setConfigState_inlock(kConfigSteady); }); }); ReplSetConfig oldConfig = _rsConfig; - auto topCoordTerm = _topCoord->getTerm(); lk.unlock(); ReplSetConfig newConfig; @@ -2936,7 +2935,10 @@ Status ReplicationCoordinatorImpl::processReplSetReconfig(OperationContext* opCt // When initializing a new config through the replSetReconfig command, ignore the term // field passed in through its args. Instead, use this node's term. - Status status = newConfig.initialize(newConfigObj, topCoordTerm, oldConfig.getReplicaSetId()); + // If it is a force reconfig, explicitly set the term to -1. + + auto term = !args.force ? _topCoord->getTerm() : OpTime::kUninitializedTerm; + Status status = newConfig.initialize(newConfigObj, term, oldConfig.getReplicaSetId()); if (!status.isOK()) { LOGV2_ERROR(21418, "replSetReconfig got {status} while parsing {newConfigObj}", -- cgit v1.2.1