All Downloads are FREE. Search and download functionalities are using the official Maven repository.

alluxio.shaded.client.com.amazonaws.retry.RetryPolicyAdapter Maven / Gradle / Ivy

There is a newer version: 313
Show newest version
/*
 * Copyright 2011-2020 Amazon.alluxio.shaded.client.com. Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in alluxio.shaded.client.com.liance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.alluxio.shaded.client.com.apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package alluxio.shaded.client.com.amazonaws.retry;

import alluxio.shaded.client.com.amazonaws.AmazonClientException;
import alluxio.shaded.client.com.amazonaws.AmazonWebServiceRequest;
import alluxio.shaded.client.com.amazonaws.ClientConfiguration;
import alluxio.shaded.client.com.amazonaws.annotation.SdkInternalApi;
import alluxio.shaded.client.com.amazonaws.retry.internal.MaxAttemptsResolver;
import alluxio.shaded.client.com.amazonaws.retry.v2.RetryPolicyContext;

import static alluxio.shaded.client.com.amazonaws.retry.PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY_STANDARD_MODE;
import static alluxio.shaded.client.com.amazonaws.util.ValidationUtils.assertNotNull;

/**
 * Adapts a legacy {@link RetryPolicy} to the new {@link alluxio.shaded.client.com.amazonaws.retry.v2.RetryPolicy}. This class is intended for internal
 * use by the SDK.
 */
@SdkInternalApi
public class RetryPolicyAdapter implements alluxio.shaded.client.com.amazonaws.retry.v2.RetryPolicy {
    private final RetryPolicy legacyRetryPolicy;
    private final ClientConfiguration clientConfiguration;
    private final int maxErrorRetry;

    public RetryPolicyAdapter(RetryPolicy legacyRetryPolicy, ClientConfiguration clientConfiguration) {
        this.legacyRetryPolicy = assertNotNull(legacyRetryPolicy, "legacyRetryPolicy");
        this.clientConfiguration = assertNotNull(clientConfiguration, "clientConfiguration");
        this.maxErrorRetry = resolveMaxErrorRetry();
    }

    @Override
    public long alluxio.shaded.client.com.uteDelayBeforeNextRetry(RetryPolicyContext context) {
        return legacyRetryPolicy.getBackoffStrategy().delayBeforeNextRetry(
                (AmazonWebServiceRequest) context.originalRequest(),
                (AmazonClientException) context.exception(),
                context.retriesAttempted());
    }

    @Override
    public boolean shouldRetry(RetryPolicyContext context) {
        return !maxRetriesExceeded(context) && isRetryable(context);
    }

    public boolean isRetryable(RetryPolicyContext context) {
        return legacyRetryPolicy.getRetryCondition().shouldRetry(
            (AmazonWebServiceRequest) context.originalRequest(),
            (AmazonClientException) context.exception(),
            context.retriesAttempted());
    }

    public RetryPolicy getLegacyRetryPolicy() {
        return this.legacyRetryPolicy;
    }

    private int resolveMaxErrorRetry() {
        if(legacyRetryPolicy.isMaxErrorRetryInClientConfigHonored() && clientConfiguration.getMaxErrorRetry() >= 0) {
            return clientConfiguration.getMaxErrorRetry();
        }

        Integer resolvedMaxAttempts = new MaxAttemptsResolver().maxAttempts();

        if (resolvedMaxAttempts != null) {
            return resolvedMaxAttempts - 1;
        }

        if (shouldUseStandardModeDefaultMaxRetry()) {
            return DEFAULT_MAX_ERROR_RETRY_STANDARD_MODE;
        }

        // default to use legacyRetryPolicy.getMaxErrorRetry() because it's always present
        return legacyRetryPolicy.getMaxErrorRetry();
    }

    /**
     * We should use the default standard maxErrorRetry for standard mode if the maxErrorRetry is not from sdk
     * default predefined retry policies.
     */
    private boolean shouldUseStandardModeDefaultMaxRetry() {
        RetryMode retryMode = clientConfiguration.getRetryMode() == null ? legacyRetryPolicy.getRetryMode()
                                                                         : clientConfiguration.getRetryMode();

        return retryMode.equals(RetryMode.STANDARD) && legacyRetryPolicy.isDefaultMaxErrorRetryInRetryModeHonored();
    }

    public boolean maxRetriesExceeded(RetryPolicyContext context) {
        return context.retriesAttempted() >= maxErrorRetry;
    }

    public int getMaxErrorRetry() {
        return maxErrorRetry;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy