Java Code Examples for org.apache.ratis.util.JavaUtils#completeExceptionally()

The following examples show how to use org.apache.ratis.util.JavaUtils#completeExceptionally() . 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
CompletableFuture<RaftClientReply> onNext(RaftClientRequest request) {
  final long callId = request.getCallId();
  final CompletableFuture<RaftClientReply> f = replies.putNew(callId);
  if (f == null) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + " is closed."));
  }
  try {
    if (!requestStreamer.onNext(ClientProtoUtils.toRaftClientRequestProto(request))) {
      return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + ": the stream is closed."));
    }
  } catch(Throwable t) {
    handleReplyFuture(request.getCallId(), future -> future.completeExceptionally(t));
    return f;
  }

  if (RaftClientRequestProto.TypeCase.WATCH.equals(request.getType().getTypeCase())) {
    scheduler.onTimeout(watchRequestTimeoutDuration, () ->
            timeoutCheck(callId, watchRequestTimeoutDuration), LOG,
        () -> "Timeout check failed for client request #" + callId);
  } else {
    scheduler.onTimeout(requestTimeoutDuration,
        () -> timeoutCheck(callId, requestTimeoutDuration), LOG,
        () -> "Timeout check failed for client request #" + callId);
  }
  return f;
}
 
Example 2
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 3
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<RaftClientReply> groupAddAsync(GroupManagementRequest request, RaftGroup newGroup) {
  if (!request.getRaftGroupId().equals(newGroup.getGroupId())) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Request group id (" + request.getRaftGroupId() + ") does not match the new group " + newGroup));
  }
  return impls.addNew(newGroup)
      .thenApplyAsync(newImpl -> {
        LOG.debug("{}: newImpl = {}", getId(), newImpl);
        final boolean started = newImpl.start();
        Preconditions.assertTrue(started, () -> getId()+ ": failed to start a new impl: " + newImpl);
        return new RaftClientReply(request, newImpl.getCommitInfos());
      })
      .whenComplete((_1, throwable) -> {
        if (throwable != null) {
          impls.remove(newGroup.getGroupId());
          LOG.warn(getId() + ": Failed groupAdd* " + request, throwable);
        }
      });
}
 
Example 4
Source File: OrderedAsync.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
CompletableFuture<RaftClientReply> send(RaftClientRequest.Type type, Message message, RaftPeerId server) {
  if (!type.is(TypeCase.WATCH) && !type.is(TypeCase.STREAM)) {
    Objects.requireNonNull(message, "message == null");
  }
  try {
    requestSemaphore.acquire();
  } catch (InterruptedException e) {
    return JavaUtils.completeExceptionally(IOUtils.toInterruptedIOException(
        "Interrupted when sending " + type + ", message=" + message, e));
  }

  final long callId = RaftClientImpl.nextCallId();
  final LongFunction<PendingOrderedRequest> constructor = seqNum -> new PendingOrderedRequest(callId, seqNum,
      slidingWindowEntry -> client.newRaftClientRequest(server, callId, message, type, slidingWindowEntry));
  return getSlidingWindow(server).submitNewRequest(constructor, this::sendRequestWithRetry
  ).getReplyFuture(
  ).thenApply(reply -> RaftClientImpl.handleRaftException(reply, CompletionException::new)
  ).whenComplete((r, e) -> requestSemaphore.release());
}
 
Example 5
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
synchronized CompletableFuture<RaftServerImpl> addNew(RaftGroup group) {
  if (isClosed) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(
        getId() + ": Failed to add " + group + " since the server is already closed"));
  }
  if (containsGroup(group.getGroupId())) {
    return JavaUtils.completeExceptionally(new AlreadyExistsException(
        getId() + ": Failed to add " + group + " since the group already exists in the map."));
  }
  final RaftGroupId groupId = group.getGroupId();
  final CompletableFuture<RaftServerImpl> newImpl = newRaftServerImpl(group);
  final CompletableFuture<RaftServerImpl> previous = map.put(groupId, newImpl);
  Preconditions.assertNull(previous, "previous");
  LOG.info("{}: addNew {} returns {}", getId(), group, toString(groupId, newImpl));
  return newImpl;
}
 
Example 6
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> groupManagementAsync(GroupManagementRequest request) {
  final RaftGroupId groupId = request.getRaftGroupId();
  if (groupId == null) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Request group id == null"));
  }
  final GroupManagementRequest.Add add = request.getAdd();
  if (add != null) {
    return groupAddAsync(request, add.getGroup());
  }
  final GroupManagementRequest.Remove remove = request.getRemove();
  if (remove != null) {
    return groupRemoveAsync(request, remove.getGroupId(), remove.isDeleteDirectory());
  }
  return JavaUtils.completeExceptionally(new UnsupportedOperationException(
      getId() + ": Request not supported " + request));
}
 
Example 7
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<RaftClientReply> groupAddAsync(GroupManagementRequest request, RaftGroup newGroup) {
  if (!request.getRaftGroupId().equals(newGroup.getGroupId())) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Request group id (" + request.getRaftGroupId() + ") does not match the new group " + newGroup));
  }
  return impls.addNew(newGroup)
      .thenApplyAsync(newImpl -> {
        LOG.debug("{}: newImpl = {}", getId(), newImpl);
        final boolean started = newImpl.start();
        Preconditions.assertTrue(started, () -> getId()+ ": failed to start a new impl: " + newImpl);
        return new RaftClientReply(request, newImpl.getCommitInfos());
      }, implExecutor)
      .whenComplete((raftClientReply, throwable) -> {
        if (throwable != null) {
          if (!(throwable.getCause() instanceof AlreadyExistsException)) {
            impls.remove(newGroup.getGroupId());
            LOG.warn(getId() + ": Failed groupAdd* " + request, throwable);
          } else {
            if (LOG.isDebugEnabled()) {
              LOG.debug(getId() + ": Failed groupAdd* " + request, throwable);
            }
          }
        }
      });
}
 
Example 8
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 9
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 6 votes vote down vote up
CompletableFuture<RaftClientReply> onNext(RaftClientRequest request) {
  final Map<Long, CompletableFuture<RaftClientReply>> map = replies.get();
  if (map == null) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + " is closed."));
  }
  final CompletableFuture<RaftClientReply> f = new CompletableFuture<>();
  CollectionUtils.putNew(request.getCallId(), f, map,
      () -> getName() + ":" + getClass().getSimpleName());
  try {
    requestStreamObserver.onNext(ClientProtoUtils.toRaftClientRequestProto(request));
    scheduler.onTimeout(requestTimeoutDuration, () -> timeoutCheck(request), LOG,
        () -> "Timeout check failed for client request: " + request);
  } catch(Throwable t) {
    handleReplyFuture(request.getCallId(), future -> future.completeExceptionally(t));
  }
  return f;
}
 
Example 10
Source File: SimpleStateMachine4Testing.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Query the n-th log entry.
 * @param request an index represented in a UTF-8 String, or an empty message.
 * @return a completed future of the n-th log entry,
 *         where n is the last applied index if the request is empty,
 *         otherwise, n is the index represented in the request.
 */
@Override
public CompletableFuture<Message> query(Message request) {
  final String string = request.getContent().toStringUtf8();
  Exception exception;
  try {
    LOG.info("query " + string);
    final LogEntryProto entry = dataMap.get(string);
    if (entry != null) {
      return CompletableFuture.completedFuture(Message.valueOf(entry.toByteString()));
    }
    exception = new IndexOutOfBoundsException(getId() + ": LogEntry not found for query " + string);
  } catch (Exception e) {
    LOG.warn("Failed request " + request, e);
    exception = e;
  }
  return JavaUtils.completeExceptionally(new StateMachineException(
      "Failed request " + request, exception));
}
 
Example 11
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> groupManagementAsync(GroupManagementRequest request) {
  final RaftGroupId groupId = request.getRaftGroupId();
  if (groupId == null) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": Request group id == null"));
  }
  final GroupManagementRequest.Add add = request.getAdd();
  if (add != null) {
    return groupAddAsync(request, add.getGroup());
  }
  final GroupManagementRequest.Remove remove = request.getRemove();
  if (remove != null) {
    return groupRemoveAsync(request, remove.getGroupId(), remove.isDeleteDirectory());
  }
  return JavaUtils.completeExceptionally(new UnsupportedOperationException(
      getId() + ": Request not supported " + request));
}
 
Example 12
Source File: InstallSnapshotNotificationTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<TermIndex> notifyInstallSnapshotFromLeader(
    RaftProtos.RoleInfoProto roleInfoProto,
    TermIndex termIndex) {
  final SingleFileSnapshotInfo leaderSnapshotInfo = (SingleFileSnapshotInfo) leaderSnapshotInfoRef.get();
  LOG.info("{}: leaderSnapshotInfo = {}", getId(), leaderSnapshotInfo);
  if (leaderSnapshotInfo == null) {
    return super.notifyInstallSnapshotFromLeader(roleInfoProto, termIndex);
  }

  try {
    Path leaderSnapshotFile = leaderSnapshotInfo.getFile().getPath();
    File followerSnapshotFilePath = new File(getSMdir(),
        leaderSnapshotFile.getFileName().toString());
    Files.copy(leaderSnapshotFile, followerSnapshotFilePath.toPath());
  } catch (IOException e) {
    LOG.error("Failed notifyInstallSnapshotFromLeader", e);
    return JavaUtils.completeExceptionally(e);
  }
  return CompletableFuture.completedFuture(leaderSnapshotInfo.getTermIndex());
}
 
Example 13
Source File: SimpleStateMachine4Testing.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Query the n-th log entry.
 * @param request an index represented in a UTF-8 String, or an empty message.
 * @return a completed future of the n-th log entry,
 *         where n is the last applied index if the request is empty,
 *         otherwise, n is the index represented in the request.
 */
@Override
public CompletableFuture<Message> query(Message request) {
  final String string = request.getContent().toStringUtf8();
  Exception exception;
  try {
    LOG.info("query " + string);
    final LogEntryProto entry = dataMap.get(string);
    if (entry != null) {
      return CompletableFuture.completedFuture(Message.valueOf(entry.toByteString()));
    }
    exception = new IndexOutOfBoundsException(getId() + ": LogEntry not found for query " + string);
  } catch (Exception e) {
    LOG.warn("Failed request " + request, e);
    exception = e;
  }
  return JavaUtils.completeExceptionally(new StateMachineException(
      "Failed request " + request, exception));
}
 
Example 14
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 5 votes vote down vote up
CompletableFuture<RaftServerImpl> get(RaftGroupId groupId) {
  final CompletableFuture<RaftServerImpl> i = map.get(groupId);
  if (i == null) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": " + groupId + " not found."));
  }
  return i;
}
 
Example 15
Source File: GrpcClientRpc.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> sendRequestAsync(
    RaftClientRequest request) {
  final RaftPeerId serverId = request.getServerId();
  try {
    final GrpcClientProtocolClient proxy = getProxies().getProxy(serverId);
    // Reuse the same grpc stream for all async calls.
    return proxy.getAppendStreamObservers().onNext(request);
  } catch (IOException e) {
    return JavaUtils.completeExceptionally(e);
  }
}
 
Example 16
Source File: RetryCache.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static CompletableFuture<RaftClientReply> failWithException(
    Throwable t, CacheEntry entry) {
  if (entry != null) {
    entry.failWithException(t);
    return entry.getReplyFuture();
  } else {
    return JavaUtils.completeExceptionally(t);
  }
}
 
Example 17
Source File: StreamRequests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
synchronized CompletableFuture<ByteString> append(long messageId, Message message) {
  if (nextId == -1) {
    nextId = messageId;
  } else if (messageId != nextId) {
    return JavaUtils.completeExceptionally(new StreamException(
        "Unexpected message id in " + key + ": messageId = " + messageId + " != nextId = " + nextId));
  }
  nextId++;
  bytes = bytes.concat(message.getContent());
  return CompletableFuture.completedFuture(bytes);
}
 
Example 18
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
CompletableFuture<RaftServerImpl> get(RaftGroupId groupId) {
  final CompletableFuture<RaftServerImpl> i = map.get(groupId);
  if (i == null) {
    return JavaUtils.completeExceptionally(new GroupMismatchException(
        getId() + ": " + groupId + " not found."));
  }
  return i;
}
 
Example 19
Source File: GrpcClientRpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> sendRequestAsync(
    RaftClientRequest request) {
  final RaftPeerId serverId = request.getServerId();
  try {
    final GrpcClientProtocolClient proxy = getProxies().getProxy(serverId);
    // Reuse the same grpc stream for all async calls.
    return proxy.getOrderedStreamObservers().onNext(request);
  } catch (Throwable e) {
    return JavaUtils.completeExceptionally(e);
  }
}
 
Example 20
Source File: FileInfo.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
CompletableFuture<Integer> submitCommit(
    long offset, int size, Function<UnderConstruction, ReadOnly> closeFunction,
    ExecutorService executor, RaftPeerId id, long index) {
  final boolean close = closeFunction != null;
  final Supplier<String> name = () -> "commit(" + getRelativePath() + ", "
      + offset + ", " + size + ", close? " + close + ") @" + id + ":" + index;

  final WriteInfo info = writeInfos.get(index);
  if (info == null) {
    return JavaUtils.completeExceptionally(
        new IOException(name.get() + " is already committed."));
  }

  final CheckedSupplier<Integer, IOException> task = LogUtils.newCheckedSupplier(LOG, () -> {
    if (offset != committedSize) {
      throw new IOException("Offset/size mismatched: offset = "
          + offset + " != committedSize = " + committedSize
          + ", path=" + getRelativePath());
    } else if (committedSize + size > writeSize) {
      throw new IOException("Offset/size mismatched: committed (=" + committedSize
          + ") + size (=" + size + ") > writeSize = " + writeSize);
    }
    committedSize += size;

    if (close) {
      closeFunction.apply(this);
      writeInfos.remove(index);
    }
    info.getCommitFuture().complete(size);
    return size;
  }, name);

  // Remove previous info, if there is any.
  final WriteInfo previous = writeInfos.remove(info.getPreviousIndex());
  final CompletableFuture<Integer> previousCommit = previous != null?
      previous.getCommitFuture(): CompletableFuture.completedFuture(0);
  // Commit after both current write and previous commit completed.
  return info.getWriteFuture().thenCombineAsync(previousCommit, (wSize, previousCommitSize) -> {
    Preconditions.assertTrue(size == wSize);
    try {
      return task.get();
    } catch (IOException e) {
      throw new CompletionException("Failed " + task, e);
    }
  }, executor);
}