diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java index c567230559..94e0d3ba42 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java @@ -38,6 +38,7 @@ import software.amazon.awssdk.http.async.AsyncExecuteRequest; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.utils.SdkAutoCloseable; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -70,8 +71,7 @@ public class AwsSdk2Transport implements OpenSearchTransport { public static final Integer DEFAULT_REQUEST_COMPRESSION_SIZE = 8192; private static final byte[] NO_BYTES = new byte[0]; - private final SdkHttpClient httpClient; - private final SdkAsyncHttpClient asyncHttpClient; + private final SdkAutoCloseable httpClient; private final String host; private final String signingServiceName; private final Region signingRegion; @@ -79,12 +79,12 @@ public class AwsSdk2Transport implements OpenSearchTransport { private final AwsSdk2TransportOptions transportOptions; /** - * Create an {@link OpenSearchTransport} with a synchronous AWS HTTP client. + * Create an {@link OpenSearchTransport} with an asynchronous AWS HTTP client. *
* Note that asynchronous OpenSearch requests sent through this transport will be dispatched * *synchronously* on the calling thread. * - * @param httpClient HTTP client to use for OpenSearch requests. + * @param asyncHttpClient Asynchronous HTTP client to use for OpenSearch requests. * @param host The fully qualified domain name to connect to. * @param signingRegion The AWS region for which requests will be signed. This should typically match the region in `host`. * @param options Options that apply to all requests. Can be null. Create with @@ -92,132 +92,82 @@ public class AwsSdk2Transport implements OpenSearchTransport { * compression options, etc. */ public AwsSdk2Transport( - @Nonnull SdkHttpClient httpClient, + @CheckForNull SdkAsyncHttpClient asyncHttpClient, @Nonnull String host, @Nonnull Region signingRegion, @CheckForNull AwsSdk2TransportOptions options) { - this(httpClient, null, host, "es", signingRegion, options); + this(asyncHttpClient, host, "es", signingRegion, options); } /** * Create an {@link OpenSearchTransport} with a synchronous AWS HTTP client. - *
- * Note that asynchronous OpenSearch requests sent through this transport will be dispatched - * *synchronously* on the calling thread. * - * @param httpClient HTTP client to use for OpenSearch requests. + * @param syncHttpClient Synchronous HTTP client to use for OpenSearch requests. * @param host The fully qualified domain name to connect to. - * @param signingServiceName The AWS signing service name, one of `es` (Amazon OpenSearch) or `aoss` (Amazon OpenSearch Serverless). * @param signingRegion The AWS region for which requests will be signed. This should typically match the region in `host`. * @param options Options that apply to all requests. Can be null. Create with * {@link AwsSdk2TransportOptions#builder()} and use these to specify non-default credentials, * compression options, etc. */ public AwsSdk2Transport( - @Nonnull SdkHttpClient httpClient, + @CheckForNull SdkHttpClient syncHttpClient, @Nonnull String host, - @Nonnull String signingServiceName, @Nonnull Region signingRegion, @CheckForNull AwsSdk2TransportOptions options) { - this(httpClient, null, host, signingServiceName, signingRegion, options); - } - - /** - * Create an {@link OpenSearchTransport} with an asynchronous AWS HTTP client - *
- * Note that synchronous OpenSearch requests sent through this transport will be dispatched - * using the asynchronous client, but the calling thread will block until they are complete. - * - * @param asyncHttpClient HTTP client to use for OpenSearch requests. - * @param host The target host. - * @param signingRegion The AWS region for which requests will be signed. This should typically match region in `host`. - * @param options Options that apply to all requests. Can be null. Create with - * {@link AwsSdk2TransportOptions#builder()} and use these to specify non-default credentials, - * compression options, etc. - */ - public AwsSdk2Transport( - @Nonnull SdkAsyncHttpClient asyncHttpClient, - @Nonnull String host, - @Nonnull Region signingRegion, - @CheckForNull AwsSdk2TransportOptions options) { - this(null, asyncHttpClient, host, "es", signingRegion, options); + this(syncHttpClient, host, "es", signingRegion, options); } /** * Create an {@link OpenSearchTransport} with an asynchronous AWS HTTP client. *
- * Note that synchronous OpenSearch requests sent through this transport will be dispatched - * using the asynchronous client, but the calling thread will block until they are complete. + * Note that asynchronous OpenSearch requests sent through this transport will be dispatched + * *synchronously* on the calling thread. * - * @param asyncHttpClient HTTP client to use for OpenSearch requests. - * @param host The target host. - * @param signingServiceName The AWS signing service name, one of `es` (Amazon OpenSearch) or `aoss` (Amazon OpenSearch Serverless). + * @param asyncHttpClient Asynchronous HTTP client to use for OpenSearch requests. + * @param host The fully qualified domain name to connect to. * @param signingRegion The AWS region for which requests will be signed. This should typically match the region in `host`. + * @param signingServiceName The AWS signing service name, one of `es` (Amazon OpenSearch) or `aoss` (Amazon OpenSearch Serverless). * @param options Options that apply to all requests. Can be null. Create with * {@link AwsSdk2TransportOptions#builder()} and use these to specify non-default credentials, * compression options, etc. */ public AwsSdk2Transport( - @Nonnull SdkAsyncHttpClient asyncHttpClient, + @CheckForNull SdkAsyncHttpClient asyncHttpClient, @Nonnull String host, @Nonnull String signingServiceName, @Nonnull Region signingRegion, @CheckForNull AwsSdk2TransportOptions options) { - this(null, asyncHttpClient, host, signingServiceName, signingRegion, options); + this((SdkAutoCloseable) asyncHttpClient, host, signingServiceName, signingRegion, options); } /** - * Create an {@link OpenSearchTransport} with both synchronous and asynchronous AWS HTTP clients. - *
- * The synchronous client will be used for synchronous OpenSearch requests, and the asynchronous client - * will be used for asynchronous HTTP requests. + * Create an {@link OpenSearchTransport} with a synchronous AWS HTTP client. * - * @param httpClient HTTP client to use for OpenSearch requests. - * @param asyncHttpClient HTTP client to use for synchronous OpenSearch requests. + * @param syncHttpClient Synchronous HTTP client to use for OpenSearch requests. * @param host The fully qualified domain name to connect to. * @param signingRegion The AWS region for which requests will be signed. This should typically match the region in `host`. + * @param signingServiceName The AWS signing service name, one of `es` (Amazon OpenSearch) or `aoss` (Amazon OpenSearch Serverless). * @param options Options that apply to all requests. Can be null. Create with * {@link AwsSdk2TransportOptions#builder()} and use these to specify non-default credentials, * compression options, etc. */ public AwsSdk2Transport( - @CheckForNull SdkHttpClient httpClient, - @CheckForNull SdkAsyncHttpClient asyncHttpClient, + @CheckForNull SdkHttpClient syncHttpClient, @Nonnull String host, + @Nonnull String signingServiceName, @Nonnull Region signingRegion, @CheckForNull AwsSdk2TransportOptions options) { - this(httpClient, asyncHttpClient, host, "es", signingRegion, options); + this((SdkAutoCloseable) syncHttpClient, host, signingServiceName, signingRegion, options); } - /** - * Create an {@link OpenSearchTransport} with both synchronous and asynchronous AWS HTTP clients. - *
- * The synchronous client will be used for synchronous OpenSearch requests, and the asynchronous client
- * will be used for asynchronous HTTP requests.
- *
- * @param httpClient HTTP client to use for OpenSearch requests.
- * @param asyncHttpClient HTTP client to use for synchronous OpenSearch requests.
- * @param host The fully qualified domain name to connect to.
- * @param signingRegion The AWS region for which requests will be signed. This should typically match the region in `host`.
- * @param signingServiceName The AWS signing service name, one of `es` (Amazon OpenSearch) or `aoss` (Amazon OpenSearch Serverless).
- * @param options Options that apply to all requests. Can be null. Create with
- * {@link AwsSdk2TransportOptions#builder()} and use these to specify non-default credentials,
- * compression options, etc.
- */
- public AwsSdk2Transport(
- @CheckForNull SdkHttpClient httpClient,
- @CheckForNull SdkAsyncHttpClient asyncHttpClient,
+ private AwsSdk2Transport(
+ @CheckForNull SdkAutoCloseable httpClient,
@Nonnull String host,
@Nonnull String signingServiceName,
@Nonnull Region signingRegion,
@CheckForNull AwsSdk2TransportOptions options) {
- if (httpClient == null && asyncHttpClient == null)
- {
- throw new IllegalArgumentException("At least one SdkHttpClient or SdkAsyncHttpClient must be provided");
- }
Objects.requireNonNull(host, "Target OpenSearch service host must not be null");
this.httpClient = httpClient;
- this.asyncHttpClient = asyncHttpClient;
this.host = host;
this.signingServiceName = signingServiceName;
this.signingRegion = signingRegion;
@@ -237,11 +187,11 @@ public