mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: address issues with election timeout
This commit is contained in:
parent
f65d117462
commit
1cd3345e00
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
|
||||||
|
|
||||||
pb "github.com/coreos/etcd/raft/raftpb"
|
pb "github.com/coreos/etcd/raft/raftpb"
|
||||||
)
|
)
|
||||||
@ -134,7 +133,7 @@ func newRaft(id int64, peers []int64, election, heartbeat int) *raft {
|
|||||||
if id == None {
|
if id == None {
|
||||||
panic("cannot use none id")
|
panic("cannot use none id")
|
||||||
}
|
}
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(id)
|
||||||
r := &raft{
|
r := &raft{
|
||||||
id: id,
|
id: id,
|
||||||
lead: None,
|
lead: None,
|
||||||
@ -589,12 +588,12 @@ func (r *raft) loadState(state pb.HardState) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// isElectionTimeout returns true if r.elapsed is greater than the
|
// isElectionTimeout returns true if r.elapsed is greater than the
|
||||||
// randomized election timeout in [electiontimeout, 2 * electiontimeout - 1).
|
// randomized election timeout in (electiontimeout, 2 * electiontimeout - 1).
|
||||||
// Otherwise, it returns false.
|
// Otherwise, it returns false.
|
||||||
func (r *raft) isElectionTimeout() bool {
|
func (r *raft) isElectionTimeout() bool {
|
||||||
d := r.elapsed - r.electionTimeout
|
d := r.elapsed - r.electionTimeout
|
||||||
if d < 0 {
|
if d < 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return d > int(rand.Int31())%r.electionTimeout
|
return d > rand.Int()%r.electionTimeout
|
||||||
}
|
}
|
||||||
|
@ -496,7 +496,7 @@ func TestCommit(t *testing.T) {
|
|||||||
func TestIsElectionTimeout(t *testing.T) {
|
func TestIsElectionTimeout(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
elapse int
|
elapse int
|
||||||
wpossibility float64
|
wprobability float64
|
||||||
round bool
|
round bool
|
||||||
}{
|
}{
|
||||||
{5, 0, false},
|
{5, 0, false},
|
||||||
@ -519,8 +519,8 @@ func TestIsElectionTimeout(t *testing.T) {
|
|||||||
if tt.round {
|
if tt.round {
|
||||||
got = math.Floor(got*10+0.5) / 10.0
|
got = math.Floor(got*10+0.5) / 10.0
|
||||||
}
|
}
|
||||||
if got != tt.wpossibility {
|
if got != tt.wprobability {
|
||||||
t.Errorf("#%d: possibility = %v, want %v", i, got, tt.wpossibility)
|
t.Errorf("#%d: possibility = %v, want %v", i, got, tt.wprobability)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user