From b15d6a49b0cca31caa77b5d0c346acf2eec4f6e9 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 5 Jan 2023 09:07:58 -0800 Subject: [PATCH 1/4] Added High Level Rest Client for SDK Signed-off-by: Owais Kazi --- build.gradle | 1 + .../java/org/opensearch/sdk/SDKClient.java | 47 +++++++++++++++---- .../org/opensearch/sdk/TestSDKClient.java | 21 ++++++++- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 0803885d..71a05c05 100644 --- a/build.gradle +++ b/build.gradle @@ -79,6 +79,7 @@ dependencies { implementation("org.opensearch:opensearch:${opensearchVersion}") implementation("org.apache.logging.log4j:log4j-api:${log4jVersion}") implementation("org.apache.logging.log4j:log4j-core:${log4jVersion}") + implementation ("org.opensearch.client:opensearch-rest-high-level-client:${opensearchVersion}") implementation("org.opensearch.client:opensearch-rest-client:${opensearchVersion}") implementation("org.opensearch.client:opensearch-java:${opensearchVersion}") implementation("org.opensearch.plugin:transport-netty4-client:${opensearchVersion}") diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index cdd998bb..06ae591a 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -28,6 +28,7 @@ import org.apache.hc.core5.ssl.SSLContextBuilder; import org.opensearch.client.RestClient; import org.opensearch.client.RestClientBuilder; +import org.opensearch.client.RestHighLevelClient; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.transport.OpenSearchTransport; @@ -41,14 +42,9 @@ public class SDKClient { private OpenSearchClient javaClient; private RestClient restClient = null; + private RestHighLevelClient highLevelClient; - /** - * Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around Java OpenSearchClient - * @param hostAddress The address of OpenSearch cluster, client can connect to - * @param port The port of OpenSearch cluster - * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client - */ - public OpenSearchClient initializeClient(String hostAddress, int port) { + private RestClientBuilder builder(String hostAddress, int port) { RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port)); builder.setStrictDeprecationMode(true); builder.setHttpClientConfigCallback(httpClientBuilder -> { @@ -74,6 +70,17 @@ public TlsDetails create(final SSLEngine sslEngine) { throw new RuntimeException(e); } }); + return builder; + } + + /** + * Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around Java OpenSearchClient + * @param hostAddress The address of OpenSearch cluster, client can connect to + * @param port The port of OpenSearch cluster + * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client + */ + public OpenSearchClient initializeJavaClient(String hostAddress, int port) { + RestClientBuilder builder = builder(hostAddress, port); restClient = builder.build(); @@ -91,7 +98,20 @@ public TlsDetails create(final SSLEngine sslEngine) { } /** - * Close this client. + * Creates High Level Rest Client for SDK. + * @param hostAddress The address of OpenSearch cluster, client can connect to + * @param port The port of OpenSearch cluster + * @return SDKClient which is internally an RestHighLevelClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client + */ + public RestHighLevelClient initializeRestClient(String hostAddress, int port) { + RestClientBuilder builder = builder(hostAddress, port); + + highLevelClient = new RestHighLevelClient(builder); + return highLevelClient; + } + + /** + * Close java client. * * @throws IOException if closing the restClient fails */ @@ -100,4 +120,15 @@ public void doCloseRestClient() throws IOException { restClient.close(); } } + + /** + * Close high level rest client. + * + * @throws IOException if closing the highLevelClient fails + */ + public void doCloseHLRClient() throws IOException { + if (highLevelClient != null) { + highLevelClient.close(); + } + } } diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index e037fa82..b8ade6ff 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -10,6 +10,8 @@ package org.opensearch.sdk; import org.junit.jupiter.api.Test; +import org.opensearch.client.RequestOptions; +import org.opensearch.client.RestHighLevelClient; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch.indices.Alias; import org.opensearch.client.opensearch.indices.CreateIndexRequest; @@ -23,9 +25,9 @@ public class TestSDKClient extends OpenSearchTestCase { SDKClient sdkClient = new SDKClient(); @Test - public void testCreateClient() throws Exception { + public void testCreateJavaClient() throws Exception { - OpenSearchClient testClient = sdkClient.initializeClient("localhost", 9200); + OpenSearchClient testClient = sdkClient.initializeJavaClient("localhost", 9200); assertInstanceOf(OpenSearchClient.class, testClient); assertThrows( @@ -41,4 +43,19 @@ public void testCreateClient() throws Exception { sdkClient.doCloseRestClient(); } + @Test + public void testCreateHighLevelRestClient() throws Exception { + RestHighLevelClient testClient = sdkClient.initializeRestClient("localhost", 9200); + + // Using the package name here as Java uses package name if the filename from different packages are same + org.opensearch.client.indices.CreateIndexRequest createIndexRequest = new org.opensearch.client.indices.CreateIndexRequest( + "my-index" + ); + + assertThrows(ConnectException.class, () -> testClient.indices().create(createIndexRequest, RequestOptions.DEFAULT)); + + sdkClient.doCloseHLRClient(); + + } + } From 3ff4b658f2384830191fd0a915fe2e4c84361f24 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Thu, 5 Jan 2023 09:24:49 -0800 Subject: [PATCH 2/4] Updates method name Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 2 +- src/test/java/org/opensearch/sdk/TestSDKClient.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index 06ae591a..bcb729a8 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -126,7 +126,7 @@ public void doCloseRestClient() throws IOException { * * @throws IOException if closing the highLevelClient fails */ - public void doCloseHLRClient() throws IOException { + public void doCloseHighLevelClient() throws IOException { if (highLevelClient != null) { highLevelClient.close(); } diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index b8ade6ff..4d75344c 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -54,7 +54,7 @@ public void testCreateHighLevelRestClient() throws Exception { assertThrows(ConnectException.class, () -> testClient.indices().create(createIndexRequest, RequestOptions.DEFAULT)); - sdkClient.doCloseHLRClient(); + sdkClient.doCloseHighLevelClient(); } From f57c047b168dbab50849419af9963c4a09502a0d Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Fri, 6 Jan 2023 10:52:39 -0800 Subject: [PATCH 3/4] Addressed PR Comments Signed-off-by: Owais Kazi --- build.gradle | 2 +- src/main/java/org/opensearch/sdk/SDKClient.java | 8 ++++---- src/test/java/org/opensearch/sdk/TestSDKClient.java | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 71a05c05..7bc05a88 100644 --- a/build.gradle +++ b/build.gradle @@ -79,7 +79,7 @@ dependencies { implementation("org.opensearch:opensearch:${opensearchVersion}") implementation("org.apache.logging.log4j:log4j-api:${log4jVersion}") implementation("org.apache.logging.log4j:log4j-core:${log4jVersion}") - implementation ("org.opensearch.client:opensearch-rest-high-level-client:${opensearchVersion}") + implementation("org.opensearch.client:opensearch-rest-high-level-client:${opensearchVersion}") implementation("org.opensearch.client:opensearch-rest-client:${opensearchVersion}") implementation("org.opensearch.client:opensearch-java:${opensearchVersion}") implementation("org.opensearch.plugin:transport-netty4-client:${opensearchVersion}") diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index bcb729a8..ed2790c3 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -41,7 +41,7 @@ */ public class SDKClient { private OpenSearchClient javaClient; - private RestClient restClient = null; + private RestClient restClient; private RestHighLevelClient highLevelClient; private RestClientBuilder builder(String hostAddress, int port) { @@ -77,7 +77,7 @@ public TlsDetails create(final SSLEngine sslEngine) { * Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around Java OpenSearchClient * @param hostAddress The address of OpenSearch cluster, client can connect to * @param port The port of OpenSearch cluster - * @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client + * @return The SDKClient implementation of OpenSearchClient. The user is responsible for calling {@link #doCloseJavaClient()} when finished with the client */ public OpenSearchClient initializeJavaClient(String hostAddress, int port) { RestClientBuilder builder = builder(hostAddress, port); @@ -101,7 +101,7 @@ public OpenSearchClient initializeJavaClient(String hostAddress, int port) { * Creates High Level Rest Client for SDK. * @param hostAddress The address of OpenSearch cluster, client can connect to * @param port The port of OpenSearch cluster - * @return SDKClient which is internally an RestHighLevelClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client + * @return The SDKClient implementation of RestHighLevelClient. The user is responsible for calling {@link #doCloseHighLevelClient()} when finished with the client */ public RestHighLevelClient initializeRestClient(String hostAddress, int port) { RestClientBuilder builder = builder(hostAddress, port); @@ -115,7 +115,7 @@ public RestHighLevelClient initializeRestClient(String hostAddress, int port) { * * @throws IOException if closing the restClient fails */ - public void doCloseRestClient() throws IOException { + public void doCloseJavaClient() throws IOException { if (restClient != null) { restClient.close(); } diff --git a/src/test/java/org/opensearch/sdk/TestSDKClient.java b/src/test/java/org/opensearch/sdk/TestSDKClient.java index 4d75344c..19c9e7cf 100644 --- a/src/test/java/org/opensearch/sdk/TestSDKClient.java +++ b/src/test/java/org/opensearch/sdk/TestSDKClient.java @@ -40,7 +40,7 @@ public void testCreateJavaClient() throws Exception { ) ); - sdkClient.doCloseRestClient(); + sdkClient.doCloseJavaClient(); } @Test From 18971a65c811caae01636dff7d60df38a264d2ad Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Fri, 6 Jan 2023 11:47:17 -0800 Subject: [PATCH 4/4] Implements Closeable for SDKClients Signed-off-by: Owais Kazi --- src/main/java/org/opensearch/sdk/SDKClient.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/sdk/SDKClient.java b/src/main/java/org/opensearch/sdk/SDKClient.java index ed2790c3..f5873edd 100644 --- a/src/main/java/org/opensearch/sdk/SDKClient.java +++ b/src/main/java/org/opensearch/sdk/SDKClient.java @@ -9,6 +9,7 @@ package org.opensearch.sdk; +import java.io.Closeable; import java.io.IOException; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -39,7 +40,7 @@ /** * This class creates SDKClient for an extension to make requests to OpenSearch */ -public class SDKClient { +public class SDKClient implements Closeable { private OpenSearchClient javaClient; private RestClient restClient; private RestHighLevelClient highLevelClient; @@ -98,11 +99,13 @@ public OpenSearchClient initializeJavaClient(String hostAddress, int port) { } /** + * @deprecated Provided for compatibility with existing plugins to permit migration. New development should not use this client * Creates High Level Rest Client for SDK. * @param hostAddress The address of OpenSearch cluster, client can connect to * @param port The port of OpenSearch cluster * @return The SDKClient implementation of RestHighLevelClient. The user is responsible for calling {@link #doCloseHighLevelClient()} when finished with the client */ + @Deprecated public RestHighLevelClient initializeRestClient(String hostAddress, int port) { RestClientBuilder builder = builder(hostAddress, port); @@ -131,4 +134,10 @@ public void doCloseHighLevelClient() throws IOException { highLevelClient.close(); } } + + @Override + public void close() throws IOException { + doCloseJavaClient(); + doCloseHighLevelClient(); + } }