Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rules: optimize the implementation of FitRegion #4012

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion server/schedule/checker/priority_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion server/schedule/checker/rule_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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)
}

Expand Down
33 changes: 14 additions & 19 deletions server/schedule/filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,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
Expand All @@ -479,12 +474,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,
}
}
Expand All @@ -497,15 +492,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
}

Expand All @@ -516,20 +511,20 @@ func (f *ruleFitFilter) GetSourceStoreID() uint64 {

type ruleLeaderFitFilter struct {
scope string
fitter RegionFitter
cluster opt.Cluster
region *core.RegionInfo
oldFit *placement.RegionFit
srcLeaderStoreID uint64
}

// 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,
}
}
Expand All @@ -542,11 +537,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()))
Expand All @@ -555,7 +550,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
}

Expand Down
2 changes: 1 addition & 1 deletion server/schedule/operator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion server/schedule/opt/healthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
7 changes: 6 additions & 1 deletion server/schedule/opt/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down