diff --git a/.changes/next-release/feature-AWSCRTHTTPClient-8e385ba.json b/.changes/next-release/feature-AWSCRTHTTPClient-8e385ba.json new file mode 100644 index 000000000000..c6697ab1320d --- /dev/null +++ b/.changes/next-release/feature-AWSCRTHTTPClient-8e385ba.json @@ -0,0 +1,6 @@ +{ + "type": "feature", + "category": "AWS CRT HTTP Client", + "contributor": "", + "description": "Add `maxPendingConnectionAcquires` to `AwsCrtHttpClient.Builder` and `AwsCrtAsyncHttpClient.Builder`. Unbounded by default." +} diff --git a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClient.java b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClient.java index c88c3b09c16e..81dcf0117f86 100644 --- a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClient.java +++ b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClient.java @@ -105,6 +105,22 @@ public interface Builder extends SdkAsyncHttpClient.Builder + * By default (when this is not set), the number of waiting requests is not limited. This differs from + * {@code NettyNioAsyncHttpClient}, whose default is 10,000. To keep the Netty behavior when migrating, set this to + * 10,000 explicitly. + * + * @param maxPendingConnectionAcquires the maximum number of waiting requests per endpoint; must be positive, or + * {@code null} to remove the limit (the default) + * @return The builder of the method chaining. + */ + AwsCrtAsyncHttpClient.Builder maxPendingConnectionAcquires(Integer maxPendingConnectionAcquires); + /** * Configures the number of bytes that can be buffered in the client for sending and receiving data. *

diff --git a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClient.java b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClient.java index c0f021cf51be..fe34e568faf9 100644 --- a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClient.java +++ b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClient.java @@ -35,6 +35,7 @@ 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.CrtUtils; import software.amazon.awssdk.utils.AttributeMap; import software.amazon.awssdk.utils.CompletableFutureUtils; @@ -137,6 +138,10 @@ public HttpExecuteResponse call() throws IOException { throw (HttpException) cause; } + if (CrtUtils.isMaxPendingAcquisitionsExceeded(cause)) { + throw (IllegalStateException) cause; + } + if (cause instanceof InterruptedException) { Thread.currentThread().interrupt(); throw new IOException("Request was cancelled", cause); @@ -165,6 +170,21 @@ public interface Builder extends SdkHttpClient.Builder */ AwsCrtHttpClient.Builder maxConcurrency(Integer maxConcurrency); + /** + * Configure the maximum number of requests to an endpoint that can wait for a connection once + * {@link #maxConcurrency(Integer)} connections are in use. When the limit is reached, a new request fails + * immediately instead of waiting. + * + *

+ * By default (when this is not set), the number of waiting requests is not limited; how long each request may wait is + * controlled by {@link #connectionAcquisitionTimeout(Duration)}. + * + * @param maxPendingConnectionAcquires the maximum number of waiting requests per endpoint; must be positive, or + * {@code null} to remove the limit (the default) + * @return The builder of the method chaining. + */ + AwsCrtHttpClient.Builder maxPendingConnectionAcquires(Integer maxPendingConnectionAcquires); + /** * Configures the number of unread bytes that can be buffered in the * client before we stop reading from the underlying TCP socket and wait for the Subscriber diff --git a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientBase.java b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientBase.java index ff4de10c171c..8d902a5caf84 100644 --- a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientBase.java +++ b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientBase.java @@ -89,6 +89,7 @@ abstract class AwsCrtHttpClientBase implements SdkAutoCloseable { private final HttpMonitoringOptions monitoringOptions; private final long maxConnectionIdleInMilliseconds; private final int maxStreamsPerEndpoint; + private final Integer maxPendingConnectionAcquires; private final long connectionAcquisitionTimeout; private final TlsContextOptions tlsContextOptions; private final Duration tlsNegotiationTimeout; @@ -142,6 +143,7 @@ abstract class AwsCrtHttpClientBase implements SdkAutoCloseable { this.readBufferSize = builder.getReadBufferSizeInBytes() == null ? DEFAULT_STREAM_WINDOW_SIZE : builder.getReadBufferSizeInBytes(); this.maxStreamsPerEndpoint = config.get(SdkHttpConfigurationOption.MAX_CONNECTIONS); + this.maxPendingConnectionAcquires = builder.getMaxPendingConnectionAcquires(); this.monitoringOptions = resolveMonitoringOptions( builder.getConnectionHealthConfiguration(), config.get(SdkHttpConfigurationOption.SDK_INTERNAL_FALLBACK_READ_WRITE_TIMEOUT)); @@ -221,6 +223,10 @@ private HttpStreamManager createConnectionPool(URI uri) { .withMaxConnectionIdleInMilliseconds(maxConnectionIdleInMilliseconds) .withConnectionAcquisitionTimeoutInMilliseconds(connectionAcquisitionTimeout); + if (maxPendingConnectionAcquires != null) { + h1Options.withMaxPendingConnectionAcquisitions(maxPendingConnectionAcquires); + } + HttpStreamManagerOptions options = new HttpStreamManagerOptions() .withHTTP1ConnectionManagerOptions(h1Options); diff --git a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtClientBuilderBase.java b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtClientBuilderBase.java index 30e2bdd446c1..cc9a60e0e296 100644 --- a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtClientBuilderBase.java +++ b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtClientBuilderBase.java @@ -37,6 +37,7 @@ public class AwsCrtClientBuilderBase { private TlsVersion minTlsVersion; private Integer numEventLoopThreads; private Boolean useTrustStoreSystemProperties; + private Integer maxPendingConnectionAcquires; protected AwsCrtClientBuilderBase() { } @@ -55,6 +56,16 @@ public BuilderT maxConcurrency(Integer maxConcurrency) { return thisBuilder(); } + public BuilderT maxPendingConnectionAcquires(Integer maxPendingConnectionAcquires) { + Validate.isPositiveOrNull(maxPendingConnectionAcquires, "maxPendingConnectionAcquires"); + this.maxPendingConnectionAcquires = maxPendingConnectionAcquires; + return thisBuilder(); + } + + public Integer getMaxPendingConnectionAcquires() { + return this.maxPendingConnectionAcquires; + } + public BuilderT readBufferSizeInBytes(Long readBufferSize) { Validate.isPositiveOrNull(readBufferSize, "readBufferSize"); this.readBufferSize = readBufferSize; diff --git a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/CrtUtils.java b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/CrtUtils.java index f8bb0a00a288..5859291c0fe7 100644 --- a/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/CrtUtils.java +++ b/http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/CrtUtils.java @@ -45,6 +45,36 @@ public final class CrtUtils { * Corresponds to CRT error: AWS_IO_SOCKET_TIMEOUT */ public static final int CRT_SOCKET_TIMEOUT = 1048; + /** + * Corresponds to CRT error: AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED + */ + public static final int CRT_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED = 2094; + /** + * Corresponds to CRT error: AWS_ERROR_HTTP_STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED + */ + public static final int CRT_STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED = 2096; + + /** + * Message for a request rejected because the endpoint's pending acquisitions reached + * {@code maxPendingConnectionAcquires}. The first sentence matches + * {@code NettyUtils#getMessageForTooManyAcquireOperationsError} in netty-nio-client; the rest is adapted to the CRT + * builder options. + */ + private static final String MAX_PENDING_ACQUISITIONS_EXCEEDED_MESSAGE = + "Maximum pending connection acquisitions exceeded. The request rate is too high for the client to keep up.\n" + + + "Consider taking any of the following actions to mitigate the issue: increase maxConcurrency, " + + "increase maxPendingConnectionAcquires, or slow the request rate.\n" + + + "Increasing maxConcurrency can increase client throughput (unless the network interface is already fully " + + "utilized), but can eventually start to hit operating system limitations on the number of file descriptors " + + "used by the process. If you are already fully utilizing your network interface or cannot further increase " + + "maxConcurrency, increasing maxPendingConnectionAcquires allows extra requests to be buffered by the client, " + + "but can cause additional request latency and higher memory usage.\n" + + + "If the above mechanisms are not able to fix the issue, try smoothing out your requests so that large " + + "traffic bursts cannot overload the client, being more efficient with the number of times you need to call " + + "AWS, or by increasing the number of hosts sending requests."; // HTTP error codes that the native CRT classifier (CRT.awsIsTransientError) does NOT mark as transient // but that the SDK considers recoverable. See enum aws_http_errors in aws-c-http/include/aws/http/http.h @@ -65,8 +95,6 @@ public final class CrtUtils { codes.add(2092); // AWS_ERROR_HTTP_CONNECTION_MANAGER_ACQUISITION_TIMEOUT codes.add(2093); - // AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED - codes.add(2094); ADDITIONAL_RETRYABLE_ERROR_CODES = unmodifiableSet(codes); } @@ -105,9 +133,20 @@ public static Throwable wrapCrtException(Throwable throwable) { return connectException; } + if (httpErrorCode == CRT_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED) { + return maxPendingAcquisitionsExceeded(httpException); + } + return wrapWithIoExceptionIfRetryable((HttpException) throwable); } + // The HTTP/2 stream manager reports its over-limit rejection as a CrtRuntimeException. Handle it like the HTTP/1.1 + // rejection, instead of wrapping it in the retryable IOException below. + if (throwable instanceof CrtRuntimeException + && ((CrtRuntimeException) throwable).errorCode == CRT_STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED) { + return maxPendingAcquisitionsExceeded(throwable); + } + if (throwable instanceof IllegalStateException || throwable instanceof CrtRuntimeException) { // CRT throws IllegalStateException if the connection is closed return new IOException("An exception occurred when making the request", throwable); @@ -116,6 +155,33 @@ public static Throwable wrapCrtException(Throwable throwable) { return throwable; } + /** + * Returns true if the given exception is the rejection {@link #wrapCrtException} creates when a request is over the + * pending acquisition limit. + */ + public static boolean isMaxPendingAcquisitionsExceeded(Throwable throwable) { + if (!(throwable instanceof IllegalStateException)) { + return false; + } + Throwable cause = throwable.getCause(); + return (cause instanceof HttpException + && ((HttpException) cause).getErrorCode() == CRT_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED) + || (cause instanceof CrtRuntimeException + && ((CrtRuntimeException) cause).errorCode == CRT_STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED); + } + + /** + * Creates the exception for a request rejected because the endpoint's pending acquisitions reached + * {@code maxPendingConnectionAcquires}. It is unchecked and not an {@link IOException}, so the SDK does not retry it, + * matching the Netty client. It is the same type Netty's connection pool raises for this condition. + * + * @param crtException the CRT exception that reported the rejection + * @return the exception to surface, with the CRT exception as its cause + */ + private static IllegalStateException maxPendingAcquisitionsExceeded(Throwable crtException) { + return new IllegalStateException(MAX_PENDING_ACQUISITIONS_EXCEEDED_MESSAGE, crtException); + } + public static void reportMetrics(HttpStreamManager connManager, MetricCollector metricCollector, long acquireStartTime) { long acquireCompletionTime = System.nanoTime(); diff --git a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtMaxPendingConnectionAcquiresTest.java b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtMaxPendingConnectionAcquiresTest.java new file mode 100644 index 000000000000..75d130a444f9 --- /dev/null +++ b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtMaxPendingConnectionAcquiresTest.java @@ -0,0 +1,274 @@ +/* + * 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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static software.amazon.awssdk.http.HttpTestUtils.createProvider; +import static software.amazon.awssdk.http.crt.CrtHttpClientTestUtils.createRequest; +import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; + +import java.io.ByteArrayInputStream; +import java.net.URI; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.crt.CrtRuntimeException; +import software.amazon.awssdk.crt.http.HttpException; +import software.amazon.awssdk.http.HttpExecuteRequest; +import software.amazon.awssdk.http.Protocol; +import software.amazon.awssdk.http.RecordingResponseHandler; +import software.amazon.awssdk.http.SdkHttpClient; +import software.amazon.awssdk.http.SdkHttpConfigurationOption; +import software.amazon.awssdk.http.async.AsyncExecuteRequest; +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; +import software.amazon.awssdk.utils.AttributeMap; +import software.amazon.awssdk.utils.IoUtils; + +/** + * With maxConcurrency(1) and maxPendingConnectionAcquires(1), one request holds the only connection (or stream, on HTTP/2), + * one waits, and a third concurrent request is over the limit. The servers defer every response until the test releases them, + * so the first two requests stay outstanding for as long as the test needs, with no fixed delay. + */ +public class AwsCrtMaxPendingConnectionAcquiresTest { + private static final int CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED = 2094; + private static final int STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED = 2096; + private static final String MAX_PENDING_MESSAGE = + "Maximum pending connection acquisitions exceeded. The request rate is too high for the client to keep up."; + + private static DeferredResponseServer h1Server; + private static DeferredResponseServer h2Server; + private static ExecutorService executor; + + @BeforeAll + static void setUp() throws InterruptedException { + h1Server = DeferredResponseServer.start(Protocol.HTTP1_1); + h2Server = DeferredResponseServer.start(Protocol.HTTP2); + executor = Executors.newFixedThreadPool(3); + } + + @AfterAll + static void tearDown() throws InterruptedException { + executor.shutdownNow(); + h1Server.close(); + h2Server.close(); + } + + @BeforeEach + void resetServers() { + h1Server.reset(); + h2Server.reset(); + } + + @ParameterizedTest + @ValueSource(ints = {0, -1}) + void asyncBuilderMaxPendingConnectionAcquires_nonPositive_throwsIllegalArgumentException(int value) { + assertThatThrownBy(() -> AwsCrtAsyncHttpClient.builder().maxPendingConnectionAcquires(value)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("maxPendingConnectionAcquires"); + } + + @ParameterizedTest + @ValueSource(ints = {0, -1}) + void syncBuilderMaxPendingConnectionAcquires_nonPositive_throwsIllegalArgumentException(int value) { + assertThatThrownBy(() -> AwsCrtHttpClient.builder().maxPendingConnectionAcquires(value)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("maxPendingConnectionAcquires"); + } + + @Test + void builderMaxPendingConnectionAcquires_null_isAccepted() { + assertThatCode(() -> AwsCrtAsyncHttpClient.builder().maxPendingConnectionAcquires(null)).doesNotThrowAnyException(); + assertThatCode(() -> AwsCrtHttpClient.builder().maxPendingConnectionAcquires(null)).doesNotThrowAnyException(); + } + + @Test + void asyncExecute_overLimit_failsFutureWithMaxPendingAcquisitionsExceeded() throws Exception { + try (SdkAsyncHttpClient client = limitedAsyncClient()) { + CompletableFuture first = execute(client, h1Server, new RecordingResponseHandler()); + CompletableFuture second = execute(client, h1Server, new RecordingResponseHandler()); + + CompletableFuture rejected = execute(client, h1Server, new RecordingResponseHandler()); + + assertThatThrownBy(() -> rejected.get(5, TimeUnit.SECONDS)) + .isInstanceOf(ExecutionException.class) + .satisfies(e -> assertHttp1MaxPendingAcquisitionsExceeded(e.getCause())); + h1Server.releaseResponses(); + CompletableFuture.allOf(first, second).get(5, TimeUnit.SECONDS); + } + } + + // Fails until awscrt.version includes awslabs/aws-crt-java#1032: aws-crt 0.50.0 and earlier ignore the limit on HTTP/2, + // so the third request waits for a stream instead of being rejected, and the bounded get times out. With #1032 the + // rejection completes the future before execute returns, so the short bound only limits the failing case. + @Test + void asyncExecuteH2_overLimit_failsFutureWithMaxPendingAcquisitionsExceeded() throws Exception { + try (SdkAsyncHttpClient client = AwsCrtAsyncHttpClient.builder() + .protocol(Protocol.HTTP2) + .maxConcurrency(1) + .maxPendingConnectionAcquires(1) + .build()) { + CompletableFuture first = execute(client, h2Server, new RecordingResponseHandler()); + CompletableFuture second = execute(client, h2Server, new RecordingResponseHandler()); + + CompletableFuture rejected = execute(client, h2Server, new RecordingResponseHandler()); + + assertThatThrownBy(() -> rejected.get(250, TimeUnit.MILLISECONDS)) + .isInstanceOf(ExecutionException.class) + .satisfies(e -> assertHttp2MaxPendingAcquisitionsExceeded(e.getCause())); + h2Server.releaseResponses(); + CompletableFuture.allOf(first, second).get(5, TimeUnit.SECONDS); + } + } + + @Test + void asyncExecute_overLimit_notifiesResponseHandlerOnError() throws Exception { + try (SdkAsyncHttpClient client = limitedAsyncClient()) { + CompletableFuture first = execute(client, h1Server, new RecordingResponseHandler()); + CompletableFuture second = execute(client, h1Server, new RecordingResponseHandler()); + RecordingResponseHandler rejectedHandler = new RecordingResponseHandler(); + + execute(client, h1Server, rejectedHandler); + + assertThatThrownBy(() -> rejectedHandler.completeFuture().get(5, TimeUnit.SECONDS)) + .hasCauseExactlyInstanceOf(IllegalStateException.class); + h1Server.releaseResponses(); + CompletableFuture.allOf(first, second).get(5, TimeUnit.SECONDS); + } + } + + @Test + void asyncExecute_overLimit_doesNotSendRejectedRequest() throws Exception { + try (SdkAsyncHttpClient client = limitedAsyncClient()) { + CompletableFuture first = execute(client, h1Server, new RecordingResponseHandler()); + CompletableFuture second = execute(client, h1Server, new RecordingResponseHandler()); + CompletableFuture rejected = execute(client, h1Server, new RecordingResponseHandler()); + + assertThatThrownBy(() -> rejected.get(5, TimeUnit.SECONDS)).isInstanceOf(ExecutionException.class); + h1Server.releaseResponses(); + CompletableFuture.allOf(first, second).get(5, TimeUnit.SECONDS); + + assertThat(h1Server.receivedRequests()).isEqualTo(2); + } + } + + @Test + void asyncExecute_limitNotSet_queuesAllRequests() throws Exception { + try (SdkAsyncHttpClient client = AwsCrtAsyncHttpClient.builder().maxConcurrency(1).build()) { + assertAllSucceedAfterRelease(executeThree(client)); + } + } + + @Test + void asyncBuildWithDefaults_maxPendingConnectionAcquiresServiceDefault_isNotApplied() throws Exception { + AttributeMap serviceDefaults = AttributeMap.builder() + .put(SdkHttpConfigurationOption.MAX_PENDING_CONNECTION_ACQUIRES, 1) + .build(); + try (SdkAsyncHttpClient client = AwsCrtAsyncHttpClient.builder().maxConcurrency(1).buildWithDefaults(serviceDefaults)) { + assertAllSucceedAfterRelease(executeThree(client)); + } + } + + @Test + void syncCall_overLimit_throwsMaxPendingAcquisitionsExceeded() throws Exception { + try (SdkHttpClient client = AwsCrtHttpClient.builder().maxConcurrency(1).maxPendingConnectionAcquires(1).build()) { + List> outcomes = startConcurrentSyncCalls(client, 3); + + // While responses are deferred, only the rejected call can finish. + CompletableFuture.anyOf(outcomes.toArray(new CompletableFuture[0])).get(5, TimeUnit.SECONDS); + h1Server.releaseResponses(); + List failures = outcomes.stream() + .map(CompletableFuture::join) + .filter(t -> t != null) + .collect(Collectors.toList()); + + assertThat(failures).hasSize(1); + assertHttp1MaxPendingAcquisitionsExceeded(failures.get(0)); + } + } + + private static SdkAsyncHttpClient limitedAsyncClient() { + return AwsCrtAsyncHttpClient.builder().maxConcurrency(1).maxPendingConnectionAcquires(1).build(); + } + + private static void assertHttp1MaxPendingAcquisitionsExceeded(Throwable failure) { + assertMaxPendingAcquisitionsExceededMessage(failure); + assertThat(failure.getCause()).isInstanceOfSatisfying(HttpException.class, e -> assertThat(e.getErrorCode()) + .isEqualTo(CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED)); + } + + private static void assertHttp2MaxPendingAcquisitionsExceeded(Throwable failure) { + assertMaxPendingAcquisitionsExceededMessage(failure); + assertThat(failure.getCause()).isInstanceOfSatisfying(CrtRuntimeException.class, e -> assertThat(e.errorCode) + .isEqualTo(STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED)); + } + + private static void assertMaxPendingAcquisitionsExceededMessage(Throwable failure) { + assertThat(failure).isExactlyInstanceOf(IllegalStateException.class) + .hasMessageStartingWith(MAX_PENDING_MESSAGE); + } + + private static List> executeThree(SdkAsyncHttpClient client) { + return IntStream.range(0, 3) + .mapToObj(i -> execute(client, h1Server, new RecordingResponseHandler())) + .collect(Collectors.toList()); + } + + private static void assertAllSucceedAfterRelease(List> futures) { + h1Server.releaseResponses(); + assertThatCode(() -> CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(5, TimeUnit.SECONDS)) + .doesNotThrowAnyException(); + } + + private static CompletableFuture execute(SdkAsyncHttpClient client, DeferredResponseServer server, + RecordingResponseHandler handler) { + return client.execute(AsyncExecuteRequest.builder() + .request(createRequest(server.uri())) + .requestContentPublisher(createProvider("")) + .responseHandler(handler) + .build()); + } + + private static List> startConcurrentSyncCalls(SdkHttpClient client, int count) { + HttpExecuteRequest request = HttpExecuteRequest.builder() + .request(createRequest(h1Server.uri())) + .contentStreamProvider(() -> new ByteArrayInputStream(new byte[0])) + .build(); + return IntStream.range(0, count) + .mapToObj(i -> CompletableFuture.runAsync(() -> callAndCloseBody(client, request), executor) + .handle((r, t) -> t instanceof CompletionException ? t.getCause() : t)) + .collect(Collectors.toList()); + } + + private static void callAndCloseBody(SdkHttpClient client, HttpExecuteRequest request) { + invokeSafely(() -> client.prepareRequest(request).call() + .responseBody() + .ifPresent(body -> IoUtils.closeQuietly(body, null))); + } +} diff --git a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/DeferredResponseServer.java b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/DeferredResponseServer.java new file mode 100644 index 000000000000..30fd93460a0b --- /dev/null +++ b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/DeferredResponseServer.java @@ -0,0 +1,156 @@ +/* + * 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; + +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.handler.codec.http2.DefaultHttp2DataFrame; +import io.netty.handler.codec.http2.DefaultHttp2Headers; +import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame; +import io.netty.handler.codec.http2.Http2DataFrame; +import io.netty.handler.codec.http2.Http2FrameCodecBuilder; +import io.netty.handler.codec.http2.Http2HeadersFrame; +import io.netty.handler.codec.http2.Http2MultiplexHandler; +import io.netty.handler.codec.http2.Http2StreamFrame; +import java.net.InetSocketAddress; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import software.amazon.awssdk.http.Protocol; + +/** + * Plaintext HTTP/1.1 or HTTP/2 (prior knowledge) test server that defers every response until the test calls + * {@link #releaseResponses()}, and answers immediately after that, until the next {@link #reset()}. + */ +final class DeferredResponseServer implements AutoCloseable { + private final NioEventLoopGroup group = new NioEventLoopGroup(1); + private final AtomicInteger receivedRequests = new AtomicInteger(); + private final List deferredResponses = new ArrayList<>(); + private boolean released; + private Channel serverChannel; + + static DeferredResponseServer start(Protocol protocol) throws InterruptedException { + DeferredResponseServer server = new DeferredResponseServer(); + server.serverChannel = new ServerBootstrap() + .group(server.group) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(Channel ch) { + if (protocol == Protocol.HTTP2) { + ch.pipeline().addLast(Http2FrameCodecBuilder.forServer().build(), + new Http2MultiplexHandler(server.h2StreamHandler())); + } else { + ch.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(8192), server.h1Handler()); + } + } + }) + .bind(0).sync().channel(); + return server; + } + + private SimpleChannelInboundHandler h1Handler() { + return new SimpleChannelInboundHandler() { + @Override + protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) { + respondOrDefer(ctx, () -> respondH1(ctx)); + } + }; + } + + private ChannelInitializer h2StreamHandler() { + return new ChannelInitializer() { + @Override + protected void initChannel(Channel stream) { + stream.pipeline().addLast(new SimpleChannelInboundHandler() { + @Override + protected void channelRead0(ChannelHandlerContext ctx, Http2StreamFrame frame) { + if (isEndOfRequest(frame)) { + respondOrDefer(ctx, () -> respondH2(ctx)); + } + } + }); + } + }; + } + + private synchronized void respondOrDefer(ChannelHandlerContext ctx, Runnable respond) { + receivedRequests.incrementAndGet(); + if (released) { + respond.run(); + } else { + deferredResponses.add(() -> ctx.executor().execute(respond)); + } + } + + synchronized void releaseResponses() { + released = true; + deferredResponses.forEach(Runnable::run); + deferredResponses.clear(); + } + + synchronized void reset() { + released = false; + deferredResponses.clear(); + receivedRequests.set(0); + } + + int receivedRequests() { + return receivedRequests.get(); + } + + URI uri() { + return URI.create("http://localhost:" + ((InetSocketAddress) serverChannel.localAddress()).getPort()); + } + + private static boolean isEndOfRequest(Http2StreamFrame frame) { + return (frame instanceof Http2HeadersFrame && ((Http2HeadersFrame) frame).isEndStream()) + || (frame instanceof Http2DataFrame && ((Http2DataFrame) frame).isEndStream()); + } + + private static void respondH1(ChannelHandlerContext ctx) { + FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); + HttpUtil.setContentLength(response, 0); + ctx.writeAndFlush(response); + } + + private static void respondH2(ChannelHandlerContext ctx) { + ctx.write(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers().status("200"), false)); + ctx.writeAndFlush(new DefaultHttp2DataFrame(true)); + } + + @Override + public void close() throws InterruptedException { + serverChannel.close().sync(); + group.shutdownGracefully(0, 1, TimeUnit.SECONDS).await(); + } +} diff --git a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/internal/CrtUtilsTest.java b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/internal/CrtUtilsTest.java index f12b401169ff..72d571543729 100644 --- a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/internal/CrtUtilsTest.java +++ b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/internal/CrtUtilsTest.java @@ -19,11 +19,13 @@ import java.io.IOException; import java.net.ConnectException; +import java.util.concurrent.CompletionException; import javax.net.ssl.SSLHandshakeException; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mockito; +import software.amazon.awssdk.crt.CrtRuntimeException; import software.amazon.awssdk.crt.http.HttpException; import software.amazon.awssdk.crt.http.HttpStreamManager; import software.amazon.awssdk.http.HttpMetric; @@ -31,6 +33,8 @@ import software.amazon.awssdk.metrics.MetricCollector; public class CrtUtilsTest { + private static final String MAX_PENDING_MESSAGE = + "Maximum pending connection acquisitions exceeded. The request rate is too high for the client to keep up."; /** * Locks in retryability of HTTP error codes that are NOT classified as transient by the native @@ -45,8 +49,7 @@ public class CrtUtilsTest { 2085, // AWS_ERROR_HTTP_MAX_CONCURRENT_STREAMS_EXCEEDED 2087, // AWS_ERROR_HTTP_STREAM_MANAGER_CONNECTION_ACQUIRE_FAILURE 2092, // AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT - 2093, // AWS_ERROR_HTTP_CONNECTION_MANAGER_ACQUISITION_TIMEOUT - 2094 // AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED + 2093 // AWS_ERROR_HTTP_CONNECTION_MANAGER_ACQUISITION_TIMEOUT }) public void wrapWithIoExceptionIfRetryable_retryableCode_wrapsInIOException(int errorCode) { HttpException httpException = new HttpException(errorCode); @@ -69,6 +72,7 @@ public void wrapWithIoExceptionIfRetryable_retryableCode_wrapsInIOException(int 2078, // AWS_ERROR_HTTP_RST_STREAM_SENT - symmetric to 2077 2079, // AWS_ERROR_HTTP_STREAM_NOT_ACTIVATED - API misuse on manual write 2080, // AWS_ERROR_HTTP_STREAM_HAS_COMPLETED - API misuse or secondary signal during teardown + 2094, // AWS_ERROR_HTTP_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED - client-side overload; matches Netty 2095 // AWS_ERROR_HTTP_STREAM_CANCELLED - explicit cancel by SDK abort path; not server-induced }) public void wrapWithIoExceptionIfRetryable_nonRetryableCode_returnsHttpExceptionUnchanged(int errorCode) { @@ -129,4 +133,51 @@ public void reportMetrics_streamManagerClosed_reportsAcquireDurationAndMaxConcur assertThat(metrics.metricValues(HttpMetric.LEASED_CONCURRENCY)).isEmpty(); assertThat(metrics.metricValues(HttpMetric.PENDING_CONCURRENCY_ACQUIRES)).isEmpty(); } + + @Test + public void wrapCrtException_connectionManagerMaxPendingAcquisitionsExceeded_wrapsWithMaxPendingMessage() { + HttpException httpException = new HttpException(CrtUtils.CRT_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED); + + Throwable result = CrtUtils.wrapCrtException(httpException); + + assertThat(result).isExactlyInstanceOf(IllegalStateException.class) + .hasMessageStartingWith(MAX_PENDING_MESSAGE) + .hasCause(httpException); + } + + @Test + public void wrapCrtException_streamManagerMaxPendingAcquisitionsExceeded_wrapsWithMaxPendingMessage() { + CrtRuntimeException crtException = + new CrtRuntimeException(CrtUtils.CRT_STREAM_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED); + + Throwable result = CrtUtils.wrapCrtException(new CompletionException(crtException)); + + assertThat(result).isExactlyInstanceOf(IllegalStateException.class) + .hasMessageStartingWith(MAX_PENDING_MESSAGE) + .hasCause(crtException); + } + + @Test + public void isMaxPendingAcquisitionsExceeded_wrappedRejection_returnsTrue() { + Throwable rejection = CrtUtils.wrapCrtException( + new HttpException(CrtUtils.CRT_CONNECTION_MANAGER_MAX_PENDING_ACQUISITIONS_EXCEEDED)); + + assertThat(CrtUtils.isMaxPendingAcquisitionsExceeded(rejection)).isTrue(); + } + + @Test + public void isMaxPendingAcquisitionsExceeded_otherIllegalStateException_returnsFalse() { + IllegalStateException other = new IllegalStateException("Client is closed", new HttpException(2087)); + + assertThat(CrtUtils.isMaxPendingAcquisitionsExceeded(other)).isFalse(); + } + + @Test + public void wrapCrtException_otherCrtRuntimeException_wrapsInIOException() { + CrtRuntimeException crtException = new CrtRuntimeException(2087); + + Throwable result = CrtUtils.wrapCrtException(crtException); + + assertThat(result).isInstanceOf(IOException.class).hasCause(crtException); + } }