org.apache.ratis.protocol.NotLeaderException Java Examples

The following examples show how to use org.apache.ratis.protocol.NotLeaderException. 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: OMNotLeaderException.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Convert {@link NotLeaderException} to {@link OMNotLeaderException}.
 * @param notLeaderException
 * @param currentPeer
 * @return OMNotLeaderException
 */
public static OMNotLeaderException convertToOMNotLeaderException(
    NotLeaderException notLeaderException, RaftPeerId currentPeer) {
  RaftPeerId suggestedLeader =
      notLeaderException.getSuggestedLeader() != null ?
          notLeaderException.getSuggestedLeader().getId() : null;
  OMNotLeaderException omNotLeaderException;
  if (suggestedLeader != null) {
    omNotLeaderException = new OMNotLeaderException(currentPeer,
        suggestedLeader);
  } else {
    omNotLeaderException =
        new OMNotLeaderException(currentPeer);
  }
  return omNotLeaderException;
}
 
Example #2
Source File: GrpcClientProtocolClient.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(RaftClientReplyProto proto) {
  final long callId = proto.getRpcReply().getCallId();
  try {
    final RaftClientReply reply = ClientProtoUtils.toRaftClientReply(proto);
    LOG.trace("{}: receive {}", getName(), reply);
    final NotLeaderException nle = reply.getNotLeaderException();
    if (nle != null) {
      completeReplyExceptionally(nle, NotLeaderException.class.getName());
      return;
    }
    final LeaderNotReadyException lnre = reply.getLeaderNotReadyException();
    if (lnre != null) {
      completeReplyExceptionally(lnre, LeaderNotReadyException.class.getName());
      return;
    }
    handleReplyFuture(callId, f -> f.complete(reply));
  } catch (Throwable t) {
    handleReplyFuture(callId, f -> f.completeExceptionally(t));
  }
}
 
Example #3
Source File: PendingRequests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
Collection<TransactionContext> setNotLeaderException(NotLeaderException nle,
                                                     Collection<CommitInfoProto> commitInfos) {
  synchronized (this) {
    resource.close();
    permits.clear();
  }

  LOG.debug("{}: PendingRequests.setNotLeaderException", name);
  final List<TransactionContext> transactions = new ArrayList<>(map.size());
  for(;;) {
    final Iterator<Long> i = map.keySet().iterator();
    if (!i.hasNext()) { // the map is empty
      return transactions;
    }

    final PendingRequest pending = map.remove(i.next());
    if (pending != null) {
      transactions.add(pending.setNotLeaderException(nle, commitInfos));
    }
  }
}
 
Example #4
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void processReply(RaftClientReply reply) throws IOException {
  // NotLeader exception is thrown only when the raft server to which the
  // request is submitted is not the leader. The request will be rejected
  // and will eventually be executed once the request comes via the leader
  // node.
  NotLeaderException notLeaderException = reply.getNotLeaderException();
  if (notLeaderException != null) {
    throw notLeaderException;
  }
  StateMachineException stateMachineException =
      reply.getStateMachineException();
  if (stateMachineException != null) {
    throw stateMachineException;
  }
}
 
Example #5
Source File: PendingRequests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * The leader state is stopped. Send NotLeaderException to all the pending
 * requests since they have not got applied to the state machine yet.
 */
Collection<TransactionContext> sendNotLeaderResponses(NotLeaderException nle,
                                                      Collection<CommitInfoProto> commitInfos) {
  LOG.info("{}: sendNotLeaderResponses", name);

  final Collection<TransactionContext> transactions = pendingRequests.setNotLeaderException(nle, commitInfos);
  if (pendingSetConf != null) {
    pendingSetConf.setNotLeaderException(nle, commitInfos);
  }
  return transactions;
}
 
Example #6
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(RaftClientReplyProto proto) {
  final long callId = proto.getRpcReply().getCallId();
  try {
    final RaftClientReply reply = ClientProtoUtils.toRaftClientReply(proto);
    final NotLeaderException nle = reply.getNotLeaderException();
    if (nle != null) {
      completeReplyExceptionally(nle, NotLeaderException.class.getName());
      return;
    }
    handleReplyFuture(callId, f -> f.complete(reply));
  } catch (Throwable t) {
    handleReplyFuture(callId, f -> f.completeExceptionally(t));
  }
}
 
Example #7
Source File: OzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Process the raftClientReply and return OMResponse.
 * @param omRequest
 * @param reply
 * @return OMResponse - response which is returned to client.
 * @throws ServiceException
 */
private OMResponse processReply(OMRequest omRequest, RaftClientReply reply)
    throws ServiceException {
  // NotLeader exception is thrown only when the raft server to which the
  // request is submitted is not the leader. This can happen first time
  // when client is submitting request to OM.

  if (!reply.isSuccess()) {
    NotLeaderException notLeaderException = reply.getNotLeaderException();
    if (notLeaderException != null) {
      throw new ServiceException(
          OMNotLeaderException.convertToOMNotLeaderException(
                notLeaderException, getRaftPeerId()));
    }

    LeaderNotReadyException leaderNotReadyException =
        reply.getLeaderNotReadyException();
    if (leaderNotReadyException != null) {
      throw new ServiceException(new OMLeaderNotReadyException(
          leaderNotReadyException.getMessage()));
    }

    StateMachineException stateMachineException =
        reply.getStateMachineException();
    if (stateMachineException != null) {
      OMResponse.Builder omResponse = OMResponse.newBuilder()
          .setCmdType(omRequest.getCmdType())
          .setSuccess(false)
          .setTraceID(omRequest.getTraceID());
      if (stateMachineException.getCause() != null) {
        omResponse.setMessage(stateMachineException.getCause().getMessage());
        omResponse.setStatus(
            exceptionToResponseStatus(stateMachineException.getCause()));
      } else {
        // Current Ratis is setting cause, this is an safer side check.
        LOG.error("StateMachine exception cause is not set");
        omResponse.setStatus(
            OzoneManagerProtocolProtos.Status.INTERNAL_ERROR);
        omResponse.setMessage(
            StringUtils.stringifyException(stateMachineException));
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("Error while executing ratis request. " +
            "stateMachineException: ", stateMachineException);
      }
      return omResponse.build();
    }
  }

  try {
    return OMRatisHelper.getOMResponseFromRaftClientReply(reply);
  } catch (InvalidProtocolBufferException ex) {
    if (ex.getMessage() != null) {
      throw new ServiceException(ex.getMessage(), ex);
    } else {
      throw new ServiceException(ex);
    }
  }

  // TODO: Still need to handle RaftRetry failure exception and
  //  NotReplicated exception.
}
 
Example #8
Source File: OrderedAsync.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private CompletableFuture<RaftClientReply> sendRequest(PendingOrderedRequest pending) {
  final RetryPolicy retryPolicy = client.getRetryPolicy();
  final CompletableFuture<RaftClientReply> f;
  final RaftClientRequest request;
  if (getSlidingWindow((RaftPeerId) null).isFirst(pending.getSeqNum())) {
    pending.setFirstRequest();
  }
  request = pending.newRequest();
  LOG.debug("{}: send* {}", client.getId(), request);
  f = client.getClientRpc().sendRequestAsync(request);
  return f.thenApply(reply -> {
    LOG.debug("{}: receive* {}", client.getId(), reply);
    getSlidingWindow(request).receiveReply(
        request.getSlidingWindowEntry().getSeqNum(), reply, this::sendRequestWithRetry);
    return reply;
  }).exceptionally(e -> {
    if (LOG.isTraceEnabled()) {
      LOG.trace(client.getId() + ": Failed* " + request, e);
    } else {
      LOG.debug("{}: Failed* {} with {}", client.getId(), request, e);
    }
    e = JavaUtils.unwrapCompletionException(e);
    if (e instanceof IOException && !(e instanceof GroupMismatchException)) {
      pending.incrementExceptionCount(e);
      final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
      if (!retryPolicy.handleAttemptFailure(event).shouldRetry()) {
        handleAsyncRetryFailure(event);
      } else {
        if (e instanceof NotLeaderException) {
          NotLeaderException nle = (NotLeaderException)e;
          client.handleNotLeaderException(request, nle, this::resetSlidingWindow);
        } else {
          client.handleIOException(request, (IOException) e, null, this::resetSlidingWindow);
        }
      }
      throw new CompletionException(e);
    }
    failAllAsyncRequests(request, e);
    return null;
  });
}
 
Example #9
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);
    }
  });
}