summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl
diff options
context:
space:
mode:
authorScott Hernandez <scotthernandez@tart.local>2016-03-01 16:15:53 -0500
committerScott Hernandez <scotthernandez@tart.local>2016-03-27 11:45:02 -0400
commit664295c8ca3152c48da572c770aa08c39e517bf4 (patch)
tree6f3db3803970f5ddf55eb20ce3ee0eb28076164b /src/mongo/db/repl
parent9be485ea5e262d7d69a5486cc60fce247b5ae57f (diff)
downloadmongo-664295c8ca3152c48da572c770aa08c39e517bf4.tar.gz
SERVER-23010: general cleanup and test fixture changes
Diffstat (limited to 'src/mongo/db/repl')
-rw-r--r--src/mongo/db/repl/replication_coordinator_external_state_mock.cpp12
-rw-r--r--src/mongo/db/repl/replication_coordinator_external_state_mock.h12
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl.cpp47
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl.h1
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl_elect.cpp10
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl_heartbeat.cpp11
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl_test.cpp4
-rw-r--r--src/mongo/db/repl/replication_coordinator_test_fixture.cpp8
-rw-r--r--src/mongo/db/repl/replication_coordinator_test_fixture.h10
-rw-r--r--src/mongo/db/repl/topology_coordinator.h4
-rw-r--r--src/mongo/db/repl/topology_coordinator_impl.cpp70
-rw-r--r--src/mongo/db/repl/topology_coordinator_impl.h4
-rw-r--r--src/mongo/db/repl/topology_coordinator_impl_test.cpp29
-rw-r--r--src/mongo/db/repl/topology_coordinator_impl_v1_test.cpp18
14 files changed, 158 insertions, 82 deletions
diff --git a/src/mongo/db/repl/replication_coordinator_external_state_mock.cpp b/src/mongo/db/repl/replication_coordinator_external_state_mock.cpp
index 88750ae5f52..a95cb6c47c2 100644
--- a/src/mongo/db/repl/replication_coordinator_external_state_mock.cpp
+++ b/src/mongo/db/repl/replication_coordinator_external_state_mock.cpp
@@ -212,7 +212,11 @@ void ReplicationCoordinatorExternalStateMock::updateCommittedSnapshot(SnapshotNa
void ReplicationCoordinatorExternalStateMock::forceSnapshotCreation() {}
bool ReplicationCoordinatorExternalStateMock::snapshotsEnabled() const {
- return true;
+ return _areSnapshotsEnabled;
+}
+
+void ReplicationCoordinatorExternalStateMock::setAreSnapshotsEnabled(bool val) {
+ _areSnapshotsEnabled = val;
}
void ReplicationCoordinatorExternalStateMock::notifyOplogMetadataWaiters() {}
@@ -223,7 +227,11 @@ double ReplicationCoordinatorExternalStateMock::getElectionTimeoutOffsetLimitFra
bool ReplicationCoordinatorExternalStateMock::isReadCommittedSupportedByStorageEngine(
OperationContext* txn) const {
- return true;
+ return _isReadCommittedSupported;
+}
+
+void ReplicationCoordinatorExternalStateMock::setIsReadCommittedEnabled(bool val) {
+ _isReadCommittedSupported = val;
}
void ReplicationCoordinatorExternalStateMock::logTransitionToPrimaryToOplog(OperationContext* txn) {
diff --git a/src/mongo/db/repl/replication_coordinator_external_state_mock.h b/src/mongo/db/repl/replication_coordinator_external_state_mock.h
index 643988047de..89fa3399206 100644
--- a/src/mongo/db/repl/replication_coordinator_external_state_mock.h
+++ b/src/mongo/db/repl/replication_coordinator_external_state_mock.h
@@ -145,6 +145,16 @@ public:
*/
bool threadsStarted() const;
+ /**
+ * Sets if the storage engine is configured to support ReadConcern::Majority (committed point).
+ */
+ void setIsReadCommittedEnabled(bool val);
+
+ /**
+ * Sets if we are taking snapshots for read concern majority use.
+ */
+ void setAreSnapshotsEnabled(bool val);
+
private:
StatusWith<BSONObj> _localRsConfigDocument;
StatusWith<LastVote> _localRsLastVoteDocument;
@@ -165,6 +175,8 @@ private:
bool _connectionsClosed;
HostAndPort _clientHostAndPort;
bool _threadsStarted;
+ bool _isReadCommittedSupported = true;
+ bool _areSnapshotsEnabled = true;
};
} // namespace repl
diff --git a/src/mongo/db/repl/replication_coordinator_impl.cpp b/src/mongo/db/repl/replication_coordinator_impl.cpp
index c34299724ce..0065cf5f53f 100644
--- a/src/mongo/db/repl/replication_coordinator_impl.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl.cpp
@@ -161,6 +161,19 @@ struct ReplicationCoordinatorImpl::WaiterInfo {
list->erase(std::remove(list->begin(), list->end(), this), list->end());
}
+ BSONObj toBSON() const {
+ BSONObjBuilder bob;
+ bob.append("opId", opID);
+ bob.append("opTime", opTime->toBSON());
+ bob.append("master", master);
+ bob.append("writeConcern", writeConcern);
+ return bob.obj();
+ };
+
+ std::string toString() const {
+ return toBSON().toString();
+ };
+
std::vector<WaiterInfo*>* list;
bool master; // Set to false to indicate that stepDown was called while waiting
const unsigned int opID;
@@ -205,6 +218,13 @@ DataReplicatorOptions createDataReplicatorOptions(ReplicationCoordinator* replCo
}
} // namespace
+std::string ReplicationCoordinatorImpl::SnapshotInfo::toString() const {
+ BSONObjBuilder bob;
+ bob.append("optime", opTime.toBSON());
+ bob.append("name-id", name.toString());
+ return bob.obj().toString();
+}
+
ReplicationCoordinatorImpl::ReplicationCoordinatorImpl(
const ReplSettings& settings,
ReplicationCoordinatorExternalState* externalState,
@@ -1286,25 +1306,30 @@ void ReplicationCoordinatorImpl::interruptAll() {
bool ReplicationCoordinatorImpl::_doneWaitingForReplication_inlock(
const OpTime& opTime, SnapshotName minSnapshot, const WriteConcernOptions& writeConcern) {
+ // The syncMode cannot be unset.
invariant(writeConcern.syncMode != WriteConcernOptions::SyncMode::UNSET);
Status status = _checkIfWriteConcernCanBeSatisfied_inlock(writeConcern);
if (!status.isOK()) {
return true;
}
- if (writeConcern.wMode.empty())
- return _haveNumNodesReachedOpTime_inlock(opTime,
- writeConcern.wNumNodes,
- writeConcern.syncMode ==
- WriteConcernOptions::SyncMode::JOURNAL);
+ const bool useDurableOpTime = writeConcern.syncMode == WriteConcernOptions::SyncMode::JOURNAL;
+
+ if (writeConcern.wMode.empty()) {
+ return _haveNumNodesReachedOpTime_inlock(opTime, writeConcern.wNumNodes, useDurableOpTime);
+ }
StringData patternName;
if (writeConcern.wMode == WriteConcernOptions::kMajority) {
- if (writeConcern.syncMode == WriteConcernOptions::SyncMode::JOURNAL &&
- _externalState->snapshotsEnabled()) {
+ if (useDurableOpTime && _externalState->snapshotsEnabled()) {
+ // Make sure we have a valid snapshot.
if (!_currentCommittedSnapshot) {
return false;
}
+
+ // Wait for the "current" snapshot to advance to/past the opTime.
+ // We cannot have this committed snapshot until we have replicated to a majority,
+ // so we can return true here once that requirement is met.
return (_currentCommittedSnapshot->opTime >= opTime &&
_currentCommittedSnapshot->name >= minSnapshot);
} else {
@@ -1318,10 +1343,7 @@ bool ReplicationCoordinatorImpl::_doneWaitingForReplication_inlock(
if (!tagPattern.isOK()) {
return true;
}
- return _haveTaggedNodesReachedOpTime_inlock(opTime,
- tagPattern.getValue(),
- writeConcern.syncMode ==
- WriteConcernOptions::SyncMode::JOURNAL);
+ return _haveTaggedNodesReachedOpTime_inlock(opTime, tagPattern.getValue(), useDurableOpTime);
}
bool ReplicationCoordinatorImpl::_haveNumNodesReachedOpTime_inlock(const OpTime& targetOpTime,
@@ -1472,7 +1494,8 @@ ReplicationCoordinator::StatusAndDuration ReplicationCoordinatorImpl::_awaitRepl
std::min<Microseconds>(Milliseconds{writeConcern.wTimeout} - elapsed, waitTime);
}
- if (waitTime == Microseconds::max()) {
+ const bool waitForever = waitTime == Microseconds::max();
+ if (waitForever) {
condVar.wait(*lock);
} else {
condVar.wait_for(*lock, waitTime);
diff --git a/src/mongo/db/repl/replication_coordinator_impl.h b/src/mongo/db/repl/replication_coordinator_impl.h
index 68f3e88ba0a..22f7a0308c3 100644
--- a/src/mongo/db/repl/replication_coordinator_impl.h
+++ b/src/mongo/db/repl/replication_coordinator_impl.h
@@ -444,6 +444,7 @@ private:
bool operator>=(const SnapshotInfo& other) const {
return std::tie(opTime, name) >= std::tie(other.opTime, other.name);
}
+ std::string toString() const;
};
class LoseElectionGuardV1;
diff --git a/src/mongo/db/repl/replication_coordinator_impl_elect.cpp b/src/mongo/db/repl/replication_coordinator_impl_elect.cpp
index e1109d0cc4d..a2cd0eb386e 100644
--- a/src/mongo/db/repl/replication_coordinator_impl_elect.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl_elect.cpp
@@ -125,7 +125,8 @@ void ReplicationCoordinatorImpl::_startElectSelf() {
if (lastOpTimeApplied.isNull()) {
log() << "not trying to elect self, "
- "do not yet have a complete set of data from any point in time";
+ "do not yet have a complete set of data from any point in time"
+ " -- lastAppliedOpTime is null";
return;
}
@@ -276,8 +277,11 @@ void ReplicationCoordinatorImpl::_recoverFromElectionTie(
}
auto now = _replExecutor.now();
auto lastOpApplied = getMyLastAppliedOpTime();
- if (_topCoord->checkShouldStandForElection(now, lastOpApplied)) {
- fassert(28817, _topCoord->becomeCandidateIfElectable(now, lastOpApplied));
+ const auto status = _topCoord->checkShouldStandForElection(now, lastOpApplied);
+ if (!status.isOK()) {
+ LOG(2) << "ReplicationCoordinatorImpl::_recoverFromElectionTie -- " << status.reason();
+ } else {
+ fassertStatusOK(28817, _topCoord->becomeCandidateIfElectable(now, lastOpApplied));
_startElectSelf();
}
}
diff --git a/src/mongo/db/repl/replication_coordinator_impl_heartbeat.cpp b/src/mongo/db/repl/replication_coordinator_impl_heartbeat.cpp
index d71e7ee4afb..4c23bab4f5b 100644
--- a/src/mongo/db/repl/replication_coordinator_impl_heartbeat.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl_heartbeat.cpp
@@ -759,12 +759,15 @@ void ReplicationCoordinatorImpl::_startElectSelfIfEligibleV1(bool isPriorityTake
_cancelAndRescheduleElectionTimeout_inlock();
}
- if (!_topCoord->becomeCandidateIfElectable(_replExecutor.now(), getMyLastDurableOpTime())) {
+ const auto status =
+ _topCoord->becomeCandidateIfElectable(_replExecutor.now(), getMyLastAppliedOpTime());
+ if (!status.isOK()) {
if (isPriorityTakeOver) {
- log() << "Not starting an election for a priority takeover, since we are not "
- "electable";
+ log() << "Not starting an election for a priority takeover, "
+ << "since we are not electable due to: " << status.reason();
} else {
- log() << "Not starting an election, since we are not electable";
+ log() << "Not starting an election, since we are not electable due to: "
+ << status.reason();
}
return;
}
diff --git a/src/mongo/db/repl/replication_coordinator_impl_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_test.cpp
index 6b948b57d0a..b3229878d66 100644
--- a/src/mongo/db/repl/replication_coordinator_impl_test.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl_test.cpp
@@ -92,6 +92,10 @@ struct OpTimeWithTermZero {
return OpTime(timestamp, 0);
}
+ OpTime asOpTime() const {
+ return this->operator mongo::repl::OpTime();
+ }
+
Timestamp timestamp;
};
diff --git a/src/mongo/db/repl/replication_coordinator_test_fixture.cpp b/src/mongo/db/repl/replication_coordinator_test_fixture.cpp
index 34b540946d3..f5074c50008 100644
--- a/src/mongo/db/repl/replication_coordinator_test_fixture.cpp
+++ b/src/mongo/db/repl/replication_coordinator_test_fixture.cpp
@@ -444,5 +444,13 @@ void ReplCoordTest::replyToReceivedHeartbeatV1() {
getNet()->exitNetwork();
}
+void ReplCoordTest::disableReadConcernMajoritySupport() {
+ _externalState->setIsReadCommittedEnabled(false);
+}
+
+void ReplCoordTest::disableSnapshots() {
+ _externalState->setAreSnapshotsEnabled(false);
+}
+
} // namespace repl
} // namespace mongo
diff --git a/src/mongo/db/repl/replication_coordinator_test_fixture.h b/src/mongo/db/repl/replication_coordinator_test_fixture.h
index 1fe1bef07d6..1dd995003a4 100644
--- a/src/mongo/db/repl/replication_coordinator_test_fixture.h
+++ b/src/mongo/db/repl/replication_coordinator_test_fixture.h
@@ -226,6 +226,16 @@ protected:
void simulateEnoughHeartbeatsForAllNodesUp();
+ /**
+ * Disables read concern majority support.
+ */
+ void disableReadConcernMajoritySupport();
+
+ /**
+ * Disables snapshot support.
+ */
+ void disableSnapshots();
+
private:
std::unique_ptr<ReplicationCoordinatorImpl> _repl;
// Owned by ReplicationCoordinatorImpl
diff --git a/src/mongo/db/repl/topology_coordinator.h b/src/mongo/db/repl/topology_coordinator.h
index c68bb08eba0..b0462f9e838 100644
--- a/src/mongo/db/repl/topology_coordinator.h
+++ b/src/mongo/db/repl/topology_coordinator.h
@@ -406,7 +406,7 @@ public:
* Considers whether or not this node should stand for election, and returns true
* if the node has transitioned to candidate role as a result of the call.
*/
- virtual bool checkShouldStandForElection(Date_t now, const OpTime& lastOpApplied) const = 0;
+ virtual Status checkShouldStandForElection(Date_t now, const OpTime& lastOpApplied) const = 0;
/**
* Set the outgoing heartbeat message from self
@@ -462,7 +462,7 @@ public:
/**
* Transitions to the candidate role if the node is electable.
*/
- virtual bool becomeCandidateIfElectable(const Date_t now, const OpTime& lastOpApplied) = 0;
+ virtual Status becomeCandidateIfElectable(const Date_t now, const OpTime& lastOpApplied) = 0;
/**
* Updates the storage engine read committed support in the TopologyCoordinator options after
diff --git a/src/mongo/db/repl/topology_coordinator_impl.cpp b/src/mongo/db/repl/topology_coordinator_impl.cpp
index 0d1cb2cd981..9f754840b8e 100644
--- a/src/mongo/db/repl/topology_coordinator_impl.cpp
+++ b/src/mongo/db/repl/topology_coordinator_impl.cpp
@@ -1289,52 +1289,58 @@ HeartbeatResponseAction TopologyCoordinatorImpl::_updatePrimaryFromHBData(
return HeartbeatResponseAction::makeNoAction();
}
- // At this point, there is no primary anywhere. Check to see if we should become a
- // candidate.
- if (!checkShouldStandForElection(now, lastOpApplied)) {
+ // At this point, there is no primary anywhere. Check to see if we should become a candidate.
+ const auto status = checkShouldStandForElection(now, lastOpApplied);
+ if (!status.isOK()) {
+ // NOTE: This log line is checked in unit test(s).
+ LOG(2) << "TopologyCoordinatorImpl::_updatePrimaryFromHBData - " << status.reason();
return HeartbeatResponseAction::makeNoAction();
}
- fassert(28816, becomeCandidateIfElectable(now, lastOpApplied));
+ fassertStatusOK(28816, becomeCandidateIfElectable(now, lastOpApplied));
return HeartbeatResponseAction::makeElectAction();
}
-bool TopologyCoordinatorImpl::checkShouldStandForElection(Date_t now,
- const OpTime& lastOpApplied) const {
+Status TopologyCoordinatorImpl::checkShouldStandForElection(Date_t now,
+ const OpTime& lastOpApplied) const {
if (_currentPrimaryIndex != -1) {
- return false;
+ return {ErrorCodes::NodeNotElectable, "Not standing for election since there is a Primary"};
}
invariant(_role != Role::leader);
if (_role == Role::candidate) {
- LOG(2) << "Not standing for election again; already candidate";
- return false;
+ return {ErrorCodes::NodeNotElectable, "Not standing for election again; already candidate"};
}
const UnelectableReasonMask unelectableReason = _getMyUnelectableReason(now, lastOpApplied);
if (NotCloseEnoughToLatestOptime & unelectableReason) {
- LOG(2) << "Not standing for election because "
- << _getUnelectableReasonString(unelectableReason) << "; my last optime is "
- << lastOpApplied << " and the newest is " << _latestKnownOpTime(lastOpApplied);
- return false;
+ return {ErrorCodes::NodeNotElectable,
+ str::stream() << "Not standing for election because "
+ << _getUnelectableReasonString(unelectableReason)
+ << "; my last optime is " << lastOpApplied.toString()
+ << " and the newest is "
+ << _latestKnownOpTime(lastOpApplied).toString()};
}
if (unelectableReason) {
- LOG(2) << "Not standing for election because "
- << _getUnelectableReasonString(unelectableReason);
- return false;
+ return {ErrorCodes::NodeNotElectable,
+ str::stream() << "Not standing for election because "
+ << _getUnelectableReasonString(unelectableReason)};
}
if (_electionSleepUntil > now) {
if (_rsConfig.getProtocolVersion() == 1) {
- LOG(2) << "Not standing for election before "
- << dateToISOStringLocal(_electionSleepUntil)
- << " because I stood up or learned about a new term too recently";
+ return {
+ ErrorCodes::NodeNotElectable,
+ str::stream() << "Not standing for election before "
+ << dateToISOStringLocal(_electionSleepUntil)
+ << " because I stood up or learned about a new term too recently"};
} else {
- LOG(2) << "Not standing for election before "
- << dateToISOStringLocal(_electionSleepUntil) << " because I stood too recently";
+ return {ErrorCodes::NodeNotElectable,
+ str::stream() << "Not standing for election before "
+ << dateToISOStringLocal(_electionSleepUntil)
+ << " because I stood too recently"};
}
- return false;
}
// All checks passed. Start election proceedings.
- return true;
+ return Status::OK();
}
bool TopologyCoordinatorImpl::_aMajoritySeemsToBeUp() const {
@@ -2440,29 +2446,27 @@ void TopologyCoordinatorImpl::setPrimaryIndex(long long primaryIndex) {
_currentPrimaryIndex = primaryIndex;
}
-bool TopologyCoordinatorImpl::becomeCandidateIfElectable(const Date_t now,
- const OpTime& lastOpApplied) {
+Status TopologyCoordinatorImpl::becomeCandidateIfElectable(const Date_t now,
+ const OpTime& lastOpApplied) {
if (_role == Role::leader) {
- LOG(2) << "Not standing for election again; already primary";
- return false;
+ return {ErrorCodes::NodeNotElectable, "Not standing for election again; already primary"};
}
if (_role == Role::candidate) {
- LOG(2) << "Not standing for election again; already candidate";
- return false;
+ return {ErrorCodes::NodeNotElectable, "Not standing for election again; already candidate"};
}
const UnelectableReasonMask unelectableReason = _getMyUnelectableReason(now, lastOpApplied);
if (unelectableReason) {
- LOG(2) << "Not standing for election because "
- << _getUnelectableReasonString(unelectableReason);
- return false;
+ return {ErrorCodes::NodeNotElectable,
+ str::stream() << "Not standing for election because "
+ << _getUnelectableReasonString(unelectableReason)};
}
// All checks passed, become a candidate and start election proceedings.
_role = Role::candidate;
- return true;
+ return Status::OK();
}
void TopologyCoordinatorImpl::setStorageEngineSupportsReadCommitted(bool supported) {
diff --git a/src/mongo/db/repl/topology_coordinator_impl.h b/src/mongo/db/repl/topology_coordinator_impl.h
index 6e192ae9425..f86c0e8d928 100644
--- a/src/mongo/db/repl/topology_coordinator_impl.h
+++ b/src/mongo/db/repl/topology_coordinator_impl.h
@@ -215,7 +215,7 @@ public:
virtual void setElectionInfo(OID electionId, Timestamp electionOpTime);
virtual void processWinElection(OID electionId, Timestamp electionOpTime);
virtual void processLoseElection();
- virtual bool checkShouldStandForElection(Date_t now, const OpTime& lastOpApplied) const;
+ virtual Status checkShouldStandForElection(Date_t now, const OpTime& lastOpApplied) const;
virtual void setMyHeartbeatMessage(const Date_t now, const std::string& message);
virtual bool stepDown(Date_t until, bool force, const OpTime& lastOpApplied);
virtual bool stepDownIfPending();
@@ -236,7 +236,7 @@ public:
virtual HeartbeatResponseAction setMemberAsDown(Date_t now,
const int memberIndex,
const OpTime& myLastOpApplied);
- virtual bool becomeCandidateIfElectable(const Date_t now, const OpTime& lastOpApplied);
+ virtual Status becomeCandidateIfElectable(const Date_t now, const OpTime& lastOpApplied);
virtual void setStorageEngineSupportsReadCommitted(bool supported);
////////////////////////////////////////////////////////////
diff --git a/src/mongo/db/repl/topology_coordinator_impl_test.cpp b/src/mongo/db/repl/topology_coordinator_impl_test.cpp
index dc8b5877849..1a3916c77ae 100644
--- a/src/mongo/db/repl/topology_coordinator_impl_test.cpp
+++ b/src/mongo/db/repl/topology_coordinator_impl_test.cpp
@@ -5270,7 +5270,9 @@ TEST_F(TopoCoordTest, ShouldNotStandForElectionWhileAwareOfPrimary) {
heartbeatFromMember(
HostAndPort("h2"), "rs0", MemberState::RS_PRIMARY, OpTime(Timestamp(1, 0), 0));
- ASSERT_FALSE(getTopoCoord().checkShouldStandForElection(now()++, OpTime()));
+ const auto status = getTopoCoord().checkShouldStandForElection(now()++, OpTime());
+ ASSERT_EQ(ErrorCodes::NodeNotElectable, status);
+ ASSERT_STRING_CONTAINS(status.reason(), "there is a Primary");
}
TEST_F(TopoCoordTest, ShouldNotStandForElectionWhileTooStale) {
@@ -5287,7 +5289,10 @@ TEST_F(TopoCoordTest, ShouldNotStandForElectionWhileTooStale) {
heartbeatFromMember(
HostAndPort("h2"), "rs0", MemberState::RS_SECONDARY, OpTime(Timestamp(10000, 0), 0));
- ASSERT_FALSE(getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(100, 0), 0)));
+ const auto status =
+ getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(100, 0), 0));
+ ASSERT_EQ(ErrorCodes::NodeNotElectable, status);
+ ASSERT_STRING_CONTAINS(status.reason(), "my last optime is");
}
TEST_F(TopoCoordTest, VoteForMyselfFailsWhileNotCandidate) {
@@ -5319,12 +5324,10 @@ TEST_F(TopoCoordTest, NodeReturnsArbiterWhenGetMemberStateRunsAgainstArbiter) {
}
TEST_F(TopoCoordTest, ShouldNotStandForElectionWhileRemovedFromTheConfig) {
- logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Debug(3));
- startCapturingLogMessages();
- ASSERT_FALSE(getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(10, 0), 0)));
- stopCapturingLogMessages();
- ASSERT_EQUALS(1, countLogLinesContaining("not a member of a valid replica set config"));
- logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Log());
+ const auto status =
+ getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(10, 0), 0));
+ ASSERT_EQ(ErrorCodes::NodeNotElectable, status);
+ ASSERT_STRING_CONTAINS(status.reason(), "not a member of a valid replica set config");
}
TEST_F(TopoCoordTest, ShouldNotStandForElectionWhenAPositiveResponseWasGivenInTheVoteLeasePeriod) {
@@ -5361,12 +5364,10 @@ TEST_F(TopoCoordTest, ShouldNotStandForElectionWhenAPositiveResponseWasGivenInTh
ASSERT_EQUALS(1, response["vote"].Int());
ASSERT_EQUALS(remoteRound, response["round"].OID());
- logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Debug(3));
- startCapturingLogMessages();
- ASSERT_FALSE(getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(10, 0), 0)));
- stopCapturingLogMessages();
- ASSERT_EQUALS(1, countLogLinesContaining("I recently voted for "));
- logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Log());
+ const auto status =
+ getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(10, 0), 0));
+ ASSERT_EQ(ErrorCodes::NodeNotElectable, status);
+ ASSERT_STRING_CONTAINS(status.reason(), "I recently voted for ");
}
TEST_F(TopoCoordTest, NodeDoesNotGrantVotesToTwoDifferentNodesInTheSameTerm) {
diff --git a/src/mongo/db/repl/topology_coordinator_impl_v1_test.cpp b/src/mongo/db/repl/topology_coordinator_impl_v1_test.cpp
index b23fb9bf73e..8307fea5318 100644
--- a/src/mongo/db/repl/topology_coordinator_impl_v1_test.cpp
+++ b/src/mongo/db/repl/topology_coordinator_impl_v1_test.cpp
@@ -2082,7 +2082,7 @@ TEST_F(TopoCoordTest, ShouldNotStandForElectionWhileAwareOfPrimary) {
heartbeatFromMember(
HostAndPort("h2"), "rs0", MemberState::RS_PRIMARY, OpTime(Timestamp(1, 0), 0));
- ASSERT_FALSE(getTopoCoord().checkShouldStandForElection(now()++, OpTime()));
+ ASSERT_NOT_OK(getTopoCoord().checkShouldStandForElection(now()++, OpTime()));
}
TEST_F(TopoCoordTest, ShouldStandForElectionDespiteNotCloseEnoughToLastOptime) {
@@ -2099,7 +2099,7 @@ TEST_F(TopoCoordTest, ShouldStandForElectionDespiteNotCloseEnoughToLastOptime) {
heartbeatFromMember(
HostAndPort("h2"), "rs0", MemberState::RS_SECONDARY, OpTime(Timestamp(10000, 0), 0));
- ASSERT_TRUE(getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(100, 0), 0)));
+ ASSERT_OK(getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(100, 0), 0)));
}
TEST_F(TopoCoordTest, VoteForMyselfFailsWhileNotCandidate) {
@@ -2131,12 +2131,10 @@ TEST_F(TopoCoordTest, NodeReturnsArbiterWhenGetMemberStateRunsAgainstArbiter) {
}
TEST_F(TopoCoordTest, ShouldNotStandForElectionWhileRemovedFromTheConfig) {
- logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Debug(3));
- startCapturingLogMessages();
- ASSERT_FALSE(getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(10, 0), 0)));
- stopCapturingLogMessages();
- ASSERT_EQUALS(1, countLogLinesContaining("not a member of a valid replica set config"));
- logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Log());
+ const auto status =
+ getTopoCoord().checkShouldStandForElection(now()++, OpTime(Timestamp(10, 0), 0));
+ ASSERT_NOT_OK(status);
+ ASSERT_STRING_CONTAINS(status.reason(), "not a member of a valid replica set config");
}
TEST_F(TopoCoordTest, NodeDoesNotGrantVotesToTwoDifferentNodesInTheSameTerm) {
@@ -3405,7 +3403,7 @@ TEST_F(HeartbeatResponseTestV1,
ASSERT_NO_ACTION(nextAction.getAction());
ASSERT_TRUE(TopologyCoordinator::Role::follower == getTopoCoord().getRole());
// We are electable now.
- ASSERT_TRUE(getTopoCoord().becomeCandidateIfElectable(now(), lastOpTimeApplied));
+ ASSERT_OK(getTopoCoord().becomeCandidateIfElectable(now(), lastOpTimeApplied));
ASSERT_TRUE(TopologyCoordinator::Role::candidate == getTopoCoord().getRole());
}
@@ -3437,7 +3435,7 @@ TEST_F(HeartbeatResponseTestV1, ScheduleElectionWhenPrimaryIsMarkedDownAndWeAreE
ASSERT_EQUALS(-1, getCurrentPrimaryIndex());
ASSERT_TRUE(TopologyCoordinator::Role::follower == getTopoCoord().getRole());
// We are electable now.
- ASSERT_TRUE(getTopoCoord().becomeCandidateIfElectable(now(), lastOpTimeApplied));
+ ASSERT_OK(getTopoCoord().becomeCandidateIfElectable(now(), lastOpTimeApplied));
ASSERT_TRUE(TopologyCoordinator::Role::candidate == getTopoCoord().getRole());
}