Skip to content

Commit

Permalink
fix a potential raft term error (vesoft-inc#2193)
Browse files Browse the repository at this point in the history
Co-authored-by: dutor <[email protected]>
  • Loading branch information
critical27 and dutor authored Jul 6, 2020
1 parent f33a0c3 commit eb8724a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/kvstore/raftex/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ DEFINE_uint32(max_outstanding_requests, 1024,
DEFINE_int32(raft_rpc_timeout_ms, 500, "rpc timeout for raft client");

DECLARE_bool(trace_raft);
DECLARE_uint32(raft_heartbeat_interval_secs);

namespace nebula {
namespace raftex {
Expand Down Expand Up @@ -80,7 +81,7 @@ folly::Future<cpp2::AskForVoteResponse> Host::askForVote(
return resp;
}
}
auto client = tcManager().client(addr_, eb, false, FLAGS_raft_rpc_timeout_ms);
auto client = tcManager().client(addr_, eb, false, FLAGS_raft_heartbeat_interval_secs * 1000);
return client->future_askForVote(req);
}

Expand Down
10 changes: 6 additions & 4 deletions src/kvstore/raftex/RaftPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,8 @@ bool RaftPart::prepareElectionRequest(

typename RaftPart::Role RaftPart::processElectionResponses(
const RaftPart::ElectionResponses& results,
std::vector<std::shared_ptr<Host>> hosts) {
std::vector<std::shared_ptr<Host>> hosts,
TermID proposedTerm) {
std::lock_guard<std::mutex> g(raftLock_);

if (UNLIKELY(status_ == Status::STOPPED)) {
Expand Down Expand Up @@ -1083,8 +1084,8 @@ typename RaftPart::Role RaftPart::processElectionResponses(
if (numSucceeded >= quorum_) {
LOG(INFO) << idStr_
<< "Partition is elected as the new leader for term "
<< proposedTerm_;
term_ = proposedTerm_;
<< proposedTerm;
term_ = proposedTerm;
role_ = Role::LEADER;
}

Expand Down Expand Up @@ -1124,6 +1125,7 @@ bool RaftPart::leaderElection() {
<< ", candidatePort = " << voteReq.get_candidate_port()
<< ")";

auto proposedTerm = voteReq.get_term();
auto resps = ElectionResponses();
if (hosts.empty()) {
VLOG(2) << idStr_ << "No peer found, I will be the leader";
Expand Down Expand Up @@ -1164,7 +1166,7 @@ bool RaftPart::leaderElection() {
}

// Process the responses
switch (processElectionResponses(resps, std::move(hosts))) {
switch (processElectionResponses(resps, std::move(hosts), proposedTerm)) {
case Role::LEADER: {
// Elected
LOG(INFO) << idStr_
Expand Down
3 changes: 2 additions & 1 deletion src/kvstore/raftex/RaftPart.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ class RaftPart : public std::enable_shared_from_this<RaftPart> {

// The method returns the partition's role after the election
Role processElectionResponses(const ElectionResponses& results,
std::vector<std::shared_ptr<Host>> hosts);
std::vector<std::shared_ptr<Host>> hosts,
TermID proposedTerm);

// Check whether new logs can be appended
// Pre-condition: The caller needs to hold the raftLock_
Expand Down

0 comments on commit eb8724a

Please sign in to comment.