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

The following examples show how to use org.apache.ratis.proto.RaftProtos.CommitInfoProto. 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: RaftClientReply.java    From ratis with Apache License 2.0 6 votes vote down vote up
public RaftClientReply(
    ClientId clientId, RaftPeerId serverId, RaftGroupId groupId,
    long callId, boolean success, Message message, RaftException exception,
    long logIndex, Collection<CommitInfoProto> commitInfos) {
  super(clientId, serverId, groupId);
  this.success = success;
  this.callId = callId;
  this.message = message;
  this.exception = exception;
  this.logIndex = logIndex;
  this.commitInfos = commitInfos != null? commitInfos: Collections.emptyList();

  if (exception != null) {
    Preconditions.assertTrue(!success,
        () -> "Inconsistent parameters: success && exception != null: " + this);
    Preconditions.assertTrue(ReflectionUtils.isInstance(exception,
        NotLeaderException.class, NotReplicatedException.class, StateMachineException.class,
        RaftRetryFailureException.class), () -> "Unexpected exception class: " + this);
  }
}
 
Example #2
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 #3
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<RaftClientReply> groupRemoveAsync(
    RaftClientRequest request, RaftGroupId groupId, boolean deleteDirectory) {
  if (!request.getRaftGroupId().equals(groupId)) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Request group id (" + request.getRaftGroupId() + ") does not match the given group id " +
            groupId));
  }
  final CompletableFuture<RaftServerImpl> f = impls.remove(groupId);
  if (f == null) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Group " + groupId + " not found."));
  }
  return f.thenApply(impl -> {
    final Collection<CommitInfoProto> commitInfos = impl.getCommitInfos();
    impl.shutdown(deleteDirectory);
    impl.getStateMachine().notifyGroupRemove();
    return new RaftClientReply(request, commitInfos);
  });
}
 
Example #4
Source File: LeaderState.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
void stop() {
  this.running = false;
  // do not interrupt event processor since it may be in the middle of logSync
  senders.forEach(LogAppender::stopAppender);
  final NotLeaderException nle = server.generateNotLeaderException();
  final Collection<CommitInfoProto> commitInfos = server.getCommitInfos();
  try {
    final Collection<TransactionContext> transactions = pendingRequests.sendNotLeaderResponses(nle, commitInfos);
    server.getStateMachine().notifyNotLeader(transactions);
    watchRequests.failWatches(nle);
  } catch (IOException e) {
    LOG.warn("{}: Caught exception in sendNotLeaderResponses", this, e);
  }
  streamRequests.clear();
  server.getServerRpc().notifyNotLeader(server.getMemberId().getGroupId());
  logAppenderMetrics.unregister();
  raftServerMetrics.unregister();
  if (pendingRequests != null) {
    pendingRequests.close();
  }
}
 
Example #5
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void checkAll(List<CompletableFuture<WatchReplies>> watches, Logger LOG) throws Exception {
  for(int i = 0; i < watches.size(); i++) {
    final WatchReplies watchReplies = watches.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
    final long logIndex = watchReplies.logIndex;
    LOG.info("checkAll {}: logIndex={}", i, logIndex);
    final RaftClientReply watchAllReply = watchReplies.getAll();
    Assert.assertTrue(watchAllReply.isSuccess());

    final RaftClientReply watchAllCommittedReply = watchReplies.getAllCommitted();
    Assert.assertTrue(watchAllCommittedReply.isSuccess());
    { // check commit infos
      final Collection<CommitInfoProto> commitInfos = watchAllCommittedReply.getCommitInfos();
      final String message = "logIndex=" + logIndex + ", " + ProtoUtils.toString(commitInfos);
      Assert.assertEquals(NUM_SERVERS, commitInfos.size());
      commitInfos.forEach(info -> Assert.assertTrue(message, logIndex <= info.getCommitIndex()));
    }
  }
}
 
Example #6
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<RaftClientReply> groupRemoveAsync(
    RaftClientRequest request, RaftGroupId groupId, boolean deleteDirectory) {
  if (!request.getRaftGroupId().equals(groupId)) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Request group id (" + request.getRaftGroupId() + ") does not match the given group id " + groupId));
  }
  final CompletableFuture<RaftServerImpl> f = impls.remove(groupId);
  if (f == null) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Group " + groupId + " not found."));
  }
  return f.thenApply(impl -> {
    final Collection<CommitInfoProto> commitInfos = impl.getCommitInfos();
    impl.shutdown(deleteDirectory);
    return new RaftClientReply(request, commitInfos);
  });
}
 
Example #7
Source File: RaftClientReply.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("parameternumber")
public RaftClientReply(
    ClientId clientId, RaftPeerId serverId, RaftGroupId groupId,
    long callId, boolean success, Message message, RaftException exception,
    long logIndex, Collection<CommitInfoProto> commitInfos) {
  super(clientId, serverId, groupId);
  this.success = success;
  this.callId = callId;
  this.message = message;
  this.exception = exception;
  this.logIndex = logIndex;
  this.commitInfos = commitInfos != null? commitInfos: Collections.emptyList();

  if (exception != null) {
    Preconditions.assertTrue(!success,
        () -> "Inconsistent parameters: success && exception != null: " + this);
    Preconditions.assertTrue(ReflectionUtils.isInstance(exception,
        AlreadyClosedException.class,
        NotLeaderException.class, NotReplicatedException.class,
        LeaderNotReadyException.class, StateMachineException.class),
        () -> "Unexpected exception class: " + this);
  }
}
 
Example #8
Source File: WatchRequestTests.java    From ratis with Apache License 2.0 6 votes vote down vote up
static void checkAll(List<CompletableFuture<WatchReplies>> watches, Logger LOG) throws Exception {
  for(int i = 0; i < watches.size(); i++) {
    final WatchReplies watchReplies = watches.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
    final long logIndex = watchReplies.logIndex;
    LOG.info("checkAll {}: logIndex={}", i, logIndex);
    final RaftClientReply watchAllReply = watchReplies.getAll();
    Assert.assertTrue(watchAllReply.isSuccess());

    final RaftClientReply watchAllCommittedReply = watchReplies.getAllCommitted();
    Assert.assertTrue(watchAllCommittedReply.isSuccess());
    { // check commit infos
      final Collection<CommitInfoProto> commitInfos = watchAllCommittedReply.getCommitInfos();
      final String message = "logIndex=" + logIndex + ", " + ProtoUtils.toString(commitInfos);
      Assert.assertEquals(NUM_SERVERS, commitInfos.size());
      commitInfos.forEach(info -> Assert.assertTrue(message, logIndex <= info.getCommitIndex()));
    }
  }
}
 
Example #9
Source File: PendingRequests.java    From ratis with Apache License 2.0 5 votes vote down vote up
void replySetConfiguration(Supplier<Collection<CommitInfoProto>> getCommitInfos) {
  // we allow the pendingRequest to be null in case that the new leader
  // commits the new configuration while it has not received the retry
  // request from the client
  if (pendingSetConf != null) {
    final RaftClientRequest request = pendingSetConf.getRequest();
    LOG.debug("{}: sends success for {}", name, request);
    // for setConfiguration we do not need to wait for statemachine. send back
    // reply after it's committed.
    pendingSetConf.setReply(new RaftClientReply(request, getCommitInfos.get()));
    pendingSetConf = null;
  }
}
 
Example #10
Source File: PendingRequests.java    From ratis with Apache License 2.0 5 votes vote down vote up
Collection<TransactionContext> setNotLeaderException(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) {
  LOG.debug("{}: PendingRequests.setNotLeaderException", name);
  try {
    return map.values().stream()
        .map(p -> p.setNotLeaderException(nle, commitInfos))
        .collect(Collectors.toList());
  } finally {
    map.clear();
  }
}
 
Example #11
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static void checkMajority(List<CompletableFuture<RaftClientReply>> replies,
    List<CompletableFuture<WatchReplies>> watches, Logger LOG) throws Exception {
  for(int i = 0; i < replies.size(); i++) {
    final RaftClientReply reply = replies.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
    LOG.info("checkMajority {}: receive {}", i, reply);
    final long logIndex = reply.getLogIndex();
    Assert.assertTrue(reply.isSuccess());

    final WatchReplies watchReplies = watches.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
    Assert.assertEquals(logIndex, watchReplies.logIndex);
    final RaftClientReply watchMajorityReply = watchReplies.getMajority();
    Assert.assertTrue(watchMajorityReply.isSuccess());

    final RaftClientReply watchMajorityCommittedReply = watchReplies.getMajorityCommitted();
    Assert.assertTrue(watchMajorityCommittedReply.isSuccess());
    { // check commit infos
      final Collection<CommitInfoProto> commitInfos = watchMajorityCommittedReply.getCommitInfos();
      final String message = "logIndex=" + logIndex + ", " + ProtoUtils.toString(commitInfos);
      Assert.assertEquals(NUM_SERVERS, commitInfos.size());

      // One follower has not committed, so min must be less than logIndex
      final long min = commitInfos.stream().map(CommitInfoProto::getCommitIndex).min(Long::compare).get();
      Assert.assertTrue(message, logIndex > min);

      // All other followers have committed
      commitInfos.stream()
          .map(CommitInfoProto::getCommitIndex).sorted(Long::compare)
          .skip(1).forEach(ci -> Assert.assertTrue(message, logIndex <= ci));
    }
  }
}
 
Example #12
Source File: GroupInfoReply.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GroupInfoReply(
        ClientId clientId, RaftPeerId serverId, RaftGroupId groupId,
        long callId, boolean success, RoleInfoProto roleInfoProto,
        boolean isRaftStorageHealthy, Collection<CommitInfoProto> commitInfos, RaftGroup group) {
  super(clientId, serverId, groupId, callId, success, null, null, 0L, commitInfos);
  this.roleInfoProto = roleInfoProto;
  this.isRaftStorageHealthy = isRaftStorageHealthy;
  this.group = group;
}
 
Example #13
Source File: GroupInfoReply.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GroupInfoReply(
        RaftClientRequest request, RoleInfoProto roleInfoProto,
        boolean isRaftStorageHealthy, Collection<CommitInfoProto> commitInfos, RaftGroup group) {
  super(request, commitInfos);
  this.roleInfoProto = roleInfoProto;
  this.isRaftStorageHealthy = isRaftStorageHealthy;
  this.group = group;
}
 
Example #14
Source File: PendingRequests.java    From 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 #15
Source File: LeaderState.java    From ratis with Apache License 2.0 5 votes vote down vote up
void stop() {
  this.running = false;
  // do not interrupt event processor since it may be in the middle of logSync
  senders.forEach(LogAppender::stopAppender);
  final NotLeaderException nle = server.generateNotLeaderException();
  final Collection<CommitInfoProto> commitInfos = server.getCommitInfos();
  try {
    final Collection<TransactionContext> transactions = pendingRequests.sendNotLeaderResponses(nle, commitInfos);
    server.getStateMachine().notifyNotLeader(transactions);
    watchRequests.failWatches(nle);
  } catch (IOException e) {
    LOG.warn(server.getId() + ": Caught exception in sendNotLeaderResponses", e);
  }
}
 
Example #16
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 #17
Source File: PendingRequests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
void replySetConfiguration(Supplier<Collection<CommitInfoProto>> getCommitInfos) {
  // we allow the pendingRequest to be null in case that the new leader
  // commits the new configuration while it has not received the retry
  // request from the client
  if (pendingSetConf != null) {
    final RaftClientRequest request = pendingSetConf.getRequest();
    LOG.debug("{}: sends success for {}", name, request);
    // for setConfiguration we do not need to wait for statemachine. send back
    // reply after it's committed.
    pendingSetConf.setReply(new RaftClientReply(request, getCommitInfos.get()));
    pendingSetConf = null;
  }
}
 
Example #18
Source File: WatchRequestTests.java    From ratis with Apache License 2.0 5 votes vote down vote up
static void checkMajority(List<CompletableFuture<RaftClientReply>> replies,
    List<CompletableFuture<WatchReplies>> watches, Logger LOG) throws Exception {
  for(int i = 0; i < replies.size(); i++) {
    final RaftClientReply reply = replies.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
    LOG.info("checkMajority {}: receive {}", i, reply);
    final long logIndex = reply.getLogIndex();
    Assert.assertTrue(reply.isSuccess());

    final WatchReplies watchReplies = watches.get(i).get(GET_TIMEOUT_SECOND, TimeUnit.SECONDS);
    Assert.assertEquals(logIndex, watchReplies.logIndex);
    final RaftClientReply watchMajorityReply = watchReplies.getMajority();
    Assert.assertTrue(watchMajorityReply.isSuccess());

    final RaftClientReply watchMajorityCommittedReply = watchReplies.getMajorityCommitted();
    Assert.assertTrue(watchMajorityCommittedReply.isSuccess());
    { // check commit infos
      final Collection<CommitInfoProto> commitInfos = watchMajorityCommittedReply.getCommitInfos();
      final String message = "logIndex=" + logIndex + ", " + ProtoUtils.toString(commitInfos);
      Assert.assertEquals(NUM_SERVERS, commitInfos.size());

      // One follower has not committed, so min must be less than logIndex
      final long min = commitInfos.stream().map(CommitInfoProto::getCommitIndex).min(Long::compare).get();
      Assert.assertTrue(message, logIndex > min);

      // All other followers have committed
      commitInfos.stream()
          .map(CommitInfoProto::getCommitIndex).sorted(Long::compare)
          .skip(1).forEach(ci -> Assert.assertTrue(message, logIndex <= ci));
    }
  }
}
 
Example #19
Source File: StateMachineUpdater.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void takeSnapshot() {
  final long i;
  try {
    Timer.Context takeSnapshotTimerContext = stateMachineMetrics.getTakeSnapshotTimer().time();
    i = stateMachine.takeSnapshot();
    takeSnapshotTimerContext.stop();

    final long lastAppliedIndex = getLastAppliedIndex();
    if (i > lastAppliedIndex) {
      throw new StateMachineException(
          "Bug in StateMachine: snapshot index = " + i + " > appliedIndex = " + lastAppliedIndex
          + "; StateMachine class=" +  stateMachine.getClass().getName() + ", stateMachine=" + stateMachine);
    }
    stateMachine.getStateMachineStorage().cleanupOldSnapshots(snapshotRetentionPolicy);
  } catch (IOException e) {
    LOG.error(name + ": Failed to take snapshot", e);
    return;
  }

  if (i >= 0) {
    LOG.info("{}: Took a snapshot at index {}", name, i);
    snapshotIndex.updateIncreasingly(i, infoIndexChange);

    final long purgeIndex;
    if (purgeUptoSnapshotIndex) {
      // We can purge up to snapshot index even if all the peers do not have
      // commitIndex up to this snapshot index.
      purgeIndex = i;
    } else {
      final LongStream commitIndexStream = server.getCommitInfos().stream().mapToLong(
          CommitInfoProto::getCommitIndex);
      purgeIndex = LongStream.concat(LongStream.of(i), commitIndexStream).min().orElse(i);
    }
    raftLog.purge(purgeIndex);
  }
}
 
Example #20
Source File: GroupInfoReply.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public GroupInfoReply(
        RaftClientRequest request, RoleInfoProto roleInfoProto,
        boolean isRaftStorageHealthy, Collection<CommitInfoProto> commitInfos, RaftGroup group) {
  super(request, commitInfos);
  this.roleInfoProto = roleInfoProto;
  this.isRaftStorageHealthy = isRaftStorageHealthy;
  this.group = group;
}
 
Example #21
Source File: GroupInfoReply.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("parameternumber")
public GroupInfoReply(
        ClientId clientId, RaftPeerId serverId, RaftGroupId groupId,
        long callId, boolean success, RoleInfoProto roleInfoProto,
        boolean isRaftStorageHealthy, Collection<CommitInfoProto> commitInfos, RaftGroup group) {
  super(clientId, serverId, groupId, callId, success, null, null, 0L, commitInfos);
  this.roleInfoProto = roleInfoProto;
  this.isRaftStorageHealthy = isRaftStorageHealthy;
  this.group = group;
}
 
Example #22
Source File: RaftClientReply.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("parameternumber")
public RaftClientReply(ClientId clientId, RaftGroupMemberId serverId,
    long callId, boolean success, Message message, RaftException exception,
    long logIndex, Collection<CommitInfoProto> commitInfos) {
  this(clientId, serverId.getPeerId(), serverId.getGroupId(),
      callId, success, message, exception, logIndex, commitInfos);
}
 
Example #23
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static CommitInfoProto toCommitInfoProto(RaftPeer peer, long commitIndex) {
  return CommitInfoProto.newBuilder()
      .setServer(peer.getRaftPeerProto())
      .setCommitIndex(commitIndex)
      .build();
}
 
Example #24
Source File: ProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static String toString(Collection<CommitInfoProto> protos) {
  return protos.stream().map(ProtoUtils::toString).collect(Collectors.toList()).toString();
}
 
Example #25
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static void addCommitInfos(Collection<CommitInfoProto> commitInfos, Consumer<CommitInfoProto> accumulator) {
  if (commitInfos != null && !commitInfos.isEmpty()) {
    commitInfos.forEach(accumulator);
  }
}
 
Example #26
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static String toString(CommitInfoProto proto) {
  return RaftPeerId.valueOf(proto.getServer().getId()) + ":c" + proto.getCommitIndex();
}
 
Example #27
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static String toString(Collection<CommitInfoProto> protos) {
  return protos.stream().map(ProtoUtils::toString).collect(Collectors.toList()).toString();
}
 
Example #28
Source File: RaftClientReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftClientReply(RaftClientRequest request, Message message, Collection<CommitInfoProto> commitInfos) {
  this(request.getClientId(), request.getServerId(), request.getRaftGroupId(),
      request.getCallId(), true, message, null, 0L, commitInfos);
}
 
Example #29
Source File: RaftClientReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftClientReply(RaftClientRequest request, Collection<CommitInfoProto> commitInfos) {
  this(request, (Message) null, commitInfos);
}
 
Example #30
Source File: RaftClientReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftClientReply(RaftClientRequest request, RaftException exception, Collection<CommitInfoProto> commitInfos) {
  this(request.getClientId(), request.getServerId(), request.getRaftGroupId(),
      request.getCallId(), false, null, exception, 0L, commitInfos);
}