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

The following examples show how to use org.apache.ratis.util.JavaUtils#unwrapCompletionException() . 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: BaseTest.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@SafeVarargs
public static Throwable testFailureCaseAsync(
    String description, Supplier<CompletableFuture<?>> testCode,
    Class<? extends Throwable> expectedThrowableClass, Logger log,
    Class<? extends Throwable>... expectedCauseClasses) {
  if (log != null) {
    log.info("run '{}'", description);
  }
  try {
    testCode.get().join();
  } catch (Throwable t) {
    t = JavaUtils.unwrapCompletionException(t);
    assertThrowable(description, t, expectedThrowableClass, log, expectedCauseClasses);
    return t;
  }
  throw new AssertionError("The test \"" + description + "\" does not throw anything.");
}
 
Example 2
Source File: SegmentedRaftLog.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public EntryWithData getEntryWithData(long index) throws RaftLogIOException {
  final LogEntryProto entry = get(index);
  if (entry == null) {
    throw new RaftLogIOException("Log entry not found: index = " + index);
  }
  if (!ServerProtoUtils.shouldReadStateMachineData(entry)) {
    return new EntryWithData(entry, null);
  }

  try {
    CompletableFuture<ByteString> future = null;
    if (stateMachine != null) {
      future = stateMachine.data().read(entry).exceptionally(ex -> {
        stateMachine.notifyLogFailed(ex, entry);
        return null;
      });
    }
    return new EntryWithData(entry, future);
  } catch (Throwable e) {
    final String err = getName() + ": Failed readStateMachineData for " +
        ServerProtoUtils.toLogEntryString(entry);
    LOG.error(err, e);
    throw new RaftLogIOException(err, JavaUtils.unwrapCompletionException(e));
  }
}
 
Example 3
Source File: SegmentedRaftLog.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public EntryWithData getEntryWithData(long index) throws RaftLogIOException {
  final LogEntryProto entry = get(index);
  if (!ServerProtoUtils.shouldReadStateMachineData(entry)) {
    return new EntryWithData(entry, null);
  }

  try {
    return new EntryWithData(entry, server.map(s -> s.getStateMachine().readStateMachineData(entry)).orElse(null));
  } catch (Throwable e) {
    final String err = getSelfId() + ": Failed readStateMachineData for " +
        ServerProtoUtils.toLogEntryString(entry);
    LOG.error(err, e);
    throw new RaftLogIOException(err, JavaUtils.unwrapCompletionException(e));
  }
}
 
Example 4
Source File: GrpcClientProtocolService.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
boolean responseError(Throwable t, Supplier<String> message) {
  if (setClose()) {
    t = JavaUtils.unwrapCompletionException(t);
    if (LOG.isDebugEnabled()) {
      LOG.debug(name + ": Failed " + message.get(), t);
    }
    responseError(GrpcUtil.wrapException(t));
    return true;
  }
  return false;
}
 
Example 5
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static StatusRuntimeException wrapException(Throwable t, long callId) {
  t = JavaUtils.unwrapCompletionException(t);
  Metadata trailers = new StatusRuntimeExceptionMetadataBuilder(t)
      .addCallId(callId)
      .build();
  return wrapException(t, trailers);
}
 
Example 6
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static StatusRuntimeException wrapException(Throwable t, long callId, boolean isHeartbeat) {
  t = JavaUtils.unwrapCompletionException(t);
  Metadata trailers = new StatusRuntimeExceptionMetadataBuilder(t)
      .addCallId(callId)
      .addIsHeartbeat(isHeartbeat)
      .build();
  return wrapException(t, trailers);
}
 
Example 7
Source File: BaseTest.java    From ratis with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
public static Throwable testFailureCaseAsync(
    String description, Supplier<CompletableFuture<?>> testCode,
    Class<? extends Throwable> expectedThrowableClass, Logger log,
    Class<? extends Throwable>... expectedCauseClasses) {
  try {
    testCode.get().join();
  } catch (Throwable t) {
    t = JavaUtils.unwrapCompletionException(t);
    assertThrowable(description, t, expectedThrowableClass, log, expectedCauseClasses);
    return t;
  }
  throw new AssertionError("The test \"" + description + "\" does not throw anything.");
}
 
Example 8
Source File: GrpcClientProtocolService.java    From ratis with Apache License 2.0 5 votes vote down vote up
void responseError(Throwable t, Supplier<String> message) {
  if (isClosed.compareAndSet(false, true)) {
    t = JavaUtils.unwrapCompletionException(t);
    if (LOG.isDebugEnabled()) {
      LOG.debug(name + ": Failed " + message.get(), t);
    }
    responseObserver.onError(GrpcUtil.wrapException(t));
    slidingWindow.close();
  }
}
 
Example 9
Source File: GrpcUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
static StatusRuntimeException wrapException(Throwable t, long callId) {
  t = JavaUtils.unwrapCompletionException(t);

  Metadata trailers = new Metadata();
  trailers.put(EXCEPTION_TYPE_KEY, t.getClass().getCanonicalName());
  if (callId > 0) {
    trailers.put(CALL_ID, String.valueOf(callId));
  }
  return new StatusRuntimeException(
      Status.INTERNAL.withCause(t).withDescription(t.getMessage()), trailers);
}