|
|
@@ -176,9 +176,14 @@ type raft struct {
|
|
|
|
|
|
heartbeatTimeout int
|
|
|
electionTimeout int
|
|
|
- rand *rand.Rand
|
|
|
- tick func()
|
|
|
- step stepFunc
|
|
|
+ // randomizedElectionTimeout is a random number between
|
|
|
+ // [electiontimeout, 2 * electiontimeout - 1]. It gets reset
|
|
|
+ // when raft changes its state to follower or candidate.
|
|
|
+ randomizedElectionTimeout int
|
|
|
+
|
|
|
+ rand *rand.Rand
|
|
|
+ tick func()
|
|
|
+ step stepFunc
|
|
|
|
|
|
logger Logger
|
|
|
}
|
|
|
@@ -392,6 +397,7 @@ func (r *raft) reset(term uint64) {
|
|
|
|
|
|
r.electionElapsed = 0
|
|
|
r.heartbeatElapsed = 0
|
|
|
+ r.resetRandomizedElectionTimeout()
|
|
|
|
|
|
r.votes = make(map[uint64]bool)
|
|
|
for id := range r.prs {
|
|
|
@@ -422,7 +428,7 @@ func (r *raft) tickElection() {
|
|
|
return
|
|
|
}
|
|
|
r.electionElapsed++
|
|
|
- if r.isElectionTimeout() {
|
|
|
+ if r.pastElectionTimeout() {
|
|
|
r.electionElapsed = 0
|
|
|
r.Step(pb.Message{From: r.id, Type: pb.MsgHup})
|
|
|
}
|
|
|
@@ -863,15 +869,15 @@ func (r *raft) loadState(state pb.HardState) {
|
|
|
r.Vote = state.Vote
|
|
|
}
|
|
|
|
|
|
-// isElectionTimeout returns true if r.electionElapsed is greater than the
|
|
|
-// randomized election timeout in (electiontimeout, 2 * electiontimeout - 1).
|
|
|
+// pastElectionTimeout returns true if r.electionElapsed is greater than the
|
|
|
+// randomized election timeout in [electiontimeout, 2 * electiontimeout - 1].
|
|
|
// Otherwise, it returns false.
|
|
|
-func (r *raft) isElectionTimeout() bool {
|
|
|
- d := r.electionElapsed - r.electionTimeout
|
|
|
- if d < 0 {
|
|
|
- return false
|
|
|
- }
|
|
|
- return d > r.rand.Int()%r.electionTimeout
|
|
|
+func (r *raft) pastElectionTimeout() bool {
|
|
|
+ return r.electionElapsed >= r.randomizedElectionTimeout
|
|
|
+}
|
|
|
+
|
|
|
+func (r *raft) resetRandomizedElectionTimeout() {
|
|
|
+ r.randomizedElectionTimeout = r.electionTimeout + r.rand.Int()%r.electionTimeout
|
|
|
}
|
|
|
|
|
|
// checkQuorumActive returns true if the quorum is active from
|