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

Use the same hpack encoder on a transport and share it between RPCs. #1536

Merged
merged 4 commits into from
Sep 28, 2017

Conversation

MakMukhi
Copy link
Contributor

@MakMukhi MakMukhi commented Sep 23, 2017

hpack encoding will now happen in the loopy writer goroutine right before the header is written on the wire. This is a more efficient use of hpack encoder since it maintains state and can be more performant in subsequent call.
As a result, the stats handler call with OutHeader can not include the wire length of the header written since it's not yet calculated. We might add this field back in the future which would require changing the code quite a bit.

@MakMukhi MakMukhi added the Type: Performance Performance improvements (CPU, network, memory, etc) label Sep 23, 2017
@dfawley dfawley self-assigned this Sep 25, 2017
@@ -209,6 +210,8 @@ func newHTTP2Client(ctx context.Context, addr TargetInfo, opts ConnectOptions) (
goAway: make(chan struct{}),
awakenKeepalive: make(chan struct{}, 1),
framer: newFramer(conn),
hBuf: &buf,
Copy link
Member

Choose a reason for hiding this comment

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

hBuf: &bytes.Buffer{}

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 know we talked about this but now I realize that hEnc needs the same buffer.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that should have been obvious, sorry!

// Check if credentials.PerRPCCredentials were provided via call options.
// Note: if these credentials are provided both via dial options and call
// options, then both sets of credentials will be applied.
if callCreds := callHdr.Creds; callCreds != nil {
if !t.isSecure && callCreds.RequireTransportSecurity() {
return nil, streamErrorf(codes.Unauthenticated, "transport: cannot send secure credentials on an insecure connection")
return nil, streamErrorf(codes.Unauthenticated, "transport: cannot send secure credentials on an insecure conneciton")
Copy link
Member

Choose a reason for hiding this comment

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

connection

// Make the slice of certain predictable size to reduce allocations made by append.
hfLen := 7 // :method, :scheme, :path, :authority, content-type, user-agent, te
hfLen += len(authData) + len(callAuthData)
headerFields := make([]hpack.HeaderField, 0, hfLen)
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 result in reallocations if there is any user-provided metadata.

It probably would be a win to count it first.

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 thought of that but we'll need to loop through the and see if there are any reserved headers in there or not twice (one to count and one to actually append in the slice). And that holds true for other headers too. So I ended up counting the ones that are easily countable. And since append allocates twice the current size. I'm guessing there won't be too many allocations happening.
Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Could you add some benchmarks to cover this so we can decide? We don't have anything that includes metadata. If the number of entries and size of each entry can be tweaked through settings, that would be ideal. Ideally we could independently configure client headers, server headers, and server trailers. It would be interesting to compare the performance of both approaches in the following scenarios:

  1. no metadata
  2. with a small amount of metadata (~2)
  3. with a large amount of metadata (~20)

Client: true,
WireLength: bufLen,
Client: true,
//WireLength: // TODO(mmukhi): Revisit this if needed.
Copy link
Member

Choose a reason for hiding this comment

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

Delete

@@ -961,7 +939,7 @@ func (t *http2Client) handleGoAway(f *http2.GoAwayFrame) {
t.notifyError(connectionErrorf(true, nil, "received illegal http2 GOAWAY frame: stream ID %d is even", f.LastStreamID))
return
}
// A client can receive multiple GoAways from server (look at https://github.com/grpc/grpc-go/issues/1387).
// A client can recieve multiple GoAways from server (look at https://github.com/grpc/grpc-go/issues/1387).
Copy link
Member

Choose a reason for hiding this comment

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

except after "c"

hEnc := hpack.NewEncoder(hBuf)
hEnc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
hEnc.WriteField(hpack.HeaderField{Name: "content-type", Value: "application/grpc"})
headerFields := make([]hpack.HeaderField, 0, 2) // aleast :status, content-type will be there if none else.
Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to do the math to avoid the reallocation/copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same argument as above.

@@ -782,18 +758,13 @@ func (t *http2Server) WriteStatus(s *Stream, st *status.Status) error {
headersSent = true
}

hBuf := bytes.NewBuffer([]byte{}) // TODO(mmukhi): Try and re-use this memory.
hEnc := hpack.NewEncoder(hBuf)
headerFields := make([]hpack.HeaderField, 0, 2) // grpc-status and grpc-message will be there if none else.
Copy link
Member

Choose a reason for hiding this comment

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

Do the math?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same argument as above.

if t.stats != nil {
outTrailer := &stats.OutTrailer{
WireLength: bufLen,
// WireLength:TODO(mmukhi): Revisit this later, if needed.
Copy link
Member

Choose a reason for hiding this comment

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

Delete

@@ -938,7 +908,7 @@ func (t *http2Server) keepalive() {
maxAge := time.NewTimer(t.kp.MaxConnectionAge)
keepalive := time.NewTimer(t.kp.Time)
// NOTE: All exit paths of this function should reset their
// respective timers. A failure to do so will cause the
// respecitve timers. A failure to do so will cause the
Copy link
Member

Choose a reason for hiding this comment

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

*respective

err = t.framer.fr.WriteHeaders(i.p)
case *continuationFrame:
err = t.framer.fr.WriteContinuation(i.streamID, i.endHeaders, i.headerBlockFragment)
t.hBuf.Reset()
Copy link
Member

Choose a reason for hiding this comment

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

Can this logic be shared with the client's?

transport.go or control.go: headerFrameHandler(hEnc, hBuf, framer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, most of these cases in item handler can be shared between the server and the client along with most of Write method's code and I'm guessing there are other such opportunities too scattered throughout the transport layer. I kept it this way to be consistent with the rest of the code and with hopes that one day we'll refactor the transport to take care of all this duplication.

Copy link
Member

Choose a reason for hiding this comment

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

SGTM, let's try not to put it off for too long. Maybe add a TODO in this PR...

@dfawley dfawley merged commit 6014154 into grpc:master Sep 28, 2017
@menghanl menghanl added this to the 1.7 Release milestone Sep 29, 2017
@MakMukhi MakMukhi deleted the reuse_hEnc branch May 4, 2018 02:09
@lock lock bot locked as resolved and limited conversation to collaborators Oct 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants