Skip to content

Commit

Permalink
resolve conflicts, simplify, add a bit more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
critical27 committed Nov 16, 2021
1 parent 33424cf commit e3b0994
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
13 changes: 6 additions & 7 deletions src/kvstore/raftex/RaftPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ folly::Future<bool> RaftPart::leaderElection() {
auto proposedTerm = voteReq.get_term();
auto resps = ElectionResponses();
if (hosts.empty()) {
auto ret = handleElectionResponses(voteReq, resps, hosts, proposedTerm);
auto ret = handleElectionResponses(resps, hosts, proposedTerm);
inElection_ = false;
return ret;
} else {
Expand All @@ -1114,21 +1114,19 @@ folly::Future<bool> RaftPart::leaderElection() {
return resp.get_error_code() == cpp2::ErrorCode::SUCCEEDED && !hosts[idx]->isLearner();
})
.via(executor_.get())
.then([self = shared_from_this(), pro = std::move(promise), voteReq, hosts, proposedTerm](
.then([self = shared_from_this(), pro = std::move(promise), hosts, proposedTerm](
auto&& t) mutable {
VLOG(2) << self->idStr_
<< "AskForVoteRequest has been sent to all peers, waiting for responses";
CHECK(!t.hasException());
pro.setValue(
self->handleElectionResponses(voteReq, t.value(), std::move(hosts), proposedTerm));
pro.setValue(self->handleElectionResponses(t.value(), std::move(hosts), proposedTerm));
self->inElection_ = false;
});
return future;
}
}

bool RaftPart::handleElectionResponses(const cpp2::AskForVoteRequest& voteReq,
const ElectionResponses& resps,
bool RaftPart::handleElectionResponses(const ElectionResponses& resps,
const std::vector<std::shared_ptr<Host>>& peers,
TermID proposedTerm) {
// Process the responses
Expand All @@ -1143,12 +1141,13 @@ bool RaftPart::handleElectionResponses(const cpp2::AskForVoteRequest& voteReq,
leader_ = addr_;
hosts = hosts_;
bgWorkers_->addTask(
[self = shared_from_this(), term = voteReq.get_term()] { self->onElected(term); });
[self = shared_from_this(), proposedTerm] { self->onElected(proposedTerm); });
lastMsgAcceptedTime_ = 0;
}
weight_ = 1;
commitInThisTerm_ = false;
}
// reset host can't be executed with raftLock_, otherwise it may encounter deadlock
for (auto& host : hosts) {
host->reset();
}
Expand Down
7 changes: 4 additions & 3 deletions src/kvstore/raftex/RaftPart.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ class RaftPart : public std::enable_shared_from_this<RaftPart> {
void cleanupSnapshot();

// The method sends out AskForVote request
// It return true if a leader is elected, otherwise returns false
// Return true if a leader is elected (the leader could be self or others),
// otherwise returns false
folly::Future<bool> leaderElection();

// The method will fill up the request object and return TRUE
Expand All @@ -353,8 +354,8 @@ class RaftPart : public std::enable_shared_from_this<RaftPart> {
bool prepareElectionRequest(cpp2::AskForVoteRequest& req,
std::vector<std::shared_ptr<Host>>& hosts);

bool handleElectionResponses(const cpp2::AskForVoteRequest& voteReq,
const ElectionResponses& resps,
// return true if elected as the leader, else return false
bool handleElectionResponses(const ElectionResponses& resps,
const std::vector<std::shared_ptr<Host>>& hosts,
TermID proposedTerm);

Expand Down

0 comments on commit e3b0994

Please sign in to comment.