Skip to content

Commit

Permalink
ISSUE-126: fix mRole & mLeaderID inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
DongbinCheng committed Mar 20, 2024
1 parent 1d91f03 commit 90254df
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/infra/raft/v2/RaftCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,13 @@ void RaftCore::becomeCandidate() {
auto currentTerm = mLog->getCurrentTerm();
/// become Candidate
mRaftRole = RaftRole::Candidate;
/// clear leaderHint
mLeaderId = 0;
/// increment currentTerm
mLog->setCurrentTerm(++currentTerm);
/// vote for self
mLog->setVoteFor(mSelfInfo.mId);
/// clear leaderHint
mLeaderId = 0;

/// reset election timer
updateElectionTimePoint();

Expand Down Expand Up @@ -993,6 +994,8 @@ void RaftCore::electionTimeout() {
if (!mPreVoteEnabled) { // request vote directly
/// become Candidate
mRaftRole = RaftRole::Candidate;
/// clear leaderHint
mLeaderId = 0;
/// increment currentTerm
mLog->setCurrentTerm(++currentTerm);
/// vote for self
Expand All @@ -1001,10 +1004,10 @@ void RaftCore::electionTimeout() {
/// become PreCandidate
mRaftRole = RaftRole::PreCandidate;
/// keep currentTerm
/// clear leaderHint
mLeaderId = 0;
}

/// clear leaderHint
mLeaderId = 0;
/// reset election timer
updateElectionTimePoint();

Expand Down Expand Up @@ -1066,9 +1069,9 @@ void RaftCore::stepDown(uint64_t newTerm) {
if (mRaftRole != RaftRole::Learner) mRaftRole = RaftRole::Follower;

if (currentTerm < newTerm) {
mLeaderId = 0;
mLog->setCurrentTerm(newTerm);
mLog->setVoteFor(0); /// 0 means vote for nobody
mLeaderId = 0;
SPDLOG_INFO("{} stepDown, increase term from {} to {}.",
selfId(), currentTerm, newTerm);
} else {
Expand Down

0 comments on commit 90254df

Please sign in to comment.