@@ -13,6 +13,7 @@ import (
1313 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gossip"
1414 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history"
1515 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/metrics"
16+ "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/mmaintegration"
1617 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/op"
1718 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/queue"
1819 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/scheduled"
@@ -29,8 +30,8 @@ type Simulator struct {
2930 curr time.Time
3031 end time.Time
3132 // interval is the step between ticks for active simulaton components, such
32- // as the queues, store rebalancer and state changers. It should be set
33- // lower than the bgInterval, as updated occur more frequently.
33+ // as the queues, store rebalancer and state changers. It should be set lower
34+ // than the bgInterval, as updates occur more frequently.
3435 interval time.Duration
3536
3637 // The simulator can run multiple workload Generators in parallel.
@@ -47,6 +48,8 @@ type Simulator struct {
4748 sqs map [state.StoreID ]queue.RangeQueue
4849 // Store rebalancers.
4950 srs map [state.StoreID ]storerebalancer.StoreRebalancer
51+ // Multi-metric store rebalancers.
52+ mmSRs map [state.StoreID ]* mmaintegration.MMAStoreRebalancer
5053 // Store operation controllers.
5154 controllers map [state.StoreID ]op.Controller
5255
@@ -87,6 +90,7 @@ func NewSimulator(
8790 lqs := make (map [state.StoreID ]queue.RangeQueue )
8891 sqs := make (map [state.StoreID ]queue.RangeQueue )
8992 srs := make (map [state.StoreID ]storerebalancer.StoreRebalancer )
93+ mmSRs := make (map [state.StoreID ]* mmaintegration.MMAStoreRebalancer )
9094 changer := state .NewReplicaChanger ()
9195 controllers := make (map [state.StoreID ]op.Controller )
9296
@@ -103,6 +107,7 @@ func NewSimulator(
103107 sqs : sqs ,
104108 controllers : controllers ,
105109 srs : srs ,
110+ mmSRs : mmSRs ,
106111 pacers : pacers ,
107112 gossip : gossip .NewGossip (initialState , settings ),
108113 metrics : m ,
@@ -178,6 +183,25 @@ func (s *Simulator) addStore(storeID state.StoreID, tick time.Time) {
178183 s .settings ,
179184 storerebalancer .GetStateRaftStatusFn (s .state ),
180185 )
186+ // TODO: We add the store to every node's allocator in the cluster
187+ // immediately. This is also updated via gossip, however there is a delay
188+ // after startup. When calling `mma.ProcessStoreLeaseholderMsg` via
189+ // tickMMStoreRebalancers, there may be not be a store state for some
190+ // replicas. Setting it here ensures that the store is always present and
191+ // initiated in each node's allocator. We should instead handle this in mma,
192+ // or integration component.
193+ for _ , node := range s .state .Nodes () {
194+ node .MMAllocator ().SetStore (state .StoreAttrAndLocFromDesc (
195+ s .state .StoreDescriptors (false , storeID )[0 ]))
196+ }
197+ s .mmSRs [storeID ] = mmaintegration .NewMMAStoreRebalancer (
198+ storeID ,
199+ store .NodeID (),
200+ s .state .Node (store .NodeID ()).MMAllocator (),
201+ s .state .Node (store .NodeID ()).AllocatorSync (),
202+ s .controllers [storeID ],
203+ s .settings ,
204+ )
181205}
182206
183207// GetNextTickTime returns a simulated tick time, or an indication that the
@@ -245,6 +269,9 @@ func (s *Simulator) RunSim(ctx context.Context) {
245269 // Simulate the store rebalancer logic.
246270 s .tickStoreRebalancers (ctx , tick , stateForAlloc )
247271
272+ // Simulate the multi-metric store rebalancer logic.
273+ s .tickMMStoreRebalancers (ctx , tick , stateForAlloc )
274+
248275 // Print tick metrics.
249276 s .tickMetrics (ctx , tick )
250277 }
@@ -345,6 +372,16 @@ func (s *Simulator) tickStoreRebalancers(ctx context.Context, tick time.Time, st
345372 }
346373}
347374
375+ // tickStoreRebalancers iterates over the multi-metric store rebalancers in the
376+ // cluster and ticks their control loop.
377+ func (s * Simulator ) tickMMStoreRebalancers (ctx context.Context , tick time.Time , state state.State ) {
378+ stores := s .state .Stores ()
379+ s .shuffler (len (stores ), func (i , j int ) { stores [i ], stores [j ] = stores [j ], stores [i ] })
380+ for _ , store := range stores {
381+ s .mmSRs [store .StoreID ()].Tick (ctx , tick , state )
382+ }
383+ }
384+
348385// tickMetrics prints the metrics up to the given tick.
349386func (s * Simulator ) tickMetrics (ctx context.Context , tick time.Time ) {
350387 s .metrics .Tick (ctx , tick , s .state )
0 commit comments