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

The following examples show how to use org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto. 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
/**
 * Create initial log entry to initialize the log.
 */
private AppendEntriesRequestProto createInitialLogEntry(long callId) {

  RaftRpcRequestProto serverRequest = createServerRequest(callId);

  long index = 0L;
  LogEntryProto logEntry = LogEntryProto.newBuilder()
      .setTerm(term)
      .setIndex(index)
      .setConfigurationEntry(
          RaftConfigurationProto.newBuilder()
              .addPeers(RaftPeerProto.newBuilder()
                  .setId(RaftPeerId.valueOf(serverAddress).toByteString())
                  .setAddress(serverAddress)
                  .build())
              .addPeers(requestor)
              .build()
      )
      .build();

  return AppendEntriesRequestProto.newBuilder()
      .setLeaderTerm(term)
      .addEntries(logEntry)
      .setServerRequest(serverRequest)
      .build();
}
 
Example #2
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 6 votes vote down vote up
private void appendLog() throws IOException {
  final AppendEntriesRequestProto pending;
  final StreamObserver<AppendEntriesRequestProto> s;
  synchronized (this) {
    // prepare and enqueue the append request. note changes on follower's
    // nextIndex and ops on pendingRequests should always be associated
    // together and protected by the lock
    pending = createRequest(callId++);
    if (pending == null) {
      return;
    }
    pendingRequests.put(pending.getServerRequest().getCallId(), pending);
    updateNextIndex(pending);
    if (appendLogRequestObserver == null) {
      appendLogRequestObserver = getClient().appendEntries(new AppendLogResponseHandler());
    }
    s = appendLogRequestObserver;
  }

  if (isAppenderRunning()) {
    sendRequest(pending, s);
  }
}
 
Example #3
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void appendLog() throws IOException {
  final AppendEntriesRequestProto pending;
  final AppendEntriesRequest request;
  final StreamObserver<AppendEntriesRequestProto> s;
  synchronized (this) {
    // prepare and enqueue the append request. note changes on follower's
    // nextIndex and ops on pendingRequests should always be associated
    // together and protected by the lock
    pending = createRequest(callId++);
    if (pending == null) {
      return;
    }
    request = new AppendEntriesRequest(pending, getFollowerId(), grpcServerMetrics);
    pendingRequests.put(request);
    increaseNextIndex(pending);
    if (appendLogRequestObserver == null) {
      appendLogRequestObserver = getClient().appendEntries(new AppendLogResponseHandler());
    }
    s = appendLogRequestObserver;
  }

  if (isAppenderRunning()) {
    sendRequest(request, pending, s);
  }
}
 
Example #4
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
private void sendRequest(AppendEntriesRequestProto request,
    StreamObserver<AppendEntriesRequestProto> s) {
  CodeInjectionForTesting.execute(GrpcService.GRPC_SEND_SERVER_REQUEST,
      server.getId(), null, request);

  s.onNext(request);
  scheduler.onTimeout(requestTimeoutDuration, () -> timeoutAppendRequest(request), LOG,
      () -> "Timeout check failed for append entry request: " + request);
  follower.updateLastRpcSendTime();
}
 
Example #5
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 #6
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void sendRequest(AppendEntriesRequest request, AppendEntriesRequestProto proto,
      StreamObserver<AppendEntriesRequestProto> s) {
  CodeInjectionForTesting.execute(GrpcService.GRPC_SEND_SERVER_REQUEST,
      getServer().getId(), null, proto);
  request.startRequestTimer();
  s.onNext(proto);
  scheduler.onTimeout(requestTimeoutDuration,
      () -> timeoutAppendRequest(request.getCallId(), request.isHeartbeat()),
      LOG, () -> "Timeout check failed for append entry request: " + request);
  getFollower().updateLastRpcSendTime();
}
 
Example #7
Source File: LeaderState.java    From ratis with Apache License 2.0 5 votes vote down vote up
AppendEntriesRequestProto newAppendEntriesRequestProto(RaftPeerId targetId,
    TermIndex previous, List<LogEntryProto> entries, boolean initializing,
    long callId) {
  return ServerProtoUtils.toAppendEntriesRequestProto(server.getId(), targetId,
      server.getGroupId(), currentTerm, entries, raftLog.getLastCommittedIndex(),
      initializing, previous, server.getCommitInfos(), callId);
}
 
Example #8
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
AppendEntriesRequest(AppendEntriesRequestProto proto, RaftPeerId followerId, GrpcServerMetrics grpcServerMetrics) {
  this.callId = proto.getServerRequest().getCallId();
  this.previousLog = proto.hasPreviousLog()? ServerProtoUtils.toTermIndex(proto.getPreviousLog()): null;
  this.entriesCount = proto.getEntriesCount();
  this.lastEntry = entriesCount > 0? ServerProtoUtils.toTermIndex(proto.getEntries(entriesCount - 1)): null;

  this.timer = grpcServerMetrics.getGrpcLogAppenderLatencyTimer(followerId.toString(), isHeartbeat());
  grpcServerMetrics.onRequestCreate(isHeartbeat());
}
 
Example #9
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
private synchronized void resetClient(AppendEntriesRequestProto request) {
  rpcService.getProxies().resetProxy(follower.getPeer().getId());
  appendLogRequestObserver = null;
  firstResponseReceived = false;

  // clear the pending requests queue and reset the next index of follower
  final long nextIndex = request != null && request.hasPreviousLog()?
      request.getPreviousLog().getIndex() + 1: raftLog.getStartIndex();
  clearPendingRequests(nextIndex);
}
 
Example #10
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 #11
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 #12
Source File: LeaderState.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
AppendEntriesRequestProto newAppendEntriesRequestProto(RaftPeerId targetId,
    TermIndex previous, List<LogEntryProto> entries, boolean initializing,
    long callId) {
  return ServerProtoUtils.toAppendEntriesRequestProto(server.getMemberId(), targetId,
      currentTerm, entries, raftLog.getLastCommittedIndex(),
      initializing, previous, server.getCommitInfos(), callId);
}
 
Example #13
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 #14
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 #15
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 #16
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 4 votes vote down vote up
private void timeoutAppendRequest(AppendEntriesRequestProto request) {
  AppendEntriesRequestProto pendingRequest = pendingRequests.remove(request.getServerRequest().getCallId());
  if (pendingRequest != null) {
    LOG.warn( "{}: appendEntries Timeout, request={}", this, ProtoUtils.toString(pendingRequest.getServerRequest()));
  }
}
 
Example #17
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 4 votes vote down vote up
private void updateNextIndex(AppendEntriesRequestProto request) {
  final int count = request.getEntriesCount();
  if (count > 0) {
    follower.updateNextIndex(request.getEntries(count - 1).getIndex() + 1);
  }
}
 
Example #18
Source File: RaftServerRequest.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
AppendEntriesRequestProto getAppendEntries() {
  return appendEntries;
}
 
Example #19
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 #20
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 #21
Source File: RaftServerRequest.java    From ratis with Apache License 2.0 4 votes vote down vote up
RaftServerRequest(AppendEntriesRequestProto a) {
  appendEntries = a;
  requestVote = null;
  installSnapshot = null;
}
 
Example #22
Source File: RaftServerRequest.java    From ratis with Apache License 2.0 4 votes vote down vote up
AppendEntriesRequestProto getAppendEntries() {
  return appendEntries;
}
 
Example #23
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 #24
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 #25
Source File: RaftServerRequest.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
RaftServerRequest(AppendEntriesRequestProto a) {
  appendEntries = a;
  requestVote = null;
  installSnapshot = null;
}
 
Example #26
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 #27
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 #28
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 #29
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private void increaseNextIndex(AppendEntriesRequestProto request) {
  final int count = request.getEntriesCount();
  if (count > 0) {
    getFollower().increaseNextIndex(request.getEntries(count - 1).getIndex() + 1);
  }
}
 
Example #30
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));
}