Skip to content

Commit 471dc65

Browse files
committed
mmaprototype: move rng and dsm into rebalanceState
Move rng and dsm fields into the rebalanceState struct instead of passing them as parameters. This reduces the parameter list and keeps related state together.
1 parent 1a36437 commit 471dc65

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

pkg/kv/kvserver/allocator/mmaprototype/cluster_state_rebalance_stores.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ var mmaid = atomic.Int64{}
2626
// rebalanceState tracks the state and outcomes of a rebalanceStores invocation.
2727
type rebalanceState struct {
2828
cs *clusterState
29+
// rng is the random number generator for non-deterministic decisions.
30+
rng *rand.Rand
31+
// dsm is the diversity scoring memo for computing diversity scores.
32+
dsm *diversityScoringMemo
2933
// changes accumulates the pending range changes made during rebalancing.
3034
changes []PendingRangeChange
3135
// rangeMoveCount tracks the number of range moves made.
@@ -166,6 +170,8 @@ func (cs *clusterState) rebalanceStores(
166170
const lastFailedChangeDelayDuration time.Duration = 60 * time.Second
167171
rs := &rebalanceState{
168172
cs: cs,
173+
rng: rng,
174+
dsm: dsm,
169175
changes: []PendingRangeChange{},
170176
rangeMoveCount: 0,
171177
leaseTransferCount: 0,
@@ -176,7 +182,7 @@ func (cs *clusterState) rebalanceStores(
176182
lastFailedChangeDelayDuration: lastFailedChangeDelayDuration,
177183
}
178184
for _, store := range sheddingStores {
179-
func(rs *rebalanceState, store sheddingStore) {
185+
func(rs *rebalanceState, store sheddingStore, ctx context.Context, localStoreID roachpb.StoreID, now time.Time) {
180186
log.KvDistribution.Infof(ctx, "start processing shedding store s%d: cpu node load %s, store load %s, worst dim %s",
181187
store.StoreID, store.nls, store.sls, store.worstDim)
182188
ss := rs.cs.stores[store.StoreID]
@@ -318,7 +324,7 @@ func (cs *clusterState) rebalanceStores(
318324
// will only add CPU to the target store (so it is ok to ignore other
319325
// dimensions on the target).
320326
targetStoreID := sortTargetCandidateSetAndPick(
321-
ctx, candsSet, sls.sls, ignoreHigherThanLoadThreshold, CPURate, rng)
327+
ctx, candsSet, sls.sls, ignoreHigherThanLoadThreshold, CPURate, rs.rng)
322328
if targetStoreID == 0 {
323329
log.KvDistribution.Infof(
324330
ctx,
@@ -505,7 +511,7 @@ func (cs *clusterState) rebalanceStores(
505511
} else {
506512
rlocalities = rstate.constraints.replicaLocalityTiers
507513
}
508-
localities := dsm.getExistingReplicaLocalities(rlocalities)
514+
localities := rs.dsm.getExistingReplicaLocalities(rlocalities)
509515
isLeaseholder := rstate.constraints.leaseholderID == store.StoreID
510516
// Set the diversity score and lease preference index of the candidates.
511517
for _, cand := range cands.candidates {
@@ -538,7 +544,7 @@ func (cs *clusterState) rebalanceStores(
538544
ignoreLevel, ssSLS.sls, rangeID, overloadDur)
539545
}
540546
targetStoreID := sortTargetCandidateSetAndPick(
541-
ctx, cands, ssSLS.sls, ignoreLevel, loadDim, rng)
547+
ctx, cands, ssSLS.sls, ignoreLevel, loadDim, rs.rng)
542548
if targetStoreID == 0 {
543549
log.KvDistribution.VInfof(ctx, 2, "result(failed): no suitable target found among candidates for r%d "+
544550
"(threshold %s; %s)", rangeID, ssSLS.sls, ignoreLevel)
@@ -602,7 +608,7 @@ func (cs *clusterState) rebalanceStores(
602608
rs.shouldContinue = true
603609
return
604610
}
605-
}(rs, store)
611+
}(rs, store, ctx, localStoreID, now)
606612
if rs.shouldReturnEarly {
607613
return rs.changes
608614
}

0 commit comments

Comments
 (0)