-
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
Open
zoewangg
wants to merge
7
commits into
master
Choose a base branch
from
zoewang/crtWriteStreamAsync
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
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 both `AwsCrtHttpClient` and `AwsCrtAsyncHttpClient` that could occur when the request body source delivered data on the CRT event loop thread. For sync, this happened when a blocking `InputStream` (e.g., a `BufferedInputStream` wrapping a `ResponseInputStream`) was used as a request body. For async, this happened when the user-supplied `Publisher<ByteBuffer>` scheduled `onNext` back onto the same event loop. Request body data is now pushed via the CRT push-based `writeData` API: sync writes from the caller thread, async writes from a Reactive Streams subscriber outside the event loop." | ||
| } |
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; | ||
|
|
||
|
|
@@ -64,7 +69,7 @@ private AwsCrtHttpClient(DefaultBuilder builder, AttributeMap config) { | |
| } | ||
| } | ||
|
|
||
| public static AwsCrtHttpClient.Builder builder() { | ||
| public static Builder builder() { | ||
| return new DefaultBuilder(); | ||
| } | ||
|
|
||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -117,37 +124,70 @@ private CrtHttpRequest(CrtRequestContext context) { | |
| @Override | ||
| public HttpExecuteResponse call() throws IOException { | ||
| HttpExecuteResponse.Builder builder = HttpExecuteResponse.builder(); | ||
| CrtRequestExecutor.Result result = new CrtRequestExecutor().execute(context); | ||
| responseFuture = result.responseFuture(); | ||
|
|
||
| try { | ||
| responseFuture = new CrtRequestExecutor().execute(context); | ||
| writeRequestBody(result.streamHandler()); | ||
|
|
||
| SdkHttpFullResponse response = CompletableFutureUtils.joinInterruptibly(responseFuture); | ||
| builder.response(response); | ||
| builder.responseBody(response.content().orElse(null)); | ||
| return builder.build(); | ||
| } catch (CompletionException e) { | ||
| Throwable cause = e.getCause(); | ||
|
|
||
| // Complete the future exceptionally to trigger connection cleanup in the response handler. | ||
| // Handles thread-interrupt case where joinInterruptibly throws due to | ||
| // InterruptedException. Without this, the | ||
| // Ensures that closeConnection() is invoked to prevent leaking the connection from the pool. | ||
| if (responseFuture != null) { | ||
| responseFuture.completeExceptionally(cause != null ? cause : e); | ||
| } | ||
| } catch (Throwable t) { | ||
| // CompletionException is the wrapper from joinInterruptibly; direct throws | ||
| // (e.g., IOException from inputStream.read in writeRequestBody) arrive unwrapped. | ||
| Throwable cause = (t instanceof CompletionException && t.getCause() != null) ? t.getCause() : t; | ||
|
|
||
| // Tear down the stream so the connection is not leaked back to the pool. | ||
| // closeConnection() is idempotent and a no-op if the stream is not yet acquired | ||
| // or is already closed. | ||
| result.streamHandler().closeConnection(); | ||
| responseFuture.completeExceptionally(cause); | ||
|
|
||
| throw mapToIoExceptionOrRethrow(cause); | ||
| } | ||
| } | ||
|
|
||
| if (cause instanceof IOException) { | ||
| throw (IOException) cause; | ||
| private static IOException mapToIoExceptionOrRethrow(Throwable cause) { | ||
| if (cause instanceof IOException) { | ||
| return (IOException) cause; | ||
| } | ||
| if (cause instanceof HttpException) { | ||
| Throwable wrapped = CrtUtils.wrapCrtException(cause); | ||
| if (wrapped instanceof IOException) { | ||
| return (IOException) wrapped; | ||
| } | ||
| throw (HttpException) cause; | ||
| } | ||
| if (cause instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| return new IOException("Request was cancelled", cause); | ||
| } | ||
| if (cause instanceof RuntimeException) { | ||
| throw (RuntimeException) cause; | ||
| } | ||
| if (cause instanceof Error) { | ||
| throw (Error) cause; | ||
| } | ||
| return new IOException(cause); | ||
| } | ||
|
|
||
| if (cause instanceof HttpException) { | ||
| throw (HttpException) cause; | ||
| } | ||
| private void writeRequestBody(CrtStreamHandler streamHandler) throws IOException { | ||
| ContentStreamProvider provider = context.sdkRequest().contentStreamProvider().orElse(null); | ||
| if (provider == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (cause instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new IOException("Request was cancelled", cause); | ||
| 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? |
||
| } | ||
| throw new RuntimeException(e.getCause()); | ||
| CompletableFutureUtils.joinInterruptibly(streamHandler.writeData(null, true)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -162,14 +202,14 @@ public void abort() { | |
| /** | ||
| * Builder that allows configuration of the AWS CRT HTTP implementation. | ||
| */ | ||
| public interface Builder extends SdkHttpClient.Builder<AwsCrtHttpClient.Builder> { | ||
| public interface Builder extends SdkHttpClient.Builder<Builder> { | ||
|
|
||
| /** | ||
| * The Maximum number of allowed concurrent requests. For HTTP/1.1 this is the same as max connections. | ||
| * @param maxConcurrency maximum concurrency per endpoint | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder maxConcurrency(Integer maxConcurrency); | ||
| Builder maxConcurrency(Integer maxConcurrency); | ||
|
|
||
| /** | ||
| * Configures the number of unread bytes that can be buffered in the | ||
|
|
@@ -179,22 +219,22 @@ public interface Builder extends SdkHttpClient.Builder<AwsCrtHttpClient.Builder> | |
| * @param readBufferSize The number of bytes that can be buffered. | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder readBufferSizeInBytes(Long readBufferSize); | ||
| Builder readBufferSizeInBytes(Long readBufferSize); | ||
|
|
||
| /** | ||
| * Sets the http proxy configuration to use for this client. | ||
| * @param proxyConfiguration The http proxy configuration to use | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder proxyConfiguration(ProxyConfiguration proxyConfiguration); | ||
| Builder proxyConfiguration(ProxyConfiguration proxyConfiguration); | ||
|
|
||
| /** | ||
| * Sets the http proxy configuration to use for this client. | ||
| * | ||
| * @param proxyConfigurationBuilderConsumer The consumer of the proxy configuration builder object. | ||
| * @return the builder for method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder proxyConfiguration(Consumer<ProxyConfiguration.Builder> proxyConfigurationBuilderConsumer); | ||
| Builder proxyConfiguration(Consumer<ProxyConfiguration.Builder> proxyConfigurationBuilderConsumer); | ||
|
|
||
| /** | ||
| * Configure the health checks for all connections established by this client. | ||
|
|
@@ -214,7 +254,7 @@ public interface Builder extends SdkHttpClient.Builder<AwsCrtHttpClient.Builder> | |
| * @param healthChecksConfiguration The health checks config to use | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder connectionHealthConfiguration(ConnectionHealthConfiguration healthChecksConfiguration); | ||
| Builder connectionHealthConfiguration(ConnectionHealthConfiguration healthChecksConfiguration); | ||
|
|
||
| /** | ||
| * A convenience method that creates an instance of the {@link ConnectionHealthConfiguration} builder, avoiding the | ||
|
|
@@ -224,29 +264,29 @@ public interface Builder extends SdkHttpClient.Builder<AwsCrtHttpClient.Builder> | |
| * @return The builder of the method chaining. | ||
| * @see #connectionHealthConfiguration(ConnectionHealthConfiguration) | ||
| */ | ||
| AwsCrtHttpClient.Builder connectionHealthConfiguration(Consumer<ConnectionHealthConfiguration.Builder> | ||
| Builder connectionHealthConfiguration(Consumer<ConnectionHealthConfiguration.Builder> | ||
| healthChecksConfigurationBuilder); | ||
|
|
||
| /** | ||
| * Configure the maximum amount of time that a connection should be allowed to remain open while idle. | ||
| * @param connectionMaxIdleTime the maximum amount of connection idle time | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder connectionMaxIdleTime(Duration connectionMaxIdleTime); | ||
| Builder connectionMaxIdleTime(Duration connectionMaxIdleTime); | ||
|
|
||
| /** | ||
| * The amount of time to wait when initially establishing a connection before giving up and timing out. | ||
| * @param connectionTimeout timeout | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder connectionTimeout(Duration connectionTimeout); | ||
| Builder connectionTimeout(Duration connectionTimeout); | ||
|
|
||
| /** | ||
| * The amount of time to wait when acquiring a connection from the pool before giving up and timing out. | ||
| * @param connectionAcquisitionTimeout the timeout duration | ||
| * @return this builder for method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder connectionAcquisitionTimeout(Duration connectionAcquisitionTimeout); | ||
| Builder connectionAcquisitionTimeout(Duration connectionAcquisitionTimeout); | ||
|
|
||
| /** | ||
| * Configure whether to enable {@code tcpKeepAlive} and relevant configuration for all connections established by this | ||
|
|
@@ -260,7 +300,7 @@ AwsCrtHttpClient.Builder connectionHealthConfiguration(Consumer<ConnectionHealth | |
| * @param tcpKeepAliveConfiguration The TCP keep-alive configuration to use | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder tcpKeepAliveConfiguration(TcpKeepAliveConfiguration tcpKeepAliveConfiguration); | ||
| Builder tcpKeepAliveConfiguration(TcpKeepAliveConfiguration tcpKeepAliveConfiguration); | ||
|
|
||
| /** | ||
| * Configure whether to enable {@code tcpKeepAlive} and relevant configuration for all connections established by this | ||
|
|
@@ -274,7 +314,7 @@ AwsCrtHttpClient.Builder connectionHealthConfiguration(Consumer<ConnectionHealth | |
| * @return The builder of the method chaining. | ||
| * @see #tcpKeepAliveConfiguration(TcpKeepAliveConfiguration) | ||
| */ | ||
| AwsCrtHttpClient.Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfiguration.Builder> | ||
| Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfiguration.Builder> | ||
| tcpKeepAliveConfigurationBuilder); | ||
|
|
||
| /** | ||
|
|
@@ -293,15 +333,15 @@ AwsCrtHttpClient.Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfigur | |
| * @param postQuantumTlsEnabled whether to prefer Post Quantum TLS | ||
| * @return The builder of the method chaining. | ||
| */ | ||
| AwsCrtHttpClient.Builder postQuantumTlsEnabled(Boolean postQuantumTlsEnabled); | ||
| Builder postQuantumTlsEnabled(Boolean postQuantumTlsEnabled); | ||
| } | ||
|
|
||
| /** | ||
| * Factory that allows more advanced configuration of the AWS CRT HTTP implementation. | ||
| * Use {@link #builder()} to configure and construct an immutable instance of the factory. | ||
| */ | ||
| private static final class DefaultBuilder | ||
| extends AwsCrtClientBuilderBase<AwsCrtHttpClient.Builder> implements AwsCrtHttpClient.Builder { | ||
| extends AwsCrtClientBuilderBase<Builder> implements Builder { | ||
|
|
||
|
|
||
| @Override | ||
|
|
||
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