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

@ -80,7 +80,9 @@ type toApply struct {
type raftNode struct {
lg *zap.Logger
tickMu *sync.Mutex
tickMu *sync.RWMutex
// timestamp of the latest tick
latestTickTs time.Time
raftNodeConfig
// a chan to send/receive snapshot
@ -132,8 +134,9 @@ func newRaftNode(cfg raftNodeConfig) *raftNode {
raft.SetLogger(lg)
r := &raftNode{
lg: cfg.lg,
tickMu: new(sync.Mutex),
tickMu: new(sync.RWMutex),
raftNodeConfig: cfg,
latestTickTs: time.Now(),
// set up contention detectors for raft heartbeat message.
// expect to send a heartbeat within 2 heartbeat intervals.
td: contention.NewTimeoutDetector(2 * cfg.heartbeat),
@ -155,9 +158,16 @@ func newRaftNode(cfg raftNodeConfig) *raftNode {
func (r *raftNode) tick() {
r.tickMu.Lock()
r.Tick()
r.latestTickTs = time.Now()
r.tickMu.Unlock()
}
func (r *raftNode) getLatestTickTs() time.Time {
r.tickMu.RLock()
defer r.tickMu.RUnlock()
return r.latestTickTs
}
// start prepares and starts raftNode in a new goroutine. It is no longer safe
// to modify the fields after it has been started.
func (r *raftNode) start(rh *raftReadyHandler) {