Skip to content

grpc: Move some stats handler callouts from transport to gRPC layer server side #6624

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

Closed
wants to merge 1 commit into from

Conversation

zasweq
Copy link
Contributor

@zasweq zasweq commented Sep 12, 2023

This PR moves the Conn Begin and InHeader calls from the transport layer to gRPC layer. The other three (OutHeader, OutTrailer, ConnEnd) will come in a future PR which will delete the need for the setContext() function. The OutHeader and OutTrailer will require some reworking of the ownership of logic between the layers of gRPC and Transport.

RELEASE NOTES: N/A

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

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

The handler_server also calls TagRPC...so now it will happen 2x. It seems we might need a test for the handler server + stats handlers to make sure the events are being produced properly.

md, _ := metadata.FromIncomingContext(ctx)
for _, sh := range s.opts.statsHandlers {
ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: stream.Method()})
stream.SetContext(ctx) // To have calls in stream callouts work. Will delete once all stats handler calls come from the gRPC layer.
Copy link
Member

Choose a reason for hiding this comment

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

You can move this to after the loop so it only happens once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Done.

@@ -167,6 +167,8 @@ func (ht *serverHandlerTransport) Close(err error) {

func (ht *serverHandlerTransport) RemoteAddr() net.Addr { return strAddr(ht.req.RemoteAddr) }

func (ht *serverHandlerTransport) LocalAddr() net.Addr { return nil }
Copy link
Member

Choose a reason for hiding this comment

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

Can you fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This server handler transport has no access to local addr unfortunately. It was actually calling into stats handler with a nil local addr set in InHeader since it has no access to this. I read LocalAddr() (method I added to server transport) in my new PR, so this will keep the same functionality as in master and not log LocalAddr().

unprocessed uint32 // set if the server sends a refused stream or GOAWAY including this stream
bytesReceived uint32 // indicates whether any bytes have been received on this stream
unprocessed uint32 // set if the server sends a refused stream or GOAWAY including this stream
headerWireLength int
Copy link
Member

Choose a reason for hiding this comment

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

This should be grouped with other header stuff probably. And it needs a comment that it's only set by the 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.

@@ -437,6 +444,12 @@ func (s *Stream) Status() *status.Status {
return s.status
}

// HeaderWireLength returns the size of theheaders of the stream as received
Copy link
Member

Choose a reason for hiding this comment

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

Addaspace pls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, done :).

@@ -437,6 +444,12 @@ func (s *Stream) Status() *status.Status {
return s.status
}

// HeaderWireLength returns the size of theheaders of the stream as received
// from the wire.
Copy link
Member

Choose a reason for hiding this comment

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

.... "Valid only on the 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.

Sure. Added. Does it not know/need to know the client side eventually?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe, but at that time we can fix the comment.

Honestly one of the next cleanups I'd love to do is to split the transport.Stream type into "client" and "server" versions instead of sharing code awkwardly where most of it needs to check a boolean to figure out whether it's a client or server stream and do radically different behavior depending on which.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm ok sounds good.

Comment on lines +917 to +921
ctx = sh.TagConn(ctx, &stats.ConnTagInfo{
RemoteAddr: rawConn.RemoteAddr(),
LocalAddr: rawConn.LocalAddr(),
})
sh.HandleConn(ctx, &stats.ConnBegin{})
Copy link
Member

Choose a reason for hiding this comment

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

This is a change in behavior, but it's subtle.

If the handshaker replaces the conn with something connected to another remote/local address, then that's not reflected here.

More importantly it doesn't match what we do later for remote/local addr, which is use the server transports addresses. We probably want to move this after NewServerTransport and use st.LocalAddr() and st.RemoteAddr() instead.

(Also this change means we call the stats handler for every connection even if it doesn't finish handshaking correctly, which is probably also not what we want.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmmm ok I was wondering whether I should do this after handshaking, I noticed it changed to log unconditionally. I remember you mentioned either passing conn around or just using the wrapper of conn to get addr info, and we chose latter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/grpc/grpc-go/blob/master/internal/transport/http2_client.go#L372 moving it after transport will slightly change behavior as well, as previously TagConn was still called if there's a connection error in say writing intial http/2 preface/settings frame. For me, this is dependent on semantically what does TagConn represent? Accepting a TCP connection? Getting an HTTP/2 connection to a point where it finishes the initial frames?

Copy link
Member

@dfawley dfawley Sep 15, 2023

Choose a reason for hiding this comment

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

As discussed offline, waiting for (HTTP/2) handshaking before calling TagConn seems fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did this after newHTTP2Transport in new PR (still comes before first stats handler call after TagConn, so as we discussed offline, this is fine).

@zasweq
Copy link
Contributor Author

zasweq commented Oct 10, 2023

Continued in #6716. Closing this PR, but I did get to all the comments here.

@zasweq zasweq closed this Oct 11, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants