Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSCRTHTTPClient-8e385ba.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS CRT HTTP Client",
"contributor": "",
"description": "Add `maxPendingConnectionAcquires` to `AwsCrtHttpClient.Builder` and `AwsCrtAsyncHttpClient.Builder`. Unbounded by default."
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ public interface Builder extends SdkAsyncHttpClient.Builder<AwsCrtAsyncHttpClien
*/
AwsCrtAsyncHttpClient.Builder maxConcurrency(Integer maxConcurrency);

/**
* Configure the maximum number of requests to an endpoint that can wait once {@link #maxConcurrency(Integer)} is in
* use: all connections for HTTP/1.1, or all streams for HTTP/2. When the limit is reached, a new request fails
* immediately instead of waiting.
*
* <p>
* 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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -165,6 +170,21 @@ public interface Builder extends SdkHttpClient.Builder<AwsCrtHttpClient.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.
*
* <p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class AwsCrtClientBuilderBase<BuilderT> {
private TlsVersion minTlsVersion;
private Integer numEventLoopThreads;
private Boolean useTrustStoreSystemProperties;
private Integer maxPendingConnectionAcquires;

protected AwsCrtClientBuilderBase() {
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing 2094 from the retryable codes doesn't change behavior for existing users because it was basically unreachable until now. aws-c-http only raises 2094 when max_pending_connection_acquisitions is set, and the SDK never set it (HttpClientConnectionManagerOptions.withMaxPendingConnectionAcquisitions was never called).

The reason we removed it here is to match netty behavior; we don't retry max pending acquisitions error for netty

codes.add(2094);
ADDITIONAL_RETRYABLE_ERROR_CODES = unmodifiableSet(codes);
}

Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider handle 2094 and 2096 error here with a more clear error message? In netty we have

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Updated

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);
Expand All @@ -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();
Expand Down
Loading
Loading