org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto Java Examples

The following examples show how to use org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto. 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: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Handle async response.
 */
@Override
public void onNext(AppendEntriesReplyProto reply) {
  long callId = reply.getServerReply().getCallId();
  if (!inFlightMessages.remove(callId)) {
    LOG.warn(
        "Received message with callId which was not used to send message: {}",
        callId);
    LOG.info("{}", reply);
  }
  long lastCommit = reply.getFollowerCommit();
  if (lastCommit % 1000 == 0) {
    long currentIndex = getAttemptCounter().get();
    if (currentIndex - lastCommit > batching * 3) {
      LOG.warn(
          "Last committed index ({}) is behind the current index ({}) on "
              + "the client side.",
          lastCommit, currentIndex);
    }
  }

}
 
Example #2
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 6 votes vote down vote up
private void onNextImpl(AppendEntriesReplyProto reply) {
  // update the last rpc time
  follower.updateLastRpcResponseTime();

  if (!firstResponseReceived) {
    firstResponseReceived = true;
  }
  switch (reply.getResult()) {
    case SUCCESS:
      onSuccess(reply);
      break;
    case NOT_LEADER:
      onNotLeader(reply);
      break;
    case INCONSISTENCY:
      onInconsistency(reply);
      break;
    default:
      break;
  }
  notifyAppend();
}
 
Example #3
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * After receiving a appendEntries reply, do the following:
 * 1. If the reply is success, update the follower's match index and submit
 *    an event to leaderState
 * 2. If the reply is NOT_LEADER, step down
 * 3. If the reply is INCONSISTENCY, increase/ decrease the follower's next
 *    index based on the response
 */
@Override
public void onNext(AppendEntriesReplyProto reply) {
  AppendEntriesRequest request = pendingRequests.remove(reply);
  if (request != null) {
    request.stopRequestTimer(); // Update completion time
  }

  if (LOG.isDebugEnabled()) {
    LOG.debug("{}: received {} reply {}, request={}",
        this, firstResponseReceived? "a": "the first",
        ServerProtoUtils.toString(reply), request);
  }

  try {
    onNextImpl(reply);
  } catch(Throwable t) {
    LOG.error("Failed onNext request=" + request
        + ", reply=" + ServerProtoUtils.toString(reply), t);
  }
}
 
Example #4
Source File: RaftServerProtocolServerSideTranslatorPB.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(
    RpcController unused, AppendEntriesRequestProto request)
    throws ServiceException {
  try {
    return impl.appendEntries(request);
  } catch(IOException ioe) {
    throw new ServiceException(ioe);
  }
}
 
Example #5
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * After receiving a appendEntries reply, do the following:
 * 1. If the reply is success, update the follower's match index and submit
 *    an event to leaderState
 * 2. If the reply is NOT_LEADER, step down
 * 3. If the reply is INCONSISTENCY, decrease the follower's next index
 *    based on the response
 */
@Override
public void onNext(AppendEntriesReplyProto reply) {
  if (LOG.isDebugEnabled()) {
    LOG.debug("{}<-{}: received {} reply {} ", server.getId(), follower.getPeer(),
        (!firstResponseReceived? "the first": "a"), ServerProtoUtils.toString(reply));
  }

  try {
    onNextImpl(reply);
  } catch(Throwable t) {
    LOG.error("Failed onNext " + reply, t);
  }
}
 
Example #6
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
protected synchronized void onSuccess(AppendEntriesReplyProto reply) {
  AppendEntriesRequestProto request = pendingRequests.remove(reply.getServerReply().getCallId());
  if (request == null) {
    // If reply comes after timeout, the reply is ignored.
    LOG.warn("{}: Request not found, ignoring reply: {}", this, ServerProtoUtils.toString(reply));
    return;
  }
  updateCommitIndex(reply.getFollowerCommit());

  final long replyNextIndex = reply.getNextIndex();
  final long lastIndex = replyNextIndex - 1;
  final boolean updateMatchIndex;

  if (request.getEntriesCount() == 0) {
    Preconditions.assertTrue(!request.hasPreviousLog() ||
            lastIndex == request.getPreviousLog().getIndex(),
        "reply's next index is %s, request's previous is %s",
        replyNextIndex, request.getPreviousLog());
    updateMatchIndex = request.hasPreviousLog() && follower.getMatchIndex() < lastIndex;
  } else {
    // check if the reply and the pending request is consistent
    final long lastEntryIndex = request
        .getEntries(request.getEntriesCount() - 1).getIndex();
    Preconditions.assertTrue(lastIndex == lastEntryIndex,
        "reply's next index is %s, request's last entry index is %s",
        replyNextIndex, lastEntryIndex);
    updateMatchIndex = true;
  }
  if (updateMatchIndex) {
    follower.updateMatchIndex(lastIndex);
    submitEventOnSuccessAppend();
  }
}
 
Example #7
Source File: SimulatedServerRpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(AppendEntriesRequestProto request)
    throws IOException {
  RaftServerReply reply = serverHandler.getRpc()
      .sendRequest(new RaftServerRequest(request));
  return reply.getAppendEntries();
}
 
Example #8
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
private synchronized void onInconsistency(AppendEntriesReplyProto reply) {
  AppendEntriesRequestProto request = pendingRequests.remove(reply.getServerReply().getCallId());
  if (request == null) {
    // If reply comes after timeout, the reply is ignored.
    LOG.warn("{}: Ignoring {}", server.getId(), reply);
    return;
  }
  Preconditions.assertTrue(request.hasPreviousLog());
  if (request.getPreviousLog().getIndex() >= reply.getNextIndex()) {
    clearPendingRequests(reply.getNextIndex());
  }
}
 
Example #9
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void onNextImpl(AppendEntriesReplyProto reply) {
  // update the last rpc time
  getFollower().updateLastRpcResponseTime();

  if (!firstResponseReceived) {
    firstResponseReceived = true;
  }

  switch (reply.getResult()) {
    case SUCCESS:
      grpcServerMetrics.onRequestSuccess(getFollowerId().toString(), reply.getIsHearbeat());
      updateCommitIndex(reply.getFollowerCommit());
      if (getFollower().updateMatchIndex(reply.getMatchIndex())) {
        submitEventOnSuccessAppend();
      }
      break;
    case NOT_LEADER:
      grpcServerMetrics.onRequestNotLeader(getFollowerId().toString());
      if (checkResponseTerm(reply.getTerm())) {
        return;
      }
      break;
    case INCONSISTENCY:
      grpcServerMetrics.onRequestInconsistency(getFollowerId().toString());
      updateNextIndex(reply.getNextIndex());
      break;
    default:
      throw new IllegalStateException("Unexpected reply result: " + reply.getResult());
  }
  notifyAppend();
}
 
Example #10
Source File: RaftServerProtocolServerSideTranslatorPB.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(
    RpcController unused, AppendEntriesRequestProto request)
    throws ServiceException {
  try {
    return impl.appendEntries(request);
  } catch(IOException ioe) {
    throw new ServiceException(ioe);
  }
}
 
Example #11
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto request) {
  final RaftGroupId groupId = ProtoUtils.toRaftGroupId(request.getServerRequest().getRaftGroupId());
  return submitRequest(groupId, impl -> impl.appendEntriesAsync(request));
}
 
Example #12
Source File: RaftServerAsynchronousProtocol.java    From ratis with Apache License 2.0 4 votes vote down vote up
CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto request)
throws IOException;
 
Example #13
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(AppendEntriesRequestProto request) throws IOException {
  return getImpl(request.getServerRequest()).appendEntries(request);
}
 
Example #14
Source File: RaftServerReply.java    From ratis with Apache License 2.0 4 votes vote down vote up
RaftServerReply(AppendEntriesReplyProto a) {
  appendEntries = Objects.requireNonNull(a);
  requestVote = null;
  installSnapshot = null;
}
 
Example #15
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 4 votes vote down vote up
private void onNotLeader(AppendEntriesReplyProto reply) {
  checkResponseTerm(reply.getTerm());
  // the running loop will end and the connection will onComplete
}
 
Example #16
Source File: FakeRatisFollower.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
public static StreamObserver<AppendEntriesRequestProto> appendEntries(
    RaftPeerId raftPeerId,
    StreamObserver<AppendEntriesReplyProto> responseHandler) {
  return new StreamObserver<AppendEntriesRequestProto>() {
    private long maxIndex = -1L;

    @Override
    public void onNext(AppendEntriesRequestProto value) {

      for (LogEntryProto entry : value.getEntriesList()) {
        if (entry.getIndex() > maxIndex) {
          maxIndex = entry.getIndex();
        }
      }

      long maxCommitted = value.getCommitInfosList()
          .stream()
          .mapToLong(CommitInfoProto::getCommitIndex)
          .max().orElseGet(() -> 0L);

      maxCommitted = Math.min(maxCommitted, maxIndex);

      AppendEntriesReplyProto response = AppendEntriesReplyProto.newBuilder()
          .setNextIndex(maxIndex + 1)
          .setFollowerCommit(maxCommitted)
          .setResult(AppendResult.SUCCESS)
          .setTerm(value.getLeaderTerm())
          .setMatchIndex(maxIndex)
          .setServerReply(RaftRpcReplyProto.newBuilder()
              .setSuccess(true)
              .setRequestorId(value.getServerRequest().getRequestorId())
              .setReplyId(raftPeerId.toByteString())
              .setCallId(value.getServerRequest().getCallId())
              .setRaftGroupId(value.getServerRequest().getRaftGroupId()))
          .build();
      maxCommitted = Math.min(value.getLeaderCommit(), maxIndex);
      addLatency();
      responseHandler.onNext(response);
    }

    @Override
    public void onError(Throwable t) {

    }

    @Override
    public void onCompleted() {

    }
  };
}
 
Example #17
Source File: RaftServerReply.java    From ratis with Apache License 2.0 4 votes vote down vote up
AppendEntriesReplyProto getAppendEntries() {
  return appendEntries;
}
 
Example #18
Source File: HadoopRpcService.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(
    AppendEntriesRequestProto request) throws IOException {
  return processRequest(request, request.getServerRequest().getReplyId(),
      proxy -> proxy.appendEntries(null, request));
}
 
Example #19
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static String toString(AppendEntriesReplyProto proto) {
  return toString(proto.getServerReply()) + "-t" + proto.getTerm()
      + ", nextIndex=" + proto.getNextIndex()
      + ", result: " + proto.getResult();
}
 
Example #20
Source File: RaftServerReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
AppendEntriesReplyProto getAppendEntries() {
  return appendEntries;
}
 
Example #21
Source File: RaftServerReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
RaftServerReply(AppendEntriesReplyProto a) {
  appendEntries = Objects.requireNonNull(a);
  requestVote = null;
  installSnapshot = null;
}
 
Example #22
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(AppendEntriesRequestProto request) throws IOException {
  return getImpl(request.getServerRequest()).appendEntries(request);
}
 
Example #23
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto request) {
  final RaftGroupId groupId = ProtoUtils.toRaftGroupId(request.getServerRequest().getRaftGroupId());
  return submitRequest(groupId, impl -> impl.appendEntriesAsync(request));
}
 
Example #24
Source File: RaftServerAsynchronousProtocol.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto request)
throws IOException;
 
Example #25
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
AppendEntriesRequest remove(AppendEntriesReplyProto reply) {
  return remove(reply.getServerReply().getCallId(), reply.getIsHearbeat());
}
 
Example #26
Source File: HadoopRpcService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public AppendEntriesReplyProto appendEntries(
    AppendEntriesRequestProto request) throws IOException {
  return processRequest(request, request.getServerRequest().getReplyId(),
      proxy -> proxy.appendEntries(null, request));
}
 
Example #27
Source File: RaftServerProtocol.java    From ratis with Apache License 2.0 votes vote down vote up
AppendEntriesReplyProto appendEntries(AppendEntriesRequestProto request) throws IOException; 
Example #28
Source File: RaftServerProtocol.java    From incubator-ratis with Apache License 2.0 votes vote down vote up
AppendEntriesReplyProto appendEntries(AppendEntriesRequestProto request) throws IOException;