-
Notifications
You must be signed in to change notification settings - Fork 999
Switch from the CRT pull-based HttpRequestBodyStream model to the #6959
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
Open
zoewangg
wants to merge
2
commits into
master
Choose a base branch
from
zoewang/crtWriteStream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
117 changes: 117 additions & 0 deletions
117
...s-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/CrtStreamHandler.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,117 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.crt.http.HttpStreamBase; | ||
|
|
||
| /** | ||
| * Manages the lifecycle of a CRT HTTP stream, providing thread-safe access to stream operations. | ||
| * Shared between the request executor (for writing body data) and the response handler (for | ||
| * incrementing the window and releasing/closing the connection). | ||
| */ | ||
| @SdkInternalApi | ||
| public final class CrtStreamHandler { | ||
|
|
||
| private final Object streamLock = new Object(); | ||
| private final CountDownLatch streamLatch = new CountDownLatch(1); | ||
| private HttpStreamBase stream; | ||
| private boolean streamClosed; | ||
|
|
||
| /** | ||
| * Sets the stream. Called once when the stream is acquired from the connection pool. | ||
| */ | ||
| public void setStream(HttpStreamBase stream) { | ||
| this.stream = stream; | ||
| streamLatch.countDown(); | ||
| } | ||
|
|
||
| /** | ||
| * Blocks until the stream has been acquired. | ||
| */ | ||
| public void waitForStream() { | ||
| try { | ||
| streamLatch.await(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException("Interrupted while waiting for stream", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write data to the stream. The caller must ensure the stream is ready (via {@link #waitForStream()}) | ||
| * before calling this method. | ||
| */ | ||
| public CompletableFuture<Void> writeData(byte[] data, boolean endStream) { | ||
| if (streamLatch.getCount() != 0) { | ||
| CompletableFuture<Void> future = new CompletableFuture<>(); | ||
| future.completeExceptionally( | ||
| new IllegalStateException("writeData called before stream is ready. Call waitForStream() first.")); | ||
| return future; | ||
| } | ||
| synchronized (streamLock) { | ||
| if (streamClosed) { | ||
| CompletableFuture<Void> future = new CompletableFuture<>(); | ||
| future.completeExceptionally( | ||
| new IOException("Stream is already closed, cannot write data.")); | ||
| return future; | ||
| } | ||
| return stream.writeData(data, endStream); | ||
| } | ||
| } | ||
|
|
||
| public void incrementWindow(int windowSize) { | ||
| if (streamLatch.getCount() != 0) { | ||
| throw new IllegalStateException("incrementWindow called before stream is ready."); | ||
| } | ||
| synchronized (streamLock) { | ||
| if (!streamClosed) { | ||
| stream.incrementWindow(windowSize); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Release the connection back to the pool so that it may be reused. This should be called when the request | ||
| * completes successfully and the response has been fully consumed. | ||
| */ | ||
| public void releaseConnection() { | ||
| synchronized (streamLock) { | ||
| if (!streamClosed && stream != null) { | ||
| streamClosed = true; | ||
| stream.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cancel and close the stream, forcing the underlying connection to shut down rather than be returned to the | ||
| * connection pool. This should be called on error paths or when the stream is aborted before the response is | ||
| * fully consumed. {@code cancel()} must be invoked before {@code close()} per the CRT contract. | ||
| */ | ||
| public void closeConnection() { | ||
| synchronized (streamLock) { | ||
| if (!streamClosed && stream != null) { | ||
| streamClosed = true; | ||
| stream.cancel(); | ||
| 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
78 changes: 0 additions & 78 deletions
78
...n/java/software/amazon/awssdk/http/crt/internal/request/CrtRequestInputStreamAdapter.java
This file was deleted.
Oops, something went wrong.
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.
when the input stream has no data available, do we just keep looping until we find data? Seems like we will end up with hot looping and wasting CPU loops for this case.
And, a bit optimization, when there is no data, it will be better to skip invoking the writeData, so that less work to be done
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.
Per InputStream contract, a valid implementation shouldn't return 0 from read() method; it just blocks until data arrives or returns -1 at EOF.