-
Notifications
You must be signed in to change notification settings - Fork 1k
Switch CRT HTTP clients (sync and async) from pull-based body to push-based writeData #6993
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
051f869
Switch from the CRT pull-based HttpRequestBodyStream model to the
zoewangg 2d49203
Extract CrtStreamHandler to manage stream lifecycle
zoewangg d25560d
Update CRT version
zoewangg f722e5d
Update CRT version
zoewangg 3a694c5
Merge branch 'master' into zoewang/crtWriteStream
zoewangg 0b4a1bf
Use async stream API in async code path
zoewangg 994989e
refactor code and address feedback
zoewangg 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": "bugfix", | ||
| "category": "AWS CRT HTTP Client", | ||
| "contributor": "", | ||
| "description": "Fixed a potential deadlock in `AwsCrtHttpClient` that could occur when the request body `InputStream` blocked waiting for data on the CRT event loop thread. This could happen when a blocking stream (e.g., a `BufferedInputStream` wrapping a `ResponseInputStream`) was used as a request body and the read depended on the same event loop thread to deliver data. Request body writing now happens on the caller thread." | ||
| } |
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 |
|---|---|---|
|
|
@@ -18,13 +18,16 @@ | |
| import static software.amazon.awssdk.http.HttpMetric.HTTP_CLIENT_NAME; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.time.Duration; | ||
| import java.util.Arrays; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.function.Consumer; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.crt.http.HttpException; | ||
| import software.amazon.awssdk.crt.http.HttpStreamManager; | ||
| import software.amazon.awssdk.http.ContentStreamProvider; | ||
| import software.amazon.awssdk.http.ExecutableHttpRequest; | ||
| import software.amazon.awssdk.http.HttpExecuteRequest; | ||
| import software.amazon.awssdk.http.HttpExecuteResponse; | ||
|
|
@@ -35,6 +38,8 @@ | |
| import software.amazon.awssdk.http.crt.internal.AwsCrtClientBuilderBase; | ||
| import software.amazon.awssdk.http.crt.internal.CrtRequestContext; | ||
| import software.amazon.awssdk.http.crt.internal.CrtRequestExecutor; | ||
| import software.amazon.awssdk.http.crt.internal.CrtStreamHandler; | ||
| import software.amazon.awssdk.http.crt.internal.CrtUtils; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
| import software.amazon.awssdk.utils.CompletableFutureUtils; | ||
|
|
||
|
|
@@ -107,6 +112,8 @@ public ExecutableHttpRequest prepareRequest(HttpExecuteRequest request) { | |
| } | ||
|
|
||
| private static final class CrtHttpRequest implements ExecutableHttpRequest { | ||
| private static final int WRITE_BUFFER_SIZE = 16 * 1024; | ||
|
|
||
| private final CrtRequestContext context; | ||
| private volatile CompletableFuture<SdkHttpFullResponse> responseFuture; | ||
|
|
||
|
|
@@ -119,7 +126,10 @@ public HttpExecuteResponse call() throws IOException { | |
| HttpExecuteResponse.Builder builder = HttpExecuteResponse.builder(); | ||
|
|
||
| try { | ||
| responseFuture = new CrtRequestExecutor().execute(context); | ||
| CrtRequestExecutor.Result result = new CrtRequestExecutor().execute(context); | ||
| responseFuture = result.responseFuture(); | ||
| writeRequestBody(result.streamHandler()); | ||
|
|
||
| SdkHttpFullResponse response = CompletableFutureUtils.joinInterruptibly(responseFuture); | ||
| builder.response(response); | ||
| builder.responseBody(response.content().orElse(null)); | ||
|
|
@@ -140,6 +150,10 @@ public HttpExecuteResponse call() throws IOException { | |
| } | ||
|
|
||
| if (cause instanceof HttpException) { | ||
| Throwable wrapped = CrtUtils.wrapCrtException(cause); | ||
| if (wrapped instanceof IOException) { | ||
| throw (IOException) wrapped; | ||
| } | ||
| throw (HttpException) cause; | ||
| } | ||
|
|
||
|
|
@@ -151,6 +165,24 @@ public HttpExecuteResponse call() throws IOException { | |
| } | ||
| } | ||
|
|
||
| private void writeRequestBody(CrtStreamHandler streamHandler) throws IOException { | ||
| ContentStreamProvider provider = context.sdkRequest().contentStreamProvider().orElse(null); | ||
| if (provider == null) { | ||
| return; | ||
| } | ||
|
|
||
| streamHandler.waitForStream(); | ||
| try (InputStream inputStream = provider.newStream()) { | ||
| byte[] buf = new byte[WRITE_BUFFER_SIZE]; | ||
| int read; | ||
| while ((read = inputStream.read(buf, 0, buf.length)) >= 0) { | ||
| byte[] chunk = read == buf.length ? buf : Arrays.copyOf(buf, read); | ||
| CompletableFutureUtils.joinInterruptibly(streamHandler.writeData(chunk, false)); | ||
|
Contributor
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. Should we run a throughput/performance benchmark comparing before and after these changes to see if there's any impact? |
||
| } | ||
| CompletableFutureUtils.joinInterruptibly(streamHandler.writeData(null, true)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void abort() { | ||
| if (responseFuture != 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
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.
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.
Query : I see caller of writeRequestBody catches CompletionException ? How is streamHandler.closeConnection() called in the case when IOException is thrown by inputStream.read() also writeRequestBody function signature also says it throws IOException)
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.
Nice catch! Will fix