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)

close #6099

Signed-off-by: HunDunDM <hundundm@gmail.com>
  • Loading branch information
HunDunDM authored Mar 9, 2023
1 parent afd79ed commit fda6fef
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 @@ -406,12 +406,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 fda6fef

Please sign in to comment.