Skip to content

Commit ad4d147

Browse files
committed
allocator: small refactor for CheckPriorityInversion
This commit refactors CheckPriorityInversion.
1 parent 763ecad commit ad4d147

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

pkg/kv/kvserver/allocator/allocatorimpl/allocator.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const (
139139
AllocatorConsiderRebalance
140140
AllocatorRangeUnavailable
141141
AllocatorFinalizeAtomicReplicationChange
142+
AllocatorMaxPriority
142143
)
143144

144145
// Add indicates an action adding a replica.
@@ -267,9 +268,12 @@ func (a AllocatorAction) SafeValue() {}
267268
// predate this comment, so we allow them as they belong to the same general
268269
// priority category.
269270
func (a AllocatorAction) Priority() float64 {
271+
const maxPriority = 12002
270272
switch a {
273+
case AllocatorMaxPriority:
274+
return maxPriority
271275
case AllocatorFinalizeAtomicReplicationChange:
272-
return 12002
276+
return maxPriority
273277
case AllocatorRemoveLearner:
274278
return 12001
275279
case AllocatorReplaceDeadVoter:
@@ -3309,12 +3313,6 @@ func roundToNearestPriorityCategory(n float64) float64 {
33093313
return math.Round(n/100.0) * 100
33103314
}
33113315

3312-
// WithinPriorityRange checks if a priority is within the range of possible
3313-
// priorities for the allocator actions.
3314-
func withinPriorityRange(priority float64) bool {
3315-
return AllocatorNoop.Priority() <= priority && priority <= AllocatorFinalizeAtomicReplicationChange.Priority()
3316-
}
3317-
33183316
// CheckPriorityInversion returns whether there was a priority inversion (and
33193317
// the range should not be processed at this time, since doing so could starve
33203318
// higher-priority items), and whether the caller should re-add the range to the
@@ -3333,18 +3331,20 @@ func withinPriorityRange(priority float64) bool {
33333331
func CheckPriorityInversion(
33343332
priorityAtEnqueue float64, actionAtProcessing AllocatorAction,
33353333
) (isInversion bool, shouldRequeue bool) {
3336-
// priorityAtEnqueue of -1 is a special case reserved for processing logic to
3337-
// run even if there’s a priority inversion. If the priority is not -1, the
3338-
// range may be re-queued to be processed with the correct priority. It is
3339-
// used for things that call into baseQueue.process without going through the
3340-
// replicate priority queue. For example, s.ReplicateQueueDryRun or
3341-
// r.scatterRangeAndRandomizeLeases.
3334+
// NB: priorityAtEnqueue is -1 for callers such as scatter, dry runs, and
3335+
// manual queue runs. Priority inversion does not apply to these calls.
3336+
if priorityAtEnqueue == -1 {
3337+
return false, false
3338+
}
33423339

33433340
// NB: we need to check for when priorityAtEnqueue falls within the range
33443341
// of the allocator actions because store.Enqueue might enqueue things with
33453342
// a very high priority (1e5). In those cases, we do not want to requeue
33463343
// these actions or count it as an inversion.
3347-
if priorityAtEnqueue == -1 || !withinPriorityRange(priorityAtEnqueue) {
3344+
withinPriorityRange := func(priority float64) bool {
3345+
return AllocatorNoop.Priority() <= priority && priority <= AllocatorMaxPriority.Priority()
3346+
}
3347+
if !withinPriorityRange(priorityAtEnqueue) {
33483348
return false, false
33493349
}
33503350

0 commit comments

Comments
 (0)