com.amazonaws.retry.RetryUtils Java Examples

The following examples show how to use com.amazonaws.retry.RetryUtils. 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: ApiImporterDefaultModule.java    From aws-apigateway-importer with Apache License 2.0 5 votes vote down vote up
@Provides
protected RetryPolicy.BackoffStrategy provideBackoffStrategy() {

    // tune these parameters to handle throttling errors
    final int maxBackoffInMilliseconds = 50 * 1000; // maximum exponential back-off time before retrying a request
    final int throttlingScaleFactor = 800; // base sleep time for throttling exceptions
    final int maxRetriesBeforeBackoff = 10; // log2(maxBackoffInMilliseconds/throttlingScaleFactor)

    final int baseScaleFactor = 600; // base sleep time for general exceptions
    final int throttlingScaleFactorRandomRange = throttlingScaleFactor / 4;

    final Random random = new Random();

    return (originalRequest, exception, retriesAttempted) -> {

        LOG.debug("Caught error from service. Retry attempt: " + retriesAttempted, exception);

        if (retriesAttempted < 0) return 0;
        if (retriesAttempted > maxRetriesBeforeBackoff) return maxBackoffInMilliseconds;

        int scaleFactor;
        if (exception instanceof AmazonServiceException
                && RetryUtils.isThrottlingException((AmazonServiceException) exception)) {
            scaleFactor = throttlingScaleFactor + random.nextInt(throttlingScaleFactorRandomRange);
        } else {
            scaleFactor = baseScaleFactor;
        }

        long delay = (1L << retriesAttempted) * scaleFactor;
        delay = Math.min(delay, maxBackoffInMilliseconds);

        LOG.info("Client backing off for " + delay + "ms");

        return delay;
    };
}
 
Example #2
Source File: TracingHandler.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public void afterError(Request<?> request, Response<?> response, Exception e) {
    if (isSubsegmentDuplicate(recorder.getCurrentSubsegmentOptional(), request)) {
        Optional<Subsegment> currentSubsegmentOptional = recorder.getCurrentSubsegmentOptional();
        if (!currentSubsegmentOptional.isPresent()) {
            return;
        }
        Subsegment currentSubsegment = currentSubsegmentOptional.get();

        int statusCode = -1;

        if (null != response) {
            statusCode = response.getHttpResponse().getStatusCode();
        } else {
            if (e instanceof AmazonServiceException) {
                AmazonServiceException ase = (AmazonServiceException) e;
                statusCode = ase.getStatusCode();
                // The S3 client will throw and re-swallow AmazonServiceExceptions if they have these status codes. Customers
                // will never see the exceptions in their application code but they still travel through our
                // TracingHandler#afterError method. We special case these status codes in order to prevent addition of the
                // full exception object to the current subsegment. Instead, we'll just add any exception error message to the
                // current subsegment's cause's message.
                if ((304 == statusCode || 412 == statusCode) && S3_SERVICE_NAME.equals(ase.getServiceName())) {
                    populateAndEndSubsegment(currentSubsegment, request, response, ase);
                    return;
                }
                if (RetryUtils.isThrottlingException(ase)) {
                    currentSubsegment.setError(true);
                    currentSubsegment.setThrottle(true);
                }
            }
        }

        if (-1 != statusCode) {
            int statusCodePrefix = statusCode / 100;
            if (4 == statusCodePrefix) {
                currentSubsegment.setError(true);
                if (429 == statusCode) {
                    currentSubsegment.setThrottle(true);
                }
            }
        }

        currentSubsegment.addException(e);

        if (e instanceof AmazonServiceException) {
            populateAndEndSubsegment(currentSubsegment, request, response, (AmazonServiceException) e);
        } else {
            populateAndEndSubsegment(currentSubsegment, request, response);
        }
    }
}
 
Example #3
Source File: CodeBuildClientRetryCondition.java    From aws-codebuild-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldRetry(AmazonWebServiceRequest amazonWebServiceRequest, AmazonClientException e, int i) {
    return RetryUtils.isThrottlingException(e);
}