Search in sources :

Example 6 with StateMachineException

use of org.apache.ratis.protocol.exceptions.StateMachineException 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 7 with StateMachineException

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

the class ClientProtoUtils method toStateMachineException.

static StateMachineException toStateMachineException(RaftGroupMemberId memberId, String className, String errorMsg, ByteString stackTraceBytes) {
    StateMachineException sme;
    if (className == null) {
        sme = new StateMachineException(errorMsg);
    } else {
        try {
            final Class<?> clazz = Class.forName(className);
            final Exception e = ReflectionUtils.instantiateException(clazz.asSubclass(Exception.class), errorMsg);
            sme = new StateMachineException(memberId, e);
        } catch (Exception e) {
            sme = new StateMachineException(className + ": " + errorMsg);
        }
    }
    final StackTraceElement[] stacktrace = (StackTraceElement[]) ProtoUtils.toObject(stackTraceBytes);
    sme.setStackTrace(stacktrace);
    return sme;
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) 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 8 with StateMachineException

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

the class FileStoreClient method send.

static ByteString send(ByteString request, CheckedFunction<Message, RaftClientReply, IOException> sendFunction) throws IOException {
    final RaftClientReply reply = sendFunction.apply(Message.valueOf(request));
    final StateMachineException sme = reply.getStateMachineException();
    if (sme != null) {
        throw new IOException("Failed to send request " + request, sme);
    }
    Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
    return reply.getMessage().getContent();
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) IOException(java.io.IOException)

Example 9 with StateMachineException

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

the class RaftServerImpl method appendTransaction.

/**
 * Handle a normal update request from client.
 */
private CompletableFuture<RaftClientReply> appendTransaction(RaftClientRequest request, TransactionContext context, CacheEntry cacheEntry) throws IOException {
    assertLifeCycleState(LifeCycle.States.RUNNING);
    CompletableFuture<RaftClientReply> reply;
    final PendingRequest pending;
    synchronized (this) {
        reply = checkLeaderState(request, cacheEntry, true);
        if (reply != null) {
            return reply;
        }
        // append the message to its local log
        final LeaderStateImpl leaderState = role.getLeaderStateNonNull();
        final PendingRequests.Permit permit = leaderState.tryAcquirePendingRequest(request.getMessage());
        if (permit == null) {
            cacheEntry.failWithException(new ResourceUnavailableException(getMemberId() + ": Failed to acquire a pending write request for " + request));
            return cacheEntry.getReplyFuture();
        }
        try {
            state.appendLog(context);
        } catch (StateMachineException e) {
            // the StateMachineException is thrown by the SM in the preAppend stage.
            // Return the exception in a RaftClientReply.
            RaftClientReply exceptionReply = newExceptionReply(request, e);
            cacheEntry.failWithReply(exceptionReply);
            // leader will step down here
            if (e.leaderShouldStepDown() && getInfo().isLeader()) {
                leaderState.submitStepDownEvent(LeaderState.StepDownReason.STATE_MACHINE_EXCEPTION);
            }
            return CompletableFuture.completedFuture(exceptionReply);
        }
        // put the request into the pending queue
        pending = leaderState.addPendingRequest(permit, request, context);
        if (pending == null) {
            cacheEntry.failWithException(new ResourceUnavailableException(getMemberId() + ": Failed to add a pending write request for " + request));
            return cacheEntry.getReplyFuture();
        }
        leaderState.notifySenders();
    }
    return pending.getFuture();
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) ResourceUnavailableException(org.apache.ratis.protocol.exceptions.ResourceUnavailableException)

Example 10 with StateMachineException

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

the class RaftServerImpl method staleReadAsync.

private CompletableFuture<RaftClientReply> staleReadAsync(RaftClientRequest request) {
    final long minIndex = request.getType().getStaleRead().getMinIndex();
    final long commitIndex = state.getLog().getLastCommittedIndex();
    LOG.debug("{}: minIndex={}, commitIndex={}", getMemberId(), minIndex, commitIndex);
    if (commitIndex < minIndex) {
        final StaleReadException e = new StaleReadException("Unable to serve stale-read due to server commit index = " + commitIndex + " < min = " + minIndex);
        return CompletableFuture.completedFuture(newExceptionReply(request, new StateMachineException(getMemberId(), e)));
    }
    return processQueryFuture(stateMachine.queryStale(request.getMessage(), minIndex), request);
}
Also used : StaleReadException(org.apache.ratis.protocol.exceptions.StaleReadException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException)

Aggregations

StateMachineException (org.apache.ratis.protocol.exceptions.StateMachineException)13 IOException (java.io.IOException)5 Timer (com.codahale.metrics.Timer)2 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)2 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)2 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)2 LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)2 TransferLeadershipException (org.apache.ratis.protocol.exceptions.TransferLeadershipException)2 CacheEntry (org.apache.ratis.server.impl.RetryCacheImpl.CacheEntry)2 InterruptedIOException (java.io.InterruptedIOException)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 LongStream (java.util.stream.LongStream)1 RaftTestUtil (org.apache.ratis.RaftTestUtil)1 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)1 RaftClient (org.apache.ratis.client.RaftClient)1 ClientRetryEvent (org.apache.ratis.client.retry.ClientRetryEvent)1 CommitInfoProto (org.apache.ratis.proto.RaftProtos.CommitInfoProto)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1 AlreadyExistsException (org.apache.ratis.protocol.exceptions.AlreadyExistsException)1