Search in sources :

Example 1 with TimeoutIOException

use of org.apache.ratis.protocol.exceptions.TimeoutIOException in project incubator-ratis by apache.

the class NettyRpcProxy method send.

public RaftNettyServerReplyProto send(RaftRpcRequestProto request, RaftNettyServerRequestProto proto) throws IOException {
    final CompletableFuture<RaftNettyServerReplyProto> reply = new CompletableFuture<>();
    final ChannelFuture channelFuture = connection.offer(proto, reply);
    try {
        channelFuture.sync();
        TimeDuration newDuration = requestTimeoutDuration.add(request.getTimeoutMs(), TimeUnit.MILLISECONDS);
        return reply.get(newDuration.getDuration(), newDuration.getUnit());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw IOUtils.toInterruptedIOException(ProtoUtils.toString(request) + " sending from " + peer + " is interrupted.", e);
    } catch (ExecutionException e) {
        throw IOUtils.toIOException(e);
    } catch (TimeoutException e) {
        throw new TimeoutIOException(e.getMessage(), e);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftNettyServerReplyProto(org.apache.ratis.proto.netty.NettyProtos.RaftNettyServerReplyProto) TimeDuration(org.apache.ratis.util.TimeDuration) ExecutionException(java.util.concurrent.ExecutionException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with TimeoutIOException

use of org.apache.ratis.protocol.exceptions.TimeoutIOException in project incubator-ratis by apache.

the class TestExceptionDependentRetry method testExceptionDependentRetrySuccess.

@Test
public void testExceptionDependentRetrySuccess() {
    ExceptionDependentRetry.Builder builder = ExceptionDependentRetry.newBuilder();
    int ioExceptionRetries = 1;
    int timeoutExceptionRetries = 2;
    int defaultExceptionRetries = 5;
    long ioExceptionSleepTime = 1;
    long timeoutExceptionSleepTime = 4;
    long defaultExceptionSleepTime = 10;
    int maxAttempts = 3;
    builder.setDefaultPolicy(RetryPolicies.retryUpToMaximumCountWithFixedSleep(defaultExceptionRetries, TimeDuration.valueOf(defaultExceptionSleepTime, TimeUnit.SECONDS)));
    builder.setExceptionToPolicy(IOException.class, RetryPolicies.retryUpToMaximumCountWithFixedSleep(ioExceptionRetries, TimeDuration.valueOf(ioExceptionSleepTime, TimeUnit.SECONDS)));
    builder.setExceptionToPolicy(TimeoutIOException.class, RetryPolicies.retryUpToMaximumCountWithFixedSleep(timeoutExceptionRetries, TimeDuration.valueOf(timeoutExceptionSleepTime, TimeUnit.SECONDS)));
    builder.setMaxAttempts(maxAttempts);
    ExceptionDependentRetry exceptionDependentRetry = builder.build();
    testException(ioExceptionRetries, maxAttempts, exceptionDependentRetry, new IOException(), ioExceptionSleepTime);
    testException(timeoutExceptionRetries, maxAttempts, exceptionDependentRetry, new TimeoutIOException("time out"), timeoutExceptionSleepTime);
    // now try with an exception which is not there in the map.
    testException(defaultExceptionRetries, maxAttempts, exceptionDependentRetry, new TimeoutException(), defaultExceptionSleepTime);
}
Also used : IOException(java.io.IOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 3 with TimeoutIOException

use of org.apache.ratis.protocol.exceptions.TimeoutIOException in project incubator-ratis by apache.

the class GrpcUtil method tryUnwrapException.

static IOException tryUnwrapException(StatusRuntimeException se) {
    final Status status = se.getStatus();
    if (status != null && status.getCode() == Status.Code.DEADLINE_EXCEEDED) {
        return new TimeoutIOException(status.getDescription(), se);
    }
    final Metadata trailers = se.getTrailers();
    if (trailers == null) {
        return null;
    }
    final byte[] bytes = trailers.get(EXCEPTION_OBJECT_KEY);
    if (bytes != null) {
        try {
            return IOUtils.bytes2Object(bytes, IOException.class);
        } catch (Exception e) {
            se.addSuppressed(e);
        }
    }
    if (status != null) {
        final String className = trailers.get(EXCEPTION_TYPE_KEY);
        if (className != null) {
            try {
                final Class<? extends Throwable> clazz = Class.forName(className).asSubclass(Throwable.class);
                final Throwable unwrapped = ReflectionUtils.instantiateException(clazz, status.getDescription());
                return IOUtils.asIOException(unwrapped.initCause(se));
            } catch (Throwable e) {
                se.addSuppressed(e);
                return new IOException(se);
            }
        }
    }
    return null;
}
Also used : Status(org.apache.ratis.thirdparty.io.grpc.Status) Metadata(org.apache.ratis.thirdparty.io.grpc.Metadata) IOException(java.io.IOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) ServerNotReadyException(org.apache.ratis.protocol.exceptions.ServerNotReadyException) IOException(java.io.IOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) StatusRuntimeException(org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException)

Aggregations

TimeoutIOException (org.apache.ratis.protocol.exceptions.TimeoutIOException)3 IOException (java.io.IOException)2 TimeoutException (java.util.concurrent.TimeoutException)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 BaseTest (org.apache.ratis.BaseTest)1 RaftNettyServerReplyProto (org.apache.ratis.proto.netty.NettyProtos.RaftNettyServerReplyProto)1 ServerNotReadyException (org.apache.ratis.protocol.exceptions.ServerNotReadyException)1 Metadata (org.apache.ratis.thirdparty.io.grpc.Metadata)1 Status (org.apache.ratis.thirdparty.io.grpc.Status)1 StatusRuntimeException (org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException)1 TimeDuration (org.apache.ratis.util.TimeDuration)1 Test (org.junit.Test)1