From 5250784b091b0f9912988625521a020d956abc7e Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 5 Apr 2016 14:21:43 -0400 Subject: [PATCH 1/2] raft: use rand.Intn instead of rand.Int and mod This provides a better random distribution and is easier to read. --- raft/raft.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raft/raft.go b/raft/raft.go index b17b292a1..4bb399c39 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -877,7 +877,7 @@ func (r *raft) pastElectionTimeout() bool { } 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 From 68db18667a1111e82fa4796629a2480bbbe366c1 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Wed, 6 Apr 2016 08:41:46 -0400 Subject: [PATCH 2/2] raft: correct doc comment --- raft/raft.go | 6 +++--- raft/raft_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/raft/raft.go b/raft/raft.go index 4bb399c39..b56f34aa8 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -869,9 +869,9 @@ 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 } diff --git a/raft/raft_test.go b/raft/raft_test.go index a3bfb294b..5187480c6 100644 --- a/raft/raft_test.go +++ b/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) } } }