-
Notifications
You must be signed in to change notification settings - Fork 962
Request compression async streaming #4262
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
Merged
davidh44
merged 15 commits into
feature/master/streaming-request-compression
from
hdavidh/request-compression-async-streaming
Aug 29, 2023
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
39ab2f2
Refactor to common class AwsChunkedInputStream
davidh44 4cb38f7
Sync streaming compression
davidh44 da42b41
Sync streaming compression functional tests
davidh44 0e561b4
Sync streaming compression integ tests
davidh44 a89b134
Fix integ test
davidh44 7550c21
Async streaming compression
davidh44 df59714
Address comments
davidh44 369be3e
Refactor ChunkBuffer class
davidh44 9e5433c
Merge branch 'feature/master/streaming-request-compression' into hdav…
davidh44 280ffc0
Address comments
davidh44 933e259
Address comments
davidh44 14f79fb
Remove unused field
davidh44 5212d3e
Handle demand in Subscriber
davidh44 563b781
Address comments
davidh44 2d1b6fe
Add back final modifier
davidh44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...rc/main/java/software/amazon/awssdk/core/internal/async/ChunkBufferWithUnknownLength.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright Amazon.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 compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.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 software.amazon.awssdk.core.internal.async; | ||
|
|
||
| import static software.amazon.awssdk.core.HttpChecksumConstant.DEFAULT_ASYNC_CHUNK_SIZE; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.utils.builder.SdkBuilder; | ||
|
|
||
| /** | ||
| * Class that will buffer incoming BufferBytes with unknown total length to chunks of bufferSize | ||
| */ | ||
| @SdkInternalApi | ||
| public final class ChunkBufferWithUnknownLength { | ||
| private ByteBuffer currentBuffer; | ||
|
|
||
| private ChunkBufferWithUnknownLength(Integer bufferSize) { | ||
| int chunkSize = bufferSize != null ? bufferSize : DEFAULT_ASYNC_CHUNK_SIZE; | ||
| this.currentBuffer = ByteBuffer.allocate(chunkSize); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new DefaultBuilder(); | ||
| } | ||
|
|
||
| public synchronized Iterable<ByteBuffer> bufferAndCreateChunks(ByteBuffer buffer) { | ||
| List<ByteBuffer> bufferedList = new ArrayList<>(); | ||
| while (buffer.hasRemaining()) { | ||
| int bytesToCopy = Math.min(buffer.remaining(), currentBuffer.remaining()); | ||
| byte[] bytes = new byte[bytesToCopy]; | ||
| buffer.get(bytes); | ||
| currentBuffer.put(bytes); | ||
|
|
||
| if (!currentBuffer.hasRemaining() || !buffer.hasRemaining()) { | ||
| currentBuffer.flip(); | ||
| ByteBuffer bufferToSend = ByteBuffer.allocate(currentBuffer.limit()); | ||
| bufferToSend.put(currentBuffer); | ||
| bufferToSend.flip(); | ||
| bufferedList.add(bufferToSend); | ||
| currentBuffer.clear(); | ||
| } | ||
| } | ||
| return bufferedList; | ||
| } | ||
|
|
||
| public interface Builder extends SdkBuilder<Builder, ChunkBufferWithUnknownLength> { | ||
| Builder bufferSize(int bufferSize); | ||
| } | ||
|
|
||
| private static final class DefaultBuilder implements Builder { | ||
| private Integer bufferSize; | ||
|
|
||
| @Override | ||
| public ChunkBufferWithUnknownLength build() { | ||
| return new ChunkBufferWithUnknownLength(bufferSize); | ||
| } | ||
|
|
||
| @Override | ||
| public Builder bufferSize(int bufferSize) { | ||
| this.bufferSize = bufferSize; | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
146 changes: 146 additions & 0 deletions
146
...src/main/java/software/amazon/awssdk/core/internal/async/CompressionAsyncRequestBody.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright Amazon.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 compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.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 software.amazon.awssdk.core.internal.async; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Optional; | ||
| import org.reactivestreams.Subscriber; | ||
| import org.reactivestreams.Subscription; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
| import software.amazon.awssdk.core.internal.compression.Compressor; | ||
| import software.amazon.awssdk.utils.Validate; | ||
| import software.amazon.awssdk.utils.builder.SdkBuilder; | ||
|
|
||
| /** | ||
| * Wrapper class to wrap an AsyncRequestBody. | ||
| * This will chunk and compress the payload with the provided {@link Compressor}. | ||
| */ | ||
| @SdkInternalApi | ||
| public class CompressionAsyncRequestBody implements AsyncRequestBody { | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static final int COMPRESSION_CHUNK_SIZE = 128 * 1024; | ||
cenedhryn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
davidh44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final AsyncRequestBody wrapped; | ||
| private final Compressor compressor; | ||
|
|
||
| private CompressionAsyncRequestBody(DefaultBuilder builder) { | ||
| Validate.notNull(builder.asyncRequestBody, "wrapped AsyncRequestBody cannot be null"); | ||
| Validate.notNull(builder.compressor, "compressor cannot be null"); | ||
| this.wrapped = builder.asyncRequestBody; | ||
cenedhryn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.compressor = builder.compressor; | ||
| } | ||
|
|
||
| /** | ||
| * @return Builder instance to construct a {@link CompressionAsyncRequestBody}. | ||
| */ | ||
| public static Builder builder() { | ||
| return new DefaultBuilder(); | ||
| } | ||
|
|
||
| public interface Builder extends SdkBuilder<CompressionAsyncRequestBody.Builder, CompressionAsyncRequestBody> { | ||
davidh44 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Sets the AsyncRequestBody that will be wrapped. | ||
| * @param asyncRequestBody | ||
| * @return This builder for method chaining. | ||
| */ | ||
| Builder asyncRequestBody(AsyncRequestBody asyncRequestBody); | ||
|
|
||
| /** | ||
| * Sets the compressor to compress the request. | ||
| * @param compressor | ||
| * @return This builder for method chaining. | ||
| */ | ||
| Builder compressor(Compressor compressor); | ||
| } | ||
|
|
||
| private static final class DefaultBuilder implements Builder { | ||
|
|
||
| private AsyncRequestBody asyncRequestBody; | ||
| private Compressor compressor; | ||
|
|
||
| @Override | ||
| public CompressionAsyncRequestBody build() { | ||
| return new CompressionAsyncRequestBody(this); | ||
| } | ||
|
|
||
| @Override | ||
| public Builder asyncRequestBody(AsyncRequestBody asyncRequestBody) { | ||
| this.asyncRequestBody = asyncRequestBody; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder compressor(Compressor compressor) { | ||
| this.compressor = compressor; | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Long> contentLength() { | ||
| return Optional.empty(); | ||
davidh44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public String contentType() { | ||
| return wrapped.contentType(); | ||
| } | ||
|
|
||
| @Override | ||
| public void subscribe(Subscriber<? super ByteBuffer> s) { | ||
| Validate.notNull(s, "Subscription MUST NOT be null."); | ||
|
|
||
| ChunkBufferWithUnknownLength chunkBuffer = ChunkBufferWithUnknownLength.builder() | ||
| .bufferSize(COMPRESSION_CHUNK_SIZE) | ||
| .build(); | ||
|
|
||
| wrapped.flatMapIterable(chunkBuffer::bufferAndCreateChunks) | ||
| .subscribe(new CompressionSubscriber(s, compressor)); | ||
| } | ||
|
|
||
| private static final class CompressionSubscriber implements Subscriber<ByteBuffer> { | ||
|
|
||
| private final Subscriber<? super ByteBuffer> wrapped; | ||
davidh44 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final Compressor compressor; | ||
|
|
||
| CompressionSubscriber(Subscriber<? super ByteBuffer> wrapped, Compressor compressor) { | ||
| this.wrapped = wrapped; | ||
| this.compressor = compressor; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSubscribe(Subscription subscription) { | ||
| wrapped.onSubscribe(subscription); | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(ByteBuffer byteBuffer) { | ||
| ByteBuffer compressedBuffer = compressor.compress(byteBuffer); | ||
| wrapped.onNext(compressedBuffer); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable t) { | ||
| wrapped.onError(t); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| wrapped.onComplete(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.