Skip to content

Commit

Permalink
region_scatter: fix a bug that PD may panic when scatter region if re…
Browse files Browse the repository at this point in the history
…gion is unhealthy (#6128) (#6129)

close #6099, ref #6128

Signed-off-by: HunDunDM <[email protected]>

Co-authored-by: HunDunDM <[email protected]>
  • Loading branch information
ti-chi-bot and HunDunDM authored Mar 9, 2023
1 parent 51f382c commit bd80719
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/schedule/region_scatterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions server/schedule/region_scatterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bd80719

Please sign in to comment.