Repository: hive Updated Branches: refs/heads/master ce695b5d4 -> 8613ef200
HIVE-16107: JDBC: HttpClient should retry one more time on NoHttpResponseException (Vaibhav Gumashta reviewed by Daniel Dai, Thejas Nair) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/8613ef20 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/8613ef20 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/8613ef20 Branch: refs/heads/master Commit: 8613ef200fb1e1372f41a225bd358f06e754f906 Parents: ce695b5 Author: Vaibhav Gumashta <vgumas...@hortonworks.com> Authored: Wed Mar 22 11:02:23 2017 -0700 Committer: Vaibhav Gumashta <vgumas...@hortonworks.com> Committed: Wed Mar 22 11:02:23 2017 -0700 ---------------------------------------------------------------------- .../apache/hive/jdbc/TestJdbcWithMiniHS2.java | 32 ++++++++++++++++++++ .../org/apache/hive/jdbc/HiveConnection.java | 24 +++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/8613ef20/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java ---------------------------------------------------------------------- diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index afe23f8..3780b4e 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -966,6 +966,38 @@ public class TestJdbcWithMiniHS2 { } /** + * Test for jdbc driver retry on NoHttpResponseException + * @throws Exception + */ + @Test + public void testHttpRetryOnServerIdleTimeout() throws Exception { + // Stop HiveServer2 + stopMiniHS2(); + HiveConf conf = new HiveConf(); + conf.set("hive.server2.transport.mode", "http"); + // Set server's idle timeout to a very low value + conf.set("hive.server2.thrift.http.max.idle.time", "5"); + startMiniHS2(conf); + String userName = System.getProperty("user.name"); + Connection conn = getConnection(miniHS2.getJdbcURL(testDbName), userName, "password"); + Statement stmt = conn.createStatement(); + stmt.execute("select from_unixtime(unix_timestamp())"); + // Sleep for longer than server's idletimeout and execute a query + TimeUnit.SECONDS.sleep(10); + try { + stmt.execute("select from_unixtime(unix_timestamp())"); + } catch (Exception e) { + fail("Not expecting exception: " + e); + } finally { + if (conn != null) { + conn.close(); + } + } + // Restore original state + restoreMiniHS2AndConnections(); + } + + /** * Tests that DataNucleus' NucleusContext.classLoaderResolverMap clears cached class objects * (& hence doesn't leak classloaders) on closing any session * http://git-wip-us.apache.org/repos/asf/hive/blob/8613ef20/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java ---------------------------------------------------------------------- diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index 1695c5d..fb18adb 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -41,6 +41,7 @@ import org.apache.hive.service.rpc.thrift.TSessionHandle; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; +import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.ServiceUnavailableRetryStrategy; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; @@ -386,9 +387,9 @@ public class HiveConnection implements java.sql.Connection { * Add an interceptor to pass username/password in the header. * In https mode, the entire information is encrypted */ - requestInterceptor = new HttpBasicAuthInterceptor(getUserName(), getPassword(), - cookieStore, cookieName, useSsl, - additionalHttpHeaders); + requestInterceptor = + new HttpBasicAuthInterceptor(getUserName(), getPassword(), cookieStore, cookieName, + useSsl, additionalHttpHeaders); } } // Configure http client for cookie based authentication @@ -421,6 +422,23 @@ public class HiveConnection implements java.sql.Connection { } else { httpClientBuilder = HttpClientBuilder.create(); } + // In case the server's idletimeout is set to a lower value, it might close it's side of + // connection. However we retry one more time on NoHttpResponseException + httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() { + @Override + public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { + if (executionCount > 1) { + LOG.info("Retry attempts to connect to server exceeded."); + return false; + } + if (exception instanceof org.apache.http.NoHttpResponseException) { + LOG.info("Could not connect to the server. Retrying one more time."); + return true; + } + return false; + } + }); + // Add the request interceptor to the client builder httpClientBuilder.addInterceptorFirst(requestInterceptor);