Skip to content

Commit eaf86ca

Browse files
HunDunDMti-chi-bot
authored andcommitted
This is an automated cherry-pick of tikv#5920
ref tikv#4570, close tikv#5909 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent 5222943 commit eaf86ca

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

server/schedule/region_scatterer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func (r *RegionScatterer) scatterRegion(region *core.RegionInfo, group string) *
320320
// it is considered that the selected peer select itself.
321321
// This origin peer re-selects.
322322
if _, ok := peers[newPeer.GetStoreId()]; !ok || peer.GetStoreId() == newPeer.GetStoreId() {
323+
selectedStores[peer.GetStoreId()] = struct{}{}
323324
break
324325
}
325326
}

server/schedule/region_scatterer_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,14 @@ func TestRegionHasLearner(t *testing.T) {
607607
checkLeader(scatterer.ordinaryEngine.selectedLeader)
608608
}
609609

610-
// TestSelectedStores tests if the peer count has changed due to the picking strategy.
610+
// TestSelectedStoresTooFewPeers tests if the peer count has changed due to the picking strategy.
611611
// Ref https://github.com/tikv/pd/issues/4565
612+
<<<<<<< HEAD
612613
func (s *testScatterRegionSuite) TestSelectedStores(c *C) {
614+
=======
615+
func TestSelectedStoresTooFewPeers(t *testing.T) {
616+
re := require.New(t)
617+
>>>>>>> f5b5391c0 (region_scatterer: fix the bug that could generate schedule with too many peers (#5920))
613618
ctx, cancel := context.WithCancel(context.Background())
614619
defer cancel()
615620
opt := config.NewTestOptions()
@@ -643,6 +648,43 @@ func (s *testScatterRegionSuite) TestSelectedStores(c *C) {
643648
}
644649
}
645650

651+
// TestSelectedStoresTooManyPeers tests if the peer count has changed due to the picking strategy.
652+
// Ref https://github.com/tikv/pd/issues/5909
653+
func TestSelectedStoresTooManyPeers(t *testing.T) {
654+
re := require.New(t)
655+
ctx, cancel := context.WithCancel(context.Background())
656+
defer cancel()
657+
opt := config.NewTestOptions()
658+
tc := mockcluster.NewCluster(ctx, opt)
659+
stream := hbstream.NewTestHeartbeatStreams(ctx, tc.ID, tc, false)
660+
oc := NewOperatorController(ctx, tc, stream)
661+
// Add 4 stores.
662+
for i := uint64(1); i <= 5; i++ {
663+
tc.AddRegionStore(i, 0)
664+
// prevent store from being disconnected
665+
tc.SetStoreLastHeartbeatInterval(i, -10*time.Minute)
666+
}
667+
group := "group"
668+
scatterer := NewRegionScatterer(ctx, tc, oc)
669+
// priority 4 > 1 > 5 > 2 == 3
670+
for i := 0; i < 1200; i++ {
671+
scatterer.ordinaryEngine.selectedPeer.Put(2, group)
672+
scatterer.ordinaryEngine.selectedPeer.Put(3, group)
673+
}
674+
for i := 0; i < 800; i++ {
675+
scatterer.ordinaryEngine.selectedPeer.Put(5, group)
676+
}
677+
for i := 0; i < 400; i++ {
678+
scatterer.ordinaryEngine.selectedPeer.Put(1, group)
679+
}
680+
// test region with peer 1 2 3
681+
for i := uint64(1); i < 20; i++ {
682+
region := tc.AddLeaderRegion(i+200, i%3+1, (i+1)%3+1, (i+2)%3+1)
683+
op := scatterer.scatterRegion(region, group)
684+
re.False(isPeerCountChanged(op))
685+
}
686+
}
687+
646688
func isPeerCountChanged(op *operator.Operator) bool {
647689
if op == nil {
648690
return false

0 commit comments

Comments
 (0)