Skip leadership check if the etcd instance is active processing heartbeat

Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
This commit is contained in:
Benjamin Wang
2024-08-09 11:26:15 +01:00
parent df4e472a2d
commit b8b0cf83d1
3 changed files with 71 additions and 2 deletions

View File

@ -904,10 +904,26 @@ func (s *EtcdServer) revokeExpiredLeases(leases []*lease.Lease) {
})
}
// isActive checks if the etcd instance is still actively processing the
// heartbeat message (ticks). It returns false if no heartbeat has been
// received within 3 * tickMs.
func (s *EtcdServer) isActive() bool {
latestTickTs := s.r.getLatestTickTs()
threshold := 3 * time.Duration(s.Cfg.TickMs) * time.Millisecond
return latestTickTs.Add(threshold).After(time.Now())
}
// ensureLeadership checks whether current member is still the leader.
func (s *EtcdServer) ensureLeadership() bool {
lg := s.Logger()
if s.isActive() {
lg.Debug("The member is active, skip checking leadership",
zap.Time("latestTickTs", s.r.getLatestTickTs()),
zap.Time("now", time.Now()))
return true
}
ctx, cancel := context.WithTimeout(s.ctx, s.Cfg.ReqTimeout())
defer cancel()
if err := s.linearizableReadNotify(ctx); err != nil {