Search in sources :

Example 1 with AlreadyClosedException

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

the class BlockingImpl method sendRequestWithRetry.

RaftClientReply sendRequestWithRetry(Supplier<RaftClientRequest> supplier) throws IOException {
    RaftClientImpl.PendingClientRequest pending = new RaftClientImpl.PendingClientRequest() {

        @Override
        public RaftClientRequest newRequestImpl() {
            return supplier.get();
        }
    };
    while (true) {
        final RaftClientRequest request = pending.newRequest();
        IOException ioe = null;
        try {
            final RaftClientReply reply = sendRequest(request);
            if (reply != null) {
                return client.handleReply(request, reply);
            }
        } catch (GroupMismatchException | StateMachineException | TransferLeadershipException | LeaderSteppingDownException | AlreadyClosedException | AlreadyExistsException e) {
            throw e;
        } catch (IOException e) {
            ioe = e;
        }
        pending.incrementExceptionCount(ioe);
        ClientRetryEvent event = new ClientRetryEvent(request, ioe, pending);
        final RetryPolicy retryPolicy = client.getRetryPolicy();
        final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
        TimeDuration sleepTime = client.getEffectiveSleepTime(ioe, action.getSleepTime());
        if (!action.shouldRetry()) {
            throw (IOException) client.noMoreRetries(event);
        }
        try {
            sleepTime.sleep();
        } catch (InterruptedException e) {
            throw new InterruptedIOException("retry policy=" + retryPolicy);
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TimeDuration(org.apache.ratis.util.TimeDuration) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Example 2 with AlreadyClosedException

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

the class ClientProtoUtils method toAlreadyClosedException.

static AlreadyClosedException toAlreadyClosedException(String className, String errorMsg, ByteString stackTraceBytes) {
    AlreadyClosedException ace;
    if (className == null) {
        ace = new AlreadyClosedException(errorMsg);
    } else {
        try {
            Class<?> clazz = Class.forName(className);
            final Exception e = ReflectionUtils.instantiateException(clazz.asSubclass(Exception.class), errorMsg);
            ace = new AlreadyClosedException(errorMsg, e);
        } catch (Exception e) {
            ace = new AlreadyClosedException(className + ": " + errorMsg);
        }
    }
    StackTraceElement[] stacktrace = (StackTraceElement[]) ProtoUtils.toObject(stackTraceBytes);
    ace.setStackTrace(stacktrace);
    return ace;
}
Also used : AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) DataStreamException(org.apache.ratis.protocol.exceptions.DataStreamException) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) RaftException(org.apache.ratis.protocol.exceptions.RaftException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)

Example 3 with AlreadyClosedException

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

the class GrpcClientRpc method sendRequest.

private CompletableFuture<RaftClientReply> sendRequest(RaftClientRequest request, GrpcClientProtocolClient proxy) throws IOException {
    final RaftClientRequestProto requestProto = toRaftClientRequestProto(request);
    final CompletableFuture<RaftClientReplyProto> replyFuture = new CompletableFuture<>();
    // create a new grpc stream for each non-async call.
    final StreamObserver<RaftClientRequestProto> requestObserver = proxy.unorderedWithTimeout(new StreamObserver<RaftClientReplyProto>() {

        @Override
        public void onNext(RaftClientReplyProto value) {
            replyFuture.complete(value);
        }

        @Override
        public void onError(Throwable t) {
            replyFuture.completeExceptionally(GrpcUtil.unwrapIOException(t));
        }

        @Override
        public void onCompleted() {
            if (!replyFuture.isDone()) {
                replyFuture.completeExceptionally(new AlreadyClosedException(clientId + ": Stream completed but no reply for request " + request));
            }
        }
    });
    requestObserver.onNext(requestProto);
    requestObserver.onCompleted();
    return replyFuture.thenApply(ClientProtoUtils::toRaftClientReply);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientRequestProto(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto) ClientProtoUtils(org.apache.ratis.client.impl.ClientProtoUtils) RaftClientReplyProto(org.apache.ratis.proto.RaftProtos.RaftClientReplyProto) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException)

Aggregations

AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)3 LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)2 StateMachineException (org.apache.ratis.protocol.exceptions.StateMachineException)2 TransferLeadershipException (org.apache.ratis.protocol.exceptions.TransferLeadershipException)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ClientProtoUtils (org.apache.ratis.client.impl.ClientProtoUtils)1 ClientRetryEvent (org.apache.ratis.client.retry.ClientRetryEvent)1 RaftClientReplyProto (org.apache.ratis.proto.RaftProtos.RaftClientReplyProto)1 RaftClientRequestProto (org.apache.ratis.proto.RaftProtos.RaftClientRequestProto)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1 AlreadyExistsException (org.apache.ratis.protocol.exceptions.AlreadyExistsException)1 DataStreamException (org.apache.ratis.protocol.exceptions.DataStreamException)1 GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)1 LeaderNotReadyException (org.apache.ratis.protocol.exceptions.LeaderNotReadyException)1 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)1 NotReplicatedException (org.apache.ratis.protocol.exceptions.NotReplicatedException)1 RaftException (org.apache.ratis.protocol.exceptions.RaftException)1