Skip to content

Commit bd80719

Browse files
ti-chi-botHunDunDM
andauthored
region_scatter: fix a bug that PD may panic when scatter region if region is unhealthy (#6128) (#6129)
close #6099, ref #6128 Signed-off-by: HunDunDM <hundundm@gmail.com> Co-authored-by: HunDunDM <hundundm@gmail.com>
1 parent 51f382c commit bd80719

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

server/schedule/region_scatterer.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,14 @@ func allowLeader(fit *placement.RegionFit, peer *metapb.Peer) bool {
394394
if peer.IsWitness {
395395
return false
396396
}
397-
398-
rule := fit.GetRuleFit(peer.GetId()).Rule
399-
if rule.IsWitness {
397+
peerFit := fit.GetRuleFit(peer.GetId())
398+
if peerFit == nil || peerFit.Rule == nil {
399+
return false
400+
}
401+
if peerFit.Rule.IsWitness {
400402
return false
401403
}
402-
switch rule.Role {
404+
switch peerFit.Rule.Role {
403405
case placement.Voter, placement.Leader:
404406
return true
405407
}

server/schedule/region_scatterer_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,11 @@ func TestBalanceRegion(t *testing.T) {
758758
re.Equal(uint64(150), scatterer.ordinaryEngine.selectedPeer.Get(i, group))
759759
re.Equal(uint64(50), scatterer.ordinaryEngine.selectedLeader.Get(i, group))
760760
}
761+
// Test for unhealthy region
762+
// ref https://github.com/tikv/pd/issues/6099
763+
region := tc.AddLeaderRegion(1500, 2, 3, 4, 6)
764+
op := scatterer.scatterRegion(region, group)
765+
re.False(isPeerCountChanged(op))
761766
}
762767

763768
func isPeerCountChanged(op *operator.Operator) bool {

0 commit comments

Comments
 (0)