Search in sources :

Example 1 with RetryMode

use of software.amazon.awssdk.core.retry.RetryMode in project openhab-addons by openhab.

the class DynamoDBConfig method fromConfig.

/**
 * @param config persistence service configuration
 * @return DynamoDB configuration. Returns null in case of configuration errors
 */
@Nullable
public static DynamoDBConfig fromConfig(Map<String, Object> config) {
    ExpectedTableSchema tableRevision;
    try {
        String regionName = (String) config.get("region");
        if (regionName == null) {
            return null;
        }
        final Region region;
        if (Region.regions().stream().noneMatch(r -> r.toString().equals(regionName))) {
            LOGGER.warn("Region {} is not matching known regions: {}. The region might not be supported.", regionName, Region.regions().stream().map(r -> r.toString()).collect(Collectors.joining(", ")));
        }
        region = Region.of(regionName);
        Optional<RetryMode> retryMode = Optional.empty();
        AwsCredentials credentials;
        String accessKey = (String) config.get("accessKey");
        String secretKey = (String) config.get("secretKey");
        if (accessKey != null && !accessKey.isBlank() && secretKey != null && !secretKey.isBlank()) {
            LOGGER.debug("accessKey and secretKey specified. Using those.");
            credentials = AwsBasicCredentials.create(accessKey, secretKey);
        } else {
            LOGGER.debug("accessKey and/or secretKey blank. Checking profilesConfigFile and profile.");
            String profilesConfigFile = (String) config.get("profilesConfigFile");
            String profile = (String) config.get("profile");
            if (profilesConfigFile == null || profilesConfigFile.isBlank() || profile == null || profile.isBlank()) {
                LOGGER.error("Specify either 1) accessKey and secretKey; or 2) profilesConfigFile and " + "profile for providing AWS credentials");
                return null;
            }
            ProfileFile profileFile = ProfileFile.builder().content(Path.of(profilesConfigFile)).type(Type.CREDENTIALS).build();
            credentials = ProfileCredentialsProvider.builder().profileFile(profileFile).profileName(profile).build().resolveCredentials();
            retryMode = profileFile.profile(profile).flatMap(p -> p.property(ProfileProperty.RETRY_MODE)).flatMap(retry_mode -> {
                for (RetryMode value : RetryMode.values()) {
                    if (retry_mode.equalsIgnoreCase(value.name())) {
                        return Optional.of(value);
                    }
                }
                LOGGER.warn("Unknown retry_mode '{}' in profile. Ignoring and using SDK default retry mode.", retry_mode);
                return Optional.empty();
            });
            LOGGER.debug("Retry mode {}", retryMode);
        }
        String table = (String) config.get("table");
        String tablePrefixLegacy;
        if (table == null || table.isBlank()) {
            // the new parameter 'table' has not been set. Check whether the legacy parameter 'tablePrefix' is set
            table = DEFAULT_TABLE_NAME;
            tablePrefixLegacy = (String) config.get("tablePrefix");
            if (tablePrefixLegacy == null || tablePrefixLegacy.isBlank()) {
                LOGGER.debug("Using default table prefix {}", DEFAULT_TABLE_PREFIX);
                // No explicit value has been specified for tablePrefix, user could be still using the legacy setup
                tableRevision = ExpectedTableSchema.MAYBE_LEGACY;
                tablePrefixLegacy = DEFAULT_TABLE_PREFIX;
            } else {
                // Explicit value for tablePrefix, user certainly prefers LEGACY
                tableRevision = ExpectedTableSchema.LEGACY;
            }
        } else {
            tableRevision = ExpectedTableSchema.NEW;
            tablePrefixLegacy = DEFAULT_TABLE_PREFIX;
        }
        final long readCapacityUnits;
        String readCapacityUnitsParam = (String) config.get("readCapacityUnits");
        if (readCapacityUnitsParam == null || readCapacityUnitsParam.isBlank()) {
            readCapacityUnits = DEFAULT_READ_CAPACITY_UNITS;
        } else {
            readCapacityUnits = Long.parseLong(readCapacityUnitsParam);
        }
        final long writeCapacityUnits;
        String writeCapacityUnitsParam = (String) config.get("writeCapacityUnits");
        if (writeCapacityUnitsParam == null || writeCapacityUnitsParam.isBlank()) {
            writeCapacityUnits = DEFAULT_WRITE_CAPACITY_UNITS;
        } else {
            writeCapacityUnits = Long.parseLong(writeCapacityUnitsParam);
        }
        @Nullable final Integer expireDays;
        String expireDaysString = (String) config.get("expireDays");
        if (expireDaysString == null || expireDaysString.isBlank()) {
            expireDays = null;
        } else {
            expireDays = Integer.parseInt(expireDaysString);
            if (expireDays <= 0) {
                LOGGER.error("expireDays should be positive integer or null");
                return null;
            }
        }
        switch(tableRevision) {
            case NEW:
                LOGGER.debug("Using new DynamoDB table schema");
                return DynamoDBConfig.newSchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), table, readCapacityUnits, writeCapacityUnits, expireDays);
            case LEGACY:
                LOGGER.warn("Using legacy DynamoDB table schema. It is recommended to transition to new schema by defining 'table' parameter and not configuring 'tablePrefix'");
                return DynamoDBConfig.legacySchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), tablePrefixLegacy, readCapacityUnits, writeCapacityUnits);
            case MAYBE_LEGACY:
                LOGGER.debug("Unclear whether we should use new legacy DynamoDB table schema. It is recommended to explicitly define new 'table' parameter. The correct table schema will be detected at runtime.");
                return DynamoDBConfig.maybeLegacySchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), table, tablePrefixLegacy, readCapacityUnits, writeCapacityUnits, expireDays);
            default:
                throw new IllegalStateException("Unhandled enum. Bug");
        }
    } catch (Exception e) {
        LOGGER.error("Error with configuration: {} {}", e.getClass().getSimpleName(), e.getMessage());
        return null;
    }
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) AwsRetryPolicy(software.amazon.awssdk.awscore.retry.AwsRetryPolicy) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Type(software.amazon.awssdk.profiles.ProfileFile.Type) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) LoggerFactory(org.slf4j.LoggerFactory) ProfileCredentialsProvider(software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider) Collectors(java.util.stream.Collectors) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Optional(java.util.Optional) ProfileProperty(software.amazon.awssdk.profiles.ProfileProperty) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) RetryMode(software.amazon.awssdk.core.retry.RetryMode) Region(software.amazon.awssdk.regions.Region) Path(java.nio.file.Path) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Region(software.amazon.awssdk.regions.Region) Nullable(org.eclipse.jdt.annotation.Nullable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with RetryMode

use of software.amazon.awssdk.core.retry.RetryMode in project aws-sdk-java-v2 by aws.

the class DynamoDbRetryPolicy method resolveRetryPolicy.

public static RetryPolicy resolveRetryPolicy(SdkClientConfiguration config) {
    RetryPolicy configuredRetryPolicy = config.option(SdkClientOption.RETRY_POLICY);
    if (configuredRetryPolicy != null) {
        return configuredRetryPolicy;
    }
    RetryMode retryMode = RetryMode.resolver().profileFile(() -> config.option(SdkClientOption.PROFILE_FILE)).profileName(config.option(SdkClientOption.PROFILE_NAME)).defaultRetryMode(config.option(SdkClientOption.DEFAULT_RETRY_MODE)).resolve();
    return AwsRetryPolicy.forRetryMode(retryMode).toBuilder().additionalRetryConditionsAllowed(false).numRetries(MAX_ERROR_RETRY).backoffStrategy(BACKOFF_STRATEGY).build();
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) AwsRetryPolicy(software.amazon.awssdk.awscore.retry.AwsRetryPolicy) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy)

Example 3 with RetryMode

use of software.amazon.awssdk.core.retry.RetryMode in project aws-sdk-java-v2 by aws.

the class BaseClientBuilderClass method mergeInternalDefaultsMethod.

private Optional<MethodSpec> mergeInternalDefaultsMethod() {
    String userAgent = model.getCustomizationConfig().getUserAgent();
    RetryMode defaultRetryMode = model.getCustomizationConfig().getDefaultRetryMode();
    // If none of the options are customized, then we do not need to bother overriding the method
    if (userAgent == null && defaultRetryMode == null) {
        return Optional.empty();
    }
    MethodSpec.Builder builder = MethodSpec.methodBuilder("mergeInternalDefaults").addAnnotation(Override.class).addModifiers(PROTECTED, FINAL).returns(SdkClientConfiguration.class).addParameter(SdkClientConfiguration.class, "config").addCode("return config.merge(c -> {\n");
    if (userAgent != null) {
        builder.addCode("c.option($T.INTERNAL_USER_AGENT, $S);\n", SdkClientOption.class, userAgent);
    }
    if (defaultRetryMode != null) {
        builder.addCode("c.option($T.DEFAULT_RETRY_MODE, $T.$L);\n", SdkClientOption.class, RetryMode.class, defaultRetryMode.name());
    }
    builder.addCode("});\n");
    return Optional.of(builder.build());
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 4 with RetryMode

use of software.amazon.awssdk.core.retry.RetryMode in project aws-sdk-java-v2 by aws.

the class AwsDefaultClientBuilder method resolveAwsRetryPolicy.

private RetryPolicy resolveAwsRetryPolicy(SdkClientConfiguration config) {
    RetryPolicy policy = config.option(SdkClientOption.RETRY_POLICY);
    if (policy != null) {
        if (policy.additionalRetryConditionsAllowed()) {
            return AwsRetryPolicy.addRetryConditions(policy);
        } else {
            return policy;
        }
    }
    RetryMode retryMode = RetryMode.resolver().profileFile(() -> config.option(SdkClientOption.PROFILE_FILE)).profileName(config.option(SdkClientOption.PROFILE_NAME)).defaultRetryMode(config.option(SdkClientOption.DEFAULT_RETRY_MODE)).resolve();
    return AwsRetryPolicy.forRetryMode(retryMode);
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) AwsRetryPolicy(software.amazon.awssdk.awscore.retry.AwsRetryPolicy) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy)

Example 5 with RetryMode

use of software.amazon.awssdk.core.retry.RetryMode in project aws-sdk-java-v2 by aws.

the class SdkDefaultClientBuilder method resolveRetryPolicy.

private RetryPolicy resolveRetryPolicy(SdkClientConfiguration config) {
    RetryPolicy policy = config.option(SdkClientOption.RETRY_POLICY);
    if (policy != null) {
        return policy;
    }
    RetryMode retryMode = RetryMode.resolver().profileFile(() -> config.option(SdkClientOption.PROFILE_FILE)).profileName(config.option(SdkClientOption.PROFILE_NAME)).defaultRetryMode(config.option(SdkClientOption.DEFAULT_RETRY_MODE)).resolve();
    return RetryPolicy.forRetryMode(retryMode);
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy)

Aggregations

RetryMode (software.amazon.awssdk.core.retry.RetryMode)6 RetryPolicy (software.amazon.awssdk.core.retry.RetryPolicy)5 AwsRetryPolicy (software.amazon.awssdk.awscore.retry.AwsRetryPolicy)4 MethodSpec (com.squareup.javapoet.MethodSpec)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)1 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)1 ProfileCredentialsProvider (software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider)1 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)1 ProfileFile (software.amazon.awssdk.profiles.ProfileFile)1 Type (software.amazon.awssdk.profiles.ProfileFile.Type)1 ProfileProperty (software.amazon.awssdk.profiles.ProfileProperty)1 Region (software.amazon.awssdk.regions.Region)1