Search in sources :

Example 21 with SQLTimeoutException

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*/
            }
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ScreenCreator(org.apache.drill.exec.physical.impl.ScreenCreator) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) PreparedStatement(java.sql.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Ignore(org.junit.Ignore) Test(org.junit.Test) JdbcTest(org.apache.drill.categories.JdbcTest)

Example 22 with SQLTimeoutException

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");
}
Also used : ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) PreparedStatement(java.sql.PreparedStatement) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) Test(org.junit.Test) JdbcTest(org.apache.drill.categories.JdbcTest)

Example 23 with SQLTimeoutException

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");
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) Test(org.junit.Test) JdbcTest(org.apache.drill.categories.JdbcTest)

Example 24 with SQLTimeoutException

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*/
            }
        }
    }
}
Also used : Statement(java.sql.Statement) ScreenCreator(org.apache.drill.exec.physical.impl.ScreenCreator) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test) JdbcTest(org.apache.drill.categories.JdbcTest)

Example 25 with SQLTimeoutException

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();
}
Also used : SQLException(java.sql.SQLException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLTimeoutException(java.sql.SQLTimeoutException) String(java.lang.String) Test(org.junit.Test)

Aggregations

SQLTimeoutException (java.sql.SQLTimeoutException)53 SQLException (java.sql.SQLException)36 ResultSet (java.sql.ResultSet)20 Statement (java.sql.Statement)17 Test (org.testng.annotations.Test)15 Test (org.junit.Test)14 BaseTest (util.BaseTest)14 Connection (java.sql.Connection)12 PreparedStatement (java.sql.PreparedStatement)10 JdbcTest (org.apache.drill.categories.JdbcTest)8 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)7 ArrayList (java.util.ArrayList)7 HadoopException (org.apache.ranger.plugin.client.HadoopException)6 ScreenCreator (org.apache.drill.exec.physical.impl.ScreenCreator)4 MessageEvent (org.cerberus.engine.entity.MessageEvent)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Ignore (org.junit.Ignore)3 IOException (java.io.IOException)2 BatchUpdateException (java.sql.BatchUpdateException)2 CallableStatement (java.sql.CallableStatement)2