diff --git a/src/kvstore/raftex/Host.cpp b/src/kvstore/raftex/Host.cpp index 1616d846adc..dfce2f2a25b 100644 --- a/src/kvstore/raftex/Host.cpp +++ b/src/kvstore/raftex/Host.cpp @@ -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 { @@ -80,7 +81,7 @@ folly::Future 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); } diff --git a/src/kvstore/raftex/RaftPart.cpp b/src/kvstore/raftex/RaftPart.cpp index 021e7e716bd..66c829f61c6 100644 --- a/src/kvstore/raftex/RaftPart.cpp +++ b/src/kvstore/raftex/RaftPart.cpp @@ -1040,7 +1040,8 @@ bool RaftPart::prepareElectionRequest( typename RaftPart::Role RaftPart::processElectionResponses( const RaftPart::ElectionResponses& results, - std::vector> hosts) { + std::vector> hosts, + TermID proposedTerm) { std::lock_guard g(raftLock_); if (UNLIKELY(status_ == Status::STOPPED)) { @@ -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; } @@ -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"; @@ -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_ diff --git a/src/kvstore/raftex/RaftPart.h b/src/kvstore/raftex/RaftPart.h index 35e4f0aeb02..3af52f49b98 100644 --- a/src/kvstore/raftex/RaftPart.h +++ b/src/kvstore/raftex/RaftPart.h @@ -347,7 +347,8 @@ class RaftPart : public std::enable_shared_from_this { // The method returns the partition's role after the election Role processElectionResponses(const ElectionResponses& results, - std::vector> hosts); + std::vector> hosts, + TermID proposedTerm); // Check whether new logs can be appended // Pre-condition: The caller needs to hold the raftLock_