diff --git a/server/schedule/checker/rule_checker.go b/server/schedule/checker/rule_checker.go index d8590fb1b2b..f7caff72760 100644 --- a/server/schedule/checker/rule_checker.go +++ b/server/schedule/checker/rule_checker.go @@ -182,6 +182,10 @@ func (c *RuleChecker) fixBetterLocation(region *core.RegionInfo, fit *placement. } oldPeer := region.GetStorePeer(oldPeerStore.GetID()) newPeerStore := SelectStoreToReplacePeerByRule("rule-checker", c.cluster, region, fit, rf, oldPeer) + if newPeerStore == nil { + log.Debug("no replacement store", zap.Uint64("region-id", region.GetID())) + return nil, nil + } stores = getRuleFitStores(c.cluster, removePeerFromRuleFit(rf, oldPeer)) oldScore := core.DistinctScore(rf.Rule.LocationLabels, stores, oldPeerStore) newScore := core.DistinctScore(rf.Rule.LocationLabels, stores, newPeerStore) diff --git a/server/schedule/checker/rule_checker_test.go b/server/schedule/checker/rule_checker_test.go index 48c1fc243b7..d463dc88ce7 100644 --- a/server/schedule/checker/rule_checker_test.go +++ b/server/schedule/checker/rule_checker_test.go @@ -15,6 +15,7 @@ package checker import ( "encoding/hex" + . "github.com/pingcap/check" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" @@ -179,3 +180,21 @@ func (s *testRuleCheckerSuite) TestBetterReplacement(c *C) { op = s.rc.Check(s.cluster.GetRegion(1)) c.Assert(op, IsNil) } + +func (s *testRuleCheckerSuite) TestNoBetterReplacement(c *C) { + s.cluster.AddLabelsStore(1, 1, map[string]string{"host": "host1"}) + s.cluster.AddLabelsStore(2, 1, map[string]string{"host": "host1"}) + s.cluster.AddLabelsStore(3, 1, map[string]string{"host": "host2"}) + s.cluster.AddLeaderRegionWithRange(1, "", "", 1, 2, 3) + s.ruleManager.SetRule(&placement.Rule{ + GroupID: "pd", + ID: "test", + Index: 100, + Override: true, + Role: placement.Voter, + Count: 3, + LocationLabels: []string{"host"}, + }) + op := s.rc.Check(s.cluster.GetRegion(1)) + c.Assert(op, IsNil) +}