From bd807190154f664d1b337df5e2281f56449e7d82 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 9 Mar 2023 18:55:13 +0800 Subject: [PATCH] region_scatter: fix a bug that PD may panic when scatter region if region is unhealthy (#6128) (#6129) close tikv/pd#6099, ref tikv/pd#6128 Signed-off-by: HunDunDM Co-authored-by: HunDunDM --- server/schedule/region_scatterer.go | 10 ++++++---- server/schedule/region_scatterer_test.go | 5 +++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/server/schedule/region_scatterer.go b/server/schedule/region_scatterer.go index 8bf58bfd91b..13e4d9215d5 100644 --- a/server/schedule/region_scatterer.go +++ b/server/schedule/region_scatterer.go @@ -394,12 +394,14 @@ func allowLeader(fit *placement.RegionFit, peer *metapb.Peer) bool { if peer.IsWitness { return false } - - rule := fit.GetRuleFit(peer.GetId()).Rule - if rule.IsWitness { + peerFit := fit.GetRuleFit(peer.GetId()) + if peerFit == nil || peerFit.Rule == nil { + return false + } + if peerFit.Rule.IsWitness { return false } - switch rule.Role { + switch peerFit.Rule.Role { case placement.Voter, placement.Leader: return true } diff --git a/server/schedule/region_scatterer_test.go b/server/schedule/region_scatterer_test.go index 432c038a92f..ea8fb80e718 100644 --- a/server/schedule/region_scatterer_test.go +++ b/server/schedule/region_scatterer_test.go @@ -758,6 +758,11 @@ func TestBalanceRegion(t *testing.T) { re.Equal(uint64(150), scatterer.ordinaryEngine.selectedPeer.Get(i, group)) re.Equal(uint64(50), scatterer.ordinaryEngine.selectedLeader.Get(i, group)) } + // Test for unhealthy region + // ref https://github.com/tikv/pd/issues/6099 + region := tc.AddLeaderRegion(1500, 2, 3, 4, 6) + op := scatterer.scatterRegion(region, group) + re.False(isPeerCountChanged(op)) } func isPeerCountChanged(op *operator.Operator) bool {