-
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 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
import static software.amazon.awssdk.utils.NumericUtils.saturatedCast; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import javax.net.ssl.SSLHandshakeException; | ||
|
@@ -33,25 +32,19 @@ | |
import software.amazon.awssdk.crt.http.HttpClientConnection; | ||
import software.amazon.awssdk.crt.http.HttpClientConnectionManager; | ||
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.HttpManagerMetrics; | ||
import software.amazon.awssdk.crt.http.HttpRequest; | ||
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.SdkCancellationException; | ||
import software.amazon.awssdk.http.SdkHttpFullResponse; | ||
import software.amazon.awssdk.http.async.AsyncExecuteRequest; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler; | ||
import software.amazon.awssdk.http.crt.internal.request.CrtRequestAdapter; | ||
import software.amazon.awssdk.http.crt.internal.response.CrtResponseAdapter; | ||
import software.amazon.awssdk.http.crt.internal.response.InputStreamAdaptingHttpStreamResponseHandler; | ||
import software.amazon.awssdk.metrics.MetricCollector; | ||
import software.amazon.awssdk.metrics.NoOpMetricCollector; | ||
import software.amazon.awssdk.utils.Logger; | ||
import software.amazon.awssdk.utils.async.InputStreamSubscriber; | ||
import software.amazon.awssdk.utils.async.SimplePublisher; | ||
|
||
@SdkInternalApi | ||
public final class CrtRequestExecutor { | ||
|
@@ -201,87 +194,6 @@ private void executeRequest(CrtAsyncRequestContext executionContext, | |
} | ||
} | ||
|
||
private static final class InputStreamAdaptingHttpStreamResponseHandler implements HttpStreamResponseHandler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this class to a separate class. |
||
private final SdkHttpFullResponse.Builder responseBuilder = SdkHttpFullResponse.builder(); | ||
private volatile InputStreamSubscriber inputStreamSubscriber; | ||
private volatile SimplePublisher<ByteBuffer> simplePublisher; | ||
|
||
private final CompletableFuture<SdkHttpFullResponse> requestCompletionFuture; | ||
private final HttpClientConnection crtConn; | ||
|
||
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()); | ||
} | ||
} | ||
|
||
if (responseBuilder.statusCode() == 0) { | ||
responseBuilder.statusCode(responseStatusCode); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onResponseComplete(HttpStream stream, int errorCode) { | ||
// always close the connection on a 5XX response code. | ||
if (HttpStatusFamily.of(responseBuilder.statusCode()) == HttpStatusFamily.SERVER_ERROR) { | ||
crtConn.shutdown(); | ||
} | ||
|
||
if (errorCode == 0) { | ||
requestCompletionFuture.complete(responseBuilder.build()); | ||
} else { | ||
Throwable toThrow = | ||
wrapWithIoExceptionIfRetryable(new HttpException(errorCode)); | ||
requestCompletionFuture.completeExceptionally(toThrow); | ||
} | ||
stream.close(); | ||
crtConn.close(); | ||
|
||
if (simplePublisher != null) { | ||
simplePublisher.complete(); | ||
} | ||
} | ||
|
||
@Override | ||
public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) { | ||
|
||
// Per the client conformance tests, unless there's an actual body the response stream must be null. | ||
// This is function is only called in one of the CRT's IO threads, or is otherwise sequentially ordered. | ||
// It is volatile in the declaration for thread visibility reasons, NOT concurrency control. | ||
if (inputStreamSubscriber == null && simplePublisher == null) { | ||
inputStreamSubscriber = new InputStreamSubscriber(); | ||
simplePublisher = new SimplePublisher<>(); | ||
simplePublisher.subscribe(inputStreamSubscriber); | ||
responseBuilder.content(AbortableInputStream.create(inputStreamSubscriber)); | ||
} | ||
|
||
CompletableFuture<Void> writeFuture = simplePublisher.send(ByteBuffer.wrap(bodyBytesIn)); | ||
|
||
writeFuture.whenComplete((result, failure) -> { | ||
if (failure != null) { | ||
requestCompletionFuture.completeExceptionally(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; | ||
} | ||
} | ||
|
||
private void executeRequest(CrtRequestContext executionContext, | ||
CompletableFuture<SdkHttpFullResponse> requestFuture, | ||
HttpClientConnection crtConn) { | ||
|
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
131 changes: 131 additions & 0 deletions
131
...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,131 @@ | ||
/* | ||
* 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); | ||
responseBuilder.content(AbortableInputStream.create(inputStreamSubscriber)); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
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
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.
This fixes the bug I found during testing that the priority order was not honored. I updated tests accordingly to catch this issue