From 302949340398b3d2b741131382466bf0daa827d3 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Tue, 19 Sep 2023 16:58:56 +0800 Subject: [PATCH] fix 7109 Signed-off-by: Cabinfever_B --- pkg/schedule/scatter/region_scatterer.go | 2 +- pkg/schedule/scatter/region_scatterer_test.go | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/schedule/scatter/region_scatterer.go b/pkg/schedule/scatter/region_scatterer.go index c47bcd27e91..68d868750e8 100644 --- a/pkg/schedule/scatter/region_scatterer.go +++ b/pkg/schedule/scatter/region_scatterer.go @@ -451,7 +451,7 @@ func (r *RegionScatterer) selectNewPeer(context engineContext, group string, pee originStorePickedCount := uint64(math.MaxUint64) for _, store := range stores { storeCount := context.selectedPeer.Get(store.GetID(), group) - if store.GetID() == peer.GetId() { + if store.GetID() == peer.GetStoreId() { originStorePickedCount = storeCount } // If storeCount is equal to the maxStoreTotalCount, we should skip this store as candidate. diff --git a/pkg/schedule/scatter/region_scatterer_test.go b/pkg/schedule/scatter/region_scatterer_test.go index 0fc7f0967d7..681b863aea6 100644 --- a/pkg/schedule/scatter/region_scatterer_test.go +++ b/pkg/schedule/scatter/region_scatterer_test.go @@ -67,6 +67,7 @@ func TestScatterRegions(t *testing.T) { scatter(re, 5, 50, true) scatter(re, 5, 500, true) scatter(re, 6, 50, true) + scatter(re, 7, 71, true) scatter(re, 5, 50, false) scatterSpecial(re, 3, 6, 50) scatterSpecial(re, 5, 5, 50) @@ -132,20 +133,36 @@ func scatter(re *require.Assertions, numStores, numRegions uint64, useRules bool } } } + maxStorePeerTotalCount := uint64(0) + minStorePeerTotalCount := uint64(math.MaxUint64) // Each store should have the same number of peers. for _, count := range countPeers { - re.LessOrEqual(float64(count), 1.1*float64(numRegions*3)/float64(numStores)) - re.GreaterOrEqual(float64(count), 0.9*float64(numRegions*3)/float64(numStores)) + if count > maxStorePeerTotalCount { + maxStorePeerTotalCount = count + } + if count < minStorePeerTotalCount { + minStorePeerTotalCount = count + } } + re.LessOrEqual(maxStorePeerTotalCount-minStorePeerTotalCount, uint64(1)) // Each store should have the same number of leaders. re.Len(countPeers, int(numStores)) re.Len(countLeader, int(numStores)) + + maxStoreLeaderTotalCount := uint64(0) + minStoreLeaderTotalCount := uint64(math.MaxUint64) for _, count := range countLeader { - re.LessOrEqual(float64(count), 1.1*float64(numRegions)/float64(numStores)) - re.GreaterOrEqual(float64(count), 0.9*float64(numRegions)/float64(numStores)) + if count > maxStoreLeaderTotalCount { + maxStoreLeaderTotalCount = count + } + if count < minStoreLeaderTotalCount { + minStoreLeaderTotalCount = count + } } + // Since the scatter leader depends on the scatter result of the peer, the maximum difference is 2. + re.LessOrEqual(maxStoreLeaderTotalCount-minStoreLeaderTotalCount, uint64(2)) re.GreaterOrEqual(noNeedMoveNum, 0) }