org.apache.ratis.retry.RetryPolicies Java Examples

The following examples show how to use org.apache.ratis.retry.RetryPolicies. 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: RatisHelper.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Table mapping exception type to retry policy used for the exception in
 * write and watch request.
 * ---------------------------------------------------------------------------
 * |        Exception            | RetryPolicy for     | RetryPolicy for     |
 * |                             | Write request       | Watch request       |
 * |-------------------------------------------------------------------------|
 * | NotReplicatedException      | NO_RETRY            | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | GroupMismatchException      | NO_RETRY            | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | StateMachineException       | NO_RETRY            | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | TimeoutIOException          | EXPONENTIAL_BACKOFF | NO_RETRY            |
 * |-------------------------------------------------------------------------|
 * | ResourceUnavailableException| EXPONENTIAL_BACKOFF | EXPONENTIAL_BACKOFF |
 * |-------------------------------------------------------------------------|
 * | Others                      | MULTILINEAR_RANDOM  | MULTILINEAR_RANDOM  |
 * |                             | _RETRY             | _RETRY               |
 * ---------------------------------------------------------------------------
 */
public static RetryPolicy createRetryPolicy(ConfigurationSource conf) {
  RatisClientConfig ratisClientConfig = OzoneConfiguration.of(conf)
      .getObject(RatisClientConfig.class);
  ExponentialBackoffRetry exponentialBackoffRetry =
      createExponentialBackoffPolicy(ratisClientConfig);
  MultipleLinearRandomRetry multipleLinearRandomRetry =
      MultipleLinearRandomRetry
          .parseCommaSeparated(ratisClientConfig.getMultilinearPolicy());

  long writeTimeout = ratisClientConfig.getWriteRequestTimeoutInMs();
  long watchTimeout = ratisClientConfig.getWatchRequestTimeoutInMs();

  return RequestTypeDependentRetryPolicy.newBuilder()
      .setRetryPolicy(RaftProtos.RaftClientRequestProto.TypeCase.WRITE,
          createExceptionDependentPolicy(exponentialBackoffRetry,
              multipleLinearRandomRetry, exponentialBackoffRetry))
      .setRetryPolicy(RaftProtos.RaftClientRequestProto.TypeCase.WATCH,
          createExceptionDependentPolicy(exponentialBackoffRetry,
              multipleLinearRandomRetry, RetryPolicies.noRetry()))
      .setTimeout(RaftProtos.RaftClientRequestProto.TypeCase.WRITE,
          TimeDuration.valueOf(writeTimeout, TimeUnit.MILLISECONDS))
      .setTimeout(RaftProtos.RaftClientRequestProto.TypeCase.WATCH,
          TimeDuration.valueOf(watchTimeout, TimeUnit.MILLISECONDS))
      .build();
}
 
Example #2
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void runTestWatchRequestClientTimeout(TestParameters p) throws Exception {
  final Logger LOG = p.log;

  CompletableFuture<RaftClientReply> watchReply;
  // watch 1000 which will never be committed
  // so client can not receive reply, and connection closed, throw TimeoutException.
  // We should not retry, because if retry, RaftClientImpl::handleIOException will random select a leader,
  // then sometimes throw NotLeaderException.
  watchReply = p.sendWatchRequest(1000, RetryPolicies.noRetry());

  try {
    watchReply.get();
    fail("runTestWatchRequestClientTimeout failed");
  } catch (Exception ex) {
    LOG.error("error occurred", ex);
    Assert.assertTrue(ex.getCause().getClass() == AlreadyClosedException.class ||
        ex.getCause().getClass() == RaftRetryFailureException.class);
    if (ex.getCause() != null) {
      if (ex.getCause().getCause() != null) {
        Assert.assertEquals(TimeoutIOException.class,
            ex.getCause().getCause().getClass());
      }
    }
  }
}
 
Example #3
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private static ExceptionDependentRetry createExceptionDependentPolicy(
    ExponentialBackoffRetry exponentialBackoffRetry,
    MultipleLinearRandomRetry multipleLinearRandomRetry,
    RetryPolicy timeoutPolicy) {
  ExceptionDependentRetry.Builder builder =
      ExceptionDependentRetry.newBuilder();
  for (Class c : NO_RETRY_EXCEPTIONS) {
    builder.setExceptionToPolicy(c, RetryPolicies.noRetry());
  }
  return builder.setExceptionToPolicy(ResourceUnavailableException.class,
          exponentialBackoffRetry)
      .setExceptionToPolicy(TimeoutIOException.class, timeoutPolicy)
      .setDefaultPolicy(multipleLinearRandomRetry)
      .build();
}
 
Example #4
Source File: RequestTypeDependentRetryPolicy.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public Action handleAttemptFailure(Event event) {
  if (!(event instanceof ClientRetryEvent)) {
    return RetryPolicies.retryForeverNoSleep().handleAttemptFailure(event);
  }
  final ClientRetryEvent clientEvent = (ClientRetryEvent) event;
  final TimeDuration timeout = timeoutMap.get(clientEvent.getRequest().getType().getTypeCase());
  if (timeout != null && clientEvent.isRequestTimeout(timeout)) {
    return NO_RETRY_ACTION;
  }
  return Optional.ofNullable(
      retryPolicyMap.get(clientEvent.getRequest().getType().getTypeCase()))
      .orElse(RetryPolicies.retryForeverNoSleep())
      .handleAttemptFailure(event);
}
 
Example #5
Source File: TestRetryPolicy.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryMultipleTimesWithFixedSleep() {
  RetryPolicy retryPolicy = RetryPolicies
      .retryUpToMaximumCountWithFixedSleep(2,
          TimeDuration.valueOf(1000L, TimeUnit.MILLISECONDS));
   boolean shouldRetry = retryPolicy.shouldRetry(1);
  Assert.assertTrue(shouldRetry);
  Assert.assertTrue(1000 == retryPolicy.getSleepTime().getDuration());
  Assert.assertFalse(retryPolicy.shouldRetry(3));
}
 
Example #6
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private RetryPolicy getDefaultRetryPolicy() {
  return RetryPolicies.retryForeverWithSleep(RETRY_INTERVAL_DEFAULT);
}
 
Example #7
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 4 votes vote down vote up
private RetryPolicy getDefaultRetryPolicy() {
  return RetryPolicies.retryForeverWithSleep(RETRY_INTERVAL_DEFAULT);
}