From f6fc8fb17af8c6495a282fa20d10e45422b9e02b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B7=B7=E6=B2=8CDM?= Date: Mon, 5 Jul 2021 21:21:28 +0800 Subject: [PATCH] scheduler: remove the useless code in hot-region-scheduler (#3833) * scheduler: move part of the code of the hot-region-scheduler to the test package Signed-off-by: HunDunDM * scheduler: remove the useless code in hot-region-scheduler Signed-off-by: HunDunDM * scheduler: fix nil pointer warning Signed-off-by: HunDunDM Co-authored-by: Ti Chi Robot --- server/schedulers/hot_region.go | 64 +++++-------------- .../{hot_test.go => hot_region_test.go} | 20 ++++++ 2 files changed, 36 insertions(+), 48 deletions(-) rename server/schedulers/{hot_test.go => hot_region_test.go} (98%) diff --git a/server/schedulers/hot_region.go b/server/schedulers/hot_region.go index 3178e8fa8e8..e46f3018c72 100644 --- a/server/schedulers/hot_region.go +++ b/server/schedulers/hot_region.go @@ -51,17 +51,6 @@ func init() { conf.storage = storage return newHotScheduler(opController, conf), nil }) - - // FIXME: remove this two schedule after the balance test move in schedulers package - { - schedule.RegisterScheduler(HotWriteRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { - return newHotWriteScheduler(opController, initHotRegionScheduleConfig()), nil - }) - schedule.RegisterScheduler(HotReadRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { - return newHotReadScheduler(opController, initHotRegionScheduleConfig()), nil - }) - - } } const ( @@ -85,10 +74,8 @@ type hotScheduler struct { name string *BaseScheduler sync.RWMutex - leaderLimit uint64 - peerLimit uint64 - types []rwType - r *rand.Rand + types []rwType + r *rand.Rand // states across multiple `Schedule` calls pendings map[*pendingInfluence]struct{} @@ -110,34 +97,18 @@ func newHotScheduler(opController *schedule.OperatorController, conf *hotRegionS ret := &hotScheduler{ name: HotRegionName, BaseScheduler: base, - leaderLimit: 1, - peerLimit: 1, types: []rwType{write, read}, r: rand.New(rand.NewSource(time.Now().UnixNano())), + pendings: map[*pendingInfluence]struct{}{}, regionPendings: make(map[uint64]*operator.Operator), conf: conf, } - ret.pendings = map[*pendingInfluence]struct{}{} for ty := resourceType(0); ty < resourceTypeLen; ty++ { ret.stLoadInfos[ty] = map[uint64]*storeLoadDetail{} } return ret } -func newHotReadScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { - ret := newHotScheduler(opController, conf) - ret.name = "" - ret.types = []rwType{read} - return ret -} - -func newHotWriteScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { - ret := newHotScheduler(opController, conf) - ret.name = "" - ret.types = []rwType{write} - return ret -} - func (h *hotScheduler) GetName() string { return h.name } @@ -541,9 +512,9 @@ func (bs *balanceSolver) solve() []*operator.Operator { } bs.cur = &solution{} var ( - best *solution - ops []*operator.Operator - infls []Influence + best *solution + op *operator.Operator + infl Influence ) for srcStoreID := range bs.filterSrcStores() { @@ -559,9 +530,9 @@ func (bs *balanceSolver) solve() []*operator.Operator { bs.cur.dstStoreID = dstStoreID bs.calcProgressiveRank() if bs.cur.progressiveRank < 0 && bs.betterThan(best) { - if newOps, newInfls := bs.buildOperators(); len(newOps) > 0 { - ops = newOps - infls = newInfls + if newOp, newInfl := bs.buildOperator(); newOp != nil { + op = newOp + infl = *newInfl clone := *bs.cur best = &clone } @@ -570,13 +541,11 @@ func (bs *balanceSolver) solve() []*operator.Operator { } } - for i := 0; i < len(ops); i++ { - // TODO: multiple operators need to be atomic. - if !bs.sche.addPendingInfluence(ops[i], best.srcStoreID, best.dstStoreID, infls[i]) { - return nil - } + if best == nil || !bs.sche.addPendingInfluence(op, best.srcStoreID, best.dstStoreID, infl) { + return nil } - return ops + + return []*operator.Operator{op} } // allowBalance check whether the operator count have exceed the hot region limit by type @@ -1028,12 +997,11 @@ func (bs *balanceSolver) isReadyToBuild() bool { return true } -func (bs *balanceSolver) buildOperators() ([]*operator.Operator, []Influence) { +func (bs *balanceSolver) buildOperator() (op *operator.Operator, infl *Influence) { if !bs.isReadyToBuild() { return nil, nil } var ( - op *operator.Operator counters []prometheus.Counter err error ) @@ -1097,11 +1065,11 @@ func (bs *balanceSolver) buildOperators() ([]*operator.Operator, []Influence) { schedulerCounter.WithLabelValues(bs.sche.GetName(), "new-operator"), schedulerCounter.WithLabelValues(bs.sche.GetName(), bs.opTy.String())) - infl := Influence{ + infl = &Influence{ Loads: append(bs.cur.srcPeerStat.Loads[:0:0], bs.cur.srcPeerStat.Loads...), Count: 1, } - return []*operator.Operator{op}, []Influence{infl} + return op, infl } func (h *hotScheduler) GetHotStatus(typ string) *statistics.StoreHotPeersInfos { diff --git a/server/schedulers/hot_test.go b/server/schedulers/hot_region_test.go similarity index 98% rename from server/schedulers/hot_test.go rename to server/schedulers/hot_region_test.go index b40478676d8..891ad998d45 100644 --- a/server/schedulers/hot_test.go +++ b/server/schedulers/hot_region_test.go @@ -34,6 +34,26 @@ import ( func init() { schedulePeerPr = 1.0 + schedule.RegisterScheduler(HotWriteRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { + return newHotWriteScheduler(opController, initHotRegionScheduleConfig()), nil + }) + schedule.RegisterScheduler(HotReadRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { + return newHotReadScheduler(opController, initHotRegionScheduleConfig()), nil + }) +} + +func newHotReadScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { + ret := newHotScheduler(opController, conf) + ret.name = "" + ret.types = []rwType{read} + return ret +} + +func newHotWriteScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { + ret := newHotScheduler(opController, conf) + ret.name = "" + ret.types = []rwType{write} + return ret } type testHotSchedulerSuite struct{}