Java Code Examples for org.apache.ratis.protocol.RaftClientReply#getNotLeaderException()

The following examples show how to use org.apache.ratis.protocol.RaftClientReply#getNotLeaderException() . 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: 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 2
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 3
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 4
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.
}