Map IOException during connecting to UNAVAILABLE

This isn't expected to have much impact once the connection is
established because AbstractNettyHandler.exceptionCaught() wraps all
unknown exceptions with Http2Exception. Fixing that is future work.

Some cases we may now be unwrapping a StatusException, such as one
thrown by MessageDeframer. In general, that seems a Good Thing™, but it
is unclear exactly if it would be perceivable.

Fixes #1181
This commit is contained in:
Eric Anderson 2015-11-25 09:43:38 -08:00
parent 1a32366242
commit 7ba9ca4861
5 changed files with 89 additions and 11 deletions

View File

@ -300,7 +300,7 @@ class NettyClientHandler extends AbstractNettyHandler {
protected void onConnectionError(ChannelHandlerContext ctx, Throwable cause,
Http2Exception http2Ex) {
logger.log(Level.FINE, "Caught a connection error", cause);
goAwayStatus(statusFromError(cause));
goAwayStatus(Utils.statusFromThrowable(cause));
super.onConnectionError(ctx, cause, http2Ex);
}
@ -310,18 +310,13 @@ class NettyClientHandler extends AbstractNettyHandler {
// Close the stream with a status that contains the cause.
NettyClientStream stream = clientStream(connection().stream(http2Ex.streamId()));
if (stream != null) {
stream.transportReportStatus(statusFromError(cause), false, new Metadata());
stream.transportReportStatus(Utils.statusFromThrowable(cause), false, new Metadata());
}
// Delegate to the base class to send a RST_STREAM.
super.onStreamError(ctx, cause, http2Ex);
}
private Status statusFromError(Throwable cause) {
return cause instanceof Http2Exception ? Status.INTERNAL.withCause(cause)
: Status.fromThrowable(cause);
}
@Override
protected boolean isGracefulShutdownComplete() {
// Only allow graceful shutdown to complete after all pending streams have completed.

View File

@ -127,7 +127,7 @@ class NettyClientTransport implements ClientTransport {
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
// Stream creation failed. Close the stream if not already closed.
stream.transportReportStatus(Status.fromThrowable(future.cause()), true,
stream.transportReportStatus(Utils.statusFromThrowable(future.cause()), true,
new Metadata());
}
}
@ -184,7 +184,7 @@ class NettyClientTransport implements ClientTransport {
if (!future.isSuccess()) {
// Need to notify of this failure, because handler.connectionError() is not guaranteed to
// have seen this cause.
notifyTerminated(Status.fromThrowable(future.cause()));
notifyTerminated(Utils.statusFromThrowable(future.cause()));
}
}
});

View File

@ -249,8 +249,7 @@ class NettyServerHandler extends AbstractNettyHandler {
connection().stream(Http2Exception.streamId(http2Ex)));
if (serverStream != null) {
// Abort the stream with a status to help the client with debugging.
serverStream.abortStream(cause instanceof Http2Exception
? Status.INTERNAL.withCause(cause) : Status.fromThrowable(cause), true);
serverStream.abortStream(Utils.statusFromThrowable(cause), true);
} else {
// Delegate to the base class to send a RST_STREAM.
super.onStreamError(ctx, cause, http2Ex);

View File

@ -40,17 +40,20 @@ import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.SharedResourceHolder.Resource;
import io.grpc.internal.TransportFrameUtil;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.util.AsciiString;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -165,6 +168,20 @@ class Utils {
return http2Headers;
}
public static Status statusFromThrowable(Throwable t) {
Status s = Status.fromThrowable(t);
if (s.getCode() != Status.Code.UNKNOWN) {
return s;
}
if (t instanceof IOException) {
return Status.UNAVAILABLE.withCause(t);
}
if (t instanceof Http2Exception) {
return Status.INTERNAL.withCause(t);
}
return s;
}
private static class DefaultEventLoopGroupResource implements Resource<EventLoopGroup> {
private final String name;
private final int numEventLoops;

View File

@ -0,0 +1,67 @@
/*
* Copyright 2015, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package io.grpc.netty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import io.grpc.Status;
import io.netty.channel.ConnectTimeoutException;
import io.netty.handler.codec.http2.Http2Error;
import io.netty.handler.codec.http2.Http2Exception;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link Utils}. */
@RunWith(JUnit4.class)
public class UtilsTest {
@Test
public void testStatusFromThrowable() {
Status s = Status.CANCELLED.withDescription("msg");
assertSame(s, Utils.statusFromThrowable(new Exception(s.asException())));
Throwable t;
t = new ConnectTimeoutException("msg");
assertStatusEquals(Status.UNAVAILABLE.withCause(t), Utils.statusFromThrowable(t));
t = new Http2Exception(Http2Error.INTERNAL_ERROR, "msg");
assertStatusEquals(Status.INTERNAL.withCause(t), Utils.statusFromThrowable(t));
t = new Exception("msg");
assertStatusEquals(Status.UNKNOWN.withCause(t), Utils.statusFromThrowable(t));
}
private static void assertStatusEquals(Status expected, Status actual) {
assertEquals(expected.getCode(), actual.getCode());
assertEquals(expected.getDescription(), actual.getDescription());
assertEquals(expected.getCause(), actual.getCause());
}
}