From 1d8f89bafa99dd18718ea3c41bccc52e3aacf714 Mon Sep 17 00:00:00 2001 From: buffer <1045931706@qq.com> Date: Mon, 4 Sep 2023 18:33:44 +0800 Subject: [PATCH] tests: make `TestBalanceRegion` more stable (#7036) close tikv/pd#7035 seperate leader and peer scatter into two test. In the `TestBalanceRegion`, there are some case can't satisfy two constraint include leader and peer such as: scatter 600 regions in 6 stores , and in the 600 selected, picked 1,3,4 as peer, but the onlt store 6 lead selected is fewest(99), so it don't select store 6 as leader. Signed-off-by: bufferflies <1045931706@qq.com> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- pkg/schedule/scatter/region_scatterer_test.go | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/schedule/scatter/region_scatterer_test.go b/pkg/schedule/scatter/region_scatterer_test.go index bc5fd9c9e29..5bdbdcbf159 100644 --- a/pkg/schedule/scatter/region_scatterer_test.go +++ b/pkg/schedule/scatter/region_scatterer_test.go @@ -696,7 +696,35 @@ func TestSelectedStoresTooManyPeers(t *testing.T) { } } -// TestBalanceRegion tests whether region peers and leaders are balanced after scatter. +// TestBalanceLeader only tests whether region leaders are balanced after scatter. +func TestBalanceLeader(t *testing.T) { + re := require.New(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + opt := mockconfig.NewTestOptions() + tc := mockcluster.NewCluster(ctx, opt) + stream := hbstream.NewTestHeartbeatStreams(ctx, tc.ID, tc, false) + oc := operator.NewController(ctx, tc.GetBasicCluster(), tc.GetSharedConfig(), stream) + // Add 3 stores + for i := uint64(2); i <= 4; i++ { + tc.AddLabelsStore(i, 0, nil) + // prevent store from being disconnected + tc.SetStoreLastHeartbeatInterval(i, -10*time.Minute) + } + group := "group" + scatterer := NewRegionScatterer(ctx, tc, oc, tc.AddSuspectRegions) + for i := uint64(1001); i <= 1300; i++ { + region := tc.AddLeaderRegion(i, 2, 3, 4) + op := scatterer.scatterRegion(region, group, false) + re.False(isPeerCountChanged(op)) + } + // all leader will be balanced in three stores. + for i := uint64(2); i <= 4; i++ { + re.Equal(uint64(100), scatterer.ordinaryEngine.selectedLeader.Get(i, group)) + } +} + +// TestBalanceRegion tests whether region peers are balanced after scatter. // ref https://github.com/tikv/pd/issues/6017 func TestBalanceRegion(t *testing.T) { re := require.New(t) @@ -722,7 +750,6 @@ func TestBalanceRegion(t *testing.T) { } for i := uint64(2); i <= 7; i++ { 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