Skip to content

Commit

Permalink
rules: optimize the implementation of FitRegion
Browse files Browse the repository at this point in the history
Signed-off-by: HunDunDM <hundundm@gmail.com>
  • Loading branch information
HunDunDM committed Aug 19, 2021
1 parent 550cf1c commit cbe775b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 34 deletions.
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 @@ -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)
}

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 @@ -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
Expand All @@ -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,
}
}
Expand All @@ -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
}

Expand All @@ -515,20 +510,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 @@ -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()))
Expand All @@ -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
}

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

0 comments on commit cbe775b

Please sign in to comment.