Skip to content

Commit

Permalink
Enforce ElectionTimeout >= HeartbeatTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Apr 24, 2014
1 parent 5f1ca85 commit 09d87a3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna
conf.LogOutput = os.Stderr
}

// Ensure the ElectionTimeout is not less than the HeartbeatTimeout
if conf.ElectionTimeout < conf.HeartbeatTimeout {
return nil, fmt.Errorf("ElectionTimeout must be equal or greater than HeartbeatTimeout")
}

// Try to restore the current term
currentTerm, err := stable.GetUint64(keyCurrentTerm)
if err != nil && err.Error() != "not found" {
Expand Down
2 changes: 2 additions & 0 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ func TestRaft_ApplyConcurrent(t *testing.T) {
// Make the cluster
conf := inmemConfig()
conf.HeartbeatTimeout = 100 * time.Millisecond
conf.ElectionTimeout = 100 * time.Millisecond
c := MakeCluster(3, t, conf)
defer c.Close()

Expand Down Expand Up @@ -600,6 +601,7 @@ func TestRaft_ApplyConcurrent_Timeout(t *testing.T) {
// Make the cluster
conf := inmemConfig()
conf.HeartbeatTimeout = 100 * time.Millisecond
conf.ElectionTimeout = 100 * time.Millisecond
c := MakeCluster(1, t, conf)
defer c.Close()

Expand Down

0 comments on commit 09d87a3

Please sign in to comment.