Skip to content

Conversation

davidh44
Copy link
Contributor

@davidh44 davidh44 commented Jun 21, 2023

Motivation and Context

Adding request compression interceptor to handle compression, and implementing non-streaming compression.

Modifications

Added CompressionType enum for supported encodings, Compressor interface and GzipCompressor implementation.

Testing

To add tests once streaming compression is implemented.

Was able to send compressed PutMetricData request to CloudWatch successfully

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@davidh44 davidh44 requested a review from a team as a code owner June 21, 2023 16:16

private static SdkHttpRequest updateContentEncodingHeader(SdkHttpRequest sdkHttpRequest,
ExecutionAttributes executionAttributes) {
Compressor compressor = resolveCompressionType(executionAttributes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this method take a Compressor as a parameter instead of resolving it? It simplifies the logic and is slightly more efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, updating

@SdkPublicApi
public interface Compressor {

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be javadoc strings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, adding second *

/*
* The compression algorithm type.
*/
String contentType();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be compressorType, per the javadoc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, updating

/*
* Compress an async stream.
*/
Publisher<ByteBuffer> compressAsyncStream(InputStream inputStream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this take a Publisher<ByteBuffer> as input?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, updating

import software.amazon.awssdk.core.exception.SdkClientException;

@SdkInternalApi
public class GzipCompressor implements Compressor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can probably be final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding modifier

Comment on lines 49 to 57
public Compressor compressor() {
if (value == null) {
return null;
}
if (value.equals("gzip")) {
return new GzipCompressor();
}
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to the Compressor interface instead, so it matches what we do in SdkChecksum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, migrating

@davidh44 davidh44 changed the title Request compression interceptor and non-streaming compression Request compression interceptor and non-streaming + sync streaming compression Jun 21, 2023
case GZIP:
return new GzipCompressor();
default:
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should throw for unrecognized types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to throw IllegalArgumentException

Comment on lines 69 to 75
InputStream inputStream = sdkHttpFullRequest.contentStreamProvider().get().newStream();
InputStream compressedStream = compressor.compress(inputStream);
SdkHttpRequest sdkHttpRequest =
sdkHttpFullRequest.toBuilder()
.contentStreamProvider(() -> compressedStream)
.build();
sdkHttpRequest = updateContentEncodingHeader(sdkHttpRequest, compressor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is is only doing a one-time compression of the wrapped stream. The new content stream provider should be wrapping the existing provider, something like this:

ContentStreamProvider compressedStreamProvider = () -> compressor.compress(wrappedProvider.newStream());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotchu, updated

Comment on lines 88 to 100
if (isTransferEncodingChunked(context)) {
InputStream compressedStream = compressor.compress(requestBody.contentStreamProvider().newStream());
try {
byte[] compressedBytes = IoUtils.toByteArray(compressedStream);
return Optional.of(RequestBody.fromBytes(compressedBytes));
} catch(IOException e){
throw SdkClientException.create(e.getMessage(), e);
}
}

CompressionContentStreamProvider streamProvider =
new CompressionContentStreamProvider(requestBody.contentStreamProvider(), compressor);
return Optional.of(RequestBody.fromContentProvider(streamProvider, requestBody.contentType()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there separate logic for chunked encoding? Does this interceptor need to care about chunked encoding at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SEP states:
If isStreaming results in Transfer-Encoding: chunked, then the stream must be compressed before it is chunked

So if its chunked encoding, we'll compress the entire body. Otherwise, we'll compress in chunks

}

@Override
public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we setting the content stream provider both here and modifyHttpRequest?

Copy link
Contributor Author

@davidh44 davidh44 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If its a non-streaming request, it won't be set in modifyHttpContent (will just return the empty Optional<RequestBody)

If its a streaming request, it won't be set in modifyHttpRequest (will just update the content encoding header and return the SdkHttpRequest)

Removing updates and moving the streaming compression to separate PR as discussed

@davidh44 davidh44 changed the title Request compression interceptor and non-streaming + sync streaming compression Request compression interceptor and non-streaming compression Jun 26, 2023
@davidh44 davidh44 merged commit 6005427 into feature/master/request-compression Jun 26, 2023
@davidh44 davidh44 deleted the hdavidh/request-compression-interceptor branch June 26, 2023 19:09
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug C 12 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 29 Code Smells

49.0% 49.0% Coverage
0.7% 0.7% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

aws-sdk-java-automation added a commit that referenced this pull request Jul 16, 2025
…cce3d0c20

Pull request: release <- staging/13c825e9-633d-4d14-b4f3-339cce3d0c20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants