Example usage for com.amazonaws.retry RetryPolicy RetryPolicy

List of usage examples for com.amazonaws.retry RetryPolicy RetryPolicy

Introduction

In this page you can find the example usage for com.amazonaws.retry RetryPolicy RetryPolicy.

Prototype

public RetryPolicy(RetryCondition retryCondition, BackoffStrategy backoffStrategy, int maxErrorRetry,
        boolean honorMaxErrorRetryInClientConfig) 

Source Link

Document

Constructs a new retry policy.

Usage

From source file:com.gu.logback.appender.kinesis.BaseKinesisAppender.java

License:Open Source License

/**
 * Configures appender instance and makes it ready for use by the consumers.
 * It validates mandatory parameters and confirms if the configured stream is
 * ready for publishing data yet./*from   w ww. j av  a  2s . c om*/
 * 
 * Error details are made available through the fallback handler for this
 * appender
 * 
 * @throws IllegalStateException if we encounter issues configuring this
 *           appender instance
 */
@Override
public void start() {
    if (layout == null) {
        initializationFailed = true;
        addError("Invalid configuration - No layout for appender: " + name);
        return;
    }

    if (streamName == null) {
        initializationFailed = true;
        addError("Invalid configuration - streamName cannot be null for appender: " + name);
        return;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setMaxErrorRetry(maxRetries);
    clientConfiguration.setRetryPolicy(new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
            PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, maxRetries, true));
    clientConfiguration.setUserAgent(AppenderConstants.USER_AGENT_STRING);

    BlockingQueue<Runnable> taskBuffer = new LinkedBlockingDeque<Runnable>(bufferSize);
    threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount,
            AppenderConstants.DEFAULT_THREAD_KEEP_ALIVE_SEC, TimeUnit.SECONDS, taskBuffer,
            new BlockFastProducerPolicy());
    threadPoolExecutor.prestartAllCoreThreads();

    this.client = createClient(credentials, clientConfiguration, threadPoolExecutor);

    client.setRegion(findRegion());
    if (!Validator.isBlank(endpoint)) {
        if (!Validator.isBlank(region)) {
            addError("Received configuration for both region as well as Amazon Kinesis endpoint. (" + endpoint
                    + ") will be used as endpoint instead of default endpoint for region (" + region + ")");
        }
        client.setEndpoint(endpoint);
    }

    validateStreamName(client, streamName);

    super.start();
}

From source file:com.gu.logback.appender.kinesis.KinesisAppender.java

License:Open Source License

/**
 * Configures this appender instance and makes it ready for use by the
 * consumers. It validates mandatory parameters and confirms if the configured
 * stream is ready for publishing data yet.
 * //w w  w .j a  va  2 s.  c  om
 * Error details are made available through the fallback handler for this
 * appender
 * 
 * @throws IllegalStateException
 *           if we encounter issues configuring this appender instance
 */
@Override
public void start() {
    if (layout == null) {
        initializationFailed = true;
        addError("Invalid configuration - No layout for appender: " + name);
        return;
    }

    if (streamName == null) {
        initializationFailed = true;
        addError("Invalid configuration - streamName cannot be null for appender: " + name);
        return;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setMaxErrorRetry(maxRetries);
    clientConfiguration.setRetryPolicy(new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
            PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, maxRetries, true));
    clientConfiguration.setUserAgent(AppenderConstants.USER_AGENT_STRING);

    BlockingQueue<Runnable> taskBuffer = new LinkedBlockingDeque<Runnable>(bufferSize);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount,
            AppenderConstants.DEFAULT_THREAD_KEEP_ALIVE_SEC, TimeUnit.SECONDS, taskBuffer,
            new BlockFastProducerPolicy());
    threadPoolExecutor.prestartAllCoreThreads();
    kinesisClient = new AmazonKinesisAsyncClient(credentials, clientConfiguration, threadPoolExecutor);

    boolean regionProvided = !Validator.isBlank(region);
    if (!regionProvided) {
        region = AppenderConstants.DEFAULT_REGION;
    }
    kinesisClient.setRegion(Region.getRegion(Regions.fromName(region)));
    if (!Validator.isBlank(endpoint)) {
        if (regionProvided) {
            addError("Received configuration for both region as well as Amazon Kinesis endpoint. (" + endpoint
                    + ") will be used as endpoint instead of default endpoint for region (" + region + ")");
        }
        kinesisClient.setEndpoint(endpoint);
    }

    DescribeStreamResult describeResult = null;
    try {
        describeResult = kinesisClient.describeStream(streamName);
        String streamStatus = describeResult.getStreamDescription().getStreamStatus();
        if (!StreamStatus.ACTIVE.name().equals(streamStatus)
                && !StreamStatus.UPDATING.name().equals(streamStatus)) {
            initializationFailed = true;
            addError(
                    "Stream " + streamName + " is not ready (in active/updating status) for appender: " + name);
        }
    } catch (ResourceNotFoundException rnfe) {
        initializationFailed = true;
        addError("Stream " + streamName + " doesn't exist for appender: " + name, rnfe);
    }

    asyncCallHander = new AsyncPutCallStatsReporter(this);

    super.start();
}

From source file:com.netflix.edda.AwsClientFactory.java

License:Apache License

private static ClientConfiguration clientConfig(AwsConfiguration config) {
    return new ClientConfiguration().withConnectionTimeout((int) config.connectionTimeout().getMillis())
            .withMaxConnections(config.maxConnections()).withMaxErrorRetry(config.maxErrorRetry())
            .withSocketTimeout((int) config.socketTimeout().getMillis())
            .withRetryPolicy(new RetryPolicy(new RetryPolicy.RetryCondition() {
                private final int maxRetries = config.maxErrorRetry();

                @Override/*w w  w  .  j av  a2 s.c o  m*/
                public boolean shouldRetry(AmazonWebServiceRequest r, AmazonClientException e,
                        int retriesAttempted) {
                    if (e instanceof AmazonServiceException) {
                        int code = ((AmazonServiceException) e).getStatusCode();
                        if (!(code % 100 == 5 || code == 400 || code == 403 || code == 429))
                            return false;
                    }
                    return retriesAttempted < maxRetries;
                }
            }, new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest r, AmazonClientException e,
                        int retriesAttempted) {
                    return retriesAttempted * 1000L;
                }
            }, config.maxErrorRetry(), true));
}

From source file:com.netflix.spinnaker.clouddriver.aws.security.AmazonClientProvider.java

License:Apache License

protected <T extends AmazonWebServiceClient> T getClient(Class<T> impl,
        AWSCredentialsProvider awsCredentialsProvider, String region) {
    checkAWSCredentialsProvider(awsCredentialsProvider);
    try {//from   w  ww. j  av a2  s.c  om
        Constructor<T> constructor = impl.getConstructor(AWSCredentialsProvider.class,
                ClientConfiguration.class);

        ClientConfiguration clientConfiguration = new ClientConfiguration();

        if (awsCredentialsProvider instanceof NetflixSTSAssumeRoleSessionCredentialsProvider) {
            RetryPolicy.RetryCondition delegatingRetryCondition = (originalRequest, exception,
                    retriesAttempted) -> {
                NetflixSTSAssumeRoleSessionCredentialsProvider stsCredentialsProvider = (NetflixSTSAssumeRoleSessionCredentialsProvider) awsCredentialsProvider;
                if (exception instanceof AmazonServiceException) {
                    ((AmazonServiceException) exception).getHttpHeaders().put("targetAccountId",
                            stsCredentialsProvider.getAccountId());
                }
                return retryPolicy.getRetryCondition().shouldRetry(originalRequest, exception,
                        retriesAttempted);
            };

            RetryPolicy delegatingRetryPolicy = new RetryPolicy(delegatingRetryCondition,
                    retryPolicy.getBackoffStrategy(), retryPolicy.getMaxErrorRetry(),
                    retryPolicy.isMaxErrorRetryInClientConfigHonored());
            clientConfiguration.setRetryPolicy(delegatingRetryPolicy);
        } else {
            clientConfiguration.setRetryPolicy(retryPolicy);
        }

        if (proxy != null && proxy.isProxyConfigMode()) {
            proxy.apply(clientConfiguration);
        }

        clientConfiguration.setUseGzip(useGzip);

        T delegate = constructor.newInstance(awsCredentialsProvider, clientConfiguration);
        for (RequestHandler2 requestHandler : requestHandlers) {
            delegate.addRequestHandler(requestHandler);
        }
        if (region != null && region.length() > 0) {
            delegate.setRegion(Region.getRegion(Regions.fromName(region)));
        }
        return delegate;
    } catch (Exception e) {
        throw new RuntimeException("Instantiation of client implementation failed!", e);
    }
}

From source file:com.tcl.gateway.firehose.log4j.FirehoseAppender.java

License:Open Source License

/**
 * Configures this appender instance and makes it ready for use by the
 * consumers. It validates mandatory parameters and confirms if the configured
 * stream is ready for publishing data yet.
 * /*from  w  ww  .  java 2  s. co  m*/
 * Error details are made available through the fallback handler for this
 * appender
 * 
 * @throws IllegalStateException
 *           if we encounter issues configuring this appender instance
 */
@Override
public void activateOptions() {
    if (deliveryStreamName == null) {
        initializationFailed = true;
        error("Invalid configuration - streamName cannot be null for appender: " + name);
    }

    if (layout == null) {
        initializationFailed = true;
        error("Invalid configuration - No layout for appender: " + name);
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration = setProxySettingsFromSystemProperties(clientConfiguration);

    clientConfiguration.setMaxErrorRetry(maxRetries);
    clientConfiguration.setRetryPolicy(new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
            PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, maxRetries, true));
    clientConfiguration.setUserAgent(AppenderConstants.USER_AGENT_STRING);

    final BlockingQueue<Runnable> taskBuffer = new LinkedBlockingDeque<Runnable>(bufferSize);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount,
            AppenderConstants.DEFAULT_THREAD_KEEP_ALIVE_SEC, TimeUnit.SECONDS, taskBuffer,
            new BlockFastProducerPolicy());
    threadPoolExecutor.prestartAllCoreThreads();
    firehoseClient = new AmazonKinesisFirehoseAsyncClient(new CustomCredentialsProviderChain(),
            clientConfiguration, threadPoolExecutor);

    boolean regionProvided = !Validator.isBlank(region);
    if (!regionProvided) {
        region = AppenderConstants.DEFAULT_REGION;
    }
    if (!Validator.isBlank(endpoint)) {
        if (regionProvided) {
            LOGGER.warn("Received configuration for both region as well as Amazon Kinesis endpoint. ("
                    + endpoint + ") will be used as endpoint instead of default endpoint for region (" + region
                    + ")");
        }
        firehoseClient.setEndpoint(endpoint);
    } else {
        firehoseClient.setRegion(Region.getRegion(Regions.fromName(region)));
    }

    DescribeDeliveryStreamResult describeResult = null;
    try {
        describeResult = firehoseClient.describeDeliveryStream(
                new DescribeDeliveryStreamRequest().withDeliveryStreamName(deliveryStreamName));
        String streamStatus = describeResult.getDeliveryStreamDescription().getDeliveryStreamStatus();
        if (!StreamStatus.ACTIVE.name().equals(streamStatus)
                && !StreamStatus.UPDATING.name().equals(streamStatus)) {
            initializationFailed = true;
            error("Delivery Stream " + deliveryStreamName
                    + " is not ready (in active/updating status) for appender: " + name);
        }
    } catch (ResourceNotFoundException rnfe) {
        initializationFailed = true;
        error("Delivery  Stream " + deliveryStreamName + " doesn't exist for appender: " + name, rnfe);
    }

    asyncCallHander = new AsyncPutCallStatsReporter(name);

    if (metric) {
        MetricRegistry registry = new MetricRegistry();
        registry.register("Gauge", new Gauge<Integer>() {

            @Override
            public Integer getValue() {
                return taskBuffer.size();
            }

        });

        ConsoleReporter.forRegistry(registry).build().start(3, TimeUnit.SECONDS);
    }
}

From source file:io.confluent.connect.s3.storage.S3Storage.java

License:Open Source License

/**
 * Creates a retry policy, based on full jitter backoff strategy
 * and default retry condition./*from ww  w . j av  a2 s  . c  o m*/
 * Visible for testing.
 *
 * @param config the S3 configuration.
 * @return retry policy
 * @see com.amazonaws.retry.PredefinedRetryPolicies.SDKDefaultRetryCondition
 * @see PredefinedBackoffStrategies.FullJitterBackoffStrategy
 */
protected RetryPolicy newFullJitterRetryPolicy(S3SinkConnectorConfig config) {

    PredefinedBackoffStrategies.FullJitterBackoffStrategy backoffStrategy = new PredefinedBackoffStrategies.FullJitterBackoffStrategy(
            config.getLong(S3_RETRY_BACKOFF_CONFIG).intValue(), S3_RETRY_MAX_BACKOFF_TIME_MS);

    RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, backoffStrategy,
            conf.getS3PartRetries(), false);
    return retryPolicy;
}

From source file:org.elasticsearch.cloud.aws.AwsEc2ServiceImpl.java

License:Apache License

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(CLOUD_EC2.PROTOCOL_SETTING.get(settings));

    if (PROXY_HOST_SETTING.exists(settings) || CLOUD_EC2.PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = CLOUD_EC2.PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = CLOUD_EC2.PROXY_PORT_SETTING.get(settings);
        String proxyUsername = CLOUD_EC2.PROXY_USERNAME_SETTING.get(settings);
        String proxyPassword = CLOUD_EC2.PROXY_PASSWORD_SETTING.get(settings);

        clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort).withProxyUsername(proxyUsername)
                .withProxyPassword(proxyPassword);
    }/* w w w . ja  v  a2  s . c  om*/

    // #155: we might have 3rd party users using older EC2 API version
    String awsSigner = CLOUD_EC2.SIGNER_SETTING.get(settings);
    if (Strings.hasText(awsSigner)) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        AwsSigner.configureSigner(awsSigner, clientConfiguration);
    }

    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
            new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest,
                        AmazonClientException exception, int retriesAttempted) {
                    // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
                    logger.warn("EC2 API request failed, retry again. Reason was:", exception);
                    return 1000L
                            * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
                }
            }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);

    return clientConfiguration;
}

From source file:org.elasticsearch.discovery.ec2.AwsEc2ServiceImpl.java

License:Apache License

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(PROTOCOL_SETTING.get(settings));

    if (PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = PROXY_PORT_SETTING.get(settings);
        try (SecureString proxyUsername = PROXY_USERNAME_SETTING.get(settings);
                SecureString proxyPassword = PROXY_PASSWORD_SETTING.get(settings)) {

            clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort)
                    .withProxyUsername(proxyUsername.toString()).withProxyPassword(proxyPassword.toString());
        }/*from w ww .j a va2  s. c  o  m*/
    }

    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
            new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest,
                        AmazonClientException exception, int retriesAttempted) {
                    // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
                    logger.warn("EC2 API request failed, retry again. Reason was:", exception);
                    return 1000L
                            * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
                }
            }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout((int) READ_TIMEOUT_SETTING.get(settings).millis());

    return clientConfiguration;
}

From source file:org.finra.herd.dao.RetryPolicyFactory.java

License:Apache License

/**
 * Gets the application's default retry policy. The policy uses PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, a SimpleExponentialBackoffStrategy, and the
 * maximum number of attempts is dynamically configurable through AWS_MAX_RETRY_ATTEMPT.
 * /* ww  w  .  j  av  a 2 s.c o m*/
 * @return RetryPolicy
 */
public RetryPolicy getRetryPolicy() {
    return new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, backoffStrategy,
            configurationHelper.getProperty(ConfigurationValue.AWS_MAX_RETRY_ATTEMPT, Integer.class), true);
}