Skip to content

Commit

Permalink
Merge pull request #127 from DongbinCheng/ISSUE-126
Browse files Browse the repository at this point in the history
ISSUE-126: fix mRole & mLeaderID inconsistency
  • Loading branch information
DongbinCheng authored Mar 20, 2024
2 parents 0b5b7c7 + d08681c commit 33930ca
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 @@ -930,12 +930,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 @@ -1029,18 +1030,20 @@ 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
mLog->setVoteFor(mSelfInfo.mId);
} else { // request prevote before vote
/// become PreCandidate
mRaftRole = RaftRole::PreCandidate;
/// clear leaderHint
mLeaderId = 0;
/// keep currentTerm
}

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

Expand Down Expand Up @@ -1102,9 +1105,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 33930ca

Please sign in to comment.