|
9 | 9 |
|
10 | 10 | . "github.com/pingcap/check" |
11 | 11 | "github.com/pingcap/failpoint" |
| 12 | + "github.com/pingcap/kvproto/pkg/metapb" |
12 | 13 | "github.com/tikv/pd/pkg/mock/mockcluster" |
13 | 14 | "github.com/tikv/pd/server/config" |
14 | 15 | "github.com/tikv/pd/server/core" |
@@ -355,8 +356,8 @@ func (s *testScatterRegionSuite) TestScatterGroupInConcurrency(c *C) { |
355 | 356 | } |
356 | 357 | // For leader, we expect each store have about 20 leader for each group |
357 | 358 | checker(scatterer.ordinaryEngine.selectedLeader, 20, 5) |
358 | | - // For peer, we expect each store have about 50 peers for each group |
359 | | - checker(scatterer.ordinaryEngine.selectedPeer, 50, 15) |
| 359 | + // For peer, we expect each store have about 60 peers for each group |
| 360 | + checker(scatterer.ordinaryEngine.selectedPeer, 60, 15) |
360 | 361 | } |
361 | 362 | } |
362 | 363 |
|
@@ -476,3 +477,54 @@ func (s *testScatterRegionSuite) TestRegionFromDifferentGroups(c *C) { |
476 | 477 | } |
477 | 478 | check(scatterer.ordinaryEngine.selectedPeer) |
478 | 479 | } |
| 480 | + |
| 481 | +// TestSelectedStores tests if the peer count has changed due to the picking strategy. |
| 482 | +// Ref https://github.com/tikv/pd/issues/4565 |
| 483 | +func (s *testScatterRegionSuite) TestSelectedStores(c *C) { |
| 484 | + ctx, cancel := context.WithCancel(context.Background()) |
| 485 | + defer cancel() |
| 486 | + opt := config.NewTestOptions() |
| 487 | + tc := mockcluster.NewCluster(ctx, opt) |
| 488 | + // Add 4 stores. |
| 489 | + for i := uint64(1); i <= 4; i++ { |
| 490 | + tc.AddRegionStore(i, 0) |
| 491 | + // prevent store from being disconnected |
| 492 | + tc.SetStoreLastHeartbeatInterval(i, -10*time.Minute) |
| 493 | + } |
| 494 | + group := "group" |
| 495 | + scatterer := NewRegionScatterer(ctx, tc) |
| 496 | + |
| 497 | + // Put a lot of regions in Store 1/2/3. |
| 498 | + for i := uint64(1); i < 100; i++ { |
| 499 | + region := tc.AddLeaderRegion(i+10, i%3+1, (i+1)%3+1, (i+2)%3+1) |
| 500 | + peers := make(map[uint64]*metapb.Peer, 3) |
| 501 | + for _, peer := range region.GetPeers() { |
| 502 | + peers[peer.GetStoreId()] = peer |
| 503 | + } |
| 504 | + scatterer.Put(peers, i%3+1, group) |
| 505 | + } |
| 506 | + |
| 507 | + // Try to scatter a region with peer store id 2/3/4 |
| 508 | + for i := uint64(1); i < 20; i++ { |
| 509 | + region := tc.AddLeaderRegion(i+200, i%3+2, (i+1)%3+2, (i+2)%3+2) |
| 510 | + op := scatterer.scatterRegion(region, group) |
| 511 | + c.Assert(isPeerCountChanged(op), IsFalse) |
| 512 | + } |
| 513 | +} |
| 514 | + |
| 515 | +func isPeerCountChanged(op *operator.Operator) bool { |
| 516 | + if op == nil { |
| 517 | + return false |
| 518 | + } |
| 519 | + add, remove := 0, 0 |
| 520 | + for i := 0; i < op.Len(); i++ { |
| 521 | + step := op.Step(i) |
| 522 | + switch step.(type) { |
| 523 | + case operator.AddPeer, operator.AddLearner, operator.AddLightPeer, operator.AddLightLearner: |
| 524 | + add++ |
| 525 | + case operator.RemovePeer: |
| 526 | + remove++ |
| 527 | + } |
| 528 | + } |
| 529 | + return add != remove |
| 530 | +} |
0 commit comments