From 21f5b885f2a7df2025faa79807de6117848d2015 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 24 Jul 2015 16:48:33 -0700 Subject: [PATCH] etcdserver: fast election timeout when bootstrap cluster The behavior accelarates the happen of the first-time leader election, so the cluster could elect its leader fast. Technically, it could help to reduce `electionMs - heartbeatMs` wait time for the first leader election. Main usage: 1. Quick start for the local cluster when setting a little longer election timeout 2. Quick start for the global cluster, which sets election timeout to its maximum 50s. --- etcdserver/raft.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/etcdserver/raft.go b/etcdserver/raft.go index f6d2da30d..7a6742bd8 100644 --- a/etcdserver/raft.go +++ b/etcdserver/raft.go @@ -195,6 +195,16 @@ func (r *raftNode) resumeSending() { p.Resume() } +// advanceTicksForElection advances ticks to the node for fast election. +// This reduces the time to wait for first leader election if bootstrapping the whole +// cluster, while leaving at least 1 heartbeat for possible existing leader +// to contact it. +func advanceTicksForElection(n raft.Node, electionTicks int) { + for i := 0; i < electionTicks-1; i++ { + n.Tick() + } +} + func startNode(cfg *ServerConfig, cl *cluster, ids []types.ID) (id types.ID, n raft.Node, s *raft.MemoryStorage, w *wal.WAL) { var err error member := cl.MemberByName(cfg.Name) @@ -231,6 +241,7 @@ func startNode(cfg *ServerConfig, cl *cluster, ids []types.ID) (id types.ID, n r } n = raft.StartNode(c, peers) raftStatus = n.Status + advanceTicksForElection(n, c.ElectionTick) return } @@ -260,6 +271,7 @@ func restartNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *clust } n := raft.RestartNode(c) raftStatus = n.Status + advanceTicksForElection(n, c.ElectionTick) return id, cl, n, s, w }