Skip to content

Commit 878b2e5

Browse files
committed
Move config resolution logic to SdkDefaultClientBuilder
1 parent b95a721 commit 878b2e5

File tree

2 files changed

+65
-80
lines changed

2 files changed

+65
-80
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import java.util.function.Supplier;
6565
import software.amazon.awssdk.annotations.SdkProtectedApi;
6666
import software.amazon.awssdk.annotations.SdkTestInternalApi;
67+
import software.amazon.awssdk.core.RequestCompressionConfiguration;
68+
import software.amazon.awssdk.core.SdkSystemSetting;
6769
import software.amazon.awssdk.core.client.config.ClientAsyncConfiguration;
6870
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
6971
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
@@ -83,9 +85,11 @@
8385
import software.amazon.awssdk.http.async.AsyncExecuteRequest;
8486
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
8587
import software.amazon.awssdk.metrics.MetricPublisher;
88+
import software.amazon.awssdk.profiles.Profile;
8689
import software.amazon.awssdk.profiles.ProfileFile;
8790
import software.amazon.awssdk.profiles.ProfileFileSupplier;
8891
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
92+
import software.amazon.awssdk.profiles.ProfileProperty;
8993
import software.amazon.awssdk.utils.AttributeMap;
9094
import software.amazon.awssdk.utils.Either;
9195
import software.amazon.awssdk.utils.ScheduledExecutorUtils;
@@ -238,8 +242,6 @@ private SdkClientConfiguration setOverrides(SdkClientConfiguration configuration
238242
builder.option(METRIC_PUBLISHERS, clientOverrideConfiguration.metricPublishers());
239243
builder.option(EXECUTION_ATTRIBUTES, clientOverrideConfiguration.executionAttributes());
240244
builder.option(TOKEN_SIGNER, clientOverrideConfiguration.advancedOption(TOKEN_SIGNER).orElse(null));
241-
builder.option(REQUEST_COMPRESSION_CONFIGURATION,
242-
clientOverrideConfiguration.requestCompressionConfiguration().orElse(null));
243245

244246
clientOverrideConfiguration.advancedOption(ENDPOINT_OVERRIDDEN_OVERRIDE).ifPresent(value -> {
245247
builder.option(ENDPOINT_OVERRIDDEN, value);
@@ -318,9 +320,60 @@ private SdkClientConfiguration finalizeConfiguration(SdkClientConfiguration conf
318320
.option(EXECUTION_INTERCEPTORS, resolveExecutionInterceptors(config))
319321
.option(RETRY_POLICY, retryPolicy)
320322
.option(CLIENT_USER_AGENT, resolveClientUserAgent(config, retryPolicy))
323+
.option(REQUEST_COMPRESSION_CONFIGURATION, resolveRequestCompressionConfiguration())
321324
.build();
322325
}
323326

327+
private RequestCompressionConfiguration resolveRequestCompressionConfiguration() {
328+
Boolean requestCompressionEnabled = null;
329+
Integer minCompressionThreshold = null;
330+
331+
// Client level
332+
RequestCompressionConfiguration clientConfig =
333+
clientOverrideConfiguration.requestCompressionConfiguration().orElse(null);
334+
if (clientConfig != null) {
335+
requestCompressionEnabled = clientConfig.requestCompressionEnabled();
336+
minCompressionThreshold = clientConfig.minimumCompressionThresholdInBytes();
337+
}
338+
339+
// Env level
340+
if (requestCompressionEnabled == null) {
341+
Optional<Boolean> systemSetting = SdkSystemSetting.AWS_DISABLE_REQUEST_COMPRESSION.getBooleanValue();
342+
if (systemSetting.isPresent()) {
343+
requestCompressionEnabled = !systemSetting.get();
344+
}
345+
}
346+
if (minCompressionThreshold == null) {
347+
minCompressionThreshold =
348+
SdkSystemSetting.AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES.getIntegerValue().orElse(null);
349+
}
350+
351+
// Profile level
352+
if (requestCompressionEnabled == null || minCompressionThreshold == null) {
353+
Supplier<ProfileFile> profileFileSupplier = ProfileFile::defaultProfileFile;
354+
String profileName = ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow();
355+
Profile profile = profileFileSupplier.get().profile(profileName).orElse(null);
356+
357+
if (requestCompressionEnabled == null && profile != null) {
358+
Optional<Boolean> profileSetting = profile.booleanProperty(ProfileProperty.DISABLE_REQUEST_COMPRESSION);
359+
if (profileSetting.isPresent()) {
360+
requestCompressionEnabled = !profileSetting.get();
361+
}
362+
}
363+
if (minCompressionThreshold == null && profile != null) {
364+
Optional<String> profileSetting = profile.property(ProfileProperty.REQUEST_MIN_COMPRESSION_SIZE_BYTES);
365+
if (profileSetting.isPresent()) {
366+
minCompressionThreshold = Integer.parseInt(profileSetting.get());
367+
}
368+
}
369+
}
370+
371+
return RequestCompressionConfiguration.builder()
372+
.requestCompressionEnabled(requestCompressionEnabled)
373+
.minimumCompressionThresholdInBytes(minCompressionThreshold)
374+
.build();
375+
}
376+
324377
private String resolveClientUserAgent(SdkClientConfiguration config, RetryPolicy retryPolicy) {
325378
return ApplyUserAgentStage.resolveClientUserAgent(config.option(USER_AGENT_PREFIX),
326379
config.option(INTERNAL_USER_AGENT),
@@ -580,6 +633,4 @@ public void close() {
580633
// Do nothing, this client is managed by the customer.
581634
}
582635
}
583-
584-
585636
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/pipeline/stages/CompressRequestStage.java

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import java.util.List;
2222
import java.util.Locale;
2323
import java.util.Optional;
24-
import java.util.function.Supplier;
2524
import software.amazon.awssdk.annotations.SdkInternalApi;
2625
import software.amazon.awssdk.core.RequestCompressionConfiguration;
2726
import software.amazon.awssdk.core.RequestOverrideConfiguration;
2827
import software.amazon.awssdk.core.SdkBytes;
29-
import software.amazon.awssdk.core.SdkSystemSetting;
3028
import software.amazon.awssdk.core.exception.SdkClientException;
3129
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
3230
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
@@ -37,9 +35,6 @@
3735
import software.amazon.awssdk.core.internal.http.pipeline.MutableRequestToRequestPipeline;
3836
import software.amazon.awssdk.http.ContentStreamProvider;
3937
import software.amazon.awssdk.http.SdkHttpFullRequest;
40-
import software.amazon.awssdk.profiles.ProfileFile;
41-
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
42-
import software.amazon.awssdk.profiles.ProfileProperty;
4338
import software.amazon.awssdk.utils.IoUtils;
4439

4540
/**
@@ -49,15 +44,6 @@
4944
public class CompressRequestStage implements MutableRequestToRequestPipeline {
5045
private static final int DEFAULT_MIN_COMPRESSION_SIZE = 10_240;
5146
private static final int MIN_COMPRESSION_SIZE_LIMIT = 10_485_760;
52-
private static final Supplier<ProfileFile> PROFILE_FILE = ProfileFile::defaultProfileFile;
53-
private static final String PROFILE_NAME = ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow();
54-
private static Boolean compressionEnabledClientLevel;
55-
private static Boolean compressionEnabledEnvLevel;
56-
private static Boolean compressionEnabledProfileLevel;
57-
private static int minCompressionSizeClientLevel = -1;
58-
private static int minCompressionSizeEnvLevel = -1;
59-
private static int minCompressionSizeProfileLevel = -1;
60-
6147

6248
@Override
6349
public SdkHttpFullRequest.Builder execute(SdkHttpFullRequest.Builder input, RequestExecutionContext context)
@@ -153,36 +139,11 @@ private static boolean resolveRequestCompressionEnabled(RequestExecutionContext
153139
return requestCompressionEnabledRequestLevel.get();
154140
}
155141

156-
if (compressionEnabledClientLevel != null) {
157-
return compressionEnabledClientLevel;
158-
}
159-
if (context.executionAttributes().getAttribute(SdkExecutionAttribute.REQUEST_COMPRESSION_CONFIGURATION) != null) {
160-
Boolean requestCompressionEnabledClientLevel = context.executionAttributes().getAttribute(
161-
SdkExecutionAttribute.REQUEST_COMPRESSION_CONFIGURATION).requestCompressionEnabled();
162-
if (requestCompressionEnabledClientLevel != null) {
163-
compressionEnabledClientLevel = requestCompressionEnabledClientLevel;
164-
return compressionEnabledClientLevel;
165-
}
166-
}
167-
168-
if (compressionEnabledEnvLevel != null) {
169-
return compressionEnabledEnvLevel;
170-
}
171-
if (SdkSystemSetting.AWS_DISABLE_REQUEST_COMPRESSION.getBooleanValue().isPresent()) {
172-
compressionEnabledEnvLevel = !SdkSystemSetting.AWS_DISABLE_REQUEST_COMPRESSION.getBooleanValue().get();
173-
return compressionEnabledEnvLevel;
174-
}
175-
176-
if (compressionEnabledProfileLevel != null) {
177-
return compressionEnabledProfileLevel;
178-
}
179-
Optional<Boolean> profileSetting =
180-
PROFILE_FILE.get()
181-
.profile(PROFILE_NAME)
182-
.flatMap(p -> p.booleanProperty(ProfileProperty.DISABLE_REQUEST_COMPRESSION));
183-
if (profileSetting.isPresent()) {
184-
compressionEnabledProfileLevel = !profileSetting.get();
185-
return compressionEnabledProfileLevel;
142+
Boolean isEnabled = context.executionAttributes()
143+
.getAttribute(SdkExecutionAttribute.REQUEST_COMPRESSION_CONFIGURATION)
144+
.requestCompressionEnabled();
145+
if (isEnabled != null) {
146+
return isEnabled;
186147
}
187148

188149
return true;
@@ -205,38 +166,11 @@ private static int resolveMinCompressionSize(RequestExecutionContext context) {
205166
return minimumCompressionSizeRequestLevel.get();
206167
}
207168

208-
if (minCompressionSizeClientLevel >= 0) {
209-
return minCompressionSizeClientLevel;
210-
}
211-
if (context.executionAttributes().getAttribute(SdkExecutionAttribute.REQUEST_COMPRESSION_CONFIGURATION) != null) {
212-
Integer minimumCompressionSizeClientLevel =
213-
context.executionAttributes()
214-
.getAttribute(SdkExecutionAttribute.REQUEST_COMPRESSION_CONFIGURATION)
215-
.minimumCompressionThresholdInBytes();
216-
if (minimumCompressionSizeClientLevel != null) {
217-
minCompressionSizeClientLevel = minimumCompressionSizeClientLevel;
218-
return minCompressionSizeClientLevel;
219-
}
220-
}
221-
222-
if (minCompressionSizeEnvLevel >= 0) {
223-
return minCompressionSizeEnvLevel;
224-
}
225-
if (SdkSystemSetting.AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES.getIntegerValue().isPresent()) {
226-
minCompressionSizeEnvLevel = SdkSystemSetting.AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES.getIntegerValue().get();
227-
return minCompressionSizeEnvLevel;
228-
}
229-
230-
if (minCompressionSizeProfileLevel >= 0) {
231-
return minCompressionSizeProfileLevel;
232-
}
233-
Optional<String> profileSetting =
234-
PROFILE_FILE.get()
235-
.profile(PROFILE_NAME)
236-
.flatMap(p -> p.property(ProfileProperty.REQUEST_MIN_COMPRESSION_SIZE_BYTES));
237-
if (profileSetting.isPresent()) {
238-
minCompressionSizeProfileLevel = Integer.parseInt(profileSetting.get());
239-
return minCompressionSizeProfileLevel;
169+
Integer threshold = context.executionAttributes()
170+
.getAttribute(SdkExecutionAttribute.REQUEST_COMPRESSION_CONFIGURATION)
171+
.minimumCompressionThresholdInBytes();
172+
if (threshold != null) {
173+
return threshold;
240174
}
241175

242176
return DEFAULT_MIN_COMPRESSION_SIZE;

0 commit comments

Comments
 (0)