Skip to content

Commit 4363353

Browse files
committed
mmaintegration: add TestingKnobs to AllocatorSync
This commit adds a TestingKnobs field to AllocatorSync and plumbs it through, enabling future commits to add tests that override decisions in InConflictWithMMA.
1 parent 6386c47 commit 4363353

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func MakeAllocatorSync(
5757
) *mmaintegration.AllocatorSync {
5858
mmAllocator := mmaprototype.NewAllocatorState(timeutil.DefaultTimeSource{},
5959
rand.New(rand.NewSource(timeutil.Now().UnixNano())))
60-
return mmaintegration.NewAllocatorSync(sp, mmAllocator, st)
60+
return mmaintegration.NewAllocatorSync(sp, mmAllocator, st, nil)
6161
}
6262

6363
var simpleSpanConfig = &roachpb.SpanConfig{

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,29 @@ import (
2828
func CreateTestAllocator(
2929
ctx context.Context, numNodes int, deterministic bool,
3030
) (*stop.Stopper, *gossip.Gossip, *storepool.StorePool, Allocator, *timeutil.ManualTime) {
31-
return CreateTestAllocatorWithKnobs(ctx, numNodes, deterministic, nil /* knobs */)
31+
return CreateTestAllocatorWithKnobs(ctx, numNodes, deterministic,
32+
nil /* allocator.TestingKnobs */, nil /* mmaintegration.TestingKnobs */)
3233
}
3334

3435
// CreateTestAllocatorWithKnobs is like `CreateTestAllocator`, but allows the
3536
// caller to pass in custom TestingKnobs. Stopper must be stopped by
3637
// the caller.
3738
func CreateTestAllocatorWithKnobs(
38-
ctx context.Context, numNodes int, deterministic bool, knobs *allocator.TestingKnobs,
39+
ctx context.Context,
40+
numNodes int,
41+
deterministic bool,
42+
allocatorKnobs *allocator.TestingKnobs,
43+
allocSyncKnobs *mmaintegration.TestingKnobs,
3944
) (*stop.Stopper, *gossip.Gossip, *storepool.StorePool, Allocator, *timeutil.ManualTime) {
4045
st := cluster.MakeTestingClusterSettings()
4146
stopper, g, manual, storePool, _ := storepool.CreateTestStorePool(ctx, st,
4247
liveness.TestTimeUntilNodeDeadOff, deterministic,
4348
func() int { return numNodes },
4449
livenesspb.NodeLivenessStatus_LIVE)
4550
mmAllocator := mmaprototype.NewAllocatorState(timeutil.DefaultTimeSource{}, rand.New(rand.NewSource(timeutil.Now().UnixNano())))
46-
as := mmaintegration.NewAllocatorSync(storePool, mmAllocator, st)
51+
as := mmaintegration.NewAllocatorSync(storePool, mmAllocator, st, allocSyncKnobs)
4752
a := MakeAllocator(st, as, deterministic, func(id roachpb.NodeID) (time.Duration, bool) {
4853
return 0, true
49-
}, knobs)
54+
}, allocatorKnobs)
5055
return stopper, g, storePool, a, manual
5156
}

pkg/kv/kvserver/asim/state/impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func (s *state) AddNode(nodeCPUCapacity int64, locality roachpb.Locality) Node {
433433
stores: []StoreID{},
434434
mmAllocator: mmAllocator,
435435
storepool: sp,
436-
as: mmaintegration.NewAllocatorSync(sp, mmAllocator, s.settings.ST),
436+
as: mmaintegration.NewAllocatorSync(sp, mmAllocator, s.settings.ST, nil),
437437
}
438438
s.nodes[nodeID] = node
439439
s.SetNodeLiveness(nodeID, livenesspb.NodeLivenessStatus_LIVE)

pkg/kv/kvserver/mmaintegration/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
"allocator_sync.go",
88
"mma_conversion.go",
99
"store_load_msg.go",
10+
"testing_knobs.go",
1011
"thrashing.go",
1112
],
1213
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/mmaintegration",

pkg/kv/kvserver/mmaintegration/allocator_sync.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type mmaState interface {
6666
// pool. When mma is disabled, its sole purpose is to track and apply changes
6767
// to the store pool upon success.
6868
type AllocatorSync struct {
69+
knobs *TestingKnobs
6970
sp storePool
7071
st *cluster.Settings
7172
mmaAllocator mmaState
@@ -82,11 +83,14 @@ type AllocatorSync struct {
8283
}
8384
}
8485

85-
func NewAllocatorSync(sp storePool, mmaAllocator mmaState, st *cluster.Settings) *AllocatorSync {
86+
func NewAllocatorSync(
87+
sp storePool, mmaAllocator mmaState, st *cluster.Settings, knobs *TestingKnobs,
88+
) *AllocatorSync {
8689
as := &AllocatorSync{
8790
sp: sp,
8891
st: st,
8992
mmaAllocator: mmaAllocator,
93+
knobs: knobs,
9094
}
9195
as.mu.trackedChanges = make(map[SyncChangeID]trackedAllocatorChange)
9296
return as
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package mmaintegration
7+
8+
import "github.com/cockroachdb/cockroach/pkg/roachpb"
9+
10+
// TestingKnobs is used for testing purposes to tune the behavior of mma.
11+
type TestingKnobs struct {
12+
// OverrideIsInConflictWithMMA is used to override the value returned by
13+
// isInConflictWithMMA.
14+
OverrideIsInConflictWithMMA func(cand roachpb.StoreID) bool
15+
}

pkg/kv/kvserver/mmaintegration/thrashing.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,8 @@ func (as *AllocatorSync) BuildMMARebalanceAdvisor(
103103
func (as *AllocatorSync) IsInConflictWithMMA(
104104
cand roachpb.StoreID, advisor mmaprototype.MMARebalanceAdvisor, cpuOnly bool,
105105
) bool {
106-
return as.mmaAllocator.IsInConflictWithMMA(cand, advisor, cpuOnly)
106+
if as.knobs != nil && as.knobs.OverrideIsInConflictWithMMA != nil {
107+
return as.knobs.OverrideIsInConflictWithMMA(cand)
108+
}
109+
return as.mmaAllocator.IsInConflictWithMMA(cand, advisor, cpuOnly)
107110
}

pkg/kv/kvserver/store_rebalancer_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ func TestChooseLeaseToTransfer(t *testing.T) {
582582
// of snapshots, in order to avoid mocking out a fake raft group for the
583583
// `replicaMayNeedSnapshot` checks inside `TransferLeaseTarget`.
584584
AllowLeaseTransfersToReplicasNeedingSnapshots: true,
585-
},
585+
}, nil, /*allocSyncKnobs*/
586586
)
587587
defer stopper.Stop(context.Background())
588588
objectiveProvider := &testRebalanceObjectiveProvider{}
@@ -1230,7 +1230,8 @@ func TestChooseRangeToRebalanceAcrossHeterogeneousZones(t *testing.T) {
12301230
t.Run(tc.name, withQPSCPU(t, objectiveProvider, func(t *testing.T) {
12311231
// Boilerplate for test setup.
12321232
testingKnobs := allocator.TestingKnobs{RaftStatusFn: TestingRaftStatusFn}
1233-
stopper, g, sp, a, _ := allocatorimpl.CreateTestAllocatorWithKnobs(ctx, 10, false /* deterministic */, &testingKnobs)
1233+
stopper, g, sp, a, _ := allocatorimpl.CreateTestAllocatorWithKnobs(
1234+
ctx, 10, false /* deterministic */, &testingKnobs, nil /*allocSyncKnobs*/)
12341235
defer stopper.Stop(context.Background())
12351236
gossiputil.NewStoreGossiper(g).GossipStores(multiRegionStores, t)
12361237

@@ -1329,6 +1330,7 @@ func TestChooseRangeToRebalanceIgnoresRangeOnBestStores(t *testing.T) {
13291330
10,
13301331
false, /* deterministic */
13311332
&allocator.TestingKnobs{AllowLeaseTransfersToReplicasNeedingSnapshots: true},
1333+
nil, /*allocSyncKnobs*/
13321334
)
13331335
defer stopper.Stop(context.Background())
13341336

@@ -1592,6 +1594,7 @@ func TestNoLeaseTransferToBehindReplicas(t *testing.T) {
15921594
AllowLeaseTransfersToReplicasNeedingSnapshots: false,
15931595
RaftStatusFn: behindTestingRaftStatusFn,
15941596
},
1597+
nil, /*allocSyncKnobs*/
15951598
)
15961599
defer stopper.Stop(context.Background())
15971600

pkg/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ func NewServer(cfg Config, stopper *stop.Stopper) (serverctl.ServerStartupInterf
904904

905905
mmaAllocator := mmaprototype.NewAllocatorState(timeutil.DefaultTimeSource{},
906906
rand.New(rand.NewSource(timeutil.Now().UnixNano())))
907-
allocatorSync := mmaintegration.NewAllocatorSync(storePool, mmaAllocator, st)
907+
allocatorSync := mmaintegration.NewAllocatorSync(storePool, mmaAllocator, st, nil)
908908
g.RegisterCallback(
909909
gossip.MakePrefixPattern(gossip.KeyStoreDescPrefix),
910910
func(_ string, content roachpb.Value, origTimestampNanos int64) {

0 commit comments

Comments
 (0)