use of java.sql.SQLTimeoutException in project drill by apache.
the class PreparedStatementTest method testServerTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out because of lack of timely server response
*/
@Ignore("Pause Injection appears broken for PreparedStatement")
@Test(expected = SqlTimeoutException.class)
public void testServerTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (2sec)
int timeoutDuration = 2;
// Server will be paused marginally longer than the test timeout
long serverPause = timeoutDuration + 2;
// Additional time for JDBC timeout and server pauses to complete
int cleanupPause = 3;
// Simulate a lack of timely server response by injecting a pause in the Screen operator's sending-data RPC
final String controls = Controls.newBuilder().addTimedPause(ScreenCreator.class, "sending-data", 0, TimeUnit.SECONDS.toMillis(serverPause)).build();
// Fetching an exclusive connection since injected pause affects all sessions on the connection
try (Connection exclusiveConnection = new Driver().connect("jdbc:drill:zk=local", null)) {
try (Statement stmt = exclusiveConnection.createStatement()) {
assertThat(stmt.execute(String.format("ALTER session SET `%s` = '%s'", ExecConstants.DRILLBIT_CONTROL_INJECTIONS, controls)), equalTo(true));
}
try (PreparedStatement pStmt = exclusiveConnection.prepareStatement(SYS_RANDOM_SQL)) {
pStmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", pStmt.getQueryTimeout());
// Executing a prepared statement with the paused server. Expecting timeout to occur here
ResultSet rs = pStmt.executeQuery();
// Fetch rows
while (rs.next()) {
rs.getBytes(1);
}
} catch (SQLTimeoutException sqlEx) {
logger.info("SQLTimeoutException thrown: {}", sqlEx.getMessage());
throw (SqlTimeoutException) sqlEx;
} finally {
// Pause briefly to wait for server to unblock
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(cleanupPause));
} catch (InterruptedException e) {
/*DoNothing*/
}
}
}
}
use of java.sql.SQLTimeoutException in project drill by apache.
the class PreparedStatementTest method testClientTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out
*/
@Test
public void testClientTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (3sec)
int timeoutDuration = 3;
int rowsCounted = 0;
try (PreparedStatement stmt = connection.prepareStatement(SYS_RANDOM_SQL)) {
stmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", stmt.getQueryTimeout());
ResultSet rs = stmt.executeQuery();
// Fetch each row and pause (simulate a slow client)
try {
while (rs.next()) {
rs.getString(1);
rowsCounted++;
// Pause briefly (a second beyond the timeout) before attempting to fetch rows
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutDuration + 1));
} catch (InterruptedException e) {
/*DoNothing*/
}
logger.info("Paused for {} seconds", (timeoutDuration + 1));
}
} catch (SQLTimeoutException sqlEx) {
logger.info("Counted " + rowsCounted + " rows before hitting timeout");
// Successfully return
return;
}
}
// Throw an exception to indicate that we shouldn't have reached this point
throw new Exception("Failed to trigger timeout of " + timeoutDuration + " sec");
}
use of java.sql.SQLTimeoutException in project drill by apache.
the class StatementTest method testClientTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out because of lack of timely client response
*/
@Test
public void testClientTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (3sec)
int timeoutDuration = 3;
int rowsCounted = 0;
try (Statement stmt = connection.createStatement()) {
stmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", stmt.getQueryTimeout());
ResultSet rs = stmt.executeQuery(SYS_RANDOM_SQL);
// Fetch each row and pause (simulate a slow client)
try {
while (rs.next()) {
rs.getString(1);
rowsCounted++;
// Pause briefly (a second beyond the timeout) before attempting to fetch rows
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutDuration + 1));
} catch (InterruptedException e) {
/*DoNothing*/
}
logger.info("Paused for {} seconds", (timeoutDuration + 1));
}
} catch (SQLTimeoutException sqlEx) {
logger.info("Counted " + rowsCounted + " rows before hitting timeout");
// Successfully return
return;
}
}
// Throw an exception to indicate that we shouldn't have reached this point
throw new Exception("Failed to trigger timeout of " + timeoutDuration + " sec");
}
use of java.sql.SQLTimeoutException in project drill by apache.
the class StatementTest method testServerTriggeredQueryTimeout.
/**
* Test setting timeout for a query that actually times out because of lack of timely server response
*/
@Test(expected = SqlTimeoutException.class)
public void testServerTriggeredQueryTimeout() throws Exception {
// Setting to a very low value (2sec)
int timeoutDuration = 2;
// Server will be paused marginally longer than the test timeout
long serverPause = timeoutDuration + 2;
// Additional time for JDBC timeout and server pauses to complete
int cleanupPause = 3;
// Simulate a lack of timely server response by injecting a pause in the Screen operator's sending-data RPC
final String controls = Controls.newBuilder().addTimedPause(ScreenCreator.class, "sending-data", 0, TimeUnit.SECONDS.toMillis(serverPause)).build();
// Fetching an exclusive connection since injected pause affects all sessions on the connection
try (Connection exclusiveConnection = new Driver().connect("jdbc:drill:zk=local", null)) {
try (Statement stmt = exclusiveConnection.createStatement()) {
assertThat(stmt.execute(String.format("ALTER session SET `%s` = '%s'", ExecConstants.DRILLBIT_CONTROL_INJECTIONS, controls)), equalTo(true));
}
try (Statement stmt = exclusiveConnection.createStatement()) {
stmt.setQueryTimeout(timeoutDuration);
logger.info("Set a timeout of {} seconds", stmt.getQueryTimeout());
// Executing a query with the paused server. Expecting timeout to occur here
ResultSet rs = stmt.executeQuery(SYS_VERSION_SQL);
// Fetch rows
while (rs.next()) {
rs.getBytes(1);
}
} catch (SQLTimeoutException sqlEx) {
logger.info("SQLTimeoutException thrown: {}", sqlEx.getMessage());
throw (SqlTimeoutException) sqlEx;
} finally {
// Pause briefly to wait for server to unblock
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(cleanupPause));
} catch (InterruptedException e) {
/*DoNothing*/
}
}
}
}
use of java.sql.SQLTimeoutException in project hive by apache.
the class TestJdbcDriver2 method testQueryTimeout.
@Test
public void testQueryTimeout() throws Exception {
String udfName = SleepMsUDF.class.getName();
Statement stmt1 = con.createStatement();
stmt1.execute("create temporary function sleepMsUDF as '" + udfName + "'");
stmt1.close();
Statement stmt = con.createStatement();
// Test a query where timeout kicks in
// Set query timeout to 1 second
stmt.setQueryTimeout(1);
System.err.println("Executing query: ");
try {
// The test table has 500 rows, so total query time should be ~ 2500ms
stmt.executeQuery("select sleepMsUDF(t1.under_col, 5) as u0, t1.under_col as u1, " + "t2.under_col as u2 from " + tableName + " t1 join " + tableName + " t2 on t1.under_col = t2.under_col");
fail("Expecting SQLTimeoutException");
} catch (SQLTimeoutException e) {
assertNotNull(e);
System.err.println(e.toString());
} catch (SQLException e) {
fail("Expecting SQLTimeoutException, but got SQLException: " + e);
e.printStackTrace();
}
// Test a query where timeout does not kick in. Set it to 5s;
// show tables should be faster than that
stmt.setQueryTimeout(5);
try {
stmt.executeQuery("show tables");
} catch (SQLException e) {
fail("Unexpected SQLException: " + e);
e.printStackTrace();
}
stmt.close();
}
Aggregations