Browse Source

Merge pull request #4970 from tamird/fix-raft-past-election

raft: correct regression in `pastElectionTimeout`
Xiang Li 9 năm trước cách đây
mục cha
commit
62990fb5fa
2 tập tin đã thay đổi với 6 bổ sung6 xóa
  1. 4 4
      raft/raft.go
  2. 2 2
      raft/raft_test.go

+ 4 - 4
raft/raft.go

@@ -869,15 +869,15 @@ func (r *raft) loadState(state pb.HardState) {
 	r.Vote = state.Vote
 }
 
-// pastElectionTimeout returns true if r.electionElapsed is greater than the
-// randomized election timeout in [electiontimeout, 2 * electiontimeout - 1].
-// Otherwise, it returns false.
+// pastElectionTimeout returns true iff r.electionElapsed is greater
+// than or equal to the randomized election timeout in
+// [electiontimeout, 2 * electiontimeout - 1].
 func (r *raft) pastElectionTimeout() bool {
 	return r.electionElapsed >= r.randomizedElectionTimeout
 }
 
 func (r *raft) resetRandomizedElectionTimeout() {
-	r.randomizedElectionTimeout = r.electionTimeout + r.rand.Int()%r.electionTimeout
+	r.randomizedElectionTimeout = r.electionTimeout + r.rand.Intn(r.electionTimeout)
 }
 
 // checkQuorumActive returns true if the quorum is active from

+ 2 - 2
raft/raft_test.go

@@ -747,7 +747,7 @@ func TestCommit(t *testing.T) {
 	}
 }
 
-func TestIsElectionTimeout(t *testing.T) {
+func TestPastElectionTimeout(t *testing.T) {
 	tests := []struct {
 		elapse       int
 		wprobability float64
@@ -776,7 +776,7 @@ func TestIsElectionTimeout(t *testing.T) {
 			got = math.Floor(got*10+0.5) / 10.0
 		}
 		if got != tt.wprobability {
-			t.Errorf("#%d: possibility = %v, want %v", i, got, tt.wprobability)
+			t.Errorf("#%d: probability = %v, want %v", i, got, tt.wprobability)
 		}
 	}
 }