Skip to content

Commit

Permalink
embed: advance ticks on restart
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Mar 3, 2018
1 parent f3ed37a commit b26594c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,44 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
return e, err
}
}

restarted := e.Server.IsRestarted()
clusterN := len(e.Server.Cluster().Members())

// bootstrapping to fresh cluster, or single-node;
// advance ticks before starting Raft
needAdv := !restarted && clusterN == 1
if needAdv {
ticks := srvcfg.ElectionTicks - 1
plog.Infof("%s is advancing %d ticks for faster election (election tick %d, restarted '%v', %d found peer(s))", e.Server.ID(), ticks, srvcfg.ElectionTicks, restarted, clusterN)
e.Server.AdvanceRaftTicks(ticks)
needAdv = false
}
e.Server.Start()

if err = e.servePeers(); err != nil {
return e, err
}

// have not advanced ticks yet AND restarted,
// BUT no active peers have been found yet
actives := e.Server.CountActivePeers()
if !needAdv && restarted && actives == 0 {
// restart node, but we still want to advance ticks
// because even after starting peer transport
// no active peers have been found, maybe this node
// is the first node that started
// instead, adjust ticks to lower value in case
// active leader sends heartbeats to this node
// thus preventing disruptive elections
ticks := srvcfg.ElectionTicks / 10
plog.Infof("%s is advancing %d ticks after serving peers (election tick %d, restarted '%v', %d found peer(s))", e.Server.ID(), ticks, srvcfg.ElectionTicks, restarted, actives)
e.Server.AdvanceRaftTicks(ticks)
needAdv = false
} else {
plog.Infof("%s started serving peers with %d found peer(s) (no fast-forwarding election ticks after serving peers)", e.Server.ID(), actives)
}

if err = e.serveClients(); err != nil {
return e, err
}
Expand Down

0 comments on commit b26594c

Please sign in to comment.