Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: replace dial with newclient #7920

Merged
merged 18 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions balancer/grpclb/grpclb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (s) TestGRPCLB_Basic(t *testing.T) {
}
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "... create a client for the backend" everywhere, please.

OR, ("grpc.NewClient(%q) failed unexpectedly: %v", target, err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
defer cc.Close()

Expand Down Expand Up @@ -517,7 +517,7 @@ func (s) TestGRPCLB_Weighted(t *testing.T) {
}
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
defer cc.Close()

Expand Down Expand Up @@ -597,7 +597,7 @@ func (s) TestGRPCLB_DropRequest(t *testing.T) {
}
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
defer cc.Close()
testC := testgrpc.NewTestServiceClient(cc)
Expand Down Expand Up @@ -769,7 +769,7 @@ func (s) TestGRPCLB_BalancerDisconnects(t *testing.T) {
}
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
defer cc.Close()
testC := testgrpc.NewTestServiceClient(cc)
Expand Down Expand Up @@ -940,7 +940,7 @@ func (s) TestGRPCLB_ExplicitFallback(t *testing.T) {
}
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
defer cc.Close()
testC := testgrpc.NewTestServiceClient(cc)
Expand Down Expand Up @@ -1008,23 +1008,24 @@ func (s) TestGRPCLB_FallBackWithNoServerAddress(t *testing.T) {
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
grpc.WithContextDialer(fakeNameDialer),
}
cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, dopts...)
// Initialize the resolver state with the backend address.
r.InitialState(resolver.State{
Addresses: []resolver.Address{{Addr: beLis.Addr().String()}},
ServiceConfig: r.CC.ParseServiceConfig(grpclbConfig),
})

cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend: %v", err)
}
defer cc.Close()
testC := testgrpc.NewTestServiceClient(cc)

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
for i := 0; i < 2; i++ {
// Send an update with only backend address. grpclb should enter
// fallback and use the fallback backend.
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: beLis.Addr().String()}},
ServiceConfig: r.CC.ParseServiceConfig(grpclbConfig),
})
arjan-bal marked this conversation as resolved.
Show resolved Hide resolved

for i := 0; i < 2; i++ {
// Ensure the client is using the fallback backend.
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if _, err := resolveNowCh.Receive(sCtx); err != context.DeadlineExceeded {
Expand Down Expand Up @@ -1102,10 +1103,11 @@ func (s) TestGRPCLB_PickFirst(t *testing.T) {
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
grpc.WithContextDialer(fakeNameDialer),
}
cc, err := grpc.Dial(r.Scheme()+":///"+beServerName, dopts...)
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient: %v", err)
}
cc.Connect()
arjan-bal marked this conversation as resolved.
Show resolved Hide resolved
defer cc.Close()

// Push a service config with grpclb as the load balancing policy and
Expand Down Expand Up @@ -1241,10 +1243,11 @@ func testGRPCLBEmptyServerList(t *testing.T, svcfg string) {
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
grpc.WithContextDialer(fakeNameDialer),
}
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, dopts...)
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
cc.Connect()
arjan-bal marked this conversation as resolved.
Show resolved Hide resolved
defer cc.Close()
testC := testgrpc.NewTestServiceClient(cc)

Expand Down Expand Up @@ -1311,15 +1314,16 @@ func (s) TestGRPCLBWithTargetNameFieldInConfig(t *testing.T) {
// Push the backend address to the remote balancer.
tss.ls.sls <- sl

cc, err := grpc.Dial(r.Scheme()+":///"+beServerName,
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName,
grpc.WithResolvers(r),
grpc.WithTransportCredentials(&serverNameCheckCreds{}),
grpc.WithContextDialer(fakeNameDialer),
grpc.WithUserAgent(testUserAgent))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
defer cc.Close()
cc.Connect()
testC := testgrpc.NewTestServiceClient(cc)

// Push a resolver update with grpclb configuration which does not contain the
Expand Down Expand Up @@ -1418,15 +1422,14 @@ func runAndCheckStats(t *testing.T, drop bool, statsChan chan *lbpb.ClientStats,
tss.ls.statsDura = 100 * time.Millisecond
creds := serverNameCheckCreds{}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds),
grpc.WithPerRPCCredentials(failPreRPCCred{}),
grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
t.Fatalf("Failed to create a newclient to the backend %v", err)
}
cc.Connect()
arjan-bal marked this conversation as resolved.
Show resolved Hide resolved
defer cc.Close()

rstate := resolver.State{ServiceConfig: r.CC.ParseServiceConfig(grpclbConfig)}
Expand Down
12 changes: 6 additions & 6 deletions balancer/rls/picker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ func (s) Test_RLSDefaultTargetPicksMetric(t *testing.T) {
r := startManualResolverWithConfig(t, rlsConfig)

tmr := stats.NewTestMetricsRecorder()
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down Expand Up @@ -314,9 +314,9 @@ func (s) Test_RLSTargetPicksMetric(t *testing.T) {

tmr := stats.NewTestMetricsRecorder()
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down Expand Up @@ -352,9 +352,9 @@ func (s) Test_RLSFailedPicksMetric(t *testing.T) {

tmr := stats.NewTestMetricsRecorder()
// Dial the backend.
cc, err := grpc.Dial(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(tmr))
if err != nil {
t.Fatalf("grpc.Dial() failed: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down
10 changes: 6 additions & 4 deletions resolver_balancer_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func (s) TestResolverBalancerInteraction(t *testing.T) {
rb.ResolveNowCallback = func(resolver.ResolveNowOptions) { close(rnCh) }
resolver.Register(rb)

cc, err := grpc.Dial(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial error: %v", err)
t.Fatalf("grpc.NewClient error: %v", err)
}
defer cc.Close()
cc.Connect()
select {
case <-rnCh:
case <-time.After(defaultTestTimeout):
Expand Down Expand Up @@ -109,11 +110,12 @@ func (s) TestResolverBuildFailure(t *testing.T) {
resolver.Register(&resolverBuilderWithErr{errCh: resErrCh, scheme: name})

resErrCh <- nil
cc, err := grpc.Dial(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := grpc.NewClient(name+":///", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.Dial error: %v", err)
t.Fatalf("grpc.NewClient error: %v", err)
}
defer cc.Close()
cc.Connect()
enterIdle(cc)
const errStr = "test error from resolver builder"
t.Log("pushing res err")
Expand Down
27 changes: 14 additions & 13 deletions test/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ func (s) TestFailFastRPCErrorOnBadCertificates(t *testing.T) {
opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
cc, err := grpc.NewClient(te.srvAddr, opts...)
if err != nil {
t.Fatalf("Dial(_) = %v, want %v", err, nil)
t.Fatalf("NewClient(_) = %v, want %v", err, nil)
}
defer cc.Close()

Expand All @@ -262,16 +262,14 @@ func (s) TestWaitForReadyRPCErrorOnBadCertificates(t *testing.T) {
defer te.tearDown()

opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
cc, err := grpc.NewClient(te.srvAddr, opts...)
if err != nil {
t.Fatalf("Dial(_) = %v, want %v", err, nil)
t.Fatalf("NewClient(_) = %v, want %v", err, nil)
}
defer cc.Close()

tc := testgrpc.NewTestServiceClient(cc)
ctx, cancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer cancel()
if _, err = tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); !strings.Contains(err.Error(), clientAlwaysFailCredErrorMsg) {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want err.Error() contains %q", err, clientAlwaysFailCredErrorMsg)
Expand Down Expand Up @@ -437,10 +435,11 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) {

r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
}
cc.Connect()
defer cc.Close()
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})

Expand Down Expand Up @@ -469,10 +468,11 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) {

r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
cc, err := grpc.NewClient(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
}
cc.Connect()
defer cc.Close()
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String(), ServerName: testServerName}}})

Expand Down Expand Up @@ -524,10 +524,11 @@ func (s) TestServerCredsDispatch(t *testing.T) {
go s.Serve(lis)
defer s.Stop()

cc, err := grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(cred))
cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(cred))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
}
cc.Connect()
defer cc.Close()

rawConn := cred.getRawConn()
Expand Down
2 changes: 1 addition & 1 deletion test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (te *test) listenAndServe(ts testgrpc.TestServiceServer, listen func(networ
if te.serverInitialConnWindowSize > 0 {
sopts = append(sopts, grpc.InitialConnWindowSize(te.serverInitialConnWindowSize))
}
la := "localhost:0"
la := "[::]:0"
Copy link
Contributor

@arjan-bal arjan-bal Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dfawley this is required to fix test failures with the new pickfirst. localhost is being resolved to both IPv4 and IPv6 addresses on Github, while net.Listen("tcp", "localhost") is listening only on the IPv4 address. The resolver list has the IPv4 address followed by the IPv6 one. The old pickfirst reports the first connectivity error while the new pickfirst reports the latest error.

Using [::] makes the listener bind to all (possibly non-local) interfaces. Is this a concern? We could instead update the test to send "127.0.0.1" through the name resolver.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid net.Listen("localhost"), and use net.Listen(":port") instead. That will make it listen on both ipv4 and ipv6 addresses.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janardhanvissa can you please change this to ":0"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

switch te.e.network {
case "unix":
la = "/tmp/testsock" + fmt.Sprintf("%d", time.Now().UnixNano())
Expand Down
Loading