63
63
import org .apache .http .client .methods .HttpPut ;
64
64
import org .apache .http .client .methods .HttpUriRequest ;
65
65
import org .apache .http .client .protocol .HttpClientContext ;
66
+ import org .apache .http .conn .HttpClientConnectionManager ;
66
67
import org .apache .http .entity .ByteArrayEntity ;
67
68
import org .apache .http .impl .client .BasicCredentialsProvider ;
69
+ import org .apache .http .impl .client .CloseableHttpClient ;
68
70
import org .apache .http .impl .client .HttpClientBuilder ;
69
71
import org .apache .http .impl .client .HttpClients ;
70
72
import org .apache .http .impl .cookie .BasicClientCookie ;
@@ -88,7 +90,7 @@ public class Client {
88
90
89
91
private static final Logger LOG = LoggerFactory .getLogger (Client .class );
90
92
91
- private HttpClient httpClient ;
93
+ private CloseableHttpClient httpClient ;
92
94
private Cluster cluster ;
93
95
private Integer lastNodeId ;
94
96
private boolean sticky = false ;
@@ -115,7 +117,7 @@ public Client() {
115
117
116
118
private void initialize (Cluster cluster , Configuration conf , boolean sslEnabled , boolean sticky ,
117
119
Optional <KeyStore > trustStore , Optional <String > userName , Optional <String > password ,
118
- Optional <String > bearerToken ) {
120
+ Optional <String > bearerToken , Optional < HttpClientConnectionManager > connManager ) {
119
121
this .cluster = cluster ;
120
122
this .conf = conf ;
121
123
this .sslEnabled = sslEnabled ;
@@ -178,6 +180,8 @@ private void initialize(Cluster cluster, Configuration conf, boolean sslEnabled,
178
180
extraHeaders .put (HttpHeaders .AUTHORIZATION , "Bearer " + bearerToken .get ());
179
181
}
180
182
183
+ connManager .ifPresent (httpClientBuilder ::setConnectionManager );
184
+
181
185
this .httpClient = httpClientBuilder .build ();
182
186
setSticky (sticky );
183
187
}
@@ -201,7 +205,7 @@ public Client(Cluster cluster) {
201
205
*/
202
206
public Client (Cluster cluster , boolean sslEnabled ) {
203
207
initialize (cluster , HBaseConfiguration .create (), sslEnabled , false , Optional .empty (),
204
- Optional .empty (), Optional .empty (), Optional .empty ());
208
+ Optional .empty (), Optional .empty (), Optional .empty (), Optional . empty () );
205
209
}
206
210
207
211
/**
@@ -214,7 +218,7 @@ public Client(Cluster cluster, boolean sslEnabled) {
214
218
*/
215
219
public Client (Cluster cluster , Configuration conf , boolean sslEnabled ) {
216
220
initialize (cluster , conf , sslEnabled , false , Optional .empty (), Optional .empty (),
217
- Optional .empty (), Optional .empty ());
221
+ Optional .empty (), Optional .empty (), Optional . empty () );
218
222
}
219
223
220
224
/**
@@ -238,7 +242,11 @@ public Client(Cluster cluster, String trustStorePath, Optional<String> trustStor
238
242
* or BEARER authentication in sticky mode, which does not use the old faulty load balancing
239
243
* logic, and enables correct session handling. If neither userName/password, nor the bearer token
240
244
* 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.
242
250
* @param cluster the cluster definition
243
251
* @param conf HBase/Hadoop configuration
244
252
* @param sslEnabled use HTTPS
@@ -247,10 +255,19 @@ public Client(Cluster cluster, String trustStorePath, Optional<String> trustStor
247
255
* @param password for BASIC auth
248
256
* @param bearerToken for BEAERER auth
249
257
*/
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
+
250
266
public Client (Cluster cluster , Configuration conf , boolean sslEnabled ,
251
267
Optional <KeyStore > trustStore , Optional <String > userName , Optional <String > password ,
252
268
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 ());
254
271
}
255
272
256
273
/**
@@ -269,7 +286,7 @@ public Client(Cluster cluster, Configuration conf, String trustStorePath,
269
286
Optional <String > trustStorePassword , Optional <String > trustStoreType ) {
270
287
KeyStore trustStore = loadTruststore (trustStorePath , trustStorePassword , trustStoreType );
271
288
initialize (cluster , conf , true , false , Optional .of (trustStore ), Optional .empty (),
272
- Optional .empty (), Optional .empty ());
289
+ Optional .empty (), Optional .empty (), Optional . empty () );
273
290
}
274
291
275
292
/**
@@ -970,4 +987,13 @@ public ClientTrustStoreInitializationException(String message, Throwable cause)
970
987
super (message , cause );
971
988
}
972
989
}
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
+
973
999
}
0 commit comments