Skip to content

Commit

Permalink
Randomise lower bits of terms
Browse files Browse the repository at this point in the history
Signed-off-by: cjen1 <[email protected]>
  • Loading branch information
Cjen1 committed Jul 20, 2023
1 parent 177ef28 commit c976ceb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,13 +838,21 @@ func (r *raft) becomeFollower(term uint64, lead uint64) {
r.logger.Infof("%x became follower at term %d", r.id, r.Term)
}

func (r *raft) nextTerm() uint64 {
// Term = [epoch:48; rand:16]
var cepoch uint64 = (r.Term & 0xffff_ffff_ffff_0000) >> 16
var tepoch uint64 = (cepoch + 1) << 16
var trdm uint64 = uint64(globalRand.Intn(65536)) & 0xffff
return tepoch | trdm
}

func (r *raft) becomeCandidate() {
// TODO(xiangli) remove the panic when the raft implementation is stable
if r.state == StateLeader {
panic("invalid transition [leader -> candidate]")
}
r.step = stepCandidate
r.reset(r.Term + 1)
r.reset(r.nextTerm())
r.tick = r.tickElection
r.Vote = r.id
r.state = StateCandidate
Expand Down Expand Up @@ -943,7 +951,7 @@ func (r *raft) campaign(t CampaignType) {
r.becomePreCandidate()
voteMsg = pb.MsgPreVote
// PreVote RPCs are sent for the next term before we've incremented r.Term.
term = r.Term + 1
term = r.nextTerm()
} else {
r.becomeCandidate()
voteMsg = pb.MsgVote
Expand Down

0 comments on commit c976ceb

Please sign in to comment.