Skip to content

core: Update HTTP status to gRPC status mapping - #2362

Merged
ejona86 merged 2 commits into
grpc:masterfrom
ejona86:map-http-to-grpc
Nov 2, 2016
Merged

ejona86 merged 2 commits into
grpc:masterfrom
ejona86:map-http-to-grpc

Conversation

@ejona86

@ejona86 ejona86 commented Oct 21, 2016 •

Copy link
Copy Markdown
Member

This also includes a commit to stop relying on Status.OK == HTTP 200, what was a necessary pre-refactoring. The commit has a general rework of the processing trying to keep the logic sane instead of just hacking the logic together.

The two commits will remain split when merged to master. It probably doesn't matter much whether you review them together or separately.

The changes to Http2ClientStreamTransportState.java were literally copied from Http2ClientStream.java, without any specialized changes.

CC @lukaszx0

@lukaszx0 lukaszx0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
if (httpStatusCode > 100 && httpStatusCode < 200) {
// 1xx. These headers should have been ignored.
return Status.Code.INTERNAL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we update doc and point out explicitly that if 100 happens, it should be INTERNAL ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The doc already calls out what should happen with 100: "Technically, 1xx should have the entire header skipped and a subsequent header be read. See RFC 7540 §8.1."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you add comments why Status.Code.UNKNOWN is returned for httpStatusCode == 100 or httpStatusCode == 200 or code the cases explicitly?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I already added a comment for 100. You want something else?

I'm not special-casing 200. 200 is caught by the default case below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, I see now. 100 itself is not caught by the condition here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed the condition. It doesn't seem any comment is needed.

@ejona86

ejona86 commented Oct 21, 2016

Copy link
Copy Markdown
Member Author

Coverage is low, so I'll add tests for Http2ClientStream*

@ejona86 ejona86 added the TODO:backport PR needs to be backported. Removed after backport complete label Oct 21, 2016
@ejona86

ejona86 commented Oct 21, 2016

Copy link
Copy Markdown
Member Author

Marked as backport for the non-reliance on HTTP 200. Just the first commit could be backported, we probably don't want/need to backport the HTTP status code mapping.

@ejona86

ejona86 commented Oct 21, 2016

Copy link
Copy Markdown
Member Author

I added tests, but just for Http2ClientStreamTransportState. Http2ClientStream is on the chopping block, so I didn't want to waste the time.

@dapengzhang0 dapengzhang0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

partial comments

private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
if (httpStatusCode > 100 && httpStatusCode < 200) {
// 1xx. These headers should have been ignored.
return Status.Code.INTERNAL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you add comments why Status.Code.UNKNOWN is returned for httpStatusCode == 100 or httpStatusCode == 200 or code the cases explicitly?

if (transportError != null) {
// Note we don't immediately report the transport error, instead we wait for more data on
// the stream so we can accumulate more detail into the error before reporting it.
transportError = transportError.augmentDescription(headers.toString());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could something like .augmentDescription("headers=" + headers) here and line 98 and 181 be more informative?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done. Used "headers: " and "trailers: "


stripTransportDetails(headers);
inboundHeadersReceived(headers);
} finally {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There seems no exception thrown here. Why use try-finally rather than else if?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

else doesn't run when you return. I could use a do {} while (false) + break to get similar effect, but finally is more natural.

@dapengzhang0 dapengzhang0 Nov 1, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With else you don't have to return. I think the following will do (might have to be with 3 layers of curly braces vs. 2 layers using try-finally). Similar for the try-finally in Http2ClientStreamTransportState.

    if (headersReceived) {
      transportError = Status.INTERNAL.withDescription("Received headers twice");
    } else {
      Integer httpStatus = headers.get(HTTP2_STATUS);
      // Ignore the headers if httpStatus is >=100 and <200. See RFC 7540 §8.1 
      if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
        headersReceived = true;
        transportError = validateInitialMetadata(headers);
        if (transportError == null) {
          stripTransportDetails(headers);
          inboundHeadersReceived(headers);
        }
      }
    }

    if (transportError != null) {
      // Note we don't immediately report the transport error, instead we wait for more data on
      // the stream so we can accumulate more detail into the error before reporting it.
      transportError = transportError.augmentDescription(headers.toString());
      transportErrorMetadata = headers;
      errorCharset = extractCharset(headers);
    }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah. I try to avoid nested ifs for verification, because the code becomes arbitrarily indented and nested. And any condition you add may cause indentation of the pre-existing conditions to change (causing review and blame noise).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What I really want is "goto error", which is actually not a horrible idiom. It can only be emulated in Java, although with the finally it isn't too bad.

@dapengzhang0 dapengzhang0 Nov 2, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"goto error" makes more sense. To avoid nested ifs is also possible though. (Single layer of curly braces vs two layers using try-final)

    Integer httpStatus = headers.get(HTTP2_STATUS);
    if (headersReceived) {
      transportError = Status.INTERNAL.withDescription("Received headers twice");
    } else if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
      // Ignore the headers if httpStatus is >=100 and <200. See RFC 7540 §8.1 
      headersReceived = true;
      transportError = validateInitialMetadata(headers);
    }

    if (transportError != null) {
      // Note we don't immediately report the transport error, instead we wait for more data on
      // the stream so we can accumulate more detail into the error before reporting it.
      transportError = transportError.augmentDescription(headers.toString());
      transportErrorMetadata = headers;
      errorCharset = extractCharset(headers);
    } else if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
      stripTransportDetails(headers);
      inboundHeadersReceived(headers);
    }

if (httpStatus != null) {
status = GrpcUtil.httpStatusToGrpcStatus(httpStatus);
} else {
status = Status.INTERNAL.withDescription("missing HTTP status code");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.withDescription("missing HTTP status code") being overridden by the return statement below?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes... I already fixed that though...

Doh. I just fixed it in Http2ClientStreamTransportState.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.


stripTransportDetails(headers);
inboundHeadersReceived(headers);
} finally {

@dapengzhang0 dapengzhang0 Nov 1, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With else you don't have to return. I think the following will do (might have to be with 3 layers of curly braces vs. 2 layers using try-finally). Similar for the try-finally in Http2ClientStreamTransportState.

    if (headersReceived) {
      transportError = Status.INTERNAL.withDescription("Received headers twice");
    } else {
      Integer httpStatus = headers.get(HTTP2_STATUS);
      // Ignore the headers if httpStatus is >=100 and <200. See RFC 7540 §8.1 
      if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
        headersReceived = true;
        transportError = validateInitialMetadata(headers);
        if (transportError == null) {
          stripTransportDetails(headers);
          inboundHeadersReceived(headers);
        }
      }
    }

    if (transportError != null) {
      // Note we don't immediately report the transport error, instead we wait for more data on
      // the stream so we can accumulate more detail into the error before reporting it.
      transportError = transportError.augmentDescription(headers.toString());
      transportErrorMetadata = headers;
      errorCharset = extractCharset(headers);
    }

private Status checkContentType(Metadata headers) {
if (contentTypeChecked) {
return null;
private Status validateInitialMetadata(Metadata headers) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This method and statusFromTrailers are exactly the same as the ones in Http2ClientStream. Can they be public static methods of io.grpc.Status?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well, one is a copy from the other. I want to delete the code, not focus on sharing it.

@dapengzhang0 dapengzhang0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

We only want to use the HTTP code for errors, when the response is not
grpc. grpc status codes may be mapped to HTTP codes in the future, and
we don't want to break when that happens. We also don't want to ever
accidentally use Status.OK without receiving it from the server, even
for HTTP 200.
Proxies can trigger errors, and we'd like to categorize common
proxy-generating errors into gRPC status codes for sane application
handling.

Fixes grpc#2264
@ejona86
ejona86 merged commit 2e7e25d into grpc:master Nov 2, 2016
@ejona86
ejona86 deleted the map-http-to-grpc branch November 2, 2016 15:52
@ejona86 ejona86 removed the TODO:backport PR needs to be backported. Removed after backport complete label Jun 29, 2017
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants