Skip to content

Commit fc3187a

Browse files
cli,testutils,ts: consolidate RPC client creation
This commit consolidates RPC client creation logic in testserver code paths in cli, testutils, and ts packages. It is a continuation of the work done in #147606. Epic: CRDB-48923 Fixes: #148158 Release note: none
1 parent 9aa122b commit fc3187a

File tree

10 files changed

+67
-35
lines changed

10 files changed

+67
-35
lines changed

pkg/cli/democluster/demo_cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ func (c *transientCluster) waitForNodeIDReadiness(
795795
if err != nil {
796796
return err
797797
}
798-
c.servers[idx].adminClient = serverpb.NewAdminClient(conn)
798+
c.servers[idx].adminClient = conn.NewAdminClient()
799799

800800
}
801801
break
@@ -1219,7 +1219,7 @@ func (c *transientCluster) startServerInternal(
12191219

12201220
c.servers[serverIdx] = serverEntry{
12211221
TestServerInterface: s,
1222-
adminClient: serverpb.NewAdminClient(conn),
1222+
adminClient: conn.NewAdminClient(),
12231223
nodeID: nodeID,
12241224
}
12251225

pkg/cli/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func generateMetricList(ctx context.Context, skipFiltering bool) (map[string]*La
407407
if err != nil {
408408
return err
409409
}
410-
client := serverpb.NewAdminClient(conn)
410+
client := conn.NewAdminClient()
411411

412412
resp, err := client.ChartCatalog(ctx, &serverpb.ChartCatalogRequest{})
413413
if err != nil {

pkg/server/server_import_ts_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/server"
1717
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
1818
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
19+
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
1920
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
2021
"github.com/cockroachdb/cockroach/pkg/ts"
2122
"github.com/cockroachdb/cockroach/pkg/ts/tspb"
@@ -24,7 +25,6 @@ import (
2425
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
2526
"github.com/cockroachdb/cockroach/pkg/util/log"
2627
"github.com/stretchr/testify/require"
27-
"google.golang.org/grpc"
2828
)
2929

3030
// TestServerWithTimeseriesImport validates the functionality gated behind the
@@ -73,11 +73,11 @@ func TestServerWithTimeseriesImport(t *testing.T) {
7373
require.Equal(t, bytesDumped, bytesDumpedAgain)
7474
}
7575

76-
func dumpTSNonempty(t *testing.T, cc *grpc.ClientConn, dest string) (bytes int64) {
77-
ac := serverpb.NewAdminClient(cc)
76+
func dumpTSNonempty(t *testing.T, cc serverutils.RPCConn, dest string) (bytes int64) {
77+
ac := cc.NewAdminClient()
7878
names, err := serverpb.GetInternalTimeseriesNamesFromServer(context.Background(), ac)
7979
require.NoError(t, err)
80-
c, err := tspb.NewTimeSeriesClient(cc).DumpRaw(context.Background(), &tspb.DumpRequest{
80+
c, err := cc.NewTimeSeriesClient().DumpRaw(context.Background(), &tspb.DumpRequest{
8181
Names: names,
8282
})
8383
require.NoError(t, err)

pkg/server/testserver.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ import (
7676
"github.com/cockroachdb/logtags"
7777
"github.com/cockroachdb/redact"
7878
"github.com/gogo/protobuf/proto"
79-
"google.golang.org/grpc"
8079
)
8180

8281
// makeTestConfig returns a config for testing. It overrides the
@@ -2621,7 +2620,7 @@ func (ts *testServer) NewClientRPCContext(
26212620
// RPCClientConn is part of the serverutils.ApplicationLayerInterface.
26222621
func (ts *testServer) RPCClientConn(
26232622
test serverutils.TestFataler, user username.SQLUsername,
2624-
) *grpc.ClientConn {
2623+
) serverutils.RPCConn {
26252624
conn, err := ts.RPCClientConnE(user)
26262625
if err != nil {
26272626
test.Fatal(err)
@@ -2630,22 +2629,26 @@ func (ts *testServer) RPCClientConn(
26302629
}
26312630

26322631
// RPCClientConnE is part of the serverutils.ApplicationLayerInterface.
2633-
func (ts *testServer) RPCClientConnE(user username.SQLUsername) (*grpc.ClientConn, error) {
2632+
func (ts *testServer) RPCClientConnE(user username.SQLUsername) (serverutils.RPCConn, error) {
26342633
ctx := context.Background()
26352634
rpcCtx := ts.NewClientRPCContext(ctx, user)
2636-
return rpcCtx.GRPCDialNode(ts.AdvRPCAddr(), ts.NodeID(), ts.Locality(), rpcbase.DefaultClass).Connect(ctx)
2635+
conn, err := rpcCtx.GRPCDialNode(ts.AdvRPCAddr(), ts.NodeID(), ts.Locality(), rpcbase.DefaultClass).Connect(ctx)
2636+
if err != nil {
2637+
return nil, err
2638+
}
2639+
return serverutils.FromGRPCConn(conn), nil
26372640
}
26382641

26392642
// GetAdminClient is part of the serverutils.ApplicationLayerInterface.
26402643
func (ts *testServer) GetAdminClient(test serverutils.TestFataler) serverpb.AdminClient {
26412644
conn := ts.RPCClientConn(test, username.RootUserName())
2642-
return serverpb.NewAdminClient(conn)
2645+
return conn.NewAdminClient()
26432646
}
26442647

26452648
// GetStatusClient is part of the serverutils.ApplicationLayerInterface.
26462649
func (ts *testServer) GetStatusClient(test serverutils.TestFataler) serverpb.StatusClient {
26472650
conn := ts.RPCClientConn(test, username.RootUserName())
2648-
return serverpb.NewStatusClient(conn)
2651+
return conn.NewStatusClient()
26492652
}
26502653

26512654
// NewClientRPCContext is part of the serverutils.ApplicationLayerInterface.
@@ -2662,7 +2665,7 @@ func (t *testTenant) NewClientRPCContext(
26622665
// RPCClientConn is part of the serverutils.ApplicationLayerInterface.
26632666
func (t *testTenant) RPCClientConn(
26642667
test serverutils.TestFataler, user username.SQLUsername,
2665-
) *grpc.ClientConn {
2668+
) serverutils.RPCConn {
26662669
conn, err := t.RPCClientConnE(user)
26672670
if err != nil {
26682671
test.Fatal(err)
@@ -2671,22 +2674,29 @@ func (t *testTenant) RPCClientConn(
26712674
}
26722675

26732676
// RPCClientConnE is part of the serverutils.ApplicationLayerInterface.
2674-
func (t *testTenant) RPCClientConnE(user username.SQLUsername) (*grpc.ClientConn, error) {
2677+
func (t *testTenant) RPCClientConnE(user username.SQLUsername) (serverutils.RPCConn, error) {
26752678
ctx := context.Background()
26762679
rpcCtx := t.NewClientRPCContext(ctx, user)
2677-
return rpcCtx.GRPCDialPod(t.AdvRPCAddr(), t.SQLInstanceID(), t.Locality(), rpcbase.DefaultClass).Connect(ctx)
2680+
if !rpcbase.TODODRPC {
2681+
conn, err := rpcCtx.GRPCDialPod(t.AdvRPCAddr(), t.SQLInstanceID(), t.Locality(), rpcbase.DefaultClass).Connect(ctx)
2682+
if err != nil {
2683+
return nil, err
2684+
}
2685+
return serverutils.FromGRPCConn(conn), nil
2686+
}
2687+
return nil, nil
26782688
}
26792689

26802690
// GetAdminClient is part of the serverutils.ApplicationLayerInterface.
26812691
func (t *testTenant) GetAdminClient(test serverutils.TestFataler) serverpb.AdminClient {
26822692
conn := t.RPCClientConn(test, username.RootUserName())
2683-
return serverpb.NewAdminClient(conn)
2693+
return conn.NewAdminClient()
26842694
}
26852695

26862696
// GetStatusClient is part of the serverutils.ApplicationLayerInterface.
26872697
func (t *testTenant) GetStatusClient(test serverutils.TestFataler) serverpb.StatusClient {
26882698
conn := t.RPCClientConn(test, username.RootUserName())
2689-
return serverpb.NewStatusClient(conn)
2699+
return conn.NewStatusClient()
26902700
}
26912701

26922702
func newClientRPCContext(

pkg/sql/flowinfra/cluster_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func TestClusterFlow(t *testing.T) {
269269
servers[i] = s
270270
conns[i] = s.SQLConn(t)
271271
conn := s.RPCClientConn(t, username.RootUserName())
272-
clients[i] = execinfrapb.NewDistSQLClient(conn)
272+
clients[i] = conn.NewDistSQLClient()
273273
}
274274

275275
runTestClusterFlow(t, servers, conns, clients)
@@ -776,7 +776,7 @@ func BenchmarkInfrastructure(b *testing.B) {
776776
for i := 0; i < numNodes; i++ {
777777
s := tc.Server(i)
778778
conn := s.RPCClientConn(b, username.RootUserName())
779-
clients = append(clients, execinfrapb.NewDistSQLClient(conn))
779+
clients = append(clients, conn.NewDistSQLClient())
780780
}
781781

782782
b.ResetTimer()

pkg/sql/flowinfra/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestServer(t *testing.T) {
119119
t.Run(fmt.Sprintf("%d", tc.version), func(t *testing.T) {
120120
req := *req
121121
req.Version = tc.version
122-
distSQLClient := execinfrapb.NewDistSQLClient(conn)
122+
distSQLClient := conn.NewDistSQLClient()
123123
resp, err := distSQLClient.SetupFlow(ctx, &req)
124124
if err == nil && resp.Error != nil {
125125
err = resp.Error.ErrorDetail(ctx)

pkg/testutils/serverutils/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
"api.go",
77
"conditional_wrap.go",
88
"options.go",
9+
"rpc_conn.go",
910
"test_cluster_shim.go",
1011
"test_cluster_utils.go",
1112
"test_server_shim.go",
@@ -23,6 +24,7 @@ go_library(
2324
"//pkg/config/zonepb",
2425
"//pkg/keys",
2526
"//pkg/kv",
27+
"//pkg/kv/kvpb",
2628
"//pkg/kv/kvprober",
2729
"//pkg/kv/kvserver/liveness/livenesspb",
2830
"//pkg/multitenant/tenantcapabilities",
@@ -37,9 +39,11 @@ go_library(
3739
"//pkg/settings/cluster",
3840
"//pkg/sql/catalog",
3941
"//pkg/sql/catalog/descpb",
42+
"//pkg/sql/execinfrapb",
4043
"//pkg/storage",
4144
"//pkg/testutils/pgurlutils",
4245
"//pkg/testutils/skip",
46+
"//pkg/ts/tspb",
4347
"//pkg/util/envutil",
4448
"//pkg/util/hlc",
4549
"//pkg/util/httputil",

pkg/testutils/serverutils/api.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
2020
"github.com/cockroachdb/cockroach/pkg/keys"
2121
"github.com/cockroachdb/cockroach/pkg/kv"
22+
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
2223
"github.com/cockroachdb/cockroach/pkg/kv/kvprober"
2324
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
2425
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities"
@@ -31,13 +32,14 @@ import (
3132
"github.com/cockroachdb/cockroach/pkg/server/status"
3233
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
3334
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
35+
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
3436
"github.com/cockroachdb/cockroach/pkg/storage"
37+
"github.com/cockroachdb/cockroach/pkg/ts/tspb"
3538
"github.com/cockroachdb/cockroach/pkg/util/hlc"
3639
"github.com/cockroachdb/cockroach/pkg/util/log"
3740
"github.com/cockroachdb/cockroach/pkg/util/stop"
3841
"github.com/cockroachdb/cockroach/pkg/util/tracing"
3942
"github.com/cockroachdb/cockroach/pkg/util/uuid"
40-
"google.golang.org/grpc"
4143
)
4244

4345
// TestServerInterfaceRaw is the interface of server.testServer.
@@ -146,6 +148,18 @@ func (d DeploymentMode) IsExternal() bool {
146148
return d == ExternalProcess
147149
}
148150

151+
// RPCConn defines a common interface for creating RPC clients. It hides the
152+
// underlying RPC connection (gRPC or DRPC), making it easy to swap
153+
// them without changing the caller code.
154+
type RPCConn interface {
155+
NewStatusClient() serverpb.StatusClient
156+
NewAdminClient() serverpb.AdminClient
157+
NewInitClient() serverpb.InitClient
158+
NewTimeSeriesClient() tspb.TimeSeriesClient
159+
NewInternalClient() kvpb.InternalClient
160+
NewDistSQLClient() execinfrapb.DistSQLClient
161+
}
162+
149163
// ApplicationLayerInterface defines accessors to the application
150164
// layer of a test server. Tests written against this interface are
151165
// effectively agnostic to whether they use a virtual cluster or not.
@@ -275,11 +289,11 @@ type ApplicationLayerInterface interface {
275289
NewClientRPCContext(ctx context.Context, userName username.SQLUsername) *rpc.Context
276290

277291
// RPCClientConn opens a RPC client connection to the server.
278-
RPCClientConn(t TestFataler, userName username.SQLUsername) *grpc.ClientConn
292+
RPCClientConn(t TestFataler, userName username.SQLUsername) RPCConn
279293

280294
// RPCClientConnE is like RPCClientConn but it allows the test to check the
281295
// error.
282-
RPCClientConnE(userName username.SQLUsername) (*grpc.ClientConn, error)
296+
RPCClientConnE(userName username.SQLUsername) (RPCConn, error)
283297

284298
// GetAdminClient creates a serverpb.AdminClient connection to the server.
285299
// Shorthand for serverpb.AdminClient(.RPCClientConn(t, "root"))

pkg/testutils/serverutils/rpc_conn.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// Use of this software is governed by the CockroachDB Software License
44
// included in the /LICENSE file.
55

6-
package srvtestutils
6+
package serverutils
77

88
import (
99
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1010
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
11-
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
11+
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
1212
"github.com/cockroachdb/cockroach/pkg/ts/tspb"
1313
"google.golang.org/grpc"
1414
)
@@ -20,7 +20,7 @@ type grpcConn struct {
2020
conn *grpc.ClientConn
2121
}
2222

23-
func FromGRPCConn(conn *grpc.ClientConn) serverutils.RPCConn {
23+
func FromGRPCConn(conn *grpc.ClientConn) RPCConn {
2424
return &grpcConn{conn: conn}
2525
}
2626

@@ -43,3 +43,7 @@ func (c *grpcConn) NewTimeSeriesClient() tspb.TimeSeriesClient {
4343
func (c *grpcConn) NewInternalClient() kvpb.InternalClient {
4444
return kvpb.NewInternalClient(c.conn)
4545
}
46+
47+
func (c *grpcConn) NewDistSQLClient() execinfrapb.DistSQLClient {
48+
return execinfrapb.NewDistSQLClient(c.conn)
49+
}

pkg/ts/server_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestServerQuery(t *testing.T) {
176176
}
177177

178178
conn := s.RPCClientConn(t, username.RootUserName())
179-
client := tspb.NewTimeSeriesClient(conn)
179+
client := conn.NewTimeSeriesClient()
180180
response, err := client.Query(context.Background(), &tspb.TimeSeriesQueryRequest{
181181
StartNanos: 500 * 1e9,
182182
EndNanos: 526 * 1e9,
@@ -272,7 +272,7 @@ func TestServerQueryStarvation(t *testing.T) {
272272
}
273273

274274
conn := s.RPCClientConn(t, username.RootUserName())
275-
client := tspb.NewTimeSeriesClient(conn)
275+
client := conn.NewTimeSeriesClient()
276276

277277
queries := make([]tspb.Query, 0, seriesCount)
278278
for i := 0; i < seriesCount; i++ {
@@ -442,7 +442,7 @@ func TestServerQueryTenant(t *testing.T) {
442442
}
443443

444444
conn := s.RPCClientConn(t, username.RootUserName())
445-
client := tspb.NewTimeSeriesClient(conn)
445+
client := conn.NewTimeSeriesClient()
446446
aggregatedResponse, err := client.Query(context.Background(), &tspb.TimeSeriesQueryRequest{
447447
StartNanos: 400 * 1e9,
448448
EndNanos: 500 * 1e9,
@@ -578,7 +578,7 @@ func TestServerQueryTenant(t *testing.T) {
578578
capability := map[tenantcapabilitiespb.ID]string{tenantcapabilitiespb.CanViewTSDBMetrics: "true"}
579579
serverutils.WaitForTenantCapabilities(t, s, tenantID, capability, "")
580580
tenantConn := tenant.RPCClientConn(t, username.RootUserName())
581-
tenantClient := tspb.NewTimeSeriesClient(tenantConn)
581+
tenantClient := tenantConn.NewTimeSeriesClient()
582582

583583
tenantResponse, err := tenantClient.Query(context.Background(), &tspb.TimeSeriesQueryRequest{
584584
StartNanos: 400 * 1e9,
@@ -696,7 +696,7 @@ func TestServerQueryMemoryManagement(t *testing.T) {
696696
}
697697

698698
conn := s.RPCClientConn(t, username.RootUserName())
699-
client := tspb.NewTimeSeriesClient(conn)
699+
client := conn.NewTimeSeriesClient()
700700

701701
queries := make([]tspb.Query, 0, seriesCount)
702702
for i := 0; i < seriesCount; i++ {
@@ -773,7 +773,7 @@ func TestServerDump(t *testing.T) {
773773
}
774774

775775
conn := s.RPCClientConn(t, username.RootUserName())
776-
client := tspb.NewTimeSeriesClient(conn)
776+
client := conn.NewTimeSeriesClient()
777777

778778
dumpClient, err := client.Dump(ctx, &tspb.DumpRequest{
779779
Names: names,
@@ -869,7 +869,7 @@ func TestServerDump(t *testing.T) {
869869
require.NoError(t, s.DB().Run(ctx, &b))
870870

871871
conn := s.RPCClientConn(t, username.RootUserName())
872-
client := tspb.NewTimeSeriesClient(conn)
872+
client := conn.NewTimeSeriesClient()
873873

874874
dumpClient, err := client.Dump(ctx, &tspb.DumpRequest{
875875
Names: names,
@@ -899,7 +899,7 @@ func BenchmarkServerQuery(b *testing.B) {
899899
}
900900

901901
conn := s.RPCClientConn(b, username.RootUserName())
902-
client := tspb.NewTimeSeriesClient(conn)
902+
client := conn.NewTimeSeriesClient()
903903

904904
queries := make([]tspb.Query, 0, seriesCount)
905905
for i := 0; i < seriesCount; i++ {

0 commit comments

Comments
 (0)