Search in sources :

Example 1 with RaftException

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

the class AbstractRatisCommand method processReply.

protected void processReply(RaftClientReply reply, Supplier<String> messageSupplier) throws IOException {
    if (reply == null || !reply.isSuccess()) {
        final RaftException e = Optional.ofNullable(reply).map(RaftClientReply::getException).orElseGet(() -> new RaftException("Reply: " + reply));
        final String message = messageSupplier.get();
        printf("%s. Error: %s%n", message, e);
        throw new IOException(message, e);
    }
}
Also used : RaftException(org.apache.ratis.protocol.exceptions.RaftException) IOException(java.io.IOException)

Example 2 with RaftException

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

the class ClientProtoUtils method toRaftClientReply.

static RaftClientReply toRaftClientReply(RaftClientReplyProto replyProto) {
    final RaftRpcReplyProto rp = replyProto.getRpcReply();
    final RaftGroupMemberId serverMemberId = ProtoUtils.toRaftGroupMemberId(rp.getReplyId(), rp.getRaftGroupId());
    final RaftException e;
    if (replyProto.getExceptionDetailsCase().equals(NOTLEADEREXCEPTION)) {
        NotLeaderExceptionProto nleProto = replyProto.getNotLeaderException();
        final RaftPeer suggestedLeader = nleProto.hasSuggestedLeader() ? ProtoUtils.toRaftPeer(nleProto.getSuggestedLeader()) : null;
        final List<RaftPeer> peers = ProtoUtils.toRaftPeers(nleProto.getPeersInConfList());
        e = new NotLeaderException(serverMemberId, suggestedLeader, peers);
    } else if (replyProto.getExceptionDetailsCase() == NOTREPLICATEDEXCEPTION) {
        final NotReplicatedExceptionProto nre = replyProto.getNotReplicatedException();
        e = new NotReplicatedException(nre.getCallId(), nre.getReplication(), nre.getLogIndex());
    } else if (replyProto.getExceptionDetailsCase().equals(STATEMACHINEEXCEPTION)) {
        e = toStateMachineException(serverMemberId, replyProto.getStateMachineException());
    } else if (replyProto.getExceptionDetailsCase().equals(DATASTREAMEXCEPTION)) {
        e = ProtoUtils.toThrowable(replyProto.getDataStreamException(), DataStreamException.class);
    } else if (replyProto.getExceptionDetailsCase().equals(LEADERNOTREADYEXCEPTION)) {
        LeaderNotReadyExceptionProto lnreProto = replyProto.getLeaderNotReadyException();
        e = new LeaderNotReadyException(ProtoUtils.toRaftGroupMemberId(lnreProto.getServerId()));
    } else if (replyProto.getExceptionDetailsCase().equals(ALREADYCLOSEDEXCEPTION)) {
        e = toAlreadyClosedException(replyProto.getAlreadyClosedException());
    } else if (replyProto.getExceptionDetailsCase().equals(LEADERSTEPPINGDOWNEXCEPTION)) {
        e = ProtoUtils.toThrowable(replyProto.getLeaderSteppingDownException(), LeaderSteppingDownException.class);
    } else if (replyProto.getExceptionDetailsCase().equals(TRANSFERLEADERSHIPEXCEPTION)) {
        e = ProtoUtils.toThrowable(replyProto.getTransferLeadershipException(), TransferLeadershipException.class);
    } else {
        e = null;
    }
    return RaftClientReply.newBuilder().setClientId(ClientId.valueOf(rp.getRequestorId())).setServerId(serverMemberId).setCallId(rp.getCallId()).setSuccess(rp.getSuccess()).setMessage(toMessage(replyProto.getMessage())).setException(e).setLogIndex(replyProto.getLogIndex()).setCommitInfos(replyProto.getCommitInfosList()).build();
}
Also used : RaftException(org.apache.ratis.protocol.exceptions.RaftException) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)

Example 3 with RaftException

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

the class UnorderedAsync method sendRequestWithRetry.

static void sendRequestWithRetry(PendingClientRequest pending, RaftClientImpl client) {
    final CompletableFuture<RaftClientReply> f = pending.getReplyFuture();
    if (f.isDone()) {
        return;
    }
    final RaftClientRequest request = pending.newRequest();
    final int attemptCount = pending.getAttemptCount();
    final ClientId clientId = client.getId();
    LOG.debug("{}: attempt #{} send~ {}", clientId, attemptCount, request);
    client.getClientRpc().sendRequestAsyncUnordered(request).whenCompleteAsync((reply, e) -> {
        try {
            LOG.debug("{}: attempt #{} receive~ {}", clientId, attemptCount, reply);
            final RaftException replyException = reply != null ? reply.getException() : null;
            reply = client.handleLeaderException(request, reply);
            if (reply != null) {
                client.handleReply(request, reply);
                f.complete(reply);
                return;
            }
            final Throwable cause = replyException != null ? replyException : e;
            pending.incrementExceptionCount(cause);
            final ClientRetryEvent event = new ClientRetryEvent(request, cause, pending);
            RetryPolicy retryPolicy = client.getRetryPolicy();
            final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
            TimeDuration sleepTime = client.getEffectiveSleepTime(cause, action.getSleepTime());
            if (!action.shouldRetry()) {
                f.completeExceptionally(client.noMoreRetries(event));
                return;
            }
            if (e != null) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace(clientId + ": attempt #" + attemptCount + " failed~ " + request, e);
                } else {
                    LOG.debug("{}: attempt #{} failed {} with {}", clientId, attemptCount, request, e);
                }
                e = JavaUtils.unwrapCompletionException(e);
                if (e instanceof IOException) {
                    if (e instanceof NotLeaderException) {
                        client.handleNotLeaderException(request, (NotLeaderException) e, null);
                    } else if (e instanceof GroupMismatchException) {
                        f.completeExceptionally(e);
                        return;
                    } else {
                        client.handleIOException(request, (IOException) e);
                    }
                } else {
                    if (!client.getClientRpc().handleException(request.getServerId(), e, false)) {
                        f.completeExceptionally(e);
                        return;
                    }
                }
            }
            LOG.debug("schedule retry for attempt #{}, policy={}, request={}", attemptCount, retryPolicy, request);
            client.getScheduler().onTimeout(sleepTime, () -> sendRequestWithRetry(pending, client), LOG, () -> clientId + ": Failed~ to retry " + request);
        } catch (Exception ex) {
            LOG.error(clientId + ": Failed " + request, ex);
            f.completeExceptionally(ex);
        }
    });
}
Also used : RaftException(org.apache.ratis.protocol.exceptions.RaftException) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) IOException(java.io.IOException) RaftException(org.apache.ratis.protocol.exceptions.RaftException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ClientId(org.apache.ratis.protocol.ClientId) TimeDuration(org.apache.ratis.util.TimeDuration) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Example 4 with RaftException

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

the class ClientProtoUtils method toRaftClientReplyProto.

static RaftClientReplyProto toRaftClientReplyProto(RaftClientReply reply) {
    final RaftClientReplyProto.Builder b = RaftClientReplyProto.newBuilder();
    if (reply != null) {
        b.setRpcReply(toRaftRpcReplyProtoBuilder(reply.getClientId().toByteString(), reply.getServerId().toByteString(), reply.getRaftGroupId(), reply.getCallId(), reply.isSuccess()));
        b.setLogIndex(reply.getLogIndex());
        if (reply.getMessage() != null) {
            b.setMessage(toClientMessageEntryProtoBuilder(reply.getMessage()));
        }
        b.addAllCommitInfos(reply.getCommitInfos());
        final NotLeaderException nle = reply.getNotLeaderException();
        if (nle != null) {
            NotLeaderExceptionProto.Builder nleBuilder = NotLeaderExceptionProto.newBuilder();
            final RaftPeer suggestedLeader = nle.getSuggestedLeader();
            if (suggestedLeader != null) {
                nleBuilder.setSuggestedLeader(suggestedLeader.getRaftPeerProto());
            }
            nleBuilder.addAllPeersInConf(ProtoUtils.toRaftPeerProtos(nle.getPeers()));
            b.setNotLeaderException(nleBuilder.build());
        }
        final NotReplicatedException nre = reply.getNotReplicatedException();
        if (nre != null) {
            final NotReplicatedExceptionProto.Builder nreBuilder = NotReplicatedExceptionProto.newBuilder().setCallId(nre.getCallId()).setReplication(nre.getRequiredReplication()).setLogIndex(nre.getLogIndex());
            b.setNotReplicatedException(nreBuilder);
        }
        final LeaderNotReadyException lnre = reply.getLeaderNotReadyException();
        if (lnre != null) {
            LeaderNotReadyExceptionProto.Builder lnreBuilder = LeaderNotReadyExceptionProto.newBuilder().setServerId(ProtoUtils.toRaftGroupMemberIdProtoBuilder(lnre.getServerId()));
            b.setLeaderNotReadyException(lnreBuilder);
        }
        Optional.ofNullable(reply.getStateMachineException()).map(ClientProtoUtils::toStateMachineExceptionProtoBuilder).ifPresent(b::setStateMachineException);
        Optional.ofNullable(reply.getDataStreamException()).map(ProtoUtils::toThrowableProto).ifPresent(b::setDataStreamException);
        Optional.ofNullable(reply.getAlreadyClosedException()).map(ClientProtoUtils::toAlreadyClosedExceptionProtoBuilder).ifPresent(b::setAlreadyClosedException);
        Optional.ofNullable(reply.getLeaderSteppingDownException()).map(ProtoUtils::toThrowableProto).ifPresent(b::setLeaderSteppingDownException);
        Optional.ofNullable(reply.getTransferLeadershipException()).map(ProtoUtils::toThrowableProto).ifPresent(b::setTransferLeadershipException);
        final RaftClientReplyProto serialized = b.build();
        final RaftException e = reply.getException();
        if (e != null) {
            final RaftClientReply deserialized = toRaftClientReply(serialized);
            if (!Optional.ofNullable(deserialized.getException()).map(Object::getClass).filter(e.getClass()::equals).isPresent()) {
                throw new AssertionError("Corruption while serializing reply= " + reply + " but serialized=" + serialized + " and deserialized=" + deserialized, e);
            }
        }
        return serialized;
    }
    return b.build();
}
Also used : NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) RaftException(org.apache.ratis.protocol.exceptions.RaftException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException)

Aggregations

RaftException (org.apache.ratis.protocol.exceptions.RaftException)4 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)3 IOException (java.io.IOException)2 LeaderNotReadyException (org.apache.ratis.protocol.exceptions.LeaderNotReadyException)2 NotReplicatedException (org.apache.ratis.protocol.exceptions.NotReplicatedException)2 CompletionException (java.util.concurrent.CompletionException)1 ClientRetryEvent (org.apache.ratis.client.retry.ClientRetryEvent)1 ClientId (org.apache.ratis.protocol.ClientId)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1 GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)1 LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)1 RetryPolicy (org.apache.ratis.retry.RetryPolicy)1 TimeDuration (org.apache.ratis.util.TimeDuration)1