Skip to content

Commit 7e040eb

Browse files
committed
allocator: add adjustRangeCountForScoring
This commit adds `adjustRangeCountForScoring` to the `ScorerOptions` interface. The method takes a provided range count, adjusts it according to the scorer’s heuristic, and returns the result. It is similar to other scoring metrics such as the convergence score, and will later be used in candidate.compare as an attribute for breaking ties. The compare method evaluates the relative difference between two stores and favors the one with fewer ranges as a better rebalance target. Currently and by default, `adjustRangeCountForScoring` simply returns the provided range count. Future scorer options will override it with 0 since the heuristics is to not consider range count at all.
1 parent 5183cb9 commit 7e040eb

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,15 @@ type ScorerOptions interface {
292292
// the store represented by `sc` classifies as underfull, aroundTheMean, or
293293
// overfull relative to all the stores in `sl`.
294294
balanceScore(sl storepool.StoreList, sc roachpb.StoreCapacity) balanceStatus
295+
// adjustRangeCountForScoring returns the adjusted range count for scoring.
296+
// Scorer options (IOOverloadOnlyScorerOptions) that do not score based on
297+
// range count convergence returns 0. Otherwise, the returned range count here
298+
// should usually be the same as the range count passed in
299+
// (RangeCountScorerOptions, LoadScorerOptions, ScatterScorerOptions). This
300+
// will later be used to calculate the relative difference when comparing the
301+
// range count of the candidate stores. Lower range count means a better
302+
// candidate to move the replica to.
303+
adjustRangeCountForScoring(rangeCount int) int
295304
// rebalanceFromConvergenceScore assigns a convergence score to the store
296305
// referred to by `eqClass.existing` based on whether moving a replica away
297306
// from this store would converge its stats towards the mean (relative to the
@@ -385,6 +394,13 @@ func (bo BaseScorerOptions) maybeJitterStoreStats(
385394
return sl
386395
}
387396

397+
// adjustRangeCountForScoring returns the provided range count since that is the
398+
// default behavior. BaseScorerOptionsNoConvergence is the only scorer option
399+
// that does not want to consider range count in its scoring, so it returns 0.
400+
func (bo BaseScorerOptions) adjustRangeCountForScoring(rangeCount int) int {
401+
return rangeCount
402+
}
403+
388404
// RangeCountScorerOptions is used by the replicateQueue to tell the Allocator's
389405
// rebalancing machinery to base its balance/convergence scores on range counts.
390406
// This means that the resulting rebalancing decisions will further the goal of
@@ -1155,14 +1171,15 @@ func rankedCandidateListForAllocation(
11551171
}
11561172
hasNonVoter = StoreHasReplica(s.StoreID, nonVoterReplTargets)
11571173
}
1174+
rangeCountScore := options.adjustRangeCountForScoring(int(s.Capacity.RangeCount))
11581175
candidates = append(candidates, candidate{
11591176
store: s,
11601177
necessary: necessary,
11611178
valid: constraintsOK,
11621179
diversityScore: diversityScore,
11631180
balanceScore: balanceScore,
11641181
hasNonVoter: hasNonVoter,
1165-
rangeCount: int(s.Capacity.RangeCount),
1182+
rangeCount: rangeCountScore,
11661183
})
11671184
}
11681185
if options.deterministicForTesting() {
@@ -1270,7 +1287,7 @@ func candidateListForRemoval(
12701287
candidates[i].balanceScore = options.balanceScore(
12711288
removalCandidateStoreList, candidates[i].store.Capacity,
12721289
)
1273-
candidates[i].rangeCount = int(candidates[i].store.Capacity.RangeCount)
1290+
candidates[i].rangeCount = options.adjustRangeCountForScoring(int(candidates[i].store.Capacity.RangeCount))
12741291
}
12751292
// Re-sort to account for the ordering changes resulting from the addition of
12761293
// convergesScore, balanceScore, etc.
@@ -1735,7 +1752,7 @@ func rankedCandidateListForRebalancing(
17351752
balanceScore := options.balanceScore(comparable.candidateSL, existing.store.Capacity)
17361753
existing.convergesScore = convergesScore
17371754
existing.balanceScore = balanceScore
1738-
existing.rangeCount = int(existing.store.Capacity.RangeCount)
1755+
existing.rangeCount = options.adjustRangeCountForScoring(int(existing.store.Capacity.RangeCount))
17391756
}
17401757

17411758
var candidates candidateList
@@ -1761,7 +1778,7 @@ func rankedCandidateListForRebalancing(
17611778
)
17621779
cand.balanceScore = options.balanceScore(comparable.candidateSL, s.Capacity)
17631780
cand.convergesScore = options.rebalanceToConvergesScore(comparable, s)
1764-
cand.rangeCount = int(s.Capacity.RangeCount)
1781+
cand.rangeCount = options.adjustRangeCountForScoring(int(s.Capacity.RangeCount))
17651782
candidates = append(candidates, cand)
17661783
}
17671784

0 commit comments

Comments
 (0)