From fad0db1fbd3afd0ce86714e741dc0c960a3ea0dc Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Tue, 6 Mar 2018 02:07:48 -0800 Subject: [PATCH] etcdserver: adjust election ticks on restart Signed-off-by: Gyuho Lee --- etcdserver/server.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/etcdserver/server.go b/etcdserver/server.go index 1cfa28e0f0cb..b52fdd65e8b6 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -539,6 +539,33 @@ func NewServer(cfg ServerConfig) (srv *EtcdServer, err error) { } srv.r.transport = tr + srv.goAttach(func() { + clusterN := len(cl.Members()) + + // single-node fresh start, or single-node recovers from snapshot + if clusterN == 1 { + ticks := cfg.ElectionTicks - 1 + plog.Infof("%s as single-node; fast-forwarding %d ticks (election ticks %d) with %d found member(s)", srv.ID(), ticks, cfg.ElectionTicks, clusterN) + srv.advanceRaftTicks(ticks) + return + } + + select { + case <-tr.InitialPeerNotify(): + // multi-node received peer connection reports + // adjust ticks, in case slow leader message receive + ticks := cfg.ElectionTicks - 2 + plog.Infof("%s initialzed peer connection; fast-forwarding %d ticks (election ticks %d) with %d found member(s)", srv.ID(), ticks, cfg.ElectionTicks, len(cl.Members())) + srv.advanceRaftTicks(ticks) + + case <-time.After(rafthttp.ConnReadTimeout): + // advancing ticks would have no effect when: + // 1. all connections failed, or + // 2. no active peers, or + // 3. restarted single-node with no snapshot + plog.Infof("%s waited %s but no active peer found (or restarted 1-node cluster); currently, %d member(s)", srv.ID(), rafthttp.ConnReadTimeout, len(cl.Members())) + } + }) return srv, nil }