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

grpc: Move some stats handler calls to gRPC layer, and add local address to peer.Peer #6716

Merged

Conversation

zasweq
Copy link
Contributor

@zasweq zasweq commented Oct 10, 2023

This PR moves ConnBegin, ConnEnd, and InHeader from the transport layer to the gRPC layer. The other two callouts still in the transport, OutHeader and OutTrailer will require a reworking of the ownership of streaming code and logic with respect to functionality in gRPC layer and transport layer. Continuation of #6624.

RELEASE NOTES:

  • peer: Add local address to peer.Peer

@zasweq zasweq force-pushed the move-stats-handler-calls-partially-server-side branch from 026fed9 to 46af5f9 Compare October 11, 2023 15:17
@codecov
Copy link

codecov bot commented Oct 11, 2023

Codecov Report

Merging #6716 (4d2490f) into master (e88e849) will increase coverage by 0.03%.
Report is 2 commits behind head on master.
The diff coverage is 97.72%.

Additional details and impacted files

@@ -167,6 +167,14 @@ 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 } // Server Handler transport has no access to local addr (was simply not calling sh with local addr).
Copy link
Member

Choose a reason for hiding this comment

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

I believe this can be obtained with: ht.req.Context().Value(http.LocalAddrContextKey)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok, done. Since you only want peer though will be added to the Peer() method and this will be deleted.

method: req.URL.Path,
recvCompress: req.Header.Get("grpc-encoding"),
contentSubtype: ht.contentSubtype,
headerWireLength: 0, // doesn't know header wire length, will call into stats handler as 0.
Copy link
Member

Choose a reason for hiding this comment

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

This one doesn't seem to be available. I think we'll need:

golang/go#18997

Please leave a comment in the code referencing the issue.

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. Thanks.

method: req.URL.Path,
recvCompress: req.Header.Get("grpc-encoding"),
contentSubtype: ht.contentSubtype,
headerWireLength: 0, // doesn't know header wire length, will call into stats handler as 0.
}
pr := &peer.Peer{
Copy link
Member

Choose a reason for hiding this comment

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

Reuse Peer()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. Done.

@@ -347,7 +355,7 @@ func (ht *serverHandlerTransport) WriteHeader(s *Stream, md metadata.MD) error {
return err
}

func (ht *serverHandlerTransport) HandleStreams(startStream func(*Stream)) {
func (ht *serverHandlerTransport) HandleStreams(_ context.Context, startStream func(*Stream)) {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't ignoring the context mess everything up?

Can grpc not pass the http.Request.Context() (with things like Peer added) so that it doesn't need to be ignored here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was being ignored in master already, and it uses the requests Context (ctx := ht.req.Context()). I think this is the same functionality, and I don't think I want to add something to gRPC layer using the http.Request since it's a server handler transport specific concept.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed offline; decided to move peer creation to construction time and read it in gRPC before calling this.

@@ -698,7 +712,7 @@ type ClientTransport interface {
// Write methods for a given Stream will be called serially.
type ServerTransport interface {
// HandleStreams receives incoming streams using the given handler.
Copy link
Member

Choose a reason for hiding this comment

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

"the Context given is used as the base context for all streams started on this transport."?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As per comment above, will wait until further discussion on the server handler transport to add this 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.

Decided to do this, added this comment.

Comment on lines +740 to +741
// Peer returns the peer of the server transport.
Peer() *peer.Peer
Copy link
Member

Choose a reason for hiding this comment

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

Should we delete Local/RemoteAddr and only have 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.

Sure, done. There's an in flight PR for this but the contributor isn't getting back to my comments so I'll go ahead and just add it to this PR.

server.go Outdated
Comment on lines 976 to 977
// GetConnection gets the connection from the context.
func GetConnection(ctx context.Context) net.Conn {
Copy link
Member

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 this to be public.

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 I'll plumb it through /internal good point

@dfawley dfawley assigned zasweq and unassigned dfawley Oct 19, 2023
@zasweq zasweq assigned dfawley and unassigned zasweq Oct 20, 2023
Copy link
Contributor Author

@zasweq zasweq left a comment

Choose a reason for hiding this comment

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

Thanks for the pass. Got to all comments.

@arvindbr8
Copy link
Member

@zasweq -- seems like there is a conflict. Adding you to the assignees to take care of that.

@zasweq
Copy link
Contributor Author

zasweq commented Oct 20, 2023

Ah thanks resolved

@zasweq zasweq removed their assignment Oct 20, 2023
@@ -62,6 +62,8 @@ var (
// gRPC server. An xDS-enabled server needs to know what type of credentials
// is configured on the underlying gRPC server. This is set by server.go.
GetServerCredentials any // func (*grpc.Server) credentials.TransportCredentials
// GetConnection gets the connection from the context.
GetConnection any // func (context.Context) net.Conn
Copy link
Member

Choose a reason for hiding this comment

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

It should be fine to give this a type to avoid the casts, since it's only things from the stdlib.

But, it should also be fine to implement it fully within the internal package. But then.. why not just leave it where it is and export the setter? Or find another place in internal for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought about this, decided to move back to transport. I don't really think any of the current internal directories make semantical sense, and as per offline discussion I think there's complications about trying to keep file paths consistent.

@zasweq zasweq force-pushed the move-stats-handler-calls-partially-server-side branch from a57125e to 8a70332 Compare October 23, 2023 23:26
@zasweq zasweq assigned dfawley and unassigned dfawley Oct 24, 2023
@zasweq zasweq changed the title grpc: Move some stats handler calls to gRPC layer grpc: Move some stats handler calls to gRPC layer, and add local address to peer.Peer Oct 24, 2023
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.

LGTM modulo the last comment.

@@ -75,11 +75,25 @@ func NewServerHandlerTransport(w http.ResponseWriter, r *http.Request, stats []s
return nil, errors.New(msg)
}

var localAddr net.Addr
if la := r.Context().Value(http.LocalAddrContextKey); la != nil {
localAddr = la.(net.Addr)
Copy link
Member

Choose a reason for hiding this comment

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

This will panic if the wrong type is here. localAddr, _ = ... is safer.

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.

@dfawley dfawley assigned zasweq and unassigned dfawley Oct 25, 2023
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