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

The following examples show how to use org.apache.ratis.proto.RaftProtos.ReplicationLevel. 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: WatchRequestTests.java    From ratis with Apache License 2.0 6 votes vote down vote up
static void checkTimeout(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("checkTimeout {}: 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 watchAllReply = watchReplies.getAll();
    assertNotReplicatedException(logIndex, ReplicationLevel.ALL, watchAllReply);

    final RaftClientReply watchAllCommittedReply = watchReplies.getAllCommitted();
    assertNotReplicatedException(logIndex, ReplicationLevel.ALL_COMMITTED, watchAllCommittedReply);
  }
}
 
Example #2
Source File: WatchRequestTests.java    From ratis with Apache License 2.0 6 votes vote down vote up
void sendRequests(List<CompletableFuture<RaftClientReply>> replies,
    List<CompletableFuture<WatchReplies>> watches) {
  for(int i = 0; i < numMessages; i++) {
    final String message = "m" + i;
    log.info("SEND_REQUEST {}: message={}", i, message);
    final CompletableFuture<RaftClientReply> replyFuture = writeClient.sendAsync(new RaftTestUtil.SimpleMessage(message));
    replies.add(replyFuture);
    final CompletableFuture<WatchReplies> watchFuture = new CompletableFuture<>();
    watches.add(watchFuture);
    replyFuture.thenAccept(reply -> {
      final long logIndex = reply.getLogIndex();
      log.info("SEND_WATCH: message={}, logIndex={}", message, logIndex);
      watchFuture.complete(new WatchReplies(logIndex,
          watchMajorityClient.sendWatchAsync(logIndex, ReplicationLevel.MAJORITY),
          watchAllClient.sendWatchAsync(logIndex, ReplicationLevel.ALL),
          watchMajorityCommittedClient.sendWatchAsync(logIndex, ReplicationLevel.MAJORITY_COMMITTED),
          watchAllCommittedClient.sendWatchAsync(logIndex, ReplicationLevel.ALL_COMMITTED),
          log));
    });
  }
}
 
Example #3
Source File: LeaderState.java    From ratis with Apache License 2.0 6 votes vote down vote up
private void updateCommit(long majority, long min) {
  final long oldLastCommitted = raftLog.getLastCommittedIndex();
  if (majority > oldLastCommitted) {
    // copy the entries out from the raftlog, in order to prevent that
    // the log gets purged after the statemachine does a snapshot
    final TermIndex[] entriesToCommit = raftLog.getEntries(
        oldLastCommitted + 1, majority + 1);
    if (server.getState().updateStatemachine(majority, currentTerm)) {
      watchRequests.update(ReplicationLevel.MAJORITY, majority);
      logMetadata(majority);
      commitIndexChanged();
    }
    checkAndUpdateConfiguration(entriesToCommit);
  }

  watchRequests.update(ReplicationLevel.ALL, min);
}
 
Example #4
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void checkTimeout(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("checkTimeout {}: 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);

    assertNotReplicatedException(logIndex, ReplicationLevel.ALL, watchReplies::getAll);

    assertNotReplicatedException(logIndex, ReplicationLevel.ALL_COMMITTED, watchReplies::getAllCommitted);
  }
}
 
Example #5
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
void sendRequests(List<CompletableFuture<RaftClientReply>> replies,
    List<CompletableFuture<WatchReplies>> watches) {
  for(int i = 0; i < numMessages; i++) {
    final String message = "m" + i;
    log.info("SEND_REQUEST {}: message={}", i, message);
    final CompletableFuture<RaftClientReply> replyFuture = writeClient.sendAsync(new RaftTestUtil.SimpleMessage(message));
    replies.add(replyFuture);
    final CompletableFuture<WatchReplies> watchFuture = new CompletableFuture<>();
    watches.add(watchFuture);
    replyFuture.thenAccept(reply -> {
      final long logIndex = reply.getLogIndex();
      log.info("SEND_WATCH: message={}, logIndex={}", message, logIndex);
      watchFuture.complete(new WatchReplies(logIndex,
          writeClient.sendWatchAsync(logIndex, ReplicationLevel.MAJORITY),
          writeClient.sendWatchAsync(logIndex, ReplicationLevel.ALL),
          writeClient.sendWatchAsync(logIndex, ReplicationLevel.MAJORITY_COMMITTED),
          writeClient.sendWatchAsync(logIndex, ReplicationLevel.ALL_COMMITTED),
          log));
    });
  }
}
 
Example #6
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static void assertNotReplicatedException(long logIndex, ReplicationLevel replication, Throwable t) {
  Assert.assertSame(NotReplicatedException.class, t.getClass());
  final NotReplicatedException nre = (NotReplicatedException) t;
  Assert.assertNotNull(nre);
  Assert.assertEquals(logIndex, nre.getLogIndex());
  Assert.assertEquals(replication, nre.getRequiredReplication());
}
 
Example #7
Source File: WatchRequestTests.java    From ratis with Apache License 2.0 5 votes vote down vote up
static void assertNotReplicatedException(long logIndex, ReplicationLevel replication, RaftClientReply reply) {
  Assert.assertFalse(reply.isSuccess());
  final NotReplicatedException nre = reply.getNotReplicatedException();
  Assert.assertNotNull(nre);
  Assert.assertEquals(logIndex, nre.getLogIndex());
  Assert.assertEquals(replication, nre.getRequiredReplication());
}
 
Example #8
Source File: LeaderState.java    From ratis with Apache License 2.0 5 votes vote down vote up
void commitIndexChanged() {
  getMajorityMin(FollowerInfo::getCommitIndex, raftLog::getLastCommittedIndex).ifPresent(m -> {
    // Normally, leader commit index is always ahead followers.
    // However, after a leader change, the new leader commit index may
    // be behind some followers in the beginning.
    watchRequests.update(ReplicationLevel.ALL_COMMITTED, m.min);
    watchRequests.update(ReplicationLevel.MAJORITY_COMMITTED, m.majority);
    watchRequests.update(ReplicationLevel.MAJORITY, m.max);
  });
}
 
Example #9
Source File: WatchRequests.java    From ratis with Apache License 2.0 5 votes vote down vote up
WatchRequests(Object name, RaftProperties properties) {
  this.name = name + "-" + getClass().getSimpleName();

  final TimeDuration watchTimeout = RaftServerConfigKeys.watchTimeout(properties);
  this.watchTimeoutNanos = watchTimeout.to(TimeUnit.NANOSECONDS);
  final TimeDuration watchTimeoutDenomination = RaftServerConfigKeys.watchTimeoutDenomination(properties);
  this.watchTimeoutDenominationNanos = watchTimeoutDenomination.to(TimeUnit.NANOSECONDS);
  Preconditions.assertTrue(watchTimeoutNanos.getDuration() % watchTimeoutDenominationNanos.getDuration() == 0L,
      () -> "watchTimeout (=" + watchTimeout + ") is not a multiple of watchTimeoutDenomination (="
          + watchTimeoutDenomination + ").");

  Arrays.stream(ReplicationLevel.values()).forEach(r -> queues.put(r, new WatchQueue(r)));
}
 
Example #10
Source File: NotReplicatedException.java    From ratis with Apache License 2.0 5 votes vote down vote up
public NotReplicatedException(long callId, ReplicationLevel requiredReplication, long logIndex) {
  super("Request with call Id " + callId + " and log index " + logIndex
      + " is not yet replicated to " + requiredReplication);
  this.callId = callId;
  this.requiredReplication = requiredReplication;
  this.logIndex = logIndex;
}
 
Example #11
Source File: NotReplicatedException.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public NotReplicatedException(long callId, ReplicationLevel requiredReplication, long logIndex) {
  super("Request with call Id " + callId + " and log index " + logIndex
      + " is not yet replicated to " + requiredReplication);
  this.callId = callId;
  this.requiredReplication = requiredReplication;
  this.logIndex = logIndex;
}
 
Example #12
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static void assertNotReplicatedException(long logIndex, ReplicationLevel replication,
    CheckedSupplier<RaftClientReply, Exception> replySupplier) throws Exception {
  try {
    replySupplier.get();
    fail();
  } catch (ExecutionException e) {
    final Throwable cause = e.getCause();
    assertNotReplicatedException(logIndex, replication, cause);
  }
}
 
Example #13
Source File: LeaderState.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void updateCommit(long majority, long min) {
  final long oldLastCommitted = raftLog.getLastCommittedIndex();
  if (majority > oldLastCommitted) {
    // copy the entries out from the raftlog, in order to prevent that
    // the log gets purged after the statemachine does a snapshot
    final TermIndex[] entriesToCommit = raftLog.getEntries(
        oldLastCommitted + 1, majority + 1);

    if (server.getState().updateStatemachine(majority, currentTerm)) {
      watchRequests.update(ReplicationLevel.MAJORITY, majority);
      logMetadata(majority);
      commitIndexChanged();
    }

    try {
      for (TermIndex entry : entriesToCommit) {
        raftLog.getRaftLogMetrics().onLogEntryCommit(raftLog.get(entry.getIndex()));
      }
    } catch (RaftLogIOException e) {
      LOG.error("Caught exception reading from RaftLog", e);
    }

    checkAndUpdateConfiguration(entriesToCommit);
  }

  watchRequests.update(ReplicationLevel.ALL, min);
}
 
Example #14
Source File: LeaderState.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
void commitIndexChanged() {
  getMajorityMin(FollowerInfo::getCommitIndex, raftLog::getLastCommittedIndex).ifPresent(m -> {
    // Normally, leader commit index is always ahead of followers.
    // However, after a leader change, the new leader commit index may
    // be behind some followers in the beginning.
    watchRequests.update(ReplicationLevel.ALL_COMMITTED, m.min);
    watchRequests.update(ReplicationLevel.MAJORITY_COMMITTED, m.majority);
    watchRequests.update(ReplicationLevel.MAJORITY, m.max);
  });
}
 
Example #15
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
CompletableFuture<RaftClientReply> sendWatchRequest(long logIndex, RetryPolicy policy)
    throws Exception {

  try (final RaftClient watchClient =
      cluster.createClient(RaftTestUtil.waitForLeader(cluster).getId(), policy)) {

    CompletableFuture<RaftClientReply> reply =
        watchClient.sendAsync(new RaftTestUtil.SimpleMessage("message"));
    long writeIndex = reply.get().getLogIndex();
    Assert.assertTrue(writeIndex > 0);
    watchClient.sendWatchAsync(writeIndex, ReplicationLevel.MAJORITY_COMMITTED);
    return watchClient.sendWatchAsync(logIndex, ReplicationLevel.MAJORITY);
  }

}
 
Example #16
Source File: WatchRequests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
WatchRequests(Object name, RaftProperties properties) {
  this.name = name + "-" + getClass().getSimpleName();

  final TimeDuration watchTimeout = RaftServerConfigKeys.Watch.timeout(properties);
  this.watchTimeoutNanos = watchTimeout.to(TimeUnit.NANOSECONDS);
  final TimeDuration watchTimeoutDenomination = RaftServerConfigKeys.Watch.timeoutDenomination(properties);
  this.watchTimeoutDenominationNanos = watchTimeoutDenomination.to(TimeUnit.NANOSECONDS);
  Preconditions.assertTrue(watchTimeoutNanos.getDuration() % watchTimeoutDenominationNanos.getDuration() == 0L,
      () -> "watchTimeout (=" + watchTimeout + ") is not a multiple of watchTimeoutDenomination (="
          + watchTimeoutDenomination + ").");

  final int elementLimit = RaftServerConfigKeys.Watch.elementLimit(properties);
  Arrays.stream(ReplicationLevel.values()).forEach(r -> queues.put(r, new WatchQueue(r, elementLimit)));
}
 
Example #17
Source File: NotReplicatedException.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public ReplicationLevel getRequiredReplication() {
  return requiredReplication;
}
 
Example #18
Source File: RaftClientImpl.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> sendWatchAsync(long index, ReplicationLevel replication) {
  return UnorderedAsync.send(RaftClientRequest.watchRequestType(index, replication), this);
}
 
Example #19
Source File: RaftClientImpl.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public RaftClientReply sendWatch(long index, ReplicationLevel replication) throws IOException {
  return send(RaftClientRequest.watchRequestType(index, replication), null, null);
}
 
Example #20
Source File: RaftClient.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** Async call to watch the given index to satisfy the given replication level. */
CompletableFuture<RaftClientReply> sendWatchAsync(long index, ReplicationLevel replication);
 
Example #21
Source File: RaftClient.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** Watch the given index to satisfy the given replication level. */
RaftClientReply sendWatch(long index, ReplicationLevel replication) throws IOException;
 
Example #22
Source File: WatchRequests.java    From ratis with Apache License 2.0 4 votes vote down vote up
void update(ReplicationLevel replication, final long newIndex) {
  final WatchQueue queue = queues.get(replication);
  if (newIndex > queue.getIndex()) { // compare without synchronization
    queue.updateIndex(newIndex);
  }
}
 
Example #23
Source File: WatchRequests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
WatchQueue(ReplicationLevel replication, int elementLimit) {
  this.replication = replication;
  this.resource = new ResourceSemaphore(elementLimit);
}
 
Example #24
Source File: WatchRequests.java    From ratis with Apache License 2.0 4 votes vote down vote up
WatchQueue(ReplicationLevel replication) {
  this.replication = replication;
}
 
Example #25
Source File: RaftClient.java    From ratis with Apache License 2.0 4 votes vote down vote up
/** Watch the given index to satisfy the given replication level. */
RaftClientReply sendWatch(long index, ReplicationLevel replication) throws IOException;
 
Example #26
Source File: RaftClient.java    From ratis with Apache License 2.0 4 votes vote down vote up
/** Async call to watch the given index to satisfy the given replication level. */
CompletableFuture<RaftClientReply> sendWatchAsync(long index, ReplicationLevel replication);
 
Example #27
Source File: RaftClientImpl.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public RaftClientReply sendWatch(long index, ReplicationLevel replication) throws IOException {
  return send(RaftClientRequest.watchRequestType(index, replication), null, null);
}
 
Example #28
Source File: RaftClientImpl.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> sendWatchAsync(long index, ReplicationLevel replication) {
  return sendAsync(RaftClientRequest.watchRequestType(index, replication), null, null);
}
 
Example #29
Source File: NotReplicatedException.java    From ratis with Apache License 2.0 4 votes vote down vote up
public ReplicationLevel getRequiredReplication() {
  return requiredReplication;
}
 
Example #30
Source File: WatchRequests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void update(ReplicationLevel replication, final long newIndex) {
  final WatchQueue queue = queues.get(replication);
  if (newIndex > queue.getIndex()) { // compare without synchronization
    queue.updateIndex(newIndex);
  }
}