Skip to content

Commit 8d2d2bf

Browse files
craig[bot]knz
andcommitted
107868: rpc,*: sanitize the configuration of rpc.Context r=yuzefovich,stevendanna a=knz The main goal of this change is to offer a `.RPCClientConn()` method on test servers. To achieve this, it was necessary to lift the code previously in `cli.getClientGRPCConn` into a more reusable version of it, now hosted in `rpc.NewClientContext()`. I also took the opportunity to remove the dependency of `rpc.Context` on `base.Config`, by spelling out precisely which fields are necessary to RPC connections. Numerous tests could be simplified as a result. Needed for cockroachdb#107866. Epic: CRDB-18499 Co-authored-by: Raphael 'kena' Poss <[email protected]>
2 parents 32f846f + 607c697 commit 8d2d2bf

File tree

87 files changed

+945
-919
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+945
-919
lines changed

pkg/acceptance/localcluster/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ go_library(
1111
visibility = ["//visibility:public"],
1212
deps = [
1313
"//pkg/acceptance/cluster",
14-
"//pkg/base",
1514
"//pkg/config/zonepb",
1615
"//pkg/roachpb",
1716
"//pkg/rpc",
18-
"//pkg/security/username",
1917
"//pkg/server/serverpb",
2018
"//pkg/settings/cluster",
2119
"//pkg/testutils",

pkg/acceptance/localcluster/cluster.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ import (
2929
"text/tabwriter"
3030
"time"
3131

32-
"github.com/cockroachdb/cockroach/pkg/base"
3332
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
3433
"github.com/cockroachdb/cockroach/pkg/roachpb"
3534
"github.com/cockroachdb/cockroach/pkg/rpc"
36-
"github.com/cockroachdb/cockroach/pkg/security/username"
3735
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
3836
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
3937
"github.com/cockroachdb/cockroach/pkg/testutils"
@@ -265,20 +263,12 @@ func (c *Cluster) RPCPort(nodeIdx int) string {
265263
}
266264

267265
func (c *Cluster) makeNode(ctx context.Context, nodeIdx int, cfg NodeConfig) (*Node, <-chan error) {
268-
baseCtx := &base.Config{
269-
User: username.NodeUserName(),
270-
Insecure: true,
271-
}
272-
rpcCtx := rpc.NewContext(ctx, rpc.ContextOptions{
273-
TenantID: roachpb.SystemTenantID,
274-
Config: baseCtx,
275-
Clock: &timeutil.DefaultTimeSource{},
276-
ToleratedOffset: 0,
277-
Stopper: c.stopper,
278-
Settings: cluster.MakeTestingClusterSettings(),
279-
280-
ClientOnly: true,
281-
})
266+
opts := rpc.DefaultContextOptions()
267+
opts.Insecure = true
268+
opts.Stopper = c.stopper
269+
opts.Settings = cluster.MakeTestingClusterSettings()
270+
opts.ClientOnly = true
271+
rpcCtx := rpc.NewContext(ctx, opts)
282272

283273
n := &Node{
284274
Cfg: cfg,

pkg/base/addr_validation_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ func TestValidateAddrs(t *testing.T) {
156156
for i, test := range testData {
157157
t.Run(fmt.Sprintf("%d/%s", i, test.in), func(t *testing.T) {
158158
cfg := base.Config{
159-
Addr: test.in.listen,
160-
AdvertiseAddr: test.in.adv,
161-
HTTPAddr: test.in.http,
162-
HTTPAdvertiseAddr: test.in.advhttp,
163-
SQLAddr: test.in.sql,
164-
SQLAdvertiseAddr: test.in.advsql,
159+
Addr: test.in.listen,
160+
HTTPAddr: test.in.http,
161+
SQLAddr: test.in.sql,
162+
AdvertiseAddrH: base.AdvertiseAddrH{AdvertiseAddr: test.in.adv},
163+
HTTPAdvertiseAddrH: base.HTTPAdvertiseAddrH{HTTPAdvertiseAddr: test.in.advhttp},
164+
SQLAdvertiseAddrH: base.SQLAdvertiseAddrH{SQLAdvertiseAddr: test.in.advsql},
165165
}
166166

167167
if err := cfg.ValidateAddrs(context.Background()); err != nil {

pkg/base/config.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ var (
192192
defaultRangeLeaseDuration = envutil.EnvOrDefaultDuration(
193193
"COCKROACH_RANGE_LEASE_DURATION", 6*time.Second)
194194

195-
// defaultRPCHeartbeatTimeout is the default RPC heartbeat timeout. It is set
195+
// DefaultRPCHeartbeatTimeout is the default RPC heartbeat timeout. It is set
196196
// very high at 3 * NetworkTimeout for several reasons: the gRPC transport may
197197
// need to complete a dial/handshake before sending the heartbeat, the
198198
// heartbeat has occasionally been seen to require 3 RTTs even post-dial (for
@@ -212,7 +212,7 @@ var (
212212
// heartbeats and reduce this to NetworkTimeout (plus DialTimeout for the
213213
// initial heartbeat), see:
214214
// https://github.com/cockroachdb/cockroach/issues/93397.
215-
defaultRPCHeartbeatTimeout = 3 * NetworkTimeout
215+
DefaultRPCHeartbeatTimeout = 3 * NetworkTimeout
216216

217217
// defaultRaftTickInterval is the default resolution of the Raft timer.
218218
defaultRaftTickInterval = envutil.EnvOrDefaultDuration(
@@ -341,10 +341,13 @@ type Config struct {
341341
// Addr is the address the server is listening on.
342342
Addr string
343343

344-
// AdvertiseAddr is the address advertised by the server to other nodes
345-
// in the cluster. It should be reachable by all other nodes and should
346-
// route to an interface that Addr is listening on.
347-
AdvertiseAddr string
344+
// AdvertiseAddrH contains the address advertised by the server to
345+
// other nodes in the cluster. It should be reachable by all other
346+
// nodes and should route to an interface that Addr is listening on.
347+
//
348+
// It is set after the server instance has been created, when the
349+
// network listeners are being set up.
350+
AdvertiseAddrH
348351

349352
// ClusterName is the name used as a sanity check when a node joins
350353
// an uninitialized cluster, or when an uninitialized node joins an
@@ -367,9 +370,12 @@ type Config struct {
367370
// This is used if SplitListenSQL is set to true.
368371
SQLAddr string
369372

370-
// SQLAdvertiseAddr is the advertised SQL address.
373+
// SQLAdvertiseAddrH contains the advertised SQL address.
371374
// This is computed from SQLAddr if specified otherwise Addr.
372-
SQLAdvertiseAddr string
375+
//
376+
// It is set after the server instance has been created, when the
377+
// network listeners are being set up.
378+
SQLAdvertiseAddrH
373379

374380
// SocketFile, if non-empty, sets up a TLS-free local listener using
375381
// a unix datagram socket at the specified path for SQL clients.
@@ -382,9 +388,12 @@ type Config struct {
382388
// DisableTLSForHTTP, if set, disables TLS for the HTTP listener.
383389
DisableTLSForHTTP bool
384390

385-
// HTTPAdvertiseAddr is the advertised HTTP address.
391+
// HTTPAdvertiseAddrH contains the advertised HTTP address.
386392
// This is computed from HTTPAddr if specified otherwise Addr.
387-
HTTPAdvertiseAddr string
393+
//
394+
// It is set after the server instance has been created, when the
395+
// network listeners are being set up.
396+
HTTPAdvertiseAddrH
388397

389398
// RPCHeartbeatInterval controls how often Ping requests are sent on peer
390399
// connections to determine connection health and update the local view of
@@ -421,6 +430,21 @@ type Config struct {
421430
LocalityAddresses []roachpb.LocalityAddress
422431
}
423432

433+
// AdvertiseAddr is the type of the AdvertiseAddr field in Config.
434+
type AdvertiseAddrH struct {
435+
AdvertiseAddr string
436+
}
437+
438+
// SQLAdvertiseAddr is the type of the SQLAdvertiseAddr field in Config.
439+
type SQLAdvertiseAddrH struct {
440+
SQLAdvertiseAddr string
441+
}
442+
443+
// HTTPAdvertiseAddr is the type of the HTTPAdvertiseAddr field in Config.
444+
type HTTPAdvertiseAddrH struct {
445+
HTTPAdvertiseAddr string
446+
}
447+
424448
// HistogramWindowInterval is used to determine the approximate length of time
425449
// that individual samples are retained in in-memory histograms. Currently,
426450
// it is set to the arbitrary length of six times the Metrics sample interval.
@@ -455,7 +479,7 @@ func (cfg *Config) InitDefaults() {
455479
cfg.SocketFile = ""
456480
cfg.SSLCertsDir = DefaultCertsDirectory
457481
cfg.RPCHeartbeatInterval = PingInterval
458-
cfg.RPCHeartbeatTimeout = defaultRPCHeartbeatTimeout
482+
cfg.RPCHeartbeatTimeout = DefaultRPCHeartbeatTimeout
459483
cfg.ClusterName = ""
460484
cfg.DisableClusterNameVerification = false
461485
cfg.ClockDevicePath = ""

pkg/ccl/oidcccl/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,17 @@ go_test(
4848
"//pkg/base",
4949
"//pkg/ccl",
5050
"//pkg/roachpb",
51-
"//pkg/rpc",
5251
"//pkg/security/securityassets",
5352
"//pkg/security/securitytest",
5453
"//pkg/security/username",
5554
"//pkg/server",
5655
"//pkg/server/serverpb",
57-
"//pkg/testutils",
5856
"//pkg/testutils/serverutils",
5957
"//pkg/testutils/sqlutils",
6058
"//pkg/testutils/testcluster",
6159
"//pkg/util/leaktest",
6260
"//pkg/util/log",
6361
"//pkg/util/randutil",
64-
"//pkg/util/timeutil",
6562
"@com_github_stretchr_testify//require",
6663
],
6764
)

pkg/ccl/oidcccl/authentication_oidc_test.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ import (
2424

2525
"github.com/cockroachdb/cockroach/pkg/base"
2626
"github.com/cockroachdb/cockroach/pkg/roachpb"
27-
"github.com/cockroachdb/cockroach/pkg/rpc"
2827
"github.com/cockroachdb/cockroach/pkg/security/username"
2928
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
30-
"github.com/cockroachdb/cockroach/pkg/testutils"
3129
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
3230
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
3331
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
3432
"github.com/cockroachdb/cockroach/pkg/util/log"
35-
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
3633
"github.com/stretchr/testify/require"
3734
)
3835

@@ -51,22 +48,7 @@ func TestOIDCBadRequestIfDisabled(t *testing.T) {
5148
s := serverutils.StartServerOnly(t, base.TestServerArgs{})
5249
defer s.Stopper().Stop(ctx)
5350

54-
newRPCContext := func(cfg *base.Config) *rpc.Context {
55-
return rpc.NewContext(ctx,
56-
rpc.ContextOptions{
57-
TenantID: roachpb.SystemTenantID,
58-
Config: cfg,
59-
Clock: &timeutil.DefaultTimeSource{},
60-
ToleratedOffset: 1,
61-
Stopper: s.Stopper(),
62-
Settings: s.ClusterSettings(),
63-
64-
ClientOnly: true,
65-
})
66-
}
67-
68-
plainHTTPCfg := testutils.NewTestBaseContext(username.TestUserName())
69-
testCertsContext := newRPCContext(plainHTTPCfg)
51+
testCertsContext := s.NewClientRPCContext(ctx, username.TestUserName())
7052

7153
client, err := testCertsContext.GetHTTPClient()
7254
require.NoError(t, err)
@@ -90,19 +72,6 @@ func TestOIDCEnabled(t *testing.T) {
9072
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{})
9173
defer s.Stopper().Stop(ctx)
9274

93-
newRPCContext := func(cfg *base.Config) *rpc.Context {
94-
return rpc.NewContext(ctx, rpc.ContextOptions{
95-
TenantID: roachpb.SystemTenantID,
96-
Config: cfg,
97-
Clock: &timeutil.DefaultTimeSource{},
98-
ToleratedOffset: 1,
99-
Stopper: s.Stopper(),
100-
Settings: s.ClusterSettings(),
101-
102-
ClientOnly: true,
103-
})
104-
}
105-
10675
// Set up a test OIDC server that serves the JSON discovery document
10776
var issuer string
10877
oidcHandler := func(w http.ResponseWriter, r *http.Request) {
@@ -184,8 +153,7 @@ func TestOIDCEnabled(t *testing.T) {
184153
sqlDB.Exec(t, `SET CLUSTER SETTING server.oidc_authentication.redirect_url = "https://cockroachlabs.com/oidc/v1/callback"`)
185154
sqlDB.Exec(t, `SET CLUSTER SETTING server.oidc_authentication.enabled = "true"`)
186155

187-
plainHTTPCfg := testutils.NewTestBaseContext(username.TestUserName())
188-
testCertsContext := newRPCContext(plainHTTPCfg)
156+
testCertsContext := s.NewClientRPCContext(ctx, username.TestUserName())
189157

190158
client, err := testCertsContext.GetHTTPClient()
191159
require.NoError(t, err)

pkg/ccl/serverccl/statusccl/tenant_grpc_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ func TestTenantGRPCServices(t *testing.T) {
5959
t.Logf("subtests starting")
6060

6161
t.Run("gRPC is running", func(t *testing.T) {
62-
grpcAddr := tenant.RPCAddr()
63-
rpcCtx := tenant.RPCContext()
64-
65-
nodeID := roachpb.NodeID(tenant.SQLInstanceID())
66-
conn, err := rpcCtx.GRPCDialNode(grpcAddr, nodeID, rpc.DefaultClass).Connect(ctx)
67-
require.NoError(t, err)
68-
69-
client := serverpb.NewStatusClient(conn)
70-
62+
client := tenant.GetStatusClient(t)
7163
resp, err := client.Statements(ctx, &serverpb.StatementsRequest{NodeID: "local"})
7264
require.NoError(t, err)
7365
require.NotEmpty(t, resp.Statements)

pkg/cli/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ go_test(
378378
"//pkg/kv/kvserver/loqrecovery/loqrecoverypb",
379379
"//pkg/kv/kvserver/stateloader",
380380
"//pkg/roachpb",
381-
"//pkg/rpc",
382381
"//pkg/security/securityassets",
383382
"//pkg/security/securitytest",
384383
"//pkg/security/username",
@@ -405,6 +404,7 @@ go_test(
405404
"//pkg/util/log",
406405
"//pkg/util/log/logconfig",
407406
"//pkg/util/log/logpb",
407+
"//pkg/util/netutil/addr",
408408
"//pkg/util/protoutil",
409409
"//pkg/util/randutil",
410410
"//pkg/util/stop",

pkg/cli/debug_recover_loss_of_quorum_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/loqrecovery"
2727
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/loqrecovery/loqrecoverypb"
2828
"github.com/cockroachdb/cockroach/pkg/roachpb"
29-
"github.com/cockroachdb/cockroach/pkg/rpc"
3029
"github.com/cockroachdb/cockroach/pkg/server"
3130
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
3231
"github.com/cockroachdb/cockroach/pkg/testutils"
@@ -258,10 +257,7 @@ func TestLossOfQuorumRecovery(t *testing.T) {
258257
// attempt. That would increase number of replicas on system ranges to 5 and we
259258
// would not be able to upreplicate properly. So we need to decommission old nodes
260259
// first before proceeding.
261-
grpcConn, err := tcAfter.Server(0).RPCContext().GRPCDialNode(
262-
tcAfter.Server(0).AdvRPCAddr(), tcAfter.Server(0).NodeID(), rpc.DefaultClass).Connect(ctx)
263-
require.NoError(t, err, "Failed to create test cluster after recovery")
264-
adminClient := serverpb.NewAdminClient(grpcConn)
260+
adminClient := tcAfter.Server(0).GetAdminClient(t)
265261

266262
require.NoError(t, runDecommissionNodeImpl(
267263
ctx, adminClient, nodeDecommissionWaitNone, nodeDecommissionChecksSkip, false,
@@ -353,10 +349,7 @@ func TestStageVersionCheck(t *testing.T) {
353349
defer tc.Stopper().Stop(ctx)
354350
tc.StopServer(3)
355351

356-
grpcConn, err := tc.Server(0).RPCContext().GRPCDialNode(tc.Server(0).AdvRPCAddr(),
357-
tc.Server(0).NodeID(), rpc.DefaultClass).Connect(ctx)
358-
require.NoError(t, err, "Failed to create test cluster after recovery")
359-
adminClient := serverpb.NewAdminClient(grpcConn)
352+
adminClient := tc.Server(0).GetAdminClient(t)
360353
v := clusterversion.ByKey(clusterversion.BinaryVersionKey)
361354
v.Internal++
362355
// To avoid crafting real replicas we use StaleLeaseholderNodeIDs to force
@@ -369,7 +362,7 @@ func TestStageVersionCheck(t *testing.T) {
369362
StaleLeaseholderNodeIDs: []roachpb.NodeID{1},
370363
}
371364
// Attempts to stage plan with different internal version must fail.
372-
_, err = adminClient.RecoveryStagePlan(ctx, &serverpb.RecoveryStagePlanRequest{
365+
_, err := adminClient.RecoveryStagePlan(ctx, &serverpb.RecoveryStagePlanRequest{
373366
Plan: &p,
374367
AllNodes: true,
375368
ForcePlan: false,
@@ -559,8 +552,7 @@ func TestHalfOnlineLossOfQuorumRecovery(t *testing.T) {
559552

560553
// Verifying that post start cleanup performed node decommissioning that
561554
// prevents old nodes from rejoining.
562-
ac, err := tc.GetAdminClient(ctx, t, 0)
563-
require.NoError(t, err, "failed to get admin client")
555+
ac := tc.GetAdminClient(t, 0)
564556
testutils.SucceedsSoon(t, func() error {
565557
dr, err := ac.DecommissionStatus(ctx,
566558
&serverpb.DecommissionStatusRequest{NodeIDs: []roachpb.NodeID{2, 3}})

pkg/cli/debug_reset_quorum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func runDebugResetQuorum(cmd *cobra.Command, args []string) error {
5050
}
5151

5252
// Set up GRPC Connection for running ResetQuorum.
53-
cc, _, finish, err := getClientGRPCConn(ctx, serverCfg)
53+
cc, finish, err := getClientGRPCConn(ctx, serverCfg)
5454
if err != nil {
5555
log.Errorf(ctx, "connection to server failed: %v", err)
5656
return err

0 commit comments

Comments
 (0)