From 53300ece3bf849fe9a876c7813b0191fe54693e2 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Fri, 13 Jan 2023 15:07:07 +0800 Subject: [PATCH] etcdserver: return membership.ErrIDNotFound when the memberID not found Backport https://github.com/etcd-io/etcd/pull/15095. When promoting a learner, we need to wait until the leader's applied ID catches up to the commitId. Afterwards, check whether the learner ID exist or not, and return `membership.ErrIDNotFound` directly in the API if the member ID not found, to avoid the request being unnecessarily delivered to raft. Signed-off-by: Benjamin Wang --- server/etcdserver/server.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 7f624fc0d42..4e1c2c041a7 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -1744,6 +1744,10 @@ func (s *EtcdServer) mayPromoteMember(id types.ID) error { // Note: it will return nil if member is not found in cluster or if member is not learner. // These two conditions will be checked before apply phase later. func (s *EtcdServer) isLearnerReady(id uint64) error { + if err := s.waitAppliedIndex(); err != nil { + return err + } + rs := s.raftStatus() // leader's raftStatus.Progress is not nil @@ -1763,12 +1767,16 @@ func (s *EtcdServer) isLearnerReady(id uint64) error { } } - if isFound { - leaderMatch := rs.Progress[leaderID].Match - // the learner's Match not caught up with leader yet - if float64(learnerMatch) < float64(leaderMatch)*readyPercent { - return ErrLearnerNotReady - } + // We should return an error in API directly, to avoid the request + // being unnecessarily delivered to raft. + if !isFound { + return membership.ErrIDNotFound + } + + leaderMatch := rs.Progress[leaderID].Match + // the learner's Match not caught up with leader yet + if float64(learnerMatch) < float64(leaderMatch)*readyPercent { + return ErrLearnerNotReady } return nil