summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl.cpp2
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl_test.cpp84
2 files changed, 85 insertions, 1 deletions
diff --git a/src/mongo/db/repl/replication_coordinator_impl.cpp b/src/mongo/db/repl/replication_coordinator_impl.cpp
index 698f810fe6e..ad00b7e86a8 100644
--- a/src/mongo/db/repl/replication_coordinator_impl.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl.cpp
@@ -3204,7 +3204,7 @@ Status ReplicationCoordinatorImpl::processReplSetRequestVotes(
return cbh.getStatus();
}
_replExecutor.wait(cbh.getValue());
- if (response->getVoteGranted()) {
+ if (!args.isADryRun() && response->getVoteGranted()) {
LastVote lastVote;
lastVote.setTerm(args.getTerm());
lastVote.setCandidateIndex(args.getCandidateIndex());
diff --git a/src/mongo/db/repl/replication_coordinator_impl_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_test.cpp
index 91fde3615b0..9dfd6f11045 100644
--- a/src/mongo/db/repl/replication_coordinator_impl_test.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl_test.cpp
@@ -46,6 +46,7 @@
#include "mongo/db/repl/read_concern_response.h"
#include "mongo/db/repl/repl_set_heartbeat_args.h"
#include "mongo/db/repl/repl_set_heartbeat_args_v1.h"
+#include "mongo/db/repl/repl_set_request_votes_args.h"
#include "mongo/db/repl/repl_settings.h"
#include "mongo/db/repl/replica_set_config.h"
#include "mongo/db/repl/replication_coordinator.h" // ReplSetReconfigArgs
@@ -4579,6 +4580,89 @@ TEST_F(ReplCoordTest, PopulateUnsetWriteConcernOptionsSyncModeReturnsInputIfWMod
getReplCoord()->populateUnsetWriteConcernOptionsSyncMode(wc).syncMode);
}
+TEST_F(ReplCoordTest, NodeStoresElectionVotes) {
+ assertStartSuccess(BSON("_id"
+ << "mySet"
+ << "version" << 2 << "members"
+ << BSON_ARRAY(BSON("host"
+ << "node1:12345"
+ << "_id" << 0)
+ << BSON("host"
+ << "node2:12345"
+ << "_id" << 1) << BSON("host"
+ << "node3:12345"
+ << "_id" << 2))),
+ HostAndPort("node1", 12345));
+ auto time = OpTime(Timestamp(100, 0), 1);
+ ASSERT(getReplCoord()->setFollowerMode(MemberState::RS_SECONDARY));
+ getReplCoord()->setMyLastAppliedOpTime(time);
+ getReplCoord()->setMyLastDurableOpTime(time);
+ simulateSuccessfulV1Election();
+
+ OperationContextReplMock txn;
+
+ ReplSetRequestVotesArgs args;
+ ASSERT_OK(args.initialize(BSON(
+ "replSetRequestVotes" << 1 << "setName"
+ << "mySet"
+ << "term" << 7LL << "candidateIndex" << 2LL << "configVersion" << 2LL
+ << "dryRun" << false << "lastCommittedOp" << time.toBSON())));
+ ReplSetRequestVotesResponse response;
+
+ ASSERT_OK(getReplCoord()->processReplSetRequestVotes(&txn, args, &response));
+ ASSERT_EQUALS("", response.getReason());
+ ASSERT_TRUE(response.getVoteGranted());
+
+ auto lastVote = getExternalState()->loadLocalLastVoteDocument(&txn);
+ ASSERT_OK(lastVote.getStatus());
+
+ // This is not a dry-run election so the last vote should include the new term and candidate.
+ ASSERT_EQUALS(lastVote.getValue().getTerm(), 7);
+ ASSERT_EQUALS(lastVote.getValue().getCandidateIndex(), 2);
+}
+
+TEST_F(ReplCoordTest, NodeDoesNotStoreDryRunVotes) {
+ assertStartSuccess(BSON("_id"
+ << "mySet"
+ << "version" << 2 << "members"
+ << BSON_ARRAY(BSON("host"
+ << "node1:12345"
+ << "_id" << 0)
+ << BSON("host"
+ << "node2:12345"
+ << "_id" << 1) << BSON("host"
+ << "node3:12345"
+ << "_id" << 2))),
+ HostAndPort("node1", 12345));
+ auto time = OpTime(Timestamp(100, 0), 1);
+ ASSERT(getReplCoord()->setFollowerMode(MemberState::RS_SECONDARY));
+ getReplCoord()->setMyLastAppliedOpTime(time);
+ getReplCoord()->setMyLastDurableOpTime(time);
+ simulateSuccessfulV1Election();
+
+ OperationContextReplMock txn;
+
+ ReplSetRequestVotesArgs args;
+ ASSERT_OK(args.initialize(BSON(
+ "replSetRequestVotes" << 1 << "setName"
+ << "mySet"
+ << "term" << 7LL << "candidateIndex" << 2LL << "configVersion" << 2LL
+ << "dryRun" << true << "lastCommittedOp" << time.toBSON())));
+ ReplSetRequestVotesResponse response;
+
+ ASSERT_OK(getReplCoord()->processReplSetRequestVotes(&txn, args, &response));
+ ASSERT_EQUALS("", response.getReason());
+ ASSERT_TRUE(response.getVoteGranted());
+
+ auto lastVote = getExternalState()->loadLocalLastVoteDocument(&txn);
+ ASSERT_OK(lastVote.getStatus());
+
+ // This is a dry-run election so the last vote should not be updated with the new term and
+ // candidate.
+ ASSERT_EQUALS(lastVote.getValue().getTerm(), 1);
+ ASSERT_EQUALS(lastVote.getValue().getCandidateIndex(), 0);
+}
+
// TODO(schwerin): Unit test election id updating
} // namespace
} // namespace repl