Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS: Enable Adaptive Retries for AWS KMS client #11420

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class AssumeRoleAwsClientFactory implements AwsClientFactory {
private HttpClientProperties httpClientProperties;
private S3FileIOProperties s3FileIOProperties;
private String roleSessionName;
private AwsClientProperties awsClientProperties;

@Override
public S3Client s3() {
Expand All @@ -64,6 +65,7 @@ public KmsClient kms() {
return KmsClient.builder()
.applyMutation(this::applyAssumeRoleConfigurations)
.applyMutation(httpClientProperties::applyHttpClientConfigurations)
.applyMutation(awsClientProperties::applyRetryConfigurations)
.build();
}

Expand All @@ -81,6 +83,7 @@ public void initialize(Map<String, String> properties) {
this.awsProperties = new AwsProperties(properties);
this.s3FileIOProperties = new S3FileIOProperties(properties);
this.httpClientProperties = new HttpClientProperties(properties);
this.awsClientProperties = new AwsClientProperties(properties);
this.roleSessionName = genSessionName();
Preconditions.checkNotNull(
awsProperties.clientAssumeRoleArn(),
Expand Down Expand Up @@ -126,6 +129,10 @@ protected S3FileIOProperties s3FileIOProperties() {
return s3FileIOProperties;
}

protected AwsClientProperties awsClientProperties() {
return awsClientProperties;
}

private StsClient sts() {
return StsClient.builder()
.applyMutation(httpClientProperties::applyHttpClientConfigurations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public KmsClient kms() {
.applyMutation(awsClientProperties::applyClientRegionConfiguration)
.applyMutation(httpClientProperties::applyHttpClientConfigurations)
.applyMutation(awsClientProperties::applyClientCredentialConfigurations)
.applyMutation(awsClientProperties::applyRetryConfigurations)
.build();
}

Expand Down
22 changes: 22 additions & 0 deletions aws/src/main/java/org/apache/iceberg/aws/AwsClientProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.regions.Region;

public class AwsClientProperties implements Serializable {
Expand Down Expand Up @@ -154,6 +156,26 @@ public AwsCredentialsProvider credentialsProvider(
return DefaultCredentialsProvider.builder().build();
}

/**
* Configure <a
* href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/retry/RetryMode.html">RetryMode</a>
* to ADAPTIVE_V2 for AWS clients
*
* <p>Sample usage:
*
* <pre>
* KmsClient.builder().applyMutation(awsClientProperties::applyRetryConfigurations)
* </pre>
*/
public <T extends AwsClientBuilder> void applyRetryConfigurations(T builder) {
ClientOverrideConfiguration.Builder configBuilder =
null != builder.overrideConfiguration()
? builder.overrideConfiguration().toBuilder()
: ClientOverrideConfiguration.builder();

builder.overrideConfiguration(configBuilder.retryStrategy(RetryMode.ADAPTIVE_V2).build());
}

private AwsCredentialsProvider credentialsProvider(String credentialsProviderClass) {
Class<?> providerClass;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public KmsClient kms() {
if (isTableRegisteredWithLakeFormation()) {
return KmsClient.builder()
.applyMutation(httpClientProperties()::applyHttpClientConfigurations)
.applyMutation(awsClientProperties()::applyRetryConfigurations)
.credentialsProvider(
new LakeFormationCredentialsProvider(lakeFormation(), buildTableArn()))
.region(Region.of(region()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.apache.iceberg.aws.kms;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.iceberg.aws.AwsClientProperties;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.services.kms.KmsClient;
import software.amazon.awssdk.services.kms.KmsClientBuilder;

public class TestKmsClientProperties {
@Test
public void testApplyRetryConfiguration() {
AwsClientProperties awsClientProperties = new AwsClientProperties();

KmsClientBuilder builder = KmsClient.builder();
awsClientProperties.applyRetryConfigurations(builder);
RetryMode retryPolicy = builder.overrideConfiguration().retryMode().get();

assertThat(retryPolicy).as("retry mode should be ADAPTIVE_V2").isEqualTo(RetryMode.ADAPTIVE_V2);
}
}