This is an automated email from the ASF dual-hosted git repository.

stoty pushed a commit to branch branch-2.5
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.5 by this push:
     new f26c74248f4 HBASE-28671 Add close method to REST client (#5998)
f26c74248f4 is described below

commit f26c74248f4ce88bd5ac69c96b6b245ad8ad8e3e
Author: Istvan Toth <[email protected]>
AuthorDate: Wed Jun 19 10:48:07 2024 +0200

    HBASE-28671 Add close method to REST client (#5998)
    
    also expose HttpClientConnectionManager
    
    Signed-off-by: Duo Zhang <[email protected]>
    (cherry picked from commit 046e9d55409755285026ff5bba2797692af42840)
---
 .../apache/hadoop/hbase/rest/client/Client.java    | 40 ++++++++++++++++++----
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git 
a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java 
b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java
index 5c89dea48e7..74d3a11d3f2 100644
--- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java
+++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java
@@ -63,8 +63,10 @@ import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.conn.HttpClientConnectionManager;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.cookie.BasicClientCookie;
@@ -88,7 +90,7 @@ public class Client {
 
   private static final Logger LOG = LoggerFactory.getLogger(Client.class);
 
-  private HttpClient httpClient;
+  private CloseableHttpClient httpClient;
   private Cluster cluster;
   private Integer lastNodeId;
   private boolean sticky = false;
@@ -115,7 +117,7 @@ public class Client {
 
   private void initialize(Cluster cluster, Configuration conf, boolean 
sslEnabled, boolean sticky,
     Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> 
password,
-    Optional<String> bearerToken) {
+    Optional<String> bearerToken, Optional<HttpClientConnectionManager> 
connManager) {
     this.cluster = cluster;
     this.conf = conf;
     this.sslEnabled = sslEnabled;
@@ -178,6 +180,8 @@ public class Client {
       extraHeaders.put(HttpHeaders.AUTHORIZATION, "Bearer " + 
bearerToken.get());
     }
 
+    connManager.ifPresent(httpClientBuilder::setConnectionManager);
+
     this.httpClient = httpClientBuilder.build();
     setSticky(sticky);
   }
@@ -201,7 +205,7 @@ public class Client {
    */
   public Client(Cluster cluster, boolean sslEnabled) {
     initialize(cluster, HBaseConfiguration.create(), sslEnabled, false, 
Optional.empty(),
-      Optional.empty(), Optional.empty(), Optional.empty());
+      Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
   }
 
   /**
@@ -214,7 +218,7 @@ public class Client {
    */
   public Client(Cluster cluster, Configuration conf, boolean sslEnabled) {
     initialize(cluster, conf, sslEnabled, false, Optional.empty(), 
Optional.empty(),
-      Optional.empty(), Optional.empty());
+      Optional.empty(), Optional.empty(), Optional.empty());
   }
 
   /**
@@ -238,7 +242,11 @@ public class Client {
    * or BEARER authentication in sticky mode, which does not use the old 
faulty load balancing
    * logic, and enables correct session handling. If neither 
userName/password, nor the bearer token
    * is specified, the client falls back to SPNEGO auth. The loadTrustsore 
static method can be used
-   * to load a local trustStore file. This is the preferred constructor to use.
+   * to load a local TrustStore file. If connManager is specified, it must be 
fully configured. Even
+   * then, the TrustStore related parameters must be specified because they 
are also used for SPNEGO
+   * authentication which uses a separate HTTP client implementation. 
Specifying the
+   * HttpClientConnectionManager is an experimental feature. It exposes the 
internal HTTP library
+   * details, and may be changed/removed when the library is updated or 
replaced.
    * @param cluster     the cluster definition
    * @param conf        HBase/Hadoop configuration
    * @param sslEnabled  use HTTPS
@@ -247,10 +255,19 @@ public class Client {
    * @param password    for BASIC auth
    * @param bearerToken for BEAERER auth
    */
+  @InterfaceAudience.Private
+  public Client(Cluster cluster, Configuration conf, boolean sslEnabled,
+    Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> 
password,
+    Optional<String> bearerToken, Optional<HttpClientConnectionManager> 
connManager) {
+    initialize(cluster, conf, sslEnabled, true, trustStore, userName, 
password, bearerToken,
+      connManager);
+  }
+
   public Client(Cluster cluster, Configuration conf, boolean sslEnabled,
     Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> 
password,
     Optional<String> bearerToken) {
-    initialize(cluster, conf, sslEnabled, true, trustStore, userName, 
password, bearerToken);
+    initialize(cluster, conf, sslEnabled, true, trustStore, userName, 
password, bearerToken,
+      Optional.empty());
   }
 
   /**
@@ -269,7 +286,7 @@ public class Client {
     Optional<String> trustStorePassword, Optional<String> trustStoreType) {
     KeyStore trustStore = loadTruststore(trustStorePath, trustStorePassword, 
trustStoreType);
     initialize(cluster, conf, true, false, Optional.of(trustStore), 
Optional.empty(),
-      Optional.empty(), Optional.empty());
+      Optional.empty(), Optional.empty(), Optional.empty());
   }
 
   /**
@@ -970,4 +987,13 @@ public class Client {
       super(message, cause);
     }
   }
+
+  public void close() {
+    try {
+      httpClient.close();
+    } catch (Exception e) {
+      LOG.info("Exception while shutting down connection manager", e);
+    }
+  }
+
 }

Reply via email to