static boolean isExceptionDueToSocketTimeout(Throwable err) { if (err == null) { return false; } if (err instanceof SocketTimeoutException) { return true; } else { return isExceptionDueToSocketTimeout(err.getCause()); } }
static boolean isExceptionDueToSocketTimeout(Throwable err) { if (err == null) { return false; } if (err instanceof SocketTimeoutException) { return true; } else { return isExceptionDueToSocketTimeout(err.getCause()); } }
LensQuery getQueryWithRetryOnTimeout(QueryHandle handle) { while (true) { try { return getQuery(handle); } catch (Exception e) { if (isExceptionDueToSocketTimeout(e)) { log.warn("Could not get query status. Encountered socket timeout. Retrying..."); continue; } else { throw e; } } } }
LensQuery getQueryWithRetryOnTimeout(QueryHandle handle) { while (true) { try { return getQuery(handle); } catch (Exception e) { if (isExceptionDueToSocketTimeout(e)) { log.warn("Could not get query status. Encountered socket timeout. Retrying..."); continue; } else { throw e; } } } }
@Test public void testTimeout() throws LensAPIException { LensClientConfig config = createLensClientConfigWithServerUrl(); LensClient lensClient = new LensClient(config); assertTrue(lensClient.setDatabase(TEST_DB)); try { // Setting very small timeout. Expecting timeouts after this // Note: Timeout values can be changed even after LensClient has been created. config.setInt(LensClientConfig.READ_TIMEOUT_MILLIS, 200); lensClient.executeQueryWithTimeout("cube select id,name from test_dim", "test1", 100000, new LensConf()); fail("Read Timeout was expected"); } catch (Exception e) { if (!(isExceptionDueToSocketTimeout(e))) { log.error("Unexpected Exception", e); fail("SocketTimeoutException was excepted as part of Read Timeout"); } else { log.debug("Expected Exception", e); } } //Setting back default timeout. Not expecting timeouts after this config.setInt(LensClientConfig.READ_TIMEOUT_MILLIS, LensClientConfig.DEFAULT_READ_TIMEOUT_MILLIS); QueryHandleWithResultSet result = lensClient.executeQueryWithTimeout("cube select id,name from test_dim", "test2", 100000, new LensConf()); assertTrue(result.getStatus().successful()); lensClient.closeConnection(); }
@Test public void testWaitForQueryToCompleteWithAndWithoutRetryOnTimeOut() throws LensAPIException { LensClientConfig config = createLensClientConfigWithServerUrl(); config.set(ENABLE_SLEEP_FOR_GET_QUERY_OP, "true"); config.setInt(LensClientConfig.READ_TIMEOUT_MILLIS, 3000); try (LensClient lensClient = new LensClient(config)) { assertTrue(lensClient.setDatabase(TEST_DB)); //Test waitForQueryToComplete without retry on timeout QueryHandle handle = lensClient.executeQueryAsynch("cube select id,name from test_dim", "test3", new LensConf()); try { lensClient.getStatement().waitForQueryToComplete(handle, false); fail("SocketTimeoutException was expected"); } catch (Exception e) { if (!isExceptionDueToSocketTimeout(e)) { fail("SocketTimeoutException was excepted as part of Read Timeout"); } } //Test waitForQueryToComplete with Retry on timeout handle = lensClient.executeQueryAsynch("cube select id,name from test_dim", "test3", new LensConf()); lensClient.getStatement().waitForQueryToComplete(handle); LensQuery query = lensClient.getQueryDetails(handle); assertTrue(query.getStatus().successful()); } }