com.github.rholder.retry.StopStrategies Java Examples

The following examples show how to use com.github.rholder.retry.StopStrategies. 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: HBaseRetryingUtils.java    From flinkDemo with Apache License 2.0 6 votes vote down vote up
/**
 * 重试发送数据到hbase
 *
 * @param table
 * @param puts      List<Put>
 * @throws Exception 连接异常
 */
public static void retrying(Table table, List<Put> puts) throws Exception {
    // 异常或者返回null都继续重试、每3秒重试一次、最多重试5次
    Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
            .retryIfException()
            .withWaitStrategy(WaitStrategies.fixedWait(500, TimeUnit.MILLISECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(6))
            .build();

    try {
        retryer.call(() -> HBaseUtils.batchPuts(table, puts));
    } catch (Exception e) {
        LOGGER.error("多次重试发送数据到hbase失败!", e);
        throw new Exception("多次重试发送数据到hbase失败!", e);
    }
}
 
Example #2
Source File: ZuoraClientImpl.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
ZuoraClientImpl(WorkUnitState workUnitState) {
  _workUnitState = workUnitState;
  _hostName = _workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_HOST_NAME);
  _postRetryer =
      RetryerBuilder.<CommandOutput<RestApiCommand, String>>newBuilder().retryIfExceptionOfType(IOException.class)
          .withStopStrategy(StopStrategies
              .stopAfterAttempt(workUnitState.getPropAsInt(ZuoraConfigurationKeys.ZUORA_API_RETRY_POST_COUNT, 20)))
          .withWaitStrategy(WaitStrategies
              .fixedWait(workUnitState.getPropAsInt(ZuoraConfigurationKeys.ZUORA_API_RETRY_POST_WAIT_TIME, 60000),
                  TimeUnit.MILLISECONDS)).build();
  _getRetryer = RetryerBuilder.<List<String>>newBuilder().retryIfExceptionOfType(IOException.class).withStopStrategy(
      StopStrategies
          .stopAfterAttempt(workUnitState.getPropAsInt(ZuoraConfigurationKeys.ZUORA_API_RETRY_GET_FILES_COUNT, 30)))
      .withWaitStrategy(WaitStrategies
          .fixedWait(workUnitState.getPropAsInt(ZuoraConfigurationKeys.ZUORA_API_RETRY_GET_FILES_WAIT_TIME, 30000),
              TimeUnit.MILLISECONDS)).build();
}
 
Example #3
Source File: BaseJdbcBufferedInserter.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
public BaseJdbcBufferedInserter(State state, Connection conn) {
  this.conn = conn;
  this.batchSize = state.getPropAsInt(WRITER_JDBC_INSERT_BATCH_SIZE, DEFAULT_WRITER_JDBC_INSERT_BATCH_SIZE);
  if (this.batchSize < 1) {
    throw new IllegalArgumentException(WRITER_JDBC_INSERT_BATCH_SIZE + " should be a positive number");
  }

  int maxWait = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_TIMEOUT, DEFAULT_WRITER_JDBC_INSERT_RETRY_TIMEOUT);
  int maxAttempts =
      state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT, DEFAULT_WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT);

  //retry after 2, 4, 8, 16... sec, allow at most maxWait sec delay
  this.retryer = RetryerBuilder.<Boolean> newBuilder().retryIfException()
      .withWaitStrategy(WaitStrategies.exponentialWait(1000, maxWait, TimeUnit.SECONDS))
      .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)).build();
}
 
Example #4
Source File: RetryWriter.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * @return RetryerBuilder that retries on all exceptions except NonTransientException with exponential back off
 */
public static RetryerBuilder<Void> createRetryBuilder(State state) {
  Predicate<Throwable> transients = new Predicate<Throwable>() {
    @Override
    public boolean apply(Throwable t) {
      return !(t instanceof NonTransientException);
    }
  };

  long multiplier = state.getPropAsLong(RETRY_MULTIPLIER, 500L);
  long maxWaitMsPerInterval = state.getPropAsLong(RETRY_MAX_WAIT_MS_PER_INTERVAL, 10000);
  int maxAttempts = state.getPropAsInt(RETRY_MAX_ATTEMPTS, 5);
  return RetryerBuilder.<Void> newBuilder()
      .retryIfException(transients)
      .withWaitStrategy(WaitStrategies.exponentialWait(multiplier, maxWaitMsPerInterval, TimeUnit.MILLISECONDS)) //1, 2, 4, 8, 16 seconds delay
      .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)); //Total 5 attempts and fail.
}
 
Example #5
Source File: DefaultPowerBiConnection.java    From powerbi-rest-java with MIT License 6 votes vote down vote up
@Override
public <T> Future<T> execute(final PowerBiOperation<T> val) {
    // use a retryer to keep attempting to send data to powerBI if we receive a rate limit exception.
    // use exponential backoff to create a window of time for the request to come through.

    // TODO : the time to wait is actually in the response header, come back and add that value.
    final Retryer<T> retryer = RetryerBuilder.<T>newBuilder()
            // we are retrying on these exceptions because they are able to be handled by just retrying this callable
            // again (assuming that the maximum retries value is greater than 1 and this isn't the last retry attempt
            // before giving up.
            .retryIfExceptionOfType(RateLimitExceededException.class)
            .retryIfExceptionOfType(RequestAuthenticationException.class)
            .withAttemptTimeLimiter(AttemptTimeLimiters.<T>fixedTimeLimit(maximumWaitTime, TimeUnit.MILLISECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(maximumRetries))
            .build();

    return executor.submit(new Callable<T>() {
        @Override
        public T call() throws Exception {
            return retryer.call(new PowerBiCallable<>(val, clientBuilder));
        }
    });
}
 
Example #6
Source File: ServiceProvider.java    From ranger with Apache License 2.0 6 votes vote down vote up
private void createPath() throws Exception {
    Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder()
            .retryIfExceptionOfType(KeeperException.NodeExistsException.class) //Ephemeral node still exists
            .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
            .withBlockStrategy(BlockStrategies.threadSleepStrategy())
            .withStopStrategy(StopStrategies.neverStop())
            .build();
    try {
        retryer.call(() -> {
            curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath(
                    String.format("/%s/%s", serviceName, serviceNode.representation()),
                    serializer.serialize(serviceNode));
            return null;
        });
    } catch (Exception e) {
        final String message = String.format("Could not create node for %s after 60 retries (1 min). " +
                        "This service will not be discoverable. Retry after some time.", serviceName);
        logger.error(message, e);
        throw new Exception(message, e);
    }

}
 
Example #7
Source File: RetryUtil.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a retryer.
 *
 * @param actionDescription a description of the action being performed
 * @param waitUntilTime     the latest time to wait to perform the action
 * @param sleepTimeMs       the sleep time in millisecond for retries
 * @param exceptionClass    the class of exception to throw
 * @param logger            the logger
 * @param <T>               the type of object returned
 * @return the retryer
 */
private static <T> Retryer<T> buildRetryer(String actionDescription, ZonedDateTime waitUntilTime, long sleepTimeMs,
        Class<? extends CcmException> exceptionClass, Logger logger) {
    StopStrategy stop;
    if (waitUntilTime != null) {
        // Given the time at which waiting should stop,
        // get the available number of millis from this instant
        stop = StopStrategyFactory.waitUntilDateTime(waitUntilTime);
        logger.info("Trying until {} to {}", waitUntilTime, actionDescription);
    } else {
        stop = StopStrategies.neverStop();
        logger.warn("Unbounded wait to {}", actionDescription);
    }

    WaitStrategy wait = WaitStrategies.fixedWait(sleepTimeMs, TimeUnit.MILLISECONDS);
    logger.info("Checking every {} milliseconds", sleepTimeMs);

    return RetryerBuilder.<T>newBuilder()
            .retryIfException(t -> exceptionClass.isInstance(t) && ((CcmException) t).isRetryable())
            .retryIfResult(Objects::isNull)
            .withStopStrategy(stop)
            .withWaitStrategy(wait)
            .build();
}
 
Example #8
Source File: DockerUtils.java    From Singularity with Apache License 2.0 6 votes vote down vote up
private <T> T callWithRetriesAndTimeout(
  Callable<T> callable,
  Optional<Integer> retryCount
)
  throws Exception {
  RetryerBuilder<T> retryerBuilder = RetryerBuilder
    .<T>newBuilder()
    .withAttemptTimeLimiter(
      new FixedTimeLimit(
        configuration.getDockerClientTimeLimitSeconds(),
        TimeUnit.SECONDS,
        executor
      )
    );
  if (retryCount.isPresent()) {
    retryerBuilder.withStopStrategy(StopStrategies.stopAfterAttempt(retryCount.get()));
  }
  return retryerBuilder.build().call(callable);
}
 
Example #9
Source File: LifecycleHelper.java    From Baragon with Apache License 2.0 6 votes vote down vote up
public void notifyService(String action) throws Exception {
  long start = System.currentTimeMillis();
  Retryer<AgentCheckInResponse> retryer = RetryerBuilder.<AgentCheckInResponse>newBuilder()
      .retryIfException()
      .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getMaxNotifyServiceAttempts()))
      .withWaitStrategy(WaitStrategies.exponentialWait(1, TimeUnit.SECONDS))
      .build();

  AgentCheckInResponse agentCheckInResponse = retryer.call(checkInCallable(action, false));
  while ((agentCheckInResponse.getState() != TrafficSourceState.DONE
      && System.currentTimeMillis() - start < configuration.getAgentCheckInTimeoutMs())) {
    try {
      Thread.sleep(agentCheckInResponse.getWaitTime());
    } catch (InterruptedException ie) {
      LOG.error("Interrupted waiting for check in with service, shutting down early");
      break;
    }
    agentCheckInResponse = retryer.call(checkInCallable(action, true));
  }
  LOG.info("Finished agent check in");
}
 
Example #10
Source File: HelloDemo.java    From retry with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Callable<Boolean> callable = new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            // do something useful here
            LOGGER.info("call...");
            throw new RuntimeException();
        }
    };

    Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
            .retryIfResult(Predicates.isNull())
            .retryIfExceptionOfType(IOException.class)
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(3))
            .build();
    try {
        retryer.call(callable);
    } catch (RetryException | ExecutionException e) {
        e.printStackTrace();
    }

}
 
Example #11
Source File: ExponentialBackoff.java    From retry with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Callable<Boolean> callable = new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            // do something useful here
            LOGGER.info("call...");
            throw new RuntimeException();
        }
    };

    Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
            .retryIfResult(Predicates.isNull())
            .retryIfExceptionOfType(IOException.class)
            .retryIfRuntimeException()
            .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(3))
            .build();
    try {
        retryer.call(callable);
    } catch (RetryException | ExecutionException e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: SingularityClient.java    From Singularity with Apache License 2.0 5 votes vote down vote up
public SingularityClient(
  String contextPath,
  HttpClient httpClient,
  Provider<List<String>> hostsProvider,
  Optional<SingularityClientCredentials> credentials,
  boolean ssl,
  int retryAttempts,
  Predicate<HttpResponse> retryStrategy
) {
  this.httpClient = httpClient;
  this.contextPath = contextPath;

  this.hostsProvider = hostsProvider;
  this.random = new Random();

  this.credentials = credentials;
  this.ssl = ssl;

  this.httpResponseRetryer =
    RetryerBuilder
      .<HttpResponse>newBuilder()
      .withStopStrategy(StopStrategies.stopAfterAttempt(retryAttempts))
      .withWaitStrategy(WaitStrategies.exponentialWait())
      .retryIfResult(retryStrategy::test)
      .retryIfException()
      .build();
}
 
Example #13
Source File: StopStrategyFactory.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a stop strategy that will stop retrying when the specified date and time is reached. Will always
 * execute the callable at least one time.
 *
 * @param stopDateTime the date and time to stop retrying
 * @return a stop strategy that stops at a specified date and time
 */
public static StopStrategy waitUntilDateTime(ZonedDateTime stopDateTime) {
    long millisecondsUntilStop = ChronoUnit.MILLIS.between(ZonedDateTime.now(), stopDateTime);

    if (millisecondsUntilStop < 0) {
        return STOP_IMMEDIATELY;
    }

    return StopStrategies.stopAfterDelay(millisecondsUntilStop, TimeUnit.MILLISECONDS);
}
 
Example #14
Source File: GobblinHelixJobScheduler.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private void cancelJobIfRequired(DeleteJobConfigArrivalEvent deleteJobArrival) throws InterruptedException {
  Properties jobConfig = deleteJobArrival.getJobConfig();
  if (PropertiesUtils.getPropAsBoolean(jobConfig, GobblinClusterConfigurationKeys.CANCEL_RUNNING_JOB_ON_DELETE,
      GobblinClusterConfigurationKeys.DEFAULT_CANCEL_RUNNING_JOB_ON_DELETE)) {
    LOGGER.info("Cancelling workflow: {}", deleteJobArrival.getJobName());

    //Workaround for preventing indefinite hangs observed in TaskDriver.getWorkflows() call.
    Callable<Map<String, String>> workflowsCallable = () -> HelixUtils.getWorkflowIdsFromJobNames(this.jobHelixManager,
        Collections.singletonList(deleteJobArrival.getJobName()));
    Retryer<Map<String, String>> retryer = RetryerBuilder.<Map<String, String>>newBuilder()
        .retryIfException()
        .withStopStrategy(StopStrategies.stopAfterAttempt(5))
        .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(this.helixWorkflowListingTimeoutMillis, TimeUnit.MILLISECONDS)).build();
    Map<String, String> jobNameToWorkflowIdMap;
    try {
      jobNameToWorkflowIdMap = retryer.call(workflowsCallable);
    } catch (ExecutionException | RetryException e) {
      LOGGER.error("Exception encountered when getting workflows from Helix; likely a Helix/Zk issue.", e);
      return;
    }

    if (jobNameToWorkflowIdMap.containsKey(deleteJobArrival.getJobName())) {
      String workflowId = jobNameToWorkflowIdMap.get(deleteJobArrival.getJobName());
      TaskDriver taskDriver = new TaskDriver(this.jobHelixManager);
      taskDriver.waitToStop(workflowId, this.helixJobStopTimeoutMillis);
      LOGGER.info("Stopped workflow: {}", deleteJobArrival.getJobName());
      //Wait until the cancelled job is complete.
      waitForJobCompletion(deleteJobArrival.getJobName());
    } else {
      LOGGER.warn("Could not find Helix Workflow Id for job: {}", deleteJobArrival.getJobName());
    }
  }
}
 
Example #15
Source File: LifecycleHelper.java    From Baragon with Apache License 2.0 5 votes vote down vote up
private Collection<BaragonServiceState> getGlobalStateWithRetry() {
  Retryer<Collection<BaragonServiceState>> retryer = RetryerBuilder.<Collection<BaragonServiceState>>newBuilder()
      .retryIfException()
      .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getMaxGetGloablStateAttempts()))
      .withWaitStrategy(WaitStrategies.exponentialWait(1, TimeUnit.SECONDS))
      .build();

  try {
    return retryer.call(this::getGlobalState);
  } catch (Exception e) {
    LOG.error("Could not get global state from Baragon Service");
    throw Throwables.propagate(e);
  }
}
 
Example #16
Source File: CommonConfig.java    From txle with Apache License 2.0 5 votes vote down vote up
@Bean
public Retryer<Boolean> retryer() {
    return RetryerBuilder.<Boolean>newBuilder()
            .retryIfException()
            .retryIfResult(Predicates.equalTo(false))
            .withWaitStrategy(WaitStrategies.fixedWait(interval, TimeUnit.SECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(times))
            .build();
}
 
Example #17
Source File: ResyncListener.java    From Baragon with Apache License 2.0 5 votes vote down vote up
private void reapplyConfigsWithRetry() {
  Callable<Void> callable = new Callable<Void>() {
    public Void call() throws Exception {
      if (!agentLock.tryLock(agentLockTimeoutMs, TimeUnit.MILLISECONDS)) {
        LOG.warn("Failed to acquire lock for config reapply");
        throw new LockTimeoutException(String.format("Failed to acquire lock to reapply most current configs in %s ms", agentLockTimeoutMs), agentLock);
      }
      try {
        lifecycleHelper.applyCurrentConfigs();
        return null;
      } finally {
        agentLock.unlock();
      }
    }
  };

  Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder()
    .retryIfException()
    .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getMaxReapplyConfigAttempts()))
    .withWaitStrategy(WaitStrategies.exponentialWait(1, TimeUnit.SECONDS))
    .build();

  try {
    retryer.call(callable);
  } catch (RetryException re) {
    LOG.error("Exception applying current configs", re.getLastFailedAttempt().getExceptionCause());
    lifecycleHelper.abort("Caught exception while trying to resync, aborting", re);
  } catch (ExecutionException ee) {
    LOG.error("Exception applying current configs", ee);
    lifecycleHelper.abort("Caught exception while trying to resync, aborting", ee);
  }
}
 
Example #18
Source File: ZuoraClientFilesStreamer.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
public ZuoraClientFilesStreamer(WorkUnitState workUnitState, ZuoraClient client) {
  _workUnitState = workUnitState;
  _client = client;
  batchSize = workUnitState.getPropAsInt(ConfigurationKeys.SOURCE_QUERYBASED_FETCH_SIZE, 2000);
  outputFormat = _workUnitState.getProp(ZuoraConfigurationKeys.ZUORA_OUTPUT_FORMAT);
  _getRetryer = RetryerBuilder.<Void>newBuilder().retryIfExceptionOfType(IOException.class).withStopStrategy(
      StopStrategies
          .stopAfterAttempt(workUnitState.getPropAsInt(ZuoraConfigurationKeys.ZUORA_API_RETRY_STREAM_FILES_COUNT, 3)))
      .withWaitStrategy(WaitStrategies
          .fixedWait(workUnitState.getPropAsInt(ZuoraConfigurationKeys.ZUORA_API_RETRY_STREAM_FILES_WAIT_TIME, 10000),
              TimeUnit.MILLISECONDS)).build();
}
 
Example #19
Source File: AzkabanClient.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Child class should have a different builderMethodName.
 */
@Builder
protected AzkabanClient(String username,
                        String password,
                        String url,
                        long sessionExpireInMin,
                        CloseableHttpClient httpClient,
                        SessionManager sessionManager,
                        ExecutorService executorService)
    throws AzkabanClientException {
  this.username = username;
  this.password = password;
  this.url = url;
  this.sessionExpireInMin = sessionExpireInMin;
  this.httpClient = httpClient;
  this.sessionManager = sessionManager;
  this.executorService = executorService;
  this.initializeClient();
  this.initializeSessionManager();
  this.intializeExecutorService();
  this.retryer = RetryerBuilder.<AzkabanClientStatus>newBuilder()
      .retryIfExceptionOfType(InvalidSessionException.class)
      .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(10, TimeUnit.SECONDS, this.executorService))
      .withWaitStrategy(WaitStrategies.exponentialWait(60, TimeUnit.SECONDS))
      .withStopStrategy(StopStrategies.stopAfterAttempt(3))
      .build();
  this.sessionId = this.sessionManager.fetchSession();
  this.sessionCreationTime = System.nanoTime();
}
 
Example #20
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private static <T> Retryer<T> newFixedAttemptBoundRetryer(Config config) {
  return RetryerBuilder.<T> newBuilder()
      .retryIfException(RETRY_EXCEPTION_PREDICATE)
      .withWaitStrategy(WaitStrategies.fixedWait(config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS))
      .withStopStrategy(StopStrategies.stopAfterAttempt(config.getInt(RETRY_TIMES)))
      .build();
}
 
Example #21
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private static <T> Retryer<T> newExponentialRetryer(Config config) {
  return RetryerBuilder.<T> newBuilder()
      .retryIfException(RETRY_EXCEPTION_PREDICATE)
      .withWaitStrategy(WaitStrategies.exponentialWait(config.getLong(RETRY_MULTIPLIER),
                                                       config.getLong(RETRY_INTERVAL_MS),
                                                       TimeUnit.MILLISECONDS))
      .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS))
      .build();
}
 
Example #22
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private static <T> Retryer<T> newFixedRetryer(Config config) {
  return RetryerBuilder.<T> newBuilder()
      .retryIfException(RETRY_EXCEPTION_PREDICATE)
      .withWaitStrategy(WaitStrategies.fixedWait(config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS))
      .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS))
      .build();
}
 
Example #23
Source File: RetryableTemplate.java    From java-samples with Apache License 2.0 5 votes vote down vote up
public static <T> T callWithRetry(Callable<T> callable)
    throws ExecutionException, RetryException {
  Retryer<T> retryer =
      RetryerBuilder.<T>newBuilder()
          .retryIfException(
              input -> {
                if (input instanceof GoogleJsonResponseException) {
                  GoogleJsonResponseException jsonException = (GoogleJsonResponseException) input;
                  int responseCode = jsonException.getDetails().getCode();
                  if (DONT_RETRY.contains(responseCode)) {
                    logger.log(
                        Level.WARNING,
                        "Encountered Non Retryable Error: " + jsonException.getMessage());
                    return false;
                  } else {
                    logger.log(
                        Level.WARNING,
                        "Encountered retryable error: "
                            + jsonException.getMessage()
                            + ".Retrying...");
                    return true;
                  }
                } else {
                  logger.log(
                      Level.WARNING,
                      "Encountered error: " + input.getMessage() + ". Retrying...");
                  return true;
                }
              })
          .withWaitStrategy(WaitStrategies.fixedWait(40, TimeUnit.SECONDS))
          .withStopStrategy(StopStrategies.stopAfterAttempt(1000))
          .build();
  return retryer.call(callable);
}
 
Example #24
Source File: IgniteMqttStreamerTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSingleTopic_NoQoS_RetryOnce() throws Exception {
    // configure streamer
    streamer.setSingleTupleExtractor(singleTupleExtractor());
    streamer.setRetryWaitStrategy(WaitStrategies.noWait());
    streamer.setRetryStopStrategy(StopStrategies.stopAfterAttempt(1));
    streamer.setTopic(SINGLE_TOPIC_NAME);

    // subscribe to cache PUT events
    CountDownLatch latch = subscribeToPutEvents(50);

    // action time
    streamer.start();

    // send messages
    sendMessages(Arrays.asList(SINGLE_TOPIC_NAME), 0, 50, false);

    // assertions
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertCacheEntriesLoaded(50);

    // now shutdown the broker, wait 2 seconds and start it again
    broker.stop();
    broker.start(true);

    broker.waitUntilStarted();

    client.connect();

    // lets send messages and ensure they are not received, because our retrier desisted
    sendMessages(Arrays.asList(SINGLE_TOPIC_NAME), 50, 50, false);

    Thread.sleep(3000);

    assertNull(grid().cache(DEFAULT_CACHE_NAME).get(50));
}
 
Example #25
Source File: ServiceAccountUsageAuthorizerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  MockitoAnnotations.initMocks(this);
  projectBinding.setRole(SERVICE_ACCOUNT_USER_ROLE);
  projectBinding.setMembers(new ArrayList<>());
  projectBinding.getMembers().add("user:[email protected]");
  projectBinding.getMembers().add("group:" + PROJECT_ADMINS_GROUP_EMAIL);
  final com.google.api.services.cloudresourcemanager.model.Policy projectPolicy =
      new com.google.api.services.cloudresourcemanager.model.Policy();
  projectPolicy.setBindings(new ArrayList<>());
  projectPolicy.getBindings().add(projectBinding);
  saBinding.setRole(SERVICE_ACCOUNT_USER_ROLE);
  saBinding.setMembers(new ArrayList<>());
  saBinding.getMembers().add("user:[email protected]");
  saBinding.getMembers().add("group:" + SERVICE_ACCOUNT_ADMINS_GROUP_EMAIL);
  final com.google.api.services.iam.v1.model.Policy saPolicy =
      new com.google.api.services.iam.v1.model.Policy();
  saPolicy.setBindings(new ArrayList<>());
  saPolicy.getBindings().add(saBinding);
  when(authorizationPolicy.shouldEnforceAuthorization(any(), any(), any())).thenReturn(true);
  when(idToken.getPayload()).thenReturn(idTokenPayload);
  when(idTokenPayload.getEmail()).thenReturn(PRINCIPAL_EMAIL);
  when((Object) getIamPolicy.execute()).thenReturn(projectPolicy);
  when((Object) crm.projects().getIamPolicy(any(), eq(GET_IAM_POLICY_REQUEST))).thenReturn(getIamPolicy);
  when((Object) iam.projects().serviceAccounts().getIamPolicy(any()).execute()).thenReturn(saPolicy);
  doReturn(members).when(directory).members();
  doReturn(isNotMember).when(members).hasMember(any(), any());
  doReturn(new MembersHasMember().setIsMember(true)).when(isMember).execute();
  doReturn(new MembersHasMember().setIsMember(false)).when(isNotMember).execute();
  when((Object) iam.projects().serviceAccounts().get(any()).execute())
      .thenReturn(new ServiceAccount()
          .setEmail(MANAGED_SERVICE_ACCOUNT)
          .setProjectId(SERVICE_ACCOUNT_PROJECT));
  credential = ServiceAccountCredentials.newBuilder()
      .setPrivateKey(privateKey)
      .setClientEmail("[email protected]")
      .build();
  sut = new ServiceAccountUsageAuthorizer.Impl(iam, crm, directory, SERVICE_ACCOUNT_USER_ROLE, authorizationPolicy,
      WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(RETRY_ATTEMPTS), MESSAGE, ADMINISTRATORS, BLACKLIST);
}
 
Example #26
Source File: GoogleApiClientUtilTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
@Parameters({ "400", "401", "403", "404" })
public void shouldNotRetry4xx(int statusCode) throws IOException {
  when(response.getStatusCode()).thenReturn(statusCode).thenReturn(200);
  try {
    GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3));
    fail();
  } catch (HttpResponseException e) {
    assertThat(e.getStatusCode(), is(statusCode));
  }
  verify(request, times(1)).execute();
}
 
Example #27
Source File: GoogleApiClientUtilTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRetryIOException() throws IOException {
  response.setStatusCode(200);
  doThrow(new IOException()).doCallRealMethod().when(request).execute();
  GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3));
  verify(request, times(2)).execute();
}
 
Example #28
Source File: GoogleApiClientUtilTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
@Parameters({ "500", "503", "599" })
public void shouldRetry5xx(int statusCode) throws IOException {
  when(response.getStatusCode()).thenReturn(statusCode).thenReturn(200);
  GoogleApiClientUtil.executeWithRetries(request, WaitStrategies.noWait(), StopStrategies.stopAfterAttempt(3));
  verify(request, times(2)).execute();
}
 
Example #29
Source File: MysqlSchemaUtil.java    From SpinalTap with Apache License 2.0 5 votes vote down vote up
private <T> Retryer<T> createRetryer() {
  return RetryerBuilder.<T>newBuilder()
      .retryIfRuntimeException()
      .withWaitStrategy(WaitStrategies.exponentialWait(2, 30, TimeUnit.SECONDS))
      .withStopStrategy(StopStrategies.stopAfterDelay(3 * DateTimeConstants.MILLIS_PER_MINUTE))
      .build();
}
 
Example #30
Source File: ShopifySdk.java    From shopify-sdk with Apache License 2.0 5 votes vote down vote up
private Retryer<Response> buildResponseRetyer() {
	return RetryerBuilder.<Response>newBuilder().retryIfResult(ShopifySdk::shouldRetryResponse).retryIfException()
			.withWaitStrategy(WaitStrategies.randomWait(minimumRequestRetryRandomDelayMilliseconds,
					TimeUnit.MILLISECONDS, maximumRequestRetryRandomDelayMilliseconds, TimeUnit.MILLISECONDS))
			.withStopStrategy(
					StopStrategies.stopAfterDelay(maximumRequestRetryTimeoutMilliseconds, TimeUnit.MILLISECONDS))
			.withRetryListener(new ShopifySdkRetryListener()).build();
}