@@ -46,6 +46,14 @@ type rebalanceState struct {
4646 maxLeaseTransferCount int
4747 // lastFailedChangeDelayDuration is the delay after a failed change before retrying.
4848 lastFailedChangeDelayDuration time.Duration
49+ // Scratch variables reused across iterations.
50+ scratch struct {
51+ disj [1 ]constraintsConj
52+ storesToExclude storeSet
53+ storesToExcludeForRange storeSet
54+ nodes map [roachpb.NodeID ]* NodeLoad
55+ stores map [roachpb.StoreID ]struct {}
56+ }
4957}
5058
5159// Called periodically, say every 10s.
@@ -151,11 +159,6 @@ func (cs *clusterState) rebalanceStores(
151159 }
152160 }
153161
154- var disj [1 ]constraintsConj
155- var storesToExclude storeSet
156- var storesToExcludeForRange storeSet
157- scratchNodes := map [roachpb.NodeID ]* NodeLoad {}
158- scratchStores := map [roachpb.StoreID ]struct {}{}
159162 // The caller has a fixed concurrency limit it can move ranges at, when it
160163 // is the sender of the snapshot. So we don't want to create too many
161164 // changes, since then the allocator gets too far ahead of what has been
@@ -181,6 +184,8 @@ func (cs *clusterState) rebalanceStores(
181184 maxLeaseTransferCount : maxLeaseTransferCount ,
182185 lastFailedChangeDelayDuration : lastFailedChangeDelayDuration ,
183186 }
187+ rs .scratch .nodes = map [roachpb.NodeID ]* NodeLoad {}
188+ rs .scratch .stores = map [roachpb.StoreID ]struct {}{}
184189 for _ , store := range sheddingStores {
185190 func (rs * rebalanceState , store sheddingStore , ctx context.Context , localStoreID roachpb.StoreID , now time.Time ) {
186191 log .KvDistribution .Infof (ctx , "start processing shedding store s%d: cpu node load %s, store load %s, worst dim %s" ,
@@ -285,8 +290,8 @@ func (cs *clusterState) rebalanceStores(
285290 if len (candsPL ) <= 1 {
286291 continue // leaseholder is the only candidate
287292 }
288- clear (scratchNodes )
289- means := computeMeansForStoreSet (rs .cs , candsPL , scratchNodes , scratchStores )
293+ clear (rs . scratch . nodes )
294+ means := computeMeansForStoreSet (rs .cs , candsPL , rs . scratch . nodes , rs . scratch . stores )
290295 sls := rs .cs .computeLoadSummary (ctx , store .StoreID , & means .storeLoad , & means .nodeLoad )
291296 log .KvDistribution .VInfof (ctx , 2 , "considering lease-transfer r%v from s%v: candidates are %v" , rangeID , store .StoreID , candsPL )
292297 if sls .dimSummary [CPURate ] < overloadSlow {
@@ -416,16 +421,16 @@ func (cs *clusterState) rebalanceStores(
416421 // the other stores on this node from receiving replicas shed by this
417422 // store.
418423 excludeStoresOnNode := store .nls > overloadSlow
419- storesToExclude = storesToExclude [:0 ]
424+ rs . scratch . storesToExclude = rs . scratch . storesToExclude [:0 ]
420425 if excludeStoresOnNode {
421426 nodeID := ss .NodeID
422427 for _ , storeID := range rs .cs .nodes [nodeID ].stores {
423- storesToExclude .insert (storeID )
428+ rs . scratch . storesToExclude .insert (storeID )
424429 }
425430 log .KvDistribution .VInfof (ctx , 2 , "excluding all stores on n%d due to overload/fd status" , nodeID )
426431 } else {
427432 // This store is excluded of course.
428- storesToExclude .insert (store .StoreID )
433+ rs . scratch . storesToExclude .insert (store .StoreID )
429434 }
430435
431436 // Iterate over top-K ranges first and try to move them.
@@ -474,8 +479,8 @@ func (cs *clusterState) rebalanceStores(
474479 log .KvDistribution .VInfof (ctx , 2 , "skipping r%d: constraint violation needs fixing first: %v" , rangeID , err )
475480 continue
476481 }
477- disj [0 ] = conj
478- storesToExcludeForRange = append (storesToExcludeForRange [:0 ], storesToExclude ... )
482+ rs . scratch . disj [0 ] = conj
483+ rs . scratch . storesToExcludeForRange = append (rs . scratch . storesToExcludeForRange [:0 ], rs . scratch . storesToExclude ... )
479484 // Also exclude all stores on nodes that have existing replicas.
480485 for _ , replica := range rstate .replicas {
481486 storeID := replica .StoreID
@@ -487,11 +492,11 @@ func (cs *clusterState) rebalanceStores(
487492 }
488493 nodeID := rs .cs .stores [storeID ].NodeID
489494 for _ , storeID := range rs .cs .nodes [nodeID ].stores {
490- storesToExcludeForRange .insert (storeID )
495+ rs . scratch . storesToExcludeForRange .insert (storeID )
491496 }
492497 }
493498 // TODO(sumeer): eliminate cands allocations by passing a scratch slice.
494- cands , ssSLS := rs .cs .computeCandidatesForRange (ctx , disj [:], storesToExcludeForRange , store .StoreID )
499+ cands , ssSLS := rs .cs .computeCandidatesForRange (ctx , rs . scratch . disj [:], rs . scratch . storesToExcludeForRange , store .StoreID )
495500 log .KvDistribution .VInfof (ctx , 2 , "considering replica-transfer r%v from s%v: store load %v" ,
496501 rangeID , store .StoreID , ss .adjusted .load )
497502 if log .V (2 ) {
0 commit comments