org.apache.ratis.protocol.RaftException Java Examples

The following examples show how to use org.apache.ratis.protocol.RaftException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnorderedAsync.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
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, null);
      if (reply != null) {
        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 (Throwable t) {
      LOG.error(clientId + ": Failed " + request, t);
      f.completeExceptionally(t);
    }
  });
}
 
Example #2
Source File: PendingRequests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void failSetConfiguration(RaftException e) {
  Preconditions.assertTrue(pendingSetConf != null);
  pendingSetConf.setException(e);
  pendingSetConf = null;
}