diff options
author | Benety Goh <benety@mongodb.com> | 2015-10-16 15:27:28 -0400 |
---|---|---|
committer | Benety Goh <benety@mongodb.com> | 2015-10-22 15:27:16 -0400 |
commit | 38f16626e9f161672ebad8ad4a18e23b7e57de87 (patch) | |
tree | 3a0724c6f6113e706754cae95b622df05ee27de2 /src/mongo | |
parent | fabee00bb0f88b05f44811726cce87dc5c985620 (diff) | |
download | mongo-38f16626e9f161672ebad8ad4a18e23b7e57de87.tar.gz |
SERVER-20977 Converted VoteRequester::VoteRequesterResult to enum class
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/repl/replication_coordinator_impl_elect_v1.cpp | 30 | ||||
-rw-r--r-- | src/mongo/db/repl/vote_requester.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/repl/vote_requester.h | 14 | ||||
-rw-r--r-- | src/mongo/db/repl/vote_requester_test.cpp | 34 |
4 files changed, 44 insertions, 44 deletions
diff --git a/src/mongo/db/repl/replication_coordinator_impl_elect_v1.cpp b/src/mongo/db/repl/replication_coordinator_impl_elect_v1.cpp index 245ef8c5a38..337a9ce3ff6 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_elect_v1.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_elect_v1.cpp @@ -168,15 +168,15 @@ void ReplicationCoordinatorImpl::_onDryRunComplete(long long originalTerm) { return; } - const VoteRequester::VoteRequestResult endResult = _voteRequester->getResult(); + const VoteRequester::Result endResult = _voteRequester->getResult(); - if (endResult == VoteRequester::InsufficientVotes) { + if (endResult == VoteRequester::Result::kInsufficientVotes) { log() << "not running for primary, we received insufficient votes"; return; - } else if (endResult == VoteRequester::StaleTerm) { + } else if (endResult == VoteRequester::Result::kStaleTerm) { log() << "not running for primary, we have been superceded already"; return; - } else if (endResult != VoteRequester::SuccessfullyElected) { + } else if (endResult != VoteRequester::Result::kSuccessfullyElected) { log() << "not running for primary, we received an unexpected problem"; return; } @@ -264,20 +264,20 @@ void ReplicationCoordinatorImpl::_onVoteRequestComplete(long long originalTerm) return; } - const VoteRequester::VoteRequestResult endResult = _voteRequester->getResult(); + const VoteRequester::Result endResult = _voteRequester->getResult(); - if (endResult == VoteRequester::InsufficientVotes) { - log() << "not becoming primary, we received insufficient votes"; - return; - } else if (endResult == VoteRequester::StaleTerm) { - log() << "not becoming primary, we have been superceded already"; - return; - } else if (endResult != VoteRequester::SuccessfullyElected) { - log() << "not becoming primary, we received an unexpected problem"; - return; + switch (endResult) { + case VoteRequester::Result::kInsufficientVotes: + log() << "not becoming primary, we received insufficient votes"; + return; + case VoteRequester::Result::kStaleTerm: + log() << "not becoming primary, we have been superceded already"; + return; + case VoteRequester::Result::kSuccessfullyElected: + log() << "election succeeded, assuming primary role in term " << _topCoord->getTerm(); + break; } - log() << "election succeeded, assuming primary role in term " << _topCoord->getTerm(); { // Mark all nodes that responded to our vote request as up to avoid immediately // relinquishing primary. diff --git a/src/mongo/db/repl/vote_requester.cpp b/src/mongo/db/repl/vote_requester.cpp index 525667abf64..06df2e289c5 100644 --- a/src/mongo/db/repl/vote_requester.cpp +++ b/src/mongo/db/repl/vote_requester.cpp @@ -127,13 +127,13 @@ bool VoteRequester::Algorithm::hasReceivedSufficientResponses() const { _responsesProcessed == static_cast<int>(_targets.size()); } -VoteRequester::VoteRequestResult VoteRequester::Algorithm::getResult() const { +VoteRequester::Result VoteRequester::Algorithm::getResult() const { if (_staleTerm) { - return StaleTerm; + return Result::kStaleTerm; } else if (_votes >= _rsConfig.getMajorityVoteCount()) { - return SuccessfullyElected; + return Result::kSuccessfullyElected; } else { - return InsufficientVotes; + return Result::kInsufficientVotes; } } @@ -162,7 +162,7 @@ void VoteRequester::cancel(ReplicationExecutor* executor) { _runner->cancel(executor); } -VoteRequester::VoteRequestResult VoteRequester::getResult() const { +VoteRequester::Result VoteRequester::getResult() const { return _algorithm->getResult(); } diff --git a/src/mongo/db/repl/vote_requester.h b/src/mongo/db/repl/vote_requester.h index 762e04476e8..8ee9b052d4d 100644 --- a/src/mongo/db/repl/vote_requester.h +++ b/src/mongo/db/repl/vote_requester.h @@ -53,10 +53,10 @@ class VoteRequester { MONGO_DISALLOW_COPYING(VoteRequester); public: - enum VoteRequestResult { - SuccessfullyElected, - StaleTerm, - InsufficientVotes, + enum class Result { + kSuccessfullyElected, + kStaleTerm, + kInsufficientVotes, }; class Algorithm : public ScatterGatherAlgorithm { @@ -73,11 +73,11 @@ public: virtual bool hasReceivedSufficientResponses() const; /** - * Returns a VoteRequestResult indicating the result of the election. + * Returns a VoteRequest::Result indicating the result of the election. * * It is invalid to call this before hasReceivedSufficientResponses returns true. */ - VoteRequestResult getResult() const; + Result getResult() const; /** * Returns the list of nodes that responded to the VoteRequest command. @@ -126,7 +126,7 @@ public: */ void cancel(ReplicationExecutor* executor); - VoteRequestResult getResult() const; + Result getResult() const; unordered_set<HostAndPort> getResponders() const; private: diff --git a/src/mongo/db/repl/vote_requester_test.cpp b/src/mongo/db/repl/vote_requester_test.cpp index 3b944df7af0..c6e1ebe7b61 100644 --- a/src/mongo/db/repl/vote_requester_test.cpp +++ b/src/mongo/db/repl/vote_requester_test.cpp @@ -109,7 +109,7 @@ protected: return _requester->getResponders().size(); } - VoteRequester::VoteRequestResult getResult() { + VoteRequester::Result getResult() { return _requester->getResult(); } @@ -217,7 +217,7 @@ TEST_F(VoteRequesterTest, ImmediateGoodResponseWinElection) { ASSERT_FALSE(hasReceivedSufficientResponses()); processResponse(requestFrom("host1"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(1, getNumResponders()); } @@ -229,7 +229,7 @@ TEST_F(VoteRequesterTest, BadConfigVersionWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -242,7 +242,7 @@ TEST_F(VoteRequesterTest, SetNameDiffersWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -255,7 +255,7 @@ TEST_F(VoteRequesterTest, LastOpTimeIsGreaterWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -268,7 +268,7 @@ TEST_F(VoteRequesterTest, FailedToContactWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got failed response from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(1, getNumResponders()); stopCapturingLogMessages(); } @@ -281,7 +281,7 @@ TEST_F(VoteRequesterTest, AlreadyVotedWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -292,7 +292,7 @@ TEST_F(VoteRequesterTest, StaleTermLoseElection) { processResponse(requestFrom("host1"), votedNoBecauseTermIsGreater()); ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::StaleTerm, getResult()); + ASSERT(VoteRequester::Result::kStaleTerm == getResult()); ASSERT_EQUALS(1, getNumResponders()); stopCapturingLogMessages(); } @@ -306,7 +306,7 @@ TEST_F(VoteRequesterTest, NotEnoughVotesLoseElection) { processResponse(requestFrom("host2"), badResponseStatus()); ASSERT_EQUALS(1, countLogLinesContaining("Got failed response from host2")); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::InsufficientVotes, getResult()); + ASSERT(VoteRequester::Result::kInsufficientVotes == getResult()); ASSERT_EQUALS(1, getNumResponders()); stopCapturingLogMessages(); } @@ -315,7 +315,7 @@ TEST_F(VoteRequesterDryRunTest, ImmediateGoodResponseWinElection) { ASSERT_FALSE(hasReceivedSufficientResponses()); processResponse(requestFrom("host1"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(1, getNumResponders()); } @@ -327,7 +327,7 @@ TEST_F(VoteRequesterDryRunTest, BadConfigVersionWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -340,7 +340,7 @@ TEST_F(VoteRequesterDryRunTest, SetNameDiffersWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -353,7 +353,7 @@ TEST_F(VoteRequesterDryRunTest, LastOpTimeIsGreaterWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -366,7 +366,7 @@ TEST_F(VoteRequesterDryRunTest, FailedToContactWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got failed response from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(1, getNumResponders()); stopCapturingLogMessages(); } @@ -379,7 +379,7 @@ TEST_F(VoteRequesterDryRunTest, AlreadyVotedWinElection) { ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); processResponse(requestFrom("host2"), votedYes()); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::SuccessfullyElected, getResult()); + ASSERT(VoteRequester::Result::kSuccessfullyElected == getResult()); ASSERT_EQUALS(2, getNumResponders()); stopCapturingLogMessages(); } @@ -390,7 +390,7 @@ TEST_F(VoteRequesterDryRunTest, StaleTermLoseElection) { processResponse(requestFrom("host1"), votedNoBecauseTermIsGreater()); ASSERT_EQUALS(1, countLogLinesContaining("Got no vote from host1")); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::StaleTerm, getResult()); + ASSERT(VoteRequester::Result::kStaleTerm == getResult()); ASSERT_EQUALS(1, getNumResponders()); stopCapturingLogMessages(); } @@ -404,7 +404,7 @@ TEST_F(VoteRequesterDryRunTest, NotEnoughVotesLoseElection) { processResponse(requestFrom("host2"), badResponseStatus()); ASSERT_EQUALS(1, countLogLinesContaining("Got failed response from host2")); ASSERT_TRUE(hasReceivedSufficientResponses()); - ASSERT_EQUALS(VoteRequester::InsufficientVotes, getResult()); + ASSERT(VoteRequester::Result::kInsufficientVotes == getResult()); ASSERT_EQUALS(1, getNumResponders()); stopCapturingLogMessages(); } |