Skip to content

Commit 1ca8501

Browse files
pankaj72981saintstack
authored andcommitted
HBASE-25374 Make REST Client connection and socket time out configurable (#2752)
Signed-off-by: Guanghao Zhang <zghao@apache.org> Signed-off-by: stack <stack@apache.org>
1 parent a93b0cd commit 1ca8501

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/Constants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,12 @@ public interface Constants {
8989
/** If this query parameter is present when processing row or scanner resources,
9090
it disables server side block caching */
9191
String NOCACHE_PARAM_NAME = "nocache";
92+
93+
/** Configuration parameter to set rest client connection timeout */
94+
String REST_CLIENT_CONN_TIMEOUT = "hbase.rest.client.conn.timeout";
95+
int DEFAULT_REST_CLIENT_CONN_TIMEOUT = 2 * 1000;
96+
97+
/** Configuration parameter to set rest client socket timeout */
98+
String REST_CLIENT_SOCKET_TIMEOUT = "hbase.rest.client.socket.timeout";
99+
int DEFAULT_REST_CLIENT_SOCKET_TIMEOUT = 30 * 1000;
92100
}

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
import java.util.Optional;
4141
import java.util.concurrent.ConcurrentHashMap;
4242
import javax.net.ssl.SSLContext;
43+
44+
import org.apache.hadoop.conf.Configuration;
45+
import org.apache.hadoop.hbase.HBaseConfiguration;
46+
import org.apache.hadoop.hbase.rest.Constants;
4347
import org.apache.hadoop.security.authentication.client.AuthenticatedURL;
4448
import org.apache.hadoop.security.authentication.client.AuthenticationException;
4549
import org.apache.hadoop.security.authentication.client.KerberosAuthenticator;
@@ -76,6 +80,7 @@ public class Client {
7680

7781
private HttpClient httpClient;
7882
private Cluster cluster;
83+
private Configuration conf;
7984
private boolean sslEnabled;
8085
private HttpResponse resp;
8186
private HttpGet httpGet = null;
@@ -93,16 +98,22 @@ public Client() {
9398
this(null);
9499
}
95100

96-
private void initialize(Cluster cluster, boolean sslEnabled, Optional<KeyStore> trustStore) {
101+
private void initialize(Cluster cluster, Configuration conf, boolean sslEnabled,
102+
Optional<KeyStore> trustStore) {
97103
this.cluster = cluster;
104+
this.conf = conf;
98105
this.sslEnabled = sslEnabled;
99106
extraHeaders = new ConcurrentHashMap<>();
100107
String clspath = System.getProperty("java.class.path");
101108
LOG.debug("classpath " + clspath);
102109
HttpClientBuilder httpClientBuilder = HttpClients.custom();
103110

104-
RequestConfig requestConfig = RequestConfig.custom().
105-
setConnectTimeout(2000).build();
111+
int connTimeout = this.conf.getInt(Constants.REST_CLIENT_CONN_TIMEOUT,
112+
Constants.DEFAULT_REST_CLIENT_CONN_TIMEOUT);
113+
int socketTimeout = this.conf.getInt(Constants.REST_CLIENT_SOCKET_TIMEOUT,
114+
Constants.DEFAULT_REST_CLIENT_SOCKET_TIMEOUT);
115+
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connTimeout)
116+
.setSocketTimeout(socketTimeout).build();
106117
httpClientBuilder.setDefaultRequestConfig(requestConfig);
107118

108119
// Since HBASE-25267 we don't use the deprecated DefaultHttpClient anymore.
@@ -138,7 +149,17 @@ public Client(Cluster cluster) {
138149
* @param sslEnabled enable SSL or not
139150
*/
140151
public Client(Cluster cluster, boolean sslEnabled) {
141-
initialize(cluster, sslEnabled, Optional.empty());
152+
initialize(cluster, HBaseConfiguration.create(), sslEnabled, Optional.empty());
153+
}
154+
155+
/**
156+
* Constructor
157+
* @param cluster the cluster definition
158+
* @param conf Configuration
159+
* @param sslEnabled enable SSL or not
160+
*/
161+
public Client(Cluster cluster, Configuration conf, boolean sslEnabled) {
162+
initialize(cluster, conf, sslEnabled, Optional.empty());
142163
}
143164

144165
/**
@@ -151,8 +172,23 @@ public Client(Cluster cluster, boolean sslEnabled) {
151172
*
152173
* @throws ClientTrustStoreInitializationException if the trust store file can not be loaded
153174
*/
154-
public Client(Cluster cluster, String trustStorePath,
155-
Optional<String> trustStorePassword, Optional<String> trustStoreType) {
175+
public Client(Cluster cluster, String trustStorePath, Optional<String> trustStorePassword,
176+
Optional<String> trustStoreType) {
177+
this(cluster, HBaseConfiguration.create(), trustStorePath, trustStorePassword, trustStoreType);
178+
}
179+
180+
/**
181+
* Constructor, allowing to define custom trust store (only for SSL connections)
182+
*
183+
* @param cluster the cluster definition
184+
* @param conf Configuration
185+
* @param trustStorePath custom trust store to use for SSL connections
186+
* @param trustStorePassword password to use for custom trust store
187+
* @param trustStoreType type of custom trust store
188+
* @throws ClientTrustStoreInitializationException if the trust store file can not be loaded
189+
*/
190+
public Client(Cluster cluster, Configuration conf, String trustStorePath,
191+
Optional<String> trustStorePassword, Optional<String> trustStoreType) {
156192

157193
char[] password = trustStorePassword.map(String::toCharArray).orElse(null);
158194
String type = trustStoreType.orElse(KeyStore.getDefaultType());
@@ -163,15 +199,15 @@ public Client(Cluster cluster, String trustStorePath,
163199
} catch (KeyStoreException e) {
164200
throw new ClientTrustStoreInitializationException("Invalid trust store type: " + type, e);
165201
}
166-
try (InputStream inputStream =
167-
new BufferedInputStream(Files.newInputStream(new File(trustStorePath).toPath()))) {
202+
try (InputStream inputStream = new BufferedInputStream(
203+
Files.newInputStream(new File(trustStorePath).toPath()))) {
168204
trustStore.load(inputStream, password);
169205
} catch (CertificateException | NoSuchAlgorithmException | IOException e) {
170206
throw new ClientTrustStoreInitializationException("Trust store load error: " + trustStorePath,
171207
e);
172208
}
173209

174-
initialize(cluster, true, Optional.of(trustStore));
210+
initialize(cluster, conf, true, Optional.of(trustStore));
175211
}
176212

177213
/**

0 commit comments

Comments
 (0)