Skip to content

Commit

Permalink
scheduler: avoid to updateCounts frequently (#7836)
Browse files Browse the repository at this point in the history
close #7837

Use O(1) method to update `counts`.

Signed-off-by: Leavrth <[email protected]>

Co-authored-by: Hu# <[email protected]>
  • Loading branch information
Leavrth and HuSharp committed Feb 29, 2024
1 parent e199866 commit b207e51
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions pkg/schedule/operator/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ func (oc *Controller) addOperatorLocked(op *Operator) bool {
return false
}
oc.operators[regionID] = op
oc.counts[op.SchedulerKind()]++
operatorCounter.WithLabelValues(op.Desc(), "start").Inc()
operatorSizeHist.WithLabelValues(op.Desc()).Observe(float64(op.ApproximateSize))
opInfluence := NewTotalOpInfluence([]*Operator{op}, oc.cluster)
Expand All @@ -505,7 +506,6 @@ func (oc *Controller) addOperatorLocked(op *Operator) bool {
storeLimitCostCounter.WithLabelValues(strconv.FormatUint(storeID, 10), n).Add(float64(stepCost) / float64(storelimit.RegionInfluence[v]))
}
}
oc.updateCounts(oc.operators)

var step OpStep
if region := oc.cluster.GetRegion(op.RegionID()); region != nil {
Expand Down Expand Up @@ -560,14 +560,14 @@ func (oc *Controller) removeOperatorsLocked() []*Operator {
var removed []*Operator
for regionID, op := range oc.operators {
delete(oc.operators, regionID)
oc.counts[op.SchedulerKind()]--
operatorCounter.WithLabelValues(op.Desc(), "remove").Inc()
oc.ack(op)
if op.Kind()&OpMerge != 0 {
oc.removeRelatedMergeOperator(op)
}
removed = append(removed, op)
}
oc.updateCounts(oc.operators)
return removed
}

Expand Down Expand Up @@ -602,7 +602,7 @@ func (oc *Controller) removeOperatorLocked(op *Operator) bool {
regionID := op.RegionID()
if cur := oc.operators[regionID]; cur == op {
delete(oc.operators, regionID)
oc.updateCounts(oc.operators)
oc.counts[op.SchedulerKind()]--
operatorCounter.WithLabelValues(op.Desc(), "remove").Inc()
oc.ack(op)
if op.Kind()&OpMerge != 0 {
Expand Down Expand Up @@ -783,16 +783,6 @@ func (oc *Controller) GetHistory(start time.Time) []OpHistory {
return history
}

// updateCounts updates resource counts using current pending operators.
func (oc *Controller) updateCounts(operators map[uint64]*Operator) {
for k := range oc.counts {
delete(oc.counts, k)
}
for _, op := range operators {
oc.counts[op.SchedulerKind()]++
}
}

// OperatorCount gets the count of operators filtered by kind.
// kind only has one OpKind.
func (oc *Controller) OperatorCount(kind OpKind) uint64 {
Expand Down Expand Up @@ -862,7 +852,7 @@ func (oc *Controller) SetOperator(op *Operator) {
oc.Lock()
defer oc.Unlock()
oc.operators[op.RegionID()] = op
oc.updateCounts(oc.operators)
oc.counts[op.SchedulerKind()]++
}

// OpWithStatus records the operator and its status.
Expand Down

0 comments on commit b207e51

Please sign in to comment.