Skip to content

Commit 2ad7348

Browse files
committed
HBASE-28671 Add close method to REST client (#5998)
also expose HttpClientConnectionManager Signed-off-by: Duo Zhang <zhangduo@apache.org> (cherry picked from commit 046e9d5)
1 parent e4b6845 commit 2ad7348

File tree

1 file changed

+33
-7
lines changed
  • hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client

1 file changed

+33
-7
lines changed

hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@
6363
import org.apache.http.client.methods.HttpPut;
6464
import org.apache.http.client.methods.HttpUriRequest;
6565
import org.apache.http.client.protocol.HttpClientContext;
66+
import org.apache.http.conn.HttpClientConnectionManager;
6667
import org.apache.http.entity.ByteArrayEntity;
6768
import org.apache.http.impl.client.BasicCredentialsProvider;
69+
import org.apache.http.impl.client.CloseableHttpClient;
6870
import org.apache.http.impl.client.HttpClientBuilder;
6971
import org.apache.http.impl.client.HttpClients;
7072
import org.apache.http.impl.cookie.BasicClientCookie;
@@ -88,7 +90,7 @@ public class Client {
8890

8991
private static final Logger LOG = LoggerFactory.getLogger(Client.class);
9092

91-
private HttpClient httpClient;
93+
private CloseableHttpClient httpClient;
9294
private Cluster cluster;
9395
private Integer lastNodeId;
9496
private boolean sticky = false;
@@ -115,7 +117,7 @@ public Client() {
115117

116118
private void initialize(Cluster cluster, Configuration conf, boolean sslEnabled, boolean sticky,
117119
Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> password,
118-
Optional<String> bearerToken) {
120+
Optional<String> bearerToken, Optional<HttpClientConnectionManager> connManager) {
119121
this.cluster = cluster;
120122
this.conf = conf;
121123
this.sslEnabled = sslEnabled;
@@ -178,6 +180,8 @@ private void initialize(Cluster cluster, Configuration conf, boolean sslEnabled,
178180
extraHeaders.put(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken.get());
179181
}
180182

183+
connManager.ifPresent(httpClientBuilder::setConnectionManager);
184+
181185
this.httpClient = httpClientBuilder.build();
182186
setSticky(sticky);
183187
}
@@ -201,7 +205,7 @@ public Client(Cluster cluster) {
201205
*/
202206
public Client(Cluster cluster, boolean sslEnabled) {
203207
initialize(cluster, HBaseConfiguration.create(), sslEnabled, false, Optional.empty(),
204-
Optional.empty(), Optional.empty(), Optional.empty());
208+
Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
205209
}
206210

207211
/**
@@ -214,7 +218,7 @@ public Client(Cluster cluster, boolean sslEnabled) {
214218
*/
215219
public Client(Cluster cluster, Configuration conf, boolean sslEnabled) {
216220
initialize(cluster, conf, sslEnabled, false, Optional.empty(), Optional.empty(),
217-
Optional.empty(), Optional.empty());
221+
Optional.empty(), Optional.empty(), Optional.empty());
218222
}
219223

220224
/**
@@ -238,7 +242,11 @@ public Client(Cluster cluster, String trustStorePath, Optional<String> trustStor
238242
* or BEARER authentication in sticky mode, which does not use the old faulty load balancing
239243
* logic, and enables correct session handling. If neither userName/password, nor the bearer token
240244
* is specified, the client falls back to SPNEGO auth. The loadTrustsore static method can be used
241-
* to load a local trustStore file. This is the preferred constructor to use.
245+
* to load a local TrustStore file. If connManager is specified, it must be fully configured. Even
246+
* then, the TrustStore related parameters must be specified because they are also used for SPNEGO
247+
* authentication which uses a separate HTTP client implementation. Specifying the
248+
* HttpClientConnectionManager is an experimental feature. It exposes the internal HTTP library
249+
* details, and may be changed/removed when the library is updated or replaced.
242250
* @param cluster the cluster definition
243251
* @param conf HBase/Hadoop configuration
244252
* @param sslEnabled use HTTPS
@@ -247,10 +255,19 @@ public Client(Cluster cluster, String trustStorePath, Optional<String> trustStor
247255
* @param password for BASIC auth
248256
* @param bearerToken for BEAERER auth
249257
*/
258+
@InterfaceAudience.Private
259+
public Client(Cluster cluster, Configuration conf, boolean sslEnabled,
260+
Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> password,
261+
Optional<String> bearerToken, Optional<HttpClientConnectionManager> connManager) {
262+
initialize(cluster, conf, sslEnabled, true, trustStore, userName, password, bearerToken,
263+
connManager);
264+
}
265+
250266
public Client(Cluster cluster, Configuration conf, boolean sslEnabled,
251267
Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> password,
252268
Optional<String> bearerToken) {
253-
initialize(cluster, conf, sslEnabled, true, trustStore, userName, password, bearerToken);
269+
initialize(cluster, conf, sslEnabled, true, trustStore, userName, password, bearerToken,
270+
Optional.empty());
254271
}
255272

256273
/**
@@ -269,7 +286,7 @@ public Client(Cluster cluster, Configuration conf, String trustStorePath,
269286
Optional<String> trustStorePassword, Optional<String> trustStoreType) {
270287
KeyStore trustStore = loadTruststore(trustStorePath, trustStorePassword, trustStoreType);
271288
initialize(cluster, conf, true, false, Optional.of(trustStore), Optional.empty(),
272-
Optional.empty(), Optional.empty());
289+
Optional.empty(), Optional.empty(), Optional.empty());
273290
}
274291

275292
/**
@@ -970,4 +987,13 @@ public ClientTrustStoreInitializationException(String message, Throwable cause)
970987
super(message, cause);
971988
}
972989
}
990+
991+
public void close() {
992+
try {
993+
httpClient.close();
994+
} catch (Exception e) {
995+
LOG.info("Exception while shutting down connection manager", e);
996+
}
997+
}
998+
973999
}

0 commit comments

Comments
 (0)