Search in sources :

Example 11 with StateMachineException

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

the class RaftServerImpl method replyPendingRequest.

/**
 * The log has been submitted to the state machine. Use the future to update
 * the pending requests and retry cache.
 * @param logEntry the log entry that has been submitted to the state machine
 * @param stateMachineFuture the future returned by the state machine
 *                           from which we will get transaction result later
 */
private CompletableFuture<Message> replyPendingRequest(LogEntryProto logEntry, CompletableFuture<Message> stateMachineFuture) {
    Preconditions.assertTrue(logEntry.hasStateMachineLogEntry());
    final ClientInvocationId invocationId = ClientInvocationId.valueOf(logEntry.getStateMachineLogEntry());
    // update the retry cache
    final CacheEntry cacheEntry = retryCache.getOrCreateEntry(invocationId);
    if (getInfo().isLeader()) {
        Preconditions.assertTrue(cacheEntry != null && !cacheEntry.isCompletedNormally(), "retry cache entry should be pending: %s", cacheEntry);
    }
    if (cacheEntry.isFailed()) {
        retryCache.refreshEntry(new CacheEntry(cacheEntry.getKey()));
    }
    final long logIndex = logEntry.getIndex();
    return stateMachineFuture.whenComplete((reply, exception) -> {
        final RaftClientReply.Builder b = newReplyBuilder(invocationId, logIndex);
        final RaftClientReply r;
        if (exception == null) {
            r = b.setSuccess().setMessage(reply).build();
        } else {
            // the exception is coming from the state machine. wrap it into the
            // reply as a StateMachineException
            final StateMachineException e = new StateMachineException(getMemberId(), exception);
            r = b.setException(e).build();
        }
        // update pending request
        role.getLeaderState().ifPresent(leader -> leader.replyPendingRequest(logIndex, r));
        cacheEntry.updateResult(r);
    });
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) CacheEntry(org.apache.ratis.server.impl.RetryCacheImpl.CacheEntry)

Example 12 with StateMachineException

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

the class SimpleStateMachine4Testing method query.

/**
 * Query the n-th log entry.
 * @param request an index represented in a UTF-8 String, or an empty message.
 * @return a completed future of the n-th log entry,
 *         where n is the last applied index if the request is empty,
 *         otherwise, n is the index represented in the request.
 */
@Override
public CompletableFuture<Message> query(Message request) {
    final String string = request.getContent().toStringUtf8();
    Exception exception;
    try {
        LOG.info("query " + string);
        final LogEntryProto entry = dataMap.get(string);
        if (entry != null) {
            return CompletableFuture.completedFuture(Message.valueOf(entry.toByteString()));
        }
        exception = new IndexOutOfBoundsException(getId() + ": LogEntry not found for query " + string);
    } catch (Exception e) {
        LOG.warn("Failed request " + request, e);
        exception = e;
    }
    return JavaUtils.completeExceptionally(new StateMachineException("Failed request " + request, exception));
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with StateMachineException

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

the class RaftStateMachineExceptionTests method runTestHandleStateMachineException.

private void runTestHandleStateMachineException(CLUSTER cluster) throws Exception {
    RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        client.io().send(new RaftTestUtil.SimpleMessage("m"));
        fail("Exception expected");
    } catch (StateMachineException e) {
        e.printStackTrace();
        Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception"));
    }
    cluster.shutdown();
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftTestUtil(org.apache.ratis.RaftTestUtil) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient)

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