forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
region_scatterer_test.go
658 lines (612 loc) · 18.9 KB
/
region_scatterer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package schedule
import (
"context"
"fmt"
"math"
"math/rand"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/schedule/hbstream"
"github.com/tikv/pd/server/schedule/operator"
"github.com/tikv/pd/server/schedule/placement"
"github.com/tikv/pd/server/versioninfo"
)
type sequencer struct {
minID uint64
maxID uint64
curID uint64
}
func newSequencer(maxID uint64) *sequencer {
return newSequencerWithMinID(1, maxID)
}
func newSequencerWithMinID(minID, maxID uint64) *sequencer {
return &sequencer{
minID: minID,
maxID: maxID,
curID: maxID,
}
}
func (s *sequencer) next() uint64 {
s.curID++
if s.curID > s.maxID {
s.curID = s.minID
}
return s.curID
}
var _ = Suite(&testScatterRegionSuite{})
type testScatterRegionSuite struct{}
func (s *testScatterRegionSuite) TestSixStores(c *C) {
s.scatter(c, 6, 100, false)
s.scatter(c, 6, 100, true)
s.scatter(c, 6, 1000, false)
s.scatter(c, 6, 1000, true)
}
func (s *testScatterRegionSuite) TestFiveStores(c *C) {
s.scatter(c, 5, 100, false)
s.scatter(c, 5, 100, true)
s.scatter(c, 5, 1000, false)
s.scatter(c, 5, 1000, true)
}
func (s *testScatterRegionSuite) TestSixSpecialStores(c *C) {
s.scatterSpecial(c, 3, 6, 100)
s.scatterSpecial(c, 3, 6, 1000)
}
func (s *testScatterRegionSuite) TestFiveSpecialStores(c *C) {
s.scatterSpecial(c, 5, 5, 100)
s.scatterSpecial(c, 5, 5, 1000)
}
func (s *testScatterRegionSuite) checkOperator(op *operator.Operator, c *C) {
for i := 0; i < op.Len(); i++ {
if rp, ok := op.Step(i).(operator.RemovePeer); ok {
for j := i + 1; j < op.Len(); j++ {
if tr, ok := op.Step(j).(operator.TransferLeader); ok {
c.Assert(rp.FromStore, Not(Equals), tr.FromStore)
c.Assert(rp.FromStore, Not(Equals), tr.ToStore)
}
}
}
}
}
func (s *testScatterRegionSuite) scatter(c *C, numStores, numRegions uint64, useRules bool) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
tc.DisableFeature(versioninfo.JointConsensus)
// Add ordinary stores.
for i := uint64(1); i <= numStores; i++ {
tc.AddRegionStore(i, 0)
}
tc.SetEnablePlacementRules(useRules)
for i := uint64(1); i <= numRegions; i++ {
// region distributed in same stores.
tc.AddLeaderRegion(i, 1, 2, 3)
}
scatterer := NewRegionScatterer(ctx, tc)
for i := uint64(1); i <= numRegions; i++ {
region := tc.GetRegion(i)
if op, _ := scatterer.Scatter(region, ""); op != nil {
s.checkOperator(op, c)
ApplyOperator(tc, op)
}
}
countPeers := make(map[uint64]uint64)
countLeader := make(map[uint64]uint64)
for i := uint64(1); i <= numRegions; i++ {
region := tc.GetRegion(i)
leaderStoreID := region.GetLeader().GetStoreId()
for _, peer := range region.GetPeers() {
countPeers[peer.GetStoreId()]++
if peer.GetStoreId() == leaderStoreID {
countLeader[peer.GetStoreId()]++
}
}
}
//Each store should have the same number of peers.
for _, count := range countPeers {
c.Assert(float64(count), LessEqual, 1.1*float64(numRegions*3)/float64(numStores))
c.Assert(float64(count), GreaterEqual, 0.9*float64(numRegions*3)/float64(numStores))
}
// Each store should have the same number of leaders.
c.Assert(len(countPeers), Equals, int(numStores))
c.Assert(len(countLeader), Equals, int(numStores))
for _, count := range countLeader {
c.Assert(float64(count), LessEqual, 1.1*float64(numRegions)/float64(numStores))
c.Assert(float64(count), GreaterEqual, 0.9*float64(numRegions)/float64(numStores))
}
}
func (s *testScatterRegionSuite) scatterSpecial(c *C, numOrdinaryStores, numSpecialStores, numRegions uint64) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
tc.DisableFeature(versioninfo.JointConsensus)
// Add ordinary stores.
for i := uint64(1); i <= numOrdinaryStores; i++ {
tc.AddRegionStore(i, 0)
}
// Add special stores.
for i := uint64(1); i <= numSpecialStores; i++ {
tc.AddLabelsStore(numOrdinaryStores+i, 0, map[string]string{"engine": "tiflash"})
}
tc.SetEnablePlacementRules(true)
c.Assert(tc.RuleManager.SetRule(&placement.Rule{
GroupID: "pd", ID: "learner", Role: placement.Learner, Count: 3,
LabelConstraints: []placement.LabelConstraint{{Key: "engine", Op: placement.In, Values: []string{"tiflash"}}}}), IsNil)
// Region 1 has the same distribution with the Region 2, which is used to test selectPeerToReplace.
tc.AddRegionWithLearner(1, 1, []uint64{2, 3}, []uint64{numOrdinaryStores + 1, numOrdinaryStores + 2, numOrdinaryStores + 3})
for i := uint64(2); i <= numRegions; i++ {
tc.AddRegionWithLearner(
i,
1,
[]uint64{2, 3},
[]uint64{numOrdinaryStores + 1, numOrdinaryStores + 2, numOrdinaryStores + 3},
)
}
scatterer := NewRegionScatterer(ctx, tc)
for i := uint64(1); i <= numRegions; i++ {
region := tc.GetRegion(i)
if op, _ := scatterer.Scatter(region, ""); op != nil {
s.checkOperator(op, c)
ApplyOperator(tc, op)
}
}
countOrdinaryPeers := make(map[uint64]uint64)
countSpecialPeers := make(map[uint64]uint64)
countOrdinaryLeaders := make(map[uint64]uint64)
for i := uint64(1); i <= numRegions; i++ {
region := tc.GetRegion(i)
leaderStoreID := region.GetLeader().GetStoreId()
for _, peer := range region.GetPeers() {
storeID := peer.GetStoreId()
store := tc.Stores.GetStore(storeID)
if store.GetLabelValue("engine") == "tiflash" {
countSpecialPeers[storeID]++
} else {
countOrdinaryPeers[storeID]++
}
if peer.GetStoreId() == leaderStoreID {
countOrdinaryLeaders[storeID]++
}
}
}
// Each store should have the same number of peers.
for _, count := range countOrdinaryPeers {
c.Assert(float64(count), LessEqual, 1.1*float64(numRegions*3)/float64(numOrdinaryStores))
c.Assert(float64(count), GreaterEqual, 0.9*float64(numRegions*3)/float64(numOrdinaryStores))
}
for _, count := range countSpecialPeers {
c.Assert(float64(count), LessEqual, 1.1*float64(numRegions*3)/float64(numSpecialStores))
c.Assert(float64(count), GreaterEqual, 0.9*float64(numRegions*3)/float64(numSpecialStores))
}
for _, count := range countOrdinaryLeaders {
c.Assert(float64(count), LessEqual, 1.1*float64(numRegions)/float64(numOrdinaryStores))
c.Assert(float64(count), GreaterEqual, 0.9*float64(numRegions)/float64(numOrdinaryStores))
}
}
func (s *testScatterRegionSuite) TestStoreLimit(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
stream := hbstream.NewTestHeartbeatStreams(ctx, tc.ID, tc, false)
oc := NewOperatorController(ctx, tc, stream)
// Add stores 1~6.
for i := uint64(1); i <= 5; i++ {
tc.AddRegionStore(i, 0)
}
// Add regions 1~4.
seq := newSequencer(3)
// Region 1 has the same distribution with the Region 2, which is used to test selectPeerToReplace.
tc.AddLeaderRegion(1, 1, 2, 3)
for i := uint64(2); i <= 5; i++ {
tc.AddLeaderRegion(i, seq.next(), seq.next(), seq.next())
}
scatterer := NewRegionScatterer(ctx, tc)
for i := uint64(1); i <= 5; i++ {
region := tc.GetRegion(i)
if op, _ := scatterer.Scatter(region, ""); op != nil {
c.Assert(oc.AddWaitingOperator(op), Equals, 1)
}
}
}
func (s *testScatterRegionSuite) TestScatterCheck(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
// Add 5 stores.
for i := uint64(1); i <= 5; i++ {
tc.AddRegionStore(i, 0)
}
testcases := []struct {
name string
checkRegion *core.RegionInfo
needFix bool
}{
{
name: "region with 4 replicas",
checkRegion: tc.AddLeaderRegion(1, 1, 2, 3, 4),
needFix: true,
},
{
name: "region with 3 replicas",
checkRegion: tc.AddLeaderRegion(1, 1, 2, 3),
needFix: false,
},
{
name: "region with 2 replicas",
checkRegion: tc.AddLeaderRegion(1, 1, 2),
needFix: true,
},
}
for _, testcase := range testcases {
c.Logf(testcase.name)
scatterer := NewRegionScatterer(ctx, tc)
_, err := scatterer.Scatter(testcase.checkRegion, "")
if testcase.needFix {
c.Assert(err, NotNil)
c.Assert(tc.CheckRegionUnderSuspect(1), Equals, true)
} else {
c.Assert(err, IsNil)
c.Assert(tc.CheckRegionUnderSuspect(1), Equals, false)
}
tc.ResetSuspectRegions()
}
}
func (s *testScatterRegionSuite) TestScatterGroupInConcurrency(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
// Add 5 stores.
for i := uint64(1); i <= 5; i++ {
tc.AddRegionStore(i, 0)
}
testcases := []struct {
name string
groupCount int
}{
{
name: "1 group",
groupCount: 1,
},
{
name: "2 group",
groupCount: 2,
},
{
name: "3 group",
groupCount: 3,
},
}
// We send scatter interweave request for each group to simulate scattering multiple region groups in concurrency.
for _, testcase := range testcases {
c.Logf(testcase.name)
scatterer := NewRegionScatterer(ctx, tc)
regionID := 1
for i := 0; i < 100; i++ {
for j := 0; j < testcase.groupCount; j++ {
scatterer.scatterRegion(tc.AddLeaderRegion(uint64(regionID), 1, 2, 3),
fmt.Sprintf("group-%v", j))
regionID++
}
}
checker := func(ss *selectedStores, expected uint64, delta float64) {
for i := 0; i < testcase.groupCount; i++ {
// comparing the leader distribution
group := fmt.Sprintf("group-%v", i)
max := uint64(0)
min := uint64(math.MaxUint64)
groupDistribution, _ := ss.groupDistribution.Get(group)
for _, count := range groupDistribution.(map[uint64]uint64) {
if count > max {
max = count
}
if count < min {
min = count
}
}
c.Assert(math.Abs(float64(max)-float64(expected)), LessEqual, delta)
c.Assert(math.Abs(float64(min)-float64(expected)), LessEqual, delta)
}
}
// For leader, we expect each store have about 20 leader for each group
checker(scatterer.ordinaryEngine.selectedLeader, 20, 5)
// For peer, we expect each store have about 60 peers for each group
checker(scatterer.ordinaryEngine.selectedPeer, 60, 15)
}
}
func (s *testScatterRegionSuite) TestScattersGroup(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
// Add 5 stores.
for i := uint64(1); i <= 5; i++ {
tc.AddRegionStore(i, 0)
}
testcases := []struct {
name string
failure bool
}{
{
name: "have failure",
failure: true,
},
{
name: "no failure",
failure: false,
},
}
group := "group"
for _, testcase := range testcases {
scatterer := NewRegionScatterer(ctx, tc)
regions := map[uint64]*core.RegionInfo{}
for i := 1; i <= 100; i++ {
regions[uint64(i)] = tc.AddLeaderRegion(uint64(i), 1, 2, 3)
}
c.Log(testcase.name)
failures := map[uint64]error{}
if testcase.failure {
c.Assert(failpoint.Enable("github.com/tikv/pd/server/schedule/scatterFail", `return(true)`), IsNil)
}
scatterer.ScatterRegions(regions, failures, group, 3)
max := uint64(0)
min := uint64(math.MaxUint64)
groupDistribution, exist := scatterer.ordinaryEngine.selectedLeader.GetGroupDistribution(group)
c.Assert(exist, Equals, true)
for _, count := range groupDistribution {
if count > max {
max = count
}
if count < min {
min = count
}
}
// 100 regions divided 5 stores, each store expected to have about 20 regions.
c.Assert(min, LessEqual, uint64(20))
c.Assert(max, GreaterEqual, uint64(20))
c.Assert(max-min, LessEqual, uint64(3))
if testcase.failure {
c.Assert(len(failures), Equals, 1)
_, ok := failures[1]
c.Assert(ok, Equals, true)
c.Assert(failpoint.Disable("github.com/tikv/pd/server/schedule/scatterFail"), IsNil)
} else {
c.Assert(len(failures), Equals, 0)
}
}
}
func (s *testScatterRegionSuite) TestSelectedStoreGC(c *C) {
// use a shorter gcTTL and gcInterval during the test
gcInterval = time.Second
gcTTL = time.Second * 3
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stores := newSelectedStores(ctx)
stores.Put(1, "testgroup")
_, ok := stores.GetGroupDistribution("testgroup")
c.Assert(ok, Equals, true)
_, ok = stores.GetGroupDistribution("testgroup")
c.Assert(ok, Equals, true)
time.Sleep(gcTTL)
_, ok = stores.GetGroupDistribution("testgroup")
c.Assert(ok, Equals, false)
_, ok = stores.GetGroupDistribution("testgroup")
c.Assert(ok, Equals, false)
}
// TestRegionFromDifferentGroups test the multi regions. each region have its own group.
// After scatter, the distribution for the whole cluster should be well.
func (s *testScatterRegionSuite) TestRegionFromDifferentGroups(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
// Add 6 stores.
storeCount := 6
for i := uint64(1); i <= uint64(storeCount); i++ {
tc.AddRegionStore(i, 0)
}
scatterer := NewRegionScatterer(ctx, tc)
regionCount := 50
for i := 1; i <= regionCount; i++ {
p := rand.Perm(storeCount)
scatterer.scatterRegion(tc.AddLeaderRegion(uint64(i), uint64(p[0])+1, uint64(p[1])+1, uint64(p[2])+1), fmt.Sprintf("t%d", i))
}
check := func(ss *selectedStores) {
max := uint64(0)
min := uint64(math.MaxUint64)
for i := uint64(1); i <= uint64(storeCount); i++ {
count := ss.TotalCountByStore(i)
if count > max {
max = count
}
if count < min {
min = count
}
}
c.Assert(max-min, LessEqual, uint64(2))
}
check(scatterer.ordinaryEngine.selectedPeer)
}
<<<<<<< HEAD
// TestSelectedStores tests if the peer count has changed due to the picking strategy.
// Ref https://github.com/tikv/pd/issues/4565
func (s *testScatterRegionSuite) TestSelectedStores(c *C) {
=======
func TestRegionHasLearner(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
stream := hbstream.NewTestHeartbeatStreams(ctx, tc.ID, tc, false)
oc := NewOperatorController(ctx, tc, stream)
// Add 8 stores.
voterCount := uint64(6)
storeCount := uint64(8)
for i := uint64(1); i <= voterCount; i++ {
tc.AddLabelsStore(i, 0, map[string]string{"zone": "z1"})
}
for i := voterCount + 1; i <= 8; i++ {
tc.AddLabelsStore(i, 0, map[string]string{"zone": "z2"})
}
tc.RuleManager.SetRule(&placement.Rule{
GroupID: "pd",
ID: "default",
Role: placement.Voter,
Count: 3,
LabelConstraints: []placement.LabelConstraint{
{
Key: "zone",
Op: placement.In,
Values: []string{"z1"},
},
},
})
tc.RuleManager.SetRule(&placement.Rule{
GroupID: "pd",
ID: "learner",
Role: placement.Learner,
Count: 1,
LabelConstraints: []placement.LabelConstraint{
{
Key: "zone",
Op: placement.In,
Values: []string{"z2"},
},
},
})
scatterer := NewRegionScatterer(ctx, tc, oc)
regionCount := 50
for i := 1; i <= regionCount; i++ {
_, err := scatterer.Scatter(tc.AddRegionWithLearner(uint64(i), uint64(1), []uint64{uint64(2), uint64(3)}, []uint64{7}), "group")
re.NoError(err)
}
check := func(ss *selectedStores) {
max := uint64(0)
min := uint64(math.MaxUint64)
for i := uint64(1); i <= max; i++ {
count := ss.TotalCountByStore(i)
if count > max {
max = count
}
if count < min {
min = count
}
}
re.LessOrEqual(max-min, uint64(2))
}
check(scatterer.ordinaryEngine.selectedPeer)
checkLeader := func(ss *selectedStores) {
max := uint64(0)
min := uint64(math.MaxUint64)
for i := uint64(1); i <= voterCount; i++ {
count := ss.TotalCountByStore(i)
if count > max {
max = count
}
if count < min {
min = count
}
}
re.LessOrEqual(max-2, uint64(regionCount)/voterCount)
re.LessOrEqual(min-1, uint64(regionCount)/voterCount)
for i := voterCount + 1; i <= storeCount; i++ {
count := ss.TotalCountByStore(i)
re.LessOrEqual(count, uint64(0))
}
}
checkLeader(scatterer.ordinaryEngine.selectedLeader)
}
// TestSelectedStoresTooFewPeers tests if the peer count has changed due to the picking strategy.
// Ref https://github.com/tikv/pd/issues/4565
func TestSelectedStoresTooFewPeers(t *testing.T) {
re := require.New(t)
>>>>>>> f5b5391c0 (region_scatterer: fix the bug that could generate schedule with too many peers (#5920))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
// Add 4 stores.
for i := uint64(1); i <= 4; i++ {
tc.AddRegionStore(i, 0)
// prevent store from being disconnected
tc.SetStoreLastHeartbeatInterval(i, -10*time.Minute)
}
group := "group"
scatterer := NewRegionScatterer(ctx, tc)
// Put a lot of regions in Store 1/2/3.
for i := uint64(1); i < 100; i++ {
region := tc.AddLeaderRegion(i+10, i%3+1, (i+1)%3+1, (i+2)%3+1)
peers := make(map[uint64]*metapb.Peer, 3)
for _, peer := range region.GetPeers() {
peers[peer.GetStoreId()] = peer
}
scatterer.Put(peers, i%3+1, group)
}
// Try to scatter a region with peer store id 2/3/4
for i := uint64(1); i < 20; i++ {
region := tc.AddLeaderRegion(i+200, i%3+2, (i+1)%3+2, (i+2)%3+2)
op := scatterer.scatterRegion(region, group)
c.Assert(isPeerCountChanged(op), IsFalse)
}
}
// TestSelectedStoresTooManyPeers tests if the peer count has changed due to the picking strategy.
// Ref https://github.com/tikv/pd/issues/5909
func TestSelectedStoresTooManyPeers(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opt := config.NewTestOptions()
tc := mockcluster.NewCluster(ctx, opt)
stream := hbstream.NewTestHeartbeatStreams(ctx, tc.ID, tc, false)
oc := NewOperatorController(ctx, tc, stream)
// Add 4 stores.
for i := uint64(1); i <= 5; i++ {
tc.AddRegionStore(i, 0)
// prevent store from being disconnected
tc.SetStoreLastHeartbeatInterval(i, -10*time.Minute)
}
group := "group"
scatterer := NewRegionScatterer(ctx, tc, oc)
// priority 4 > 1 > 5 > 2 == 3
for i := 0; i < 1200; i++ {
scatterer.ordinaryEngine.selectedPeer.Put(2, group)
scatterer.ordinaryEngine.selectedPeer.Put(3, group)
}
for i := 0; i < 800; i++ {
scatterer.ordinaryEngine.selectedPeer.Put(5, group)
}
for i := 0; i < 400; i++ {
scatterer.ordinaryEngine.selectedPeer.Put(1, group)
}
// test region with peer 1 2 3
for i := uint64(1); i < 20; i++ {
region := tc.AddLeaderRegion(i+200, i%3+1, (i+1)%3+1, (i+2)%3+1)
op := scatterer.scatterRegion(region, group)
re.False(isPeerCountChanged(op))
}
}
func isPeerCountChanged(op *operator.Operator) bool {
if op == nil {
return false
}
add, remove := 0, 0
for i := 0; i < op.Len(); i++ {
step := op.Step(i)
switch step.(type) {
case operator.AddPeer, operator.AddLearner, operator.AddLightPeer, operator.AddLightLearner:
add++
case operator.RemovePeer:
remove++
}
}
return add != remove
}