From 930156c18ae2bd18a89b881bfc436ea6f49759c7 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Wed, 7 Jan 2015 10:46:57 -0800 Subject: [PATCH] integration: adjust election ticks using env var --- etcdserver/config.go | 3 +++ etcdserver/server.go | 12 ++++++++++-- integration/cluster_test.go | 12 ++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/etcdserver/config.go b/etcdserver/config.go index e83680ad0..f405effc4 100644 --- a/etcdserver/config.go +++ b/etcdserver/config.go @@ -43,6 +43,9 @@ type ServerConfig struct { NewCluster bool ForceNewCluster bool Transport *http.Transport + + // Only for testing purpose + ElectionTimeoutTicks int } // VerifyBootstrapConfig sanity-checks the initial config and returns an error diff --git a/etcdserver/server.go b/etcdserver/server.go index 57abc0326..32e2c5a3f 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -867,8 +867,12 @@ func startNode(cfg *ServerConfig, ids []types.ID) (id types.ID, n raft.Node, s * } id = member.ID log.Printf("etcdserver: start member %s in cluster %s", id, cfg.Cluster.ID()) + election := cfg.ElectionTimeoutTicks + if election == 0 { + election = 10 + } s = raft.NewMemoryStorage() - n = raft.StartNode(uint64(id), peers, 10, 1, s) + n = raft.StartNode(uint64(id), peers, election, 1, s) return } @@ -877,13 +881,17 @@ func restartNode(cfg *ServerConfig, index uint64, snapshot *raftpb.Snapshot) (ty cfg.Cluster.SetID(cid) log.Printf("etcdserver: restart member %s in cluster %s at commit index %d", id, cfg.Cluster.ID(), st.Commit) + election := cfg.ElectionTimeoutTicks + if election == 0 { + election = 10 + } s := raft.NewMemoryStorage() if snapshot != nil { s.ApplySnapshot(*snapshot) } s.SetHardState(st) s.Append(ents) - n := raft.RestartNode(uint64(id), 10, 1, s) + n := raft.RestartNode(uint64(id), election, 1, s) return id, n, s, w } diff --git a/integration/cluster_test.go b/integration/cluster_test.go index c64d9392b..938cd8f3e 100644 --- a/integration/cluster_test.go +++ b/integration/cluster_test.go @@ -27,6 +27,7 @@ import ( "os" "reflect" "sort" + "strconv" "strings" "testing" "time" @@ -49,9 +50,18 @@ const ( requestTimeout = 2 * time.Second ) +var ( + electionTicks = 10 +) + func init() { // open microsecond-level time log for integration test debugging log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile) + if t := os.Getenv("ETCD_ELECTION_TIMEOUT_TICKS"); t != "" { + if i, err := strconv.ParseInt(t, 10, 64); err == nil { + electionTicks = int(i) + } + } } func TestClusterOf1(t *testing.T) { testCluster(t, 1) } @@ -431,6 +441,7 @@ func mustNewMember(t *testing.T, name string) *member { } m.NewCluster = true m.Transport = mustNewTransport(t) + m.ElectionTimeoutTicks = electionTicks return m } @@ -460,6 +471,7 @@ func (m *member) Clone(t *testing.T) *member { panic(err) } mm.Transport = mustNewTransport(t) + mm.ElectionTimeoutTicks = m.ElectionTimeoutTicks return mm }