-
Notifications
You must be signed in to change notification settings - Fork 912
Various performance optimization for CRT sync HTTP client #4776
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
zoewangg
merged 2 commits into
feature/master/synccrtclient
from
zoewang/master/crt-performanceOptimization
Dec 14, 2023
Merged
Changes from all commits
Commits
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
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
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
136 changes: 136 additions & 0 deletions
136
...mazon/awssdk/http/crt/internal/response/InputStreamAdaptingHttpStreamResponseHandler.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,136 @@ | ||
/* | ||
* 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.http.crt.internal.response; | ||
|
||
import static software.amazon.awssdk.http.crt.internal.CrtUtils.wrapWithIoExceptionIfRetryable; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CompletableFuture; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.crt.CRT; | ||
import software.amazon.awssdk.crt.http.HttpClientConnection; | ||
import software.amazon.awssdk.crt.http.HttpException; | ||
import software.amazon.awssdk.crt.http.HttpHeader; | ||
import software.amazon.awssdk.crt.http.HttpHeaderBlock; | ||
import software.amazon.awssdk.crt.http.HttpStream; | ||
import software.amazon.awssdk.crt.http.HttpStreamResponseHandler; | ||
import software.amazon.awssdk.http.AbortableInputStream; | ||
import software.amazon.awssdk.http.HttpStatusFamily; | ||
import software.amazon.awssdk.http.SdkHttpFullResponse; | ||
import software.amazon.awssdk.http.crt.AwsCrtHttpClient; | ||
import software.amazon.awssdk.utils.async.InputStreamSubscriber; | ||
import software.amazon.awssdk.utils.async.SimplePublisher; | ||
|
||
/** | ||
* Response handler adaptor for {@link AwsCrtHttpClient}. | ||
*/ | ||
@SdkInternalApi | ||
public final class InputStreamAdaptingHttpStreamResponseHandler implements HttpStreamResponseHandler { | ||
private final SdkHttpFullResponse.Builder responseBuilder = SdkHttpFullResponse.builder(); | ||
private volatile InputStreamSubscriber inputStreamSubscriber; | ||
private final SimplePublisher<ByteBuffer> simplePublisher = new SimplePublisher<>(); | ||
|
||
private final CompletableFuture<SdkHttpFullResponse> requestCompletionFuture; | ||
private final HttpClientConnection crtConn; | ||
|
||
public InputStreamAdaptingHttpStreamResponseHandler(HttpClientConnection crtConn, | ||
CompletableFuture<SdkHttpFullResponse> requestCompletionFuture) { | ||
this.crtConn = crtConn; | ||
this.requestCompletionFuture = requestCompletionFuture; | ||
} | ||
|
||
@Override | ||
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, | ||
HttpHeader[] nextHeaders) { | ||
if (blockType == HttpHeaderBlock.MAIN.getValue()) { | ||
for (HttpHeader h : nextHeaders) { | ||
responseBuilder.appendHeader(h.getName(), h.getValue()); | ||
} | ||
} | ||
|
||
responseBuilder.statusCode(responseStatusCode); | ||
} | ||
|
||
@Override | ||
public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) { | ||
if (inputStreamSubscriber == null) { | ||
inputStreamSubscriber = new InputStreamSubscriber(); | ||
simplePublisher.subscribe(inputStreamSubscriber); | ||
// For response with a payload, we need to complete the future here to allow downstream to retrieve the data from | ||
// the stream directly. | ||
responseBuilder.content(AbortableInputStream.create(inputStreamSubscriber)); | ||
requestCompletionFuture.complete(responseBuilder.build()); | ||
} | ||
|
||
CompletableFuture<Void> writeFuture = simplePublisher.send(ByteBuffer.wrap(bodyBytesIn)); | ||
|
||
if (writeFuture.isDone() && !writeFuture.isCompletedExceptionally()) { | ||
// Optimization: If write succeeded immediately, return non-zero to avoid the extra call back into the CRT. | ||
return bodyBytesIn.length; | ||
} | ||
|
||
writeFuture.whenComplete((result, failure) -> { | ||
if (failure != null) { | ||
failFutureAndCloseConnection(stream, failure); | ||
return; | ||
} | ||
|
||
// increment the window upon buffer consumption. | ||
stream.incrementWindow(bodyBytesIn.length); | ||
}); | ||
|
||
// the bodyBytesIn have not cleared the queues yet, so do let backpressure do its thing. | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void onResponseComplete(HttpStream stream, int errorCode) { | ||
if (errorCode == CRT.AWS_CRT_SUCCESS) { | ||
onSuccessfulResponseComplete(stream); | ||
} else { | ||
onFailedResponseComplete(stream, errorCode); | ||
} | ||
} | ||
|
||
private void failFutureAndCloseConnection(HttpStream stream, Throwable failure) { | ||
requestCompletionFuture.completeExceptionally(failure); | ||
crtConn.shutdown(); | ||
crtConn.close(); | ||
stream.close(); | ||
} | ||
|
||
private void onFailedResponseComplete(HttpStream stream, int errorCode) { | ||
Throwable toThrow = | ||
wrapWithIoExceptionIfRetryable(new HttpException(errorCode)); | ||
|
||
simplePublisher.error(toThrow); | ||
failFutureAndCloseConnection(stream, toThrow); | ||
} | ||
|
||
private void onSuccessfulResponseComplete(HttpStream stream) { | ||
// always close the connection on a 5XX response code. | ||
if (HttpStatusFamily.of(responseBuilder.statusCode()) == HttpStatusFamily.SERVER_ERROR) { | ||
crtConn.shutdown(); | ||
} | ||
|
||
// For response without a payload, for example, S3 PutObjectResponse, we need to complete the future | ||
// in onResponseComplete callback since onResponseBody will never be invoked. | ||
requestCompletionFuture.complete(responseBuilder.build()); | ||
simplePublisher.complete(); | ||
crtConn.close(); | ||
stream.close(); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this class to a separate class.