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

stats/opentelemetry: add trace event for name resolution delay #8074

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

vinothkumarr227
Copy link
Contributor

@vinothkumarr227 vinothkumarr227 commented Feb 10, 2025

stats/opentelemetry: add trace event for name resolution delay.

RELEASE NOTES: None

Copy link

codecov bot commented Feb 10, 2025

Codecov Report

Attention: Patch coverage is 77.41935% with 7 lines in your changes missing coverage. Please review.

Project coverage is 82.20%. Comparing base (e0d191d) to head (0a7df7e).
Report is 25 commits behind head on master.

Files with missing lines Patch % Lines
clientconn.go 50.00% 3 Missing and 1 partial ⚠️
stats/opentelemetry/trace.go 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8074      +/-   ##
==========================================
- Coverage   82.29%   82.20%   -0.10%     
==========================================
  Files         387      387              
  Lines       39065    38953     -112     
==========================================
- Hits        32150    32020     -130     
- Misses       5584     5608      +24     
+ Partials     1331     1325       -6     
Files with missing lines Coverage Δ
stats/opentelemetry/client_metrics.go 89.44% <100.00%> (+0.05%) ⬆️
stats/opentelemetry/opentelemetry.go 75.00% <ø> (ø)
stream.go 81.55% <100.00%> (-0.14%) ⬇️
stats/opentelemetry/trace.go 78.43% <0.00%> (-4.91%) ⬇️
clientconn.go 90.55% <50.00%> (-1.63%) ⬇️

... and 47 files with indirect coverage changes

@arjan-bal arjan-bal requested a review from purnesh42H February 17, 2025 07:16
@arjan-bal arjan-bal added this to the 1.71 Release milestone Feb 17, 2025
@arjan-bal arjan-bal added Type: Feature New features or improvements in behavior Area: Observability Includes Stats, Tracing, Channelz, Healthz, Binlog, Reflection, Admin, GCP Observability labels Feb 17, 2025
clientconn.go Outdated
func (cc *ClientConn) waitForResolvedAddrs(ctx context.Context) error {
// context expires, whichever happens first.
//
// If the name resolution took longer than expected (indicating a delay before
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: If the name resolution did not succeed in first attempt, it returns true indicating delay in name resolution completion. In all other cases, including name resolution failure and name resolution succeeding in first attempt, it returns false.

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

@@ -38,6 +38,8 @@ type RPCTagInfo struct {
// FailFast indicates if this RPC is failfast.
// This field is only valid on client side, it's always false on server side.
FailFast bool
// NameResolutionDelay indicates if the RPC was delayed due to address resolution.
Copy link
Contributor

Choose a reason for hiding this comment

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

// NameResolutionDelay indicates if there was a delay in the name resolution.
// This field is only valid on client side, it's always false on server side.

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

@@ -211,6 +211,8 @@ type attemptInfo struct {
countSentMsg uint32
countRecvMsg uint32
previousRPCAttempts uint32
// nameResolutionDelayed indicates if the RPC was delayed by address resolution.
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above about docstring

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

stream.go Outdated
@@ -212,9 +216,13 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
}
// Provide an opportunity for the first RPC to see the first service config
// provided by the resolver.
if err := cc.waitForResolvedAddrs(ctx); err != nil {
isDelayed, err := cc.waitForResolvedAddrs(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: isDelayed -> nameResDelayed/nameResolutionDelayed

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

stream.go Outdated
return nil, err
}
if isDelayed {
Copy link
Contributor

Choose a reason for hiding this comment

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

i think its fine to always add the nameResolutionDelay key. It will be either true/false. That we don't have to check again and again.

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

stream.go Outdated
@@ -416,8 +424,9 @@ func (cs *clientStream) newAttemptLocked(isTransparent bool) (*csAttempt, error)
method := cs.callHdr.Method
var beginTime time.Time
shs := cs.cc.dopts.copts.StatsHandlers
isDelayed, _ := ctx.Value(nameResolutionDelayKey).(bool)
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment about naming the variable as above

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

stream.go Outdated
return nil, err
}
if isDelayed {
ctx = context.WithValue(ctx, nameResolutionDelayKey, isDelayed)
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of adding it to context, can't we add to the rpcInfo below at line 233? and then we can pass that rpcInfo to newClientStreamWithParams. We can also just pass the bool value

Adding something less scoped like this to context has several downsides like context pollution, ambiguity, potential for misuse. It is because context is passed to different parts of application or even beyond current application

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion. I’ve updated the code to move nameResolutionDelay to rpcInfo as recommended. rpcInfo is now passed to newClientStreamWithParams,

stream.go Outdated
@@ -212,9 +216,13 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
}
// Provide an opportunity for the first RPC to see the first service config
// provided by the resolver.
if err := cc.waitForResolvedAddrs(ctx); err != nil {
isDelayed, err := cc.waitForResolvedAddrs(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

returning a bool and error is not good practice for go. It breaks the established pattern of error handling in Go because returned bool indicates success/failure in general. Can we do something better? It might be fine if we can't but we can try to look for better ways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have an approach as follows:

Add a nameResolutionDelay field: Add a new nameResolutionDelay field to the ClientConn struct to store the delay state.
Modify waitForResolvedAddrs: Set the nameResolutionDelay field directly in ClientConn instead of returning a boolean.
Access in newAttemptLocked: Use the nameResolutionDelay field from ClientConn within newAttemptLocked.

Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think we want to add the field to clientconn because clientconn is not restricted to only single rpc. Returning a struct sounds better but we don't have any other field apart from boolean field. Let's keep bool, err for now. But make sure docstring is updated to indicate the bool correctly.

@purnesh42H
Copy link
Contributor

just to give you more context

  • The firstResolveEvent is a grpcsync.Event (a custom synchronization primitive). It's used to signal whether the name resolver has sent at least one address update. HasFired() is a method on this event that tells you if the signal has already been sent.
  • If firstResolveEvent.HasFired() returns true, it means the resolver has already provided addresses at least once. In this case, there is no need to wait, so we can immediately return nil to signal that resolution has already occurred in the past. This is the fast path indicating no delay in name resolution
  • case <-cc.firstResolveEvent.Done() is part of a select statement. It's waiting for a signal on the cc.firstResolveEvent.Done() channel. The select statement will block here until something is sent on the firstResolveEvent.Done() channel. When something is sent on this channel, it means the firstResolveEvent has been fired. This is the waiting path. If cc.firstResolveEvent.HasFired() was false in the previous step, the code enters the select block. This case is specifically waiting for the resolver to send the first address update. Until then, it will block. So, its indicated delayed name resolution because rpc was blocked

@purnesh42H
Copy link
Contributor

Test Case 1: Fast Path (Line 699)

Setup:
Create a ClientConn with a manual resolver that immediately returns a set of addresses.
Make one RPC call (or call a function that uses waitForResolvedAddrs).
Verify:
The first RPC call should pass without blocking. Because the resolver returned addresses quickly.
Make another RPC call
When the second RPC call happens, the firstResolveEvent will already be fired.

Test Case 2: Waiting Path (Line 703)

Setup:
Create a ClientConn with a manual resolver that delays returning any addresses.
Start an RPC call (or call a function that uses waitForResolvedAddrs).
The RPC should block.
Verify:
The RPC should block (we can use a channel mechanism to block/unblock. There are examples in resolver tests).
After a timeout or a manual trigger, make the manual resolver return addresses.
The blocked RPC should now continue and complete.

@vinothkumarr227
Copy link
Contributor Author

Test Case 1: Fast Path (Line 699)

Setup: Create a ClientConn with a manual resolver that immediately returns a set of addresses. Make one RPC call (or call a function that uses waitForResolvedAddrs). Verify: The first RPC call should pass without blocking. Because the resolver returned addresses quickly. Make another RPC call When the second RPC call happens, the firstResolveEvent will already be fired.

Test Case 2: Waiting Path (Line 703)

Setup: Create a ClientConn with a manual resolver that delays returning any addresses. Start an RPC call (or call a function that uses waitForResolvedAddrs). The RPC should block. Verify: The RPC should block (we can use a channel mechanism to block/unblock. There are examples in resolver tests). After a timeout or a manual trigger, make the manual resolver return addresses. The blocked RPC should now continue and complete.

Done

clientconn.go Outdated
func (cc *ClientConn) waitForResolvedAddrs(ctx context.Context) error {
// context expires, whichever happens first.
//
// If the name resolution did not succeed in first attempt, it returns true
Copy link
Contributor

Choose a reason for hiding this comment

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

This statement is not entirely correct. Please modify this based on my other comment where I provided more background. If firstResolveEvent.HasFired() returns true, it means the resolver has already provided addresses at least once. In this case, there is no need to wait, so we can immediately return nil to signal that resolution has already occurred in the past. This is the fast path indicating no delay in name resolution.

Copy link
Contributor

Choose a reason for hiding this comment

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

other case is specifically waiting for the resolver to send the first address update. Until then, it will block. So, its indicated delayed name resolution.

Please modify the docstring accordingly and mention when we return true and when not. Your first statement should mention that the bool return value indicate whether there wait for resolved address happened or not. If it didn't happen or there was an error, its false. It we were blocked for name resolution then it happened, so value is true.

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

stream.go Outdated
var mc serviceconfig.MethodConfig
var onCommit func()
newStream := func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
return newClientStreamWithParams(ctx, desc, cc, method, mc, onCommit, done, opts...)
return newClientStreamWithParams(ctx, desc, cc, method, mc, onCommit, done, rpcInfo, opts...)
Copy link
Contributor

Choose a reason for hiding this comment

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

what if we just send the bool value instead of struct? Does it work? If yes, then that's more simple.

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

@@ -53,7 +55,7 @@ func (s) TestClientConnClose_WithPendingRPC(t *testing.T) {
go func() {
// This RPC would block until the ClientConn is closed, because the
// resolver has not provided its first update yet.
_, err := client.EmptyCall(ctx, &testpb.Empty{})
Copy link
Contributor

Choose a reason for hiding this comment

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

keep it testpb please. Don't change this. This workaround is done only for google3 because there protos are separated from server defs.

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


// EmptyCall is a simple RPC that returns an empty response.
func (s *server) EmptyCall(_ context.Context, _ *testgrpc.Empty) (*testgrpc.Empty, error) {
return &testgrpc.Empty{}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

same. It should be testpb.Empty{}

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


// startTestGRPCServer initializes a test gRPC server on a random port.
// Returns the server address and a cleanup function.
func startTestGRPCServer(t *testing.T) (string, func()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use stubserver instead? Please see the other tests in the package for stubserver and use in the same for empty call

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

client := testgrpc.NewTestServiceClient(clientConn)

// First RPC call should succeed immediately
if _, err := client.EmptyCall(ctx, &testgrpc.Empty{}); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't verify if name resolution field is set or not. We should have some way of verifying the nameResolutionDelay field.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think if you pass the statsHandler in grpc.NewClient then you can check the value of the bool field that you are setting

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 cleanup()

// Create a stats handler to track name resolution delay
statsHandler := &testStatsHandler{}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can do the same for non delay test as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this one for the non-delay test as well

}
t.Log("Second RPC succeeded after resolver update, confirming resolution completion.")

if !statsHandler.nameResolutionDelayed {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should do the same in non delay case. It will be reverse of this there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this one for the non-delay test as well

}()

// Simulate a delay before updating resolver
time.Sleep(2 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not use time.Sleep. You should use the resolutionReady channel to unblock resolution

Copy link
Contributor

Choose a reason for hiding this comment

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

Refer this test https://github.com/grpc/grpc-go/blob/master/internal/resolver/dns/dns_resolver_test.go#L641. I think you can structure this test in similar way.

Block on resolutionReady in a select block and for case <- resolutionReady:, update resolver state to unblock. Is there a way we can make the first RPC wait for resolution instead of failing?

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

resolverBuilder.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: serverAddress}}})

// Second RPC should succeed after resolver update
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

please use single context throughout test which is cancelable.

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 cancel()
client := testgrpc.NewTestServiceClient(clientConn)
// First RPC call should succeed immediately.
if _, err := client.EmptyCall(ctx, &testgrpc.Empty{}); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be a testpb.Empty{}

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 cleanup()

statsHandler := &testStatsHandler{}
resolverBuilder := manual.NewBuilderWithScheme("instant")
Copy link
Contributor

Choose a reason for hiding this comment

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

keep it simple like rb := manual.NewBuilderWithScheme("")

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

resolverBuilder := manual.NewBuilderWithScheme("instant")
resolverBuilder.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: stub.Address}}})
// Create a ClientConn using the manual resolver.
clientConn, err := grpc.NewClient(resolverBuilder.Scheme()+":///test.server",
Copy link
Contributor

Choose a reason for hiding this comment

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

keep it simple like cc, err := grpc.NewClient(resolverBuilder.Scheme()+":///test.server",

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

close(resolutionReady)
case <-rpcCompleted:
t.Fatal("RPC completed prematurely before resolution was updated!")
case <-time.After(5 * time.Second):
Copy link
Contributor

Choose a reason for hiding this comment

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

Use test context for timeout

case <-ctx.Done():
       t.Fatalf("Test setup timed out: %v", ctx.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

t.Fatalf("RPC failed after resolution: %v", err)
}
t.Log("RPC completed successfully after resolution.")
case <-time.After(5 * time.Second):
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, use test context for timeout

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

grpc.WithStatsHandler(statsHandler),
)
if err != nil {
t.Fatalf("grpc.NewClient error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be t.Fatalf("NewClient() failed: %v", err)

defer cleanup()

statsHandler := &testStatsHandler{}
clientConn, resolverBuilder := createTestClient(t, "delayed", statsHandler)
Copy link
Contributor

@janardhanvissa janardhanvissa Feb 25, 2025

Choose a reason for hiding this comment

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

this can be cc, rb := createTestClient(t, "delayed", statsHandler)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Observability Includes Stats, Tracing, Channelz, Healthz, Binlog, Reflection, Admin, GCP Observability Type: Feature New features or improvements in behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants