Skip to content

Commit 4cab865

Browse files
committed
testcluster: fix TestDRPCEnabledRandomly for cluster with multiple nodes
This change fixes the behavior where it was deciding the DRPC enablement for each node independently for TestDRPCEnabledRandomly option, leading to a cluster startup with some nodes with DRPC enabled, and others not. Now the DRPC option is resolved once at the cluster level and applied consistently to all nodes, ensuring uniform DRPC configuration across the entire test cluster.
1 parent 4cc03d4 commit 4cab865

File tree

3 files changed

+64
-36
lines changed

3 files changed

+64
-36
lines changed

pkg/server/testserver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ func makeTestConfigFromParams(params base.TestServerArgs) Config {
333333
cfg.TestingKnobs.AdmissionControlOptions = &admission.Options{}
334334
}
335335

336+
if params.DefaultDRPCOption == base.TestDRPCEnabled {
337+
rpc.ExperimentalDRPCEnabled.Override(context.Background(), &st.SV, true)
338+
}
339+
336340
return cfg
337341
}
338342

pkg/testutils/serverutils/test_server_shim.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ import (
2424
"github.com/cockroachdb/cockroach/pkg/kv"
2525
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilitiespb"
2626
"github.com/cockroachdb/cockroach/pkg/roachpb"
27-
"github.com/cockroachdb/cockroach/pkg/rpc"
2827
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
2928
"github.com/cockroachdb/cockroach/pkg/security/username"
30-
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
3129
"github.com/cockroachdb/cockroach/pkg/testutils/pgurlutils"
3230
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
3331
"github.com/cockroachdb/cockroach/pkg/util/envutil"
@@ -274,11 +272,9 @@ func StartServerOnlyE(t TestLogger, params base.TestServerArgs) (TestServerInter
274272
ctx := context.Background()
275273
allowAdditionalTenants := params.DefaultTestTenant.AllowAdditionalTenants()
276274

277-
// Update the flags with the actual decision as to whether we should
278-
// start the service for a default test tenant.
275+
// Update the flags with the actual decisions for test configuration.
279276
params.DefaultTestTenant = ShouldStartDefaultTestTenant(t, params.DefaultTestTenant)
280-
281-
TryEnableDRPCSetting(ctx, t, &params)
277+
params.DefaultDRPCOption = ShouldEnableDRPC(ctx, t, params.DefaultDRPCOption)
282278

283279
s, err := NewServer(params)
284280
if err != nil {
@@ -541,11 +537,12 @@ func WaitForTenantCapabilities(
541537
}
542538
}
543539

544-
// TryEnableDRPCSetting determines whether to enable the DRPC cluster setting
545-
// based on the `TestServerArgs.DefaultDRPCOption` and updates the
546-
// `TestServerArgs.Settings` based on that.
547-
func TryEnableDRPCSetting(ctx context.Context, t TestLogger, args *base.TestServerArgs) {
548-
option := args.DefaultDRPCOption
540+
// ShouldEnableDRPC determines the final DRPC option based on the input
541+
// option and any global overrides, resolving random choices to a concrete
542+
// enabled/disabled state.
543+
func ShouldEnableDRPC(
544+
ctx context.Context, t TestLogger, option base.DefaultTestDRPCOption,
545+
) base.DefaultTestDRPCOption {
549546
var logSuffix string
550547
if option == base.TestDRPCUnset && globalDefaultDRPCOptionOverride.isSet {
551548
option = globalDefaultDRPCOptionOverride.value
@@ -559,13 +556,11 @@ func TryEnableDRPCSetting(ctx context.Context, t TestLogger, args *base.TestServ
559556
rng, _ := randutil.NewTestRand()
560557
enableDRPC = rng.Intn(2) == 0
561558
}
562-
if !enableDRPC {
563-
return
564-
}
565559

566-
t.Log("DRPC is enabled" + logSuffix)
567-
if args.Settings == nil {
568-
args.Settings = cluster.MakeClusterSettings()
560+
if enableDRPC {
561+
t.Log("DRPC is enabled" + logSuffix)
562+
return base.TestDRPCEnabled
569563
}
570-
rpc.ExperimentalDRPCEnabled.Override(ctx, &args.Settings.SV, true)
564+
565+
return base.TestDRPCDisabled
571566
}

pkg/testutils/testcluster/testcluster.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type TestCluster struct {
7979
clusterArgs base.TestClusterArgs
8080

8181
defaultTestTenantOptions base.DefaultTestTenantOptions
82+
defaultDRPCOption base.DefaultTestDRPCOption
8283

8384
t serverutils.TestFataler
8485
}
@@ -260,6 +261,40 @@ func PrintTimings(testMain time.Duration) {
260261
}
261262
}
262263

264+
// validateDefaultTestTenant checks that per-server args don't override
265+
// the top-level DefaultTestTenant setting inconsistently.
266+
func (tc *TestCluster) validateDefaultTestTenant(
267+
t serverutils.TestFataler, nodes int, defaultTestTenantOptions base.DefaultTestTenantOptions,
268+
) {
269+
for i := range nodes {
270+
if args, ok := tc.clusterArgs.ServerArgsPerNode[i]; ok &&
271+
args.DefaultTestTenant != (base.DefaultTestTenantOptions{}) &&
272+
args.DefaultTestTenant != defaultTestTenantOptions {
273+
tc.Stopper().Stop(context.Background())
274+
t.Fatalf("improper use of DefaultTestTenantOptions in per-server args: %v vs %v\n"+
275+
"Tip: use the top-level ServerArgs to set the default test tenant options.",
276+
args.DefaultTestTenant, defaultTestTenantOptions)
277+
}
278+
}
279+
}
280+
281+
// validateDefaultDRPCOption checks that per-server args don't override
282+
// the top-level DefaultDRPCOption setting inconsistently.
283+
func (tc *TestCluster) validateDefaultDRPCOption(
284+
t serverutils.TestFataler, nodes int, defaultDRPCOption base.DefaultTestDRPCOption,
285+
) {
286+
for i := range nodes {
287+
if args, ok := tc.clusterArgs.ServerArgsPerNode[i]; ok &&
288+
args.DefaultDRPCOption != base.TestDRPCUnset &&
289+
args.DefaultDRPCOption != defaultDRPCOption {
290+
tc.Stopper().Stop(context.Background())
291+
t.Fatalf("improper use of DefaultDRPCOption in per-server args: %v vs %v\n"+
292+
"Tip: use the top-level ServerArgs to set the default DRPC option.",
293+
args.DefaultDRPCOption, defaultDRPCOption)
294+
}
295+
}
296+
}
297+
263298
// StartTestCluster creates and starts up a TestCluster made up of `nodes`
264299
// in-memory testing servers.
265300
// The cluster should be stopped using TestCluster.Stopper().Stop().
@@ -314,23 +349,18 @@ func NewTestCluster(
314349
noLocalities = false
315350
}
316351

317-
// Find out how to do the default test tenant.
318-
// The choice should be made by the top-level ServerArgs.
352+
// Resolve the test tenant configuration for the cluster. The choice should
353+
// be made by the top-level ServerArgs.
319354
defaultTestTenantOptions := tc.clusterArgs.ServerArgs.DefaultTestTenant
320-
// API check: verify that no non-default choice was made via per-server args,
321-
// and inform the user otherwise.
322-
for i := 0; i < nodes; i++ {
323-
if args, ok := tc.clusterArgs.ServerArgsPerNode[i]; ok &&
324-
args.DefaultTestTenant != (base.DefaultTestTenantOptions{}) &&
325-
args.DefaultTestTenant != defaultTestTenantOptions {
326-
tc.Stopper().Stop(context.Background())
327-
t.Fatalf("improper use of DefaultTestTenantOptions in per-server args: %v vs %v\n"+
328-
"Tip: use the top-level ServerArgs to set the default test tenant options.",
329-
args.DefaultTestTenant, defaultTestTenantOptions)
330-
}
331-
}
355+
tc.validateDefaultTestTenant(t, nodes, defaultTestTenantOptions)
332356
tc.defaultTestTenantOptions = serverutils.ShouldStartDefaultTestTenant(t, defaultTestTenantOptions)
333357

358+
// Resolve the DRPC configuration for the cluster. The choice should be made
359+
// by the top-level ServerArgs.
360+
defaultDRPCOption := tc.clusterArgs.ServerArgs.DefaultDRPCOption
361+
tc.validateDefaultDRPCOption(t, nodes, defaultDRPCOption)
362+
tc.defaultDRPCOption = serverutils.ShouldEnableDRPC(context.Background(), t, defaultDRPCOption)
363+
334364
var firstListener net.Listener
335365
for i := 0; i < nodes; i++ {
336366
var serverArgs base.TestServerArgs
@@ -346,8 +376,6 @@ func NewTestCluster(
346376
serverArgs.Settings = cluster.TestingCloneClusterSettings(serverArgs.Settings)
347377
}
348378

349-
serverutils.TryEnableDRPCSetting(context.Background(), t, &serverArgs)
350-
351379
// If a reusable listener registry is provided, create reusable listeners
352380
// for every server that doesn't have a custom listener provided. (Only
353381
// servers with a reusable listener can be restarted).
@@ -650,9 +678,10 @@ func (tc *TestCluster) AddServer(
650678
serverArgs.Addr = serverArgs.Listener.Addr().String()
651679
}
652680

653-
// Inject the decision that was made about whether or not to start a
654-
// test tenant server, into this new server's configuration.
681+
// Inject the decisions that were made about test configuration
682+
// into this new server's configuration.
655683
serverArgs.DefaultTestTenant = tc.defaultTestTenantOptions
684+
serverArgs.DefaultDRPCOption = tc.defaultDRPCOption
656685

657686
s, err := serverutils.NewServer(serverArgs)
658687
if err != nil {

0 commit comments

Comments
 (0)