Skip to content

Commit 3282d90

Browse files
committed
etcdserver: adjust election ticks on restart
Signed-off-by: Gyuho Lee <[email protected]>
1 parent b2d5c6c commit 3282d90

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎etcdserver/raft.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type raftNode struct {
9595
term uint64
9696
lead uint64
9797

98+
tickMu *sync.Mutex
9899
raftNodeConfig
99100

100101
// a chan to send/receive snapshot
@@ -131,6 +132,7 @@ type raftNodeConfig struct {
131132

132133
func newRaftNode(cfg raftNodeConfig) *raftNode {
133134
r := &raftNode{
135+
tickMu: new(sync.Mutex),
134136
raftNodeConfig: cfg,
135137
// set up contention detectors for raft heartbeat message.
136138
// expect to send a heartbeat within 2 heartbeat intervals.
@@ -149,6 +151,13 @@ func newRaftNode(cfg raftNodeConfig) *raftNode {
149151
return r
150152
}
151153

154+
// raft.Node does not have locks in Raft package
155+
func (r *raftNode) tick() {
156+
r.tickMu.Lock()
157+
r.Tick()
158+
r.tickMu.Unlock()
159+
}
160+
152161
// start prepares and starts raftNode in a new goroutine. It is no longer safe
153162
// to modify the fields after it has been started.
154163
func (r *raftNode) start(rh *raftReadyHandler) {
@@ -161,7 +170,7 @@ func (r *raftNode) start(rh *raftReadyHandler) {
161170
for {
162171
select {
163172
case <-r.ticker.C:
164-
r.Tick()
173+
r.tick()
165174
case rd := <-r.Ready():
166175
if rd.SoftState != nil {
167176
newLeader := rd.SoftState.Lead != raft.None && atomic.LoadUint64(&r.lead) != rd.SoftState.Lead
@@ -374,7 +383,7 @@ func (r *raftNode) resumeSending() {
374383
// speeding up election process.
375384
func (r *raftNode) advanceTicks(ticks int) {
376385
for i := 0; i < ticks; i++ {
377-
r.Tick()
386+
r.tick()
378387
}
379388
}
380389

‎etcdserver/server.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,51 @@ func NewServer(cfg ServerConfig) (srv *EtcdServer, err error) {
521521
return srv, nil
522522
}
523523

524+
func (s *EtcdServer) adjustTicks() {
525+
clusterN := len(s.cluster.Members())
526+
527+
// single-node fresh start, or single-node recovers from snapshot
528+
if clusterN == 1 {
529+
ticks := s.Cfg.ElectionTicks - 1
530+
plog.Infof("%s as single-node; fast-forwarding %d ticks (election ticks %d)", s.ID(), ticks, s.Cfg.ElectionTicks)
531+
s.r.advanceTicks(ticks)
532+
return
533+
}
534+
535+
// retry up to "rafthttp.ConnReadTimeout", which is 5-sec
536+
// until peer connection reports; otherwise:
537+
// 1. all connections failed, or
538+
// 2. no active peers, or
539+
// 3. restarted single-node with no snapshot
540+
// then, do nothing, because advancing ticks would have no effect
541+
waitTime := rafthttp.ConnReadTimeout
542+
itv := 50 * time.Millisecond
543+
for i := int64(0); i < int64(waitTime/itv); i++ {
544+
select {
545+
case <-time.After(itv):
546+
case <-s.stopping:
547+
return
548+
}
549+
550+
peerN := s.r.transport.ActivePeers()
551+
if peerN > 1 {
552+
// multi-node received peer connection reports
553+
// adjust ticks, in case slow leader message receive
554+
ticks := s.Cfg.ElectionTicks - 2
555+
plog.Infof("%s initialzed peer connection; fast-forwarding %d ticks (election ticks %d) with %d active peer(s)", s.ID(), ticks, s.Cfg.ElectionTicks, peerN)
556+
s.r.advanceTicks(ticks)
557+
return
558+
}
559+
}
560+
}
561+
524562
// Start performs any initialization of the Server necessary for it to
525563
// begin serving requests. It must be called before Do or Process.
526564
// Start must be non-blocking; any long-running server functionality
527565
// should be implemented in goroutines.
528566
func (s *EtcdServer) Start() {
529567
s.start()
568+
s.goAttach(func() { s.adjustTicks() })
530569
s.goAttach(func() { s.publish(s.Cfg.ReqTimeout()) })
531570
s.goAttach(s.purgeFile)
532571
s.goAttach(func() { monitorFileDescriptor(s.stopping) })

0 commit comments

Comments
 (0)