From f1c4f3109b5f5af414dd5d2dec6adebc25709365 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Thu, 9 Nov 2023 10:54:50 +0800 Subject: [PATCH] checker: refactor fixOrphanPeers Signed-off-by: lhy1024 --- pkg/schedule/checker/rule_checker.go | 43 ++++++++++++++++------------ server/cluster/cluster_test.go | 2 +- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/pkg/schedule/checker/rule_checker.go b/pkg/schedule/checker/rule_checker.go index c4e7c242dea..e09c008626d 100644 --- a/pkg/schedule/checker/rule_checker.go +++ b/pkg/schedule/checker/rule_checker.go @@ -449,23 +449,31 @@ func (c *RuleChecker) fixOrphanPeers(region *core.RegionInfo, fit *placement.Reg return nil, nil } - isUnhealthyPeer := func(id uint64) bool { - for _, downPeer := range region.GetDownPeers() { - if downPeer.Peer.GetId() == id { + isPendingPeer := func(id uint64) bool { + for _, pendingPeer := range region.GetPendingPeers() { + if pendingPeer.GetId() == id { return true } } - for _, pendingPeer := range region.GetPendingPeers() { - if pendingPeer.GetId() == id { + return false + } + + isDownPeer := func(id uint64) bool { + for _, downPeer := range region.GetDownPeers() { + if downPeer.Peer.GetId() == id { return true } } return false } - isDisconnectedPeer := func(p *metapb.Peer) bool { + isUnhealthyPeer := func(id uint64) bool { + return isPendingPeer(id) || isDownPeer(id) + } + + isInDisconnectedStore := func(p *metapb.Peer) bool { // avoid to meet down store when fix orphan peers, - // Isdisconnected is more strictly than IsUnhealthy. + // isInDisconnectedStore is usually more strictly than IsUnhealthy. store := c.cluster.GetStore(p.GetStoreId()) if store == nil { return true @@ -475,16 +483,15 @@ func (c *RuleChecker) fixOrphanPeers(region *core.RegionInfo, fit *placement.Reg checkDownPeer := func(peers []*metapb.Peer) (*metapb.Peer, bool) { for _, p := range peers { - if isUnhealthyPeer(p.GetId()) { - // make sure is down peer. - if region.GetDownPeer(p.GetId()) != nil { - return p, true - } - return nil, true + if isInDisconnectedStore(p) { + return p, true } - if isDisconnectedPeer(p) { + if isDownPeer(p.GetId()) { return p, true } + if isPendingPeer(p.GetId()) { + return nil, true + } } return nil, false } @@ -517,7 +524,7 @@ func (c *RuleChecker) fixOrphanPeers(region *core.RegionInfo, fit *placement.Reg continue } // make sure the orphan peer is healthy. - if isUnhealthyPeer(orphanPeer.GetId()) || isDisconnectedPeer(orphanPeer) { + if isUnhealthyPeer(orphanPeer.GetId()) || isInDisconnectedStore(orphanPeer) { continue } // no consider witness in this path. @@ -525,7 +532,7 @@ func (c *RuleChecker) fixOrphanPeers(region *core.RegionInfo, fit *placement.Reg continue } // pinDownPeer's store should be disconnected, because we use more strict judge before. - if !isDisconnectedPeer(pinDownPeer) { + if !isInDisconnectedStore(pinDownPeer) { continue } // check if down peer can replace with orphan peer. @@ -539,7 +546,7 @@ func (c *RuleChecker) fixOrphanPeers(region *core.RegionInfo, fit *placement.Reg return operator.CreatePromoteLearnerOperatorAndRemovePeer("replace-down-peer-with-orphan-peer", c.cluster, region, orphanPeer, pinDownPeer) case orphanPeerRole == metapb.PeerRole_Voter && destRole == metapb.PeerRole_Learner: return operator.CreateDemoteLearnerOperatorAndRemovePeer("replace-down-peer-with-orphan-peer", c.cluster, region, orphanPeer, pinDownPeer) - case orphanPeerRole == destRole && isDisconnectedPeer(pinDownPeer) && !dstStore.IsDisconnected(): + case orphanPeerRole == destRole && isInDisconnectedStore(pinDownPeer) && !dstStore.IsDisconnected(): return operator.CreateRemovePeerOperator("remove-replaced-orphan-peer", c.cluster, 0, region, pinDownPeer.GetStoreId()) default: // destRole should not same with orphanPeerRole. if role is same, it fit with orphanPeer should be better than now. @@ -557,7 +564,7 @@ func (c *RuleChecker) fixOrphanPeers(region *core.RegionInfo, fit *placement.Reg hasHealthPeer := false var disconnectedPeer *metapb.Peer for _, orphanPeer := range fit.OrphanPeers { - if isDisconnectedPeer(orphanPeer) { + if isInDisconnectedStore(orphanPeer) { disconnectedPeer = orphanPeer break } diff --git a/server/cluster/cluster_test.go b/server/cluster/cluster_test.go index 89c9ea32f19..70782e27cd3 100644 --- a/server/cluster/cluster_test.go +++ b/server/cluster/cluster_test.go @@ -2792,7 +2792,7 @@ func TestReplica(t *testing.T) { re.NoError(dispatchHeartbeat(co, region, stream)) waitNoResponse(re, stream) - // Remove peer from store 4. + // Remove peer from store 3. re.NoError(tc.addLeaderRegion(2, 1, 2, 3, 4)) region = tc.GetRegion(2) re.NoError(dispatchHeartbeat(co, region, stream))