From 459b66fa0930058f994439038cdb4fdc3fcba362 Mon Sep 17 00:00:00 2001 From: Chao Chen Date: Sun, 29 Oct 2023 09:45:58 -0700 Subject: [PATCH] etcdserver/raft.go: separate raft tick and ready Signed-off-by: Chao Chen --- server/etcdserver/api/v3rpc/interceptor.go | 2 +- server/etcdserver/raft.go | 13 +++++++++++-- server/etcdserver/server.go | 8 ++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/server/etcdserver/api/v3rpc/interceptor.go b/server/etcdserver/api/v3rpc/interceptor.go index 776488ef919..c764c5c516b 100644 --- a/server/etcdserver/api/v3rpc/interceptor.go +++ b/server/etcdserver/api/v3rpc/interceptor.go @@ -323,7 +323,7 @@ func monitorLeader(s *etcdserver.EtcdServer) *streamsMap { case <-s.StoppingNotify(): return case <-time.After(election): - if s.Leader() == types.ID(raft.None) { + if s.RaftStatus().Lead == raft.None { noLeaderCnt++ } else { noLeaderCnt = 0 diff --git a/server/etcdserver/raft.go b/server/etcdserver/raft.go index 2a315ea5865..d73b79bd33c 100644 --- a/server/etcdserver/raft.go +++ b/server/etcdserver/raft.go @@ -163,14 +163,23 @@ func (r *raftNode) tick() { func (r *raftNode) start(rh *raftReadyHandler) { internalTimeout := time.Second + go func() { + for { + select { + case <-r.ticker.C: + r.tick() + case <-r.done: + return + } + } + }() + go func() { defer r.onStop() islead := false for { select { - case <-r.ticker.C: - r.tick() case rd := <-r.Ready(): if rd.SoftState != nil { newLeader := rd.SoftState.Lead != raft.None && rh.getLead() != rd.SoftState.Lead diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 4b40e32bada..aebd8b627b2 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -1492,9 +1492,9 @@ func (s *EtcdServer) isLearnerReady(id uint64) error { return err } - rs := s.raftStatus() + rs := s.RaftStatus() - // leader's raftStatus.Progress is not nil + // leader's RaftStatus.Progress is not nil if rs.Progress == nil { return errors.ErrNotLeader } @@ -2451,8 +2451,8 @@ func (s *EtcdServer) IsMemberExist(id types.ID) bool { return s.cluster.IsMemberExist(id) } -// raftStatus returns the raft status of this etcd node. -func (s *EtcdServer) raftStatus() raft.Status { +// RaftStatus returns the raft status of this etcd node. +func (s *EtcdServer) RaftStatus() raft.Status { return s.r.Node.Status() }