From 51dfe6fdc1ea005bb30344cdd787e8241d3f0d01 Mon Sep 17 00:00:00 2001 From: disksing Date: Mon, 7 Jun 2021 15:42:28 +0800 Subject: [PATCH] schedulers: avoid recalculate tolerantRatio (#3745) * schedulers: avoid recalculate tolerantRatio Signed-off-by: disksing * fix test Signed-off-by: disksing Co-authored-by: Ti Chi Robot --- server/schedulers/balance_test.go | 36 ++++++++++++++++++++++--------- server/schedulers/utils.go | 34 +++++++++++++++-------------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/server/schedulers/balance_test.go b/server/schedulers/balance_test.go index 5a72fd89ec4..b5b14c9de4e 100644 --- a/server/schedulers/balance_test.go +++ b/server/schedulers/balance_test.go @@ -171,22 +171,38 @@ func (s *testBalanceSuite) TestTolerantRatio(c *C) { tbl := []struct { ratio float64 kind core.ScheduleKind - expectTolerantResource func() int64 + expectTolerantResource func(core.ScheduleKind) int64 }{ - {0, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.ByCount}, func() int64 { return int64(leaderTolerantSizeRatio) }}, - {0, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.BySize}, func() int64 { return int64(adjustTolerantRatio(tc) * float64(regionSize)) }}, - {0, core.ScheduleKind{Resource: core.RegionKind, Policy: core.ByCount}, func() int64 { return int64(adjustTolerantRatio(tc) * float64(regionSize)) }}, - {0, core.ScheduleKind{Resource: core.RegionKind, Policy: core.BySize}, func() int64 { return int64(adjustTolerantRatio(tc) * float64(regionSize)) }}, - {10, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.ByCount}, func() int64 { return int64(tc.GetScheduleConfig().TolerantSizeRatio) }}, - {10, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.BySize}, func() int64 { return int64(adjustTolerantRatio(tc) * float64(regionSize)) }}, - {10, core.ScheduleKind{Resource: core.RegionKind, Policy: core.ByCount}, func() int64 { return int64(adjustTolerantRatio(tc) * float64(regionSize)) }}, - {10, core.ScheduleKind{Resource: core.RegionKind, Policy: core.BySize}, func() int64 { return int64(adjustTolerantRatio(tc) * float64(regionSize)) }}, + {0, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.ByCount}, func(k core.ScheduleKind) int64 { + return int64(leaderTolerantSizeRatio) + }}, + {0, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.BySize}, func(k core.ScheduleKind) int64 { + return int64(adjustTolerantRatio(tc, k) * float64(regionSize)) + }}, + {0, core.ScheduleKind{Resource: core.RegionKind, Policy: core.ByCount}, func(k core.ScheduleKind) int64 { + return int64(adjustTolerantRatio(tc, k) * float64(regionSize)) + }}, + {0, core.ScheduleKind{Resource: core.RegionKind, Policy: core.BySize}, func(k core.ScheduleKind) int64 { + return int64(adjustTolerantRatio(tc, k) * float64(regionSize)) + }}, + {10, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.ByCount}, func(k core.ScheduleKind) int64 { + return int64(tc.GetScheduleConfig().TolerantSizeRatio) + }}, + {10, core.ScheduleKind{Resource: core.LeaderKind, Policy: core.BySize}, func(k core.ScheduleKind) int64 { + return int64(adjustTolerantRatio(tc, k) * float64(regionSize)) + }}, + {10, core.ScheduleKind{Resource: core.RegionKind, Policy: core.ByCount}, func(k core.ScheduleKind) int64 { + return int64(adjustTolerantRatio(tc, k) * float64(regionSize)) + }}, + {10, core.ScheduleKind{Resource: core.RegionKind, Policy: core.BySize}, func(k core.ScheduleKind) int64 { + return int64(adjustTolerantRatio(tc, k) * float64(regionSize)) + }}, } for i, t := range tbl { tc.SetTolerantSizeRatio(t.ratio) plan := newBalancePlan(t.kind, tc, operator.OpInfluence{}) plan.region = region - c.Assert(plan.getTolerantResource(), Equals, t.expectTolerantResource(), Commentf("case #%d", i+1)) + c.Assert(plan.getTolerantResource(), Equals, t.expectTolerantResource(t.kind), Commentf("case #%d", i+1)) } } diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index 4ea4bf036b5..64fc78c20d2 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -38,9 +38,10 @@ const ( ) type balancePlan struct { - kind core.ScheduleKind - cluster opt.Cluster - opInfluence operator.OpInfluence + kind core.ScheduleKind + cluster opt.Cluster + opInfluence operator.OpInfluence + tolerantSizeRatio float64 source *core.StoreInfo target *core.StoreInfo @@ -52,9 +53,10 @@ type balancePlan struct { func newBalancePlan(kind core.ScheduleKind, cluster opt.Cluster, opInfluence operator.OpInfluence) *balancePlan { return &balancePlan{ - kind: kind, - cluster: cluster, - opInfluence: opInfluence, + kind: kind, + cluster: cluster, + opInfluence: opInfluence, + tolerantSizeRatio: adjustTolerantRatio(cluster, kind), } } @@ -134,24 +136,24 @@ func (p *balancePlan) shouldBalance(scheduleName string) bool { func (p *balancePlan) getTolerantResource() int64 { if p.kind.Resource == core.LeaderKind && p.kind.Policy == core.ByCount { - tolerantSizeRatio := p.cluster.GetOpts().GetTolerantSizeRatio() - if tolerantSizeRatio == 0 { - tolerantSizeRatio = leaderTolerantSizeRatio - } - leaderCount := int64(1.0 * tolerantSizeRatio) - return leaderCount + return int64(p.tolerantSizeRatio) } - regionSize := p.region.GetApproximateSize() if regionSize < p.cluster.GetAverageRegionSize() { regionSize = p.cluster.GetAverageRegionSize() } - regionSize = int64(float64(regionSize) * adjustTolerantRatio(p.cluster)) - return regionSize + return int64(float64(regionSize) * p.tolerantSizeRatio) } -func adjustTolerantRatio(cluster opt.Cluster) float64 { +func adjustTolerantRatio(cluster opt.Cluster, kind core.ScheduleKind) float64 { tolerantSizeRatio := cluster.GetOpts().GetTolerantSizeRatio() + if kind.Resource == core.LeaderKind && kind.Policy == core.ByCount { + if tolerantSizeRatio == 0 { + return leaderTolerantSizeRatio + } + return tolerantSizeRatio + } + if tolerantSizeRatio == 0 { var maxRegionCount float64 stores := cluster.GetStores()