-
Notifications
You must be signed in to change notification settings - Fork 932
Use CRT response file in low level CRT S3AsyncClient for getObject #6289
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
alextwoods
merged 5 commits into
master
from
feature/master/s3-crt-client-response-file
Jul 28, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e48a0f6
Use CRT response file in getObject
alextwoods 50e38ca
Undo cancel changes not required for low level client only
alextwoods 2be8d80
Add changelog + fix warning usage
alextwoods b9452fb
PR cleanups
alextwoods 1a7d7fc
Merge branch 'master' into feature/master/s3-crt-client-response-file
alextwoods 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS S3", | ||
"contributor": "", | ||
"description": "Add support for using CRT's response file in the CRT based S3AsyncClient - CRT will directly write to the file when calling getObject with a Path." | ||
} |
110 changes: 110 additions & 0 deletions
110
...a/software/amazon/awssdk/services/s3/internal/crt/CrtResponseFileResponseTransformer.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,110 @@ | ||
/* | ||
* 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.services.s3.internal.crt; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Consumer; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
import software.amazon.awssdk.core.async.SdkPublisher; | ||
import software.amazon.awssdk.utils.Logger; | ||
|
||
/** | ||
* When the CRT Response File option is used in a request, the body is streamed directly to the file. | ||
* The S3CrtResponseHandlerAdapter in this case will never receive a response body but will call onStream | ||
* when the request is complete with a publisher that will complete immediately. | ||
* This transformer is effectively a no-op transformer that waits for the stream to complete and then | ||
* completes the future with the response. | ||
* | ||
* @param <ResponseT> Pojo response type. | ||
*/ | ||
@SdkInternalApi | ||
public final class CrtResponseFileResponseTransformer<ResponseT> implements AsyncResponseTransformer<ResponseT, | ||
ResponseT> { | ||
|
||
private static final Logger log = Logger.loggerFor(CrtResponseFileResponseTransformer.class); | ||
|
||
private volatile CompletableFuture<Void> cf; | ||
private volatile ResponseT response; | ||
|
||
@Override | ||
public CompletableFuture<ResponseT> prepare() { | ||
cf = new CompletableFuture<>(); | ||
return cf.thenApply(ignored -> response); | ||
} | ||
|
||
@Override | ||
public void onResponse(ResponseT response) { | ||
this.response = response; | ||
} | ||
|
||
@Override | ||
public void onStream(SdkPublisher<ByteBuffer> publisher) { | ||
publisher.subscribe(new OnCompleteSubscriber(cf, this::exceptionOccurred)); | ||
} | ||
|
||
@Override | ||
public void exceptionOccurred(Throwable throwable) { | ||
if (cf != null) { | ||
cf.completeExceptionally(throwable); | ||
} else { | ||
log.warn(() -> "An exception occurred before the call to prepare() was able to instantiate the CompletableFuture. " | ||
+ "The future cannot be completed exceptionally because it is null"); | ||
} | ||
} | ||
|
||
private static final class OnCompleteSubscriber implements Subscriber<ByteBuffer> { | ||
|
||
private final CompletableFuture<Void> future; | ||
private final Consumer<Throwable> onErrorMethod; | ||
private Subscription subscription; | ||
|
||
private OnCompleteSubscriber(CompletableFuture<Void> future, Consumer<Throwable> onErrorMethod) { | ||
this.future = future; | ||
this.onErrorMethod = onErrorMethod; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
if (this.subscription != null) { | ||
s.cancel(); | ||
return; | ||
} | ||
this.subscription = s; | ||
// do not request data from the subscription since body is written directly to file | ||
alextwoods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void onNext(ByteBuffer byteBuffer) { | ||
alextwoods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The response body is streamed directly to the file - this method should never be called. | ||
// ensure the future is completed exceptionally if this occurs | ||
onErrorMethod.accept(new IllegalStateException("OnCompleteSubscriber received unexpected call to onNext.")); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
onErrorMethod.accept(throwable); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
future.complete(null); | ||
} | ||
} | ||
} |
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
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
91 changes: 91 additions & 0 deletions
91
...ftware/amazon/awssdk/services/s3/internal/crt/CrtResponseFileResponseTransformerTest.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,91 @@ | ||
/* | ||
* 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.services.s3.internal.crt; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.reactivestreams.Subscriber; | ||
import software.amazon.awssdk.core.SdkResponse; | ||
import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
import software.amazon.awssdk.core.async.SdkPublisher; | ||
|
||
public class CrtResponseFileResponseTransformerTest { | ||
private CrtResponseFileResponseTransformer<SdkResponse> transformer; | ||
private SdkResponse response; | ||
private MockCrtPublisher publisher; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
transformer = new CrtResponseFileResponseTransformer<>(); | ||
response = Mockito.mock(SdkResponse.class); | ||
publisher = new MockCrtPublisher(); | ||
} | ||
|
||
@Test | ||
void successfulResponseAndStream_returnsResponsePublisher() throws Exception { | ||
CompletableFuture<SdkResponse> responseFuture = transformer.prepare(); | ||
transformer.onResponse(response); | ||
assertThat(responseFuture.isDone()).isFalse(); | ||
transformer.onStream(publisher); | ||
publisher.complete(); | ||
assertThat(responseFuture.isDone()).isTrue(); | ||
SdkResponse returnedResponse = responseFuture.get(); | ||
assertThat(returnedResponse).isEqualTo(response); | ||
} | ||
|
||
@Test | ||
void failedResponse_completesExceptionally() { | ||
CompletableFuture<SdkResponse> responseFuture = transformer.prepare(); | ||
assertThat(responseFuture.isDone()).isFalse(); | ||
transformer.exceptionOccurred(new RuntimeException("Intentional exception for testing purposes - before response.")); | ||
assertThat(responseFuture.isDone()).isTrue(); | ||
assertThatThrownBy(responseFuture::get) | ||
.isInstanceOf(ExecutionException.class) | ||
.hasCauseInstanceOf(RuntimeException.class); | ||
} | ||
|
||
@Test | ||
void failedStream_completesExceptionally() { | ||
CompletableFuture<SdkResponse> responseFuture = transformer.prepare(); | ||
transformer.onResponse(response); | ||
assertThat(responseFuture.isDone()).isFalse(); | ||
transformer.exceptionOccurred(new RuntimeException("Intentional exception for testing purposes - after response.")); | ||
assertThat(responseFuture.isDone()).isTrue(); | ||
assertThatThrownBy(responseFuture::get) | ||
.isInstanceOf(ExecutionException.class) | ||
.hasCauseInstanceOf(RuntimeException.class); | ||
} | ||
|
||
private static class MockCrtPublisher implements SdkPublisher<ByteBuffer> { | ||
private Subscriber<? super ByteBuffer> subscriber; | ||
@Override | ||
public void subscribe(Subscriber<? super ByteBuffer> s) { | ||
subscriber = s; | ||
} | ||
|
||
public void complete() { | ||
subscriber.onComplete(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
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.