From cbe775b9dc7929f9510a87079ca69d593f7757c2 Mon Sep 17 00:00:00 2001 From: HunDunDM Date: Thu, 19 Aug 2021 19:57:17 +0800 Subject: [PATCH] rules: optimize the implementation of FitRegion Signed-off-by: HunDunDM --- pkg/mock/mockcluster/mockcluster.go | 5 ---- server/cluster/cluster.go | 5 ---- server/schedule/checker/priority_checker.go | 2 +- server/schedule/checker/rule_checker.go | 2 +- server/schedule/filter/filters.go | 33 +++++++++------------ server/schedule/operator/builder.go | 2 +- server/schedule/opt/healthy.go | 2 +- server/schedule/opt/opts.go | 7 ++++- 8 files changed, 24 insertions(+), 34 deletions(-) diff --git a/pkg/mock/mockcluster/mockcluster.go b/pkg/mock/mockcluster/mockcluster.go index e0c2895fd50..ce0ebca26fd 100644 --- a/pkg/mock/mockcluster/mockcluster.go +++ b/pkg/mock/mockcluster/mockcluster.go @@ -163,11 +163,6 @@ func (mc *Cluster) initRuleManager() { } } -// FitRegion fits a region to the rules it matches. -func (mc *Cluster) FitRegion(region *core.RegionInfo) *placement.RegionFit { - return mc.RuleManager.FitRegion(mc.BasicCluster, region) -} - // GetRuleManager returns the ruleManager of the cluster. func (mc *Cluster) GetRuleManager() *placement.RuleManager { return mc.RuleManager diff --git a/server/cluster/cluster.go b/server/cluster/cluster.go index a628de51fe4..163ae1a2fb3 100644 --- a/server/cluster/cluster.go +++ b/server/cluster/cluster.go @@ -1506,11 +1506,6 @@ func (c *RaftCluster) GetRuleManager() *placement.RuleManager { return c.ruleManager } -// FitRegion tries to fit the region with placement rules. -func (c *RaftCluster) FitRegion(region *core.RegionInfo) *placement.RegionFit { - return c.GetRuleManager().FitRegion(c, region) -} - // GetRegionLabeler returns the region labeler. func (c *RaftCluster) GetRegionLabeler() *labeler.RegionLabeler { c.RLock() diff --git a/server/schedule/checker/priority_checker.go b/server/schedule/checker/priority_checker.go index c5f91737eb6..5fea457c759 100644 --- a/server/schedule/checker/priority_checker.go +++ b/server/schedule/checker/priority_checker.go @@ -80,7 +80,7 @@ func (p *PriorityChecker) Check(region *core.RegionInfo) (fit *placement.RegionF // checkRegionInPlacementRule check region in placement rule mode func (p *PriorityChecker) checkRegionInPlacementRule(region *core.RegionInfo) (makeupCount int, fit *placement.RegionFit) { - fit = p.cluster.FitRegion(region) + fit = opt.FitRegion(p.cluster, region) if len(fit.RuleFits) == 0 { return } diff --git a/server/schedule/checker/rule_checker.go b/server/schedule/checker/rule_checker.go index 35044a94aa2..16d69aea555 100644 --- a/server/schedule/checker/rule_checker.go +++ b/server/schedule/checker/rule_checker.go @@ -59,7 +59,7 @@ func (c *RuleChecker) GetType() string { // Check checks if the region matches placement rules and returns Operator to // fix it. func (c *RuleChecker) Check(region *core.RegionInfo) *operator.Operator { - fit := c.cluster.FitRegion(region) + fit := opt.FitRegion(c.cluster, region) return c.CheckWithFit(region, fit) } diff --git a/server/schedule/filter/filters.go b/server/schedule/filter/filters.go index 366af51adba..c7c6833080f 100644 --- a/server/schedule/filter/filters.go +++ b/server/schedule/filter/filters.go @@ -462,14 +462,9 @@ func (f labelConstraintFilter) Target(opt *config.PersistOptions, store *core.St return placement.MatchLabelConstraints(store, f.constraints) } -// RegionFitter is the interface that can fit a region against placement rules. -type RegionFitter interface { - FitRegion(*core.RegionInfo) *placement.RegionFit -} - type ruleFitFilter struct { scope string - fitter RegionFitter + cluster opt.Cluster region *core.RegionInfo oldFit *placement.RegionFit srcStore uint64 @@ -478,12 +473,12 @@ type ruleFitFilter struct { // newRuleFitFilter creates a filter that ensures after replace a peer with new // one, the isolation level will not decrease. Its function is the same as // distinctScoreFilter but used when placement rules is enabled. -func newRuleFitFilter(scope string, fitter RegionFitter, region *core.RegionInfo, oldStoreID uint64) Filter { +func newRuleFitFilter(scope string, cluster opt.Cluster, region *core.RegionInfo, oldStoreID uint64) Filter { return &ruleFitFilter{ scope: scope, - fitter: fitter, + cluster: cluster, region: region, - oldFit: fitter.FitRegion(region), + oldFit: opt.FitRegion(cluster, region), srcStore: oldStoreID, } } @@ -496,15 +491,15 @@ func (f *ruleFitFilter) Type() string { return "rule-fit-filter" } -func (f *ruleFitFilter) Source(opt *config.PersistOptions, store *core.StoreInfo) bool { +func (f *ruleFitFilter) Source(options *config.PersistOptions, store *core.StoreInfo) bool { return true } -func (f *ruleFitFilter) Target(opt *config.PersistOptions, store *core.StoreInfo) bool { +func (f *ruleFitFilter) Target(options *config.PersistOptions, store *core.StoreInfo) bool { region := createRegionForRuleFit(f.region.GetStartKey(), f.region.GetEndKey(), f.region.GetPeers(), f.region.GetLeader(), core.WithReplacePeerStore(f.srcStore, store.GetID())) - newFit := f.fitter.FitRegion(region) + newFit := opt.FitRegion(f.cluster, region) return placement.CompareRegionFit(f.oldFit, newFit) <= 0 } @@ -515,7 +510,7 @@ func (f *ruleFitFilter) GetSourceStoreID() uint64 { type ruleLeaderFitFilter struct { scope string - fitter RegionFitter + cluster opt.Cluster region *core.RegionInfo oldFit *placement.RegionFit srcLeaderStoreID uint64 @@ -523,12 +518,12 @@ type ruleLeaderFitFilter struct { // newRuleLeaderFitFilter creates a filter that ensures after transfer leader with new store, // the isolation level will not decrease. -func newRuleLeaderFitFilter(scope string, fitter RegionFitter, region *core.RegionInfo, srcLeaderStoreID uint64) Filter { +func newRuleLeaderFitFilter(scope string, cluster opt.Cluster, region *core.RegionInfo, srcLeaderStoreID uint64) Filter { return &ruleLeaderFitFilter{ scope: scope, - fitter: fitter, + cluster: cluster, region: region, - oldFit: fitter.FitRegion(region), + oldFit: opt.FitRegion(cluster, region), srcLeaderStoreID: srcLeaderStoreID, } } @@ -541,11 +536,11 @@ func (f *ruleLeaderFitFilter) Type() string { return "rule-fit-leader-filter" } -func (f *ruleLeaderFitFilter) Source(opt *config.PersistOptions, store *core.StoreInfo) bool { +func (f *ruleLeaderFitFilter) Source(options *config.PersistOptions, store *core.StoreInfo) bool { return true } -func (f *ruleLeaderFitFilter) Target(opt *config.PersistOptions, store *core.StoreInfo) bool { +func (f *ruleLeaderFitFilter) Target(options *config.PersistOptions, store *core.StoreInfo) bool { targetPeer := f.region.GetStorePeer(store.GetID()) if targetPeer == nil { log.Warn("ruleLeaderFitFilter couldn't find peer on target Store", zap.Uint64("target-store", store.GetID())) @@ -554,7 +549,7 @@ func (f *ruleLeaderFitFilter) Target(opt *config.PersistOptions, store *core.Sto copyRegion := createRegionForRuleFit(f.region.GetStartKey(), f.region.GetEndKey(), f.region.GetPeers(), f.region.GetLeader(), core.WithLeader(targetPeer)) - newFit := f.fitter.FitRegion(copyRegion) + newFit := opt.FitRegion(f.cluster, copyRegion) return placement.CompareRegionFit(f.oldFit, newFit) <= 0 } diff --git a/server/schedule/operator/builder.go b/server/schedule/operator/builder.go index c989f27f8c4..f597d015539 100644 --- a/server/schedule/operator/builder.go +++ b/server/schedule/operator/builder.go @@ -125,7 +125,7 @@ func NewBuilder(desc string, cluster opt.Cluster, region *core.RegionInfo, opts // placement rules var rules []*placement.Rule if err == nil && cluster.GetOpts().IsPlacementRulesEnabled() { - fit := cluster.FitRegion(region) + fit := opt.FitRegion(cluster, region) for _, rf := range fit.RuleFits { rules = append(rules, rf.Rule) } diff --git a/server/schedule/opt/healthy.go b/server/schedule/opt/healthy.go index 8b44610a167..5d65ddba1c9 100644 --- a/server/schedule/opt/healthy.go +++ b/server/schedule/opt/healthy.go @@ -62,7 +62,7 @@ func AllowBalanceEmptyRegion(cluster Cluster) func(*core.RegionInfo) bool { // rules is disabled, it should have enough replicas and no any learner peer. func IsRegionReplicated(cluster Cluster, region *core.RegionInfo) bool { if cluster.GetOpts().IsPlacementRulesEnabled() { - return cluster.FitRegion(region).IsSatisfied() + return FitRegion(cluster, region).IsSatisfied() } return len(region.GetLearners()) == 0 && len(region.GetPeers()) == cluster.GetOpts().GetMaxReplicas() } diff --git a/server/schedule/opt/opts.go b/server/schedule/opt/opts.go index 18d418edaa8..ada2651e216 100644 --- a/server/schedule/opt/opts.go +++ b/server/schedule/opt/opts.go @@ -41,12 +41,17 @@ type Cluster interface { GetOpts() *config.PersistOptions AllocID() (uint64, error) - FitRegion(*core.RegionInfo) *placement.RegionFit + GetRuleManager() *placement.RuleManager RemoveScheduler(name string) error IsFeatureSupported(f versioninfo.Feature) bool AddSuspectRegions(ids ...uint64) } +// FitRegion tries to fit the region with placement rules. +func FitRegion(c Cluster, region *core.RegionInfo) *placement.RegionFit { + return c.GetRuleManager().FitRegion(c, region) +} + // cacheCluster include cache info type cacheCluster struct { Cluster