Search in sources :

Example 1 with QueryState

use of io.trino.execution.QueryState in project trino by trinodb.

the class UiQueryResource method getAllQueryInfo.

@ResourceSecurity(WEB_UI)
@GET
public List<TrimmedBasicQueryInfo> getAllQueryInfo(@QueryParam("state") String stateFilter, @Context HttpServletRequest servletRequest, @Context HttpHeaders httpHeaders) {
    QueryState expectedState = stateFilter == null ? null : QueryState.valueOf(stateFilter.toUpperCase(Locale.ENGLISH));
    List<BasicQueryInfo> queries = dispatchManager.getQueries();
    queries = filterQueries(sessionContextFactory.extractAuthorizedIdentity(servletRequest, httpHeaders, alternateHeaderName), queries, accessControl);
    ImmutableList.Builder<TrimmedBasicQueryInfo> builder = ImmutableList.builder();
    for (BasicQueryInfo queryInfo : queries) {
        if (stateFilter == null || queryInfo.getState() == expectedState) {
            builder.add(new TrimmedBasicQueryInfo(queryInfo));
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) BasicQueryInfo(io.trino.server.BasicQueryInfo) QueryState(io.trino.execution.QueryState) GET(javax.ws.rs.GET) ResourceSecurity(io.trino.server.security.ResourceSecurity)

Example 2 with QueryState

use of io.trino.execution.QueryState in project trino by trinodb.

the class TestTrinoDriver method testUpdateCancelExplicit.

@Test(timeOut = 10000)
public void testUpdateCancelExplicit() throws Exception {
    CountDownLatch queryFinished = new CountDownLatch(1);
    AtomicReference<Throwable> queryFailure = new AtomicReference<>();
    String queryUuid = "/* " + UUID.randomUUID().toString() + " */";
    try (Connection connection = createConnection("blackhole", "blackhole");
        Statement statement = connection.createStatement()) {
        // execute the slow update on another thread
        executorService.execute(() -> {
            try {
                statement.executeUpdate("CREATE TABLE test_cancel_create AS SELECT * FROM slow_test_table " + queryUuid);
            } catch (SQLException t) {
                queryFailure.set(t);
            } finally {
                queryFinished.countDown();
            }
        });
        // start query and make sure it is not finished
        while (true) {
            Optional<QueryState> state = findQueryState(queryUuid);
            if (state.isPresent()) {
                assertFalse(state.get().isDone());
                break;
            }
            MILLISECONDS.sleep(50);
        }
        // cancel the query from this test thread
        statement.cancel();
        // make sure the query was aborted
        queryFinished.await(10, SECONDS);
        assertNotNull(queryFailure.get());
        assertEquals(findQueryState(queryUuid), Optional.of(FAILED));
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) QueryState(io.trino.execution.QueryState) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 3 with QueryState

use of io.trino.execution.QueryState in project trino by trinodb.

the class QueryResource method getAllQueryInfo.

@ResourceSecurity(AUTHENTICATED_USER)
@GET
public List<BasicQueryInfo> getAllQueryInfo(@QueryParam("state") String stateFilter, @Context HttpServletRequest servletRequest, @Context HttpHeaders httpHeaders) {
    QueryState expectedState = stateFilter == null ? null : QueryState.valueOf(stateFilter.toUpperCase(Locale.ENGLISH));
    List<BasicQueryInfo> queries = dispatchManager.getQueries();
    queries = filterQueries(sessionContextFactory.extractAuthorizedIdentity(servletRequest, httpHeaders, alternateHeaderName), queries, accessControl);
    ImmutableList.Builder<BasicQueryInfo> builder = ImmutableList.builder();
    for (BasicQueryInfo queryInfo : queries) {
        if (stateFilter == null || queryInfo.getState() == expectedState) {
            builder.add(queryInfo);
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) QueryState(io.trino.execution.QueryState) GET(javax.ws.rs.GET) ResourceSecurity(io.trino.server.security.ResourceSecurity)

Example 4 with QueryState

use of io.trino.execution.QueryState in project trino by trinodb.

the class TestTrinoDriver method findQueryState.

private Optional<QueryState> findQueryState(String text) throws SQLException {
    String sql = format("SELECT state FROM system.runtime.queries WHERE regexp_like(query, '%s$') /* */", Pattern.quote(text));
    try (Connection connection = createConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql)) {
        if (!resultSet.next()) {
            return Optional.empty();
        }
        QueryState state = QueryState.valueOf(requireNonNull(resultSet.getString(1)));
        assertFalse(resultSet.next(), "Found multiple queries");
        return Optional.of(state);
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) QueryState(io.trino.execution.QueryState)

Example 5 with QueryState

use of io.trino.execution.QueryState in project trino by trinodb.

the class Query method toQueryError.

private static QueryError toQueryError(QueryInfo queryInfo, Optional<Throwable> exception) {
    QueryState state = queryInfo.getState();
    if (state != FAILED && exception.isEmpty()) {
        return null;
    }
    ExecutionFailureInfo executionFailure;
    if (queryInfo.getFailureInfo() != null) {
        executionFailure = queryInfo.getFailureInfo();
    } else if (exception.isPresent()) {
        executionFailure = toFailure(exception.get());
    } else {
        log.warn("Query %s in state %s has no failure info", queryInfo.getQueryId(), state);
        executionFailure = toFailure(new RuntimeException(format("Query is %s (reason unknown)", state)));
    }
    FailureInfo failure = executionFailure.toFailureInfo();
    ErrorCode errorCode;
    if (queryInfo.getErrorCode() != null) {
        errorCode = queryInfo.getErrorCode();
    } else if (exception.isPresent()) {
        errorCode = SERIALIZATION_ERROR.toErrorCode();
    } else {
        errorCode = GENERIC_INTERNAL_ERROR.toErrorCode();
        log.warn("Failed query %s has no error code", queryInfo.getQueryId());
    }
    return new QueryError(firstNonNull(failure.getMessage(), "Internal error"), null, errorCode.getCode(), errorCode.getName(), errorCode.getType().toString(), failure.getErrorLocation(), failure);
}
Also used : ExecutionFailureInfo(io.trino.execution.ExecutionFailureInfo) FailureInfo(io.trino.client.FailureInfo) QueryState(io.trino.execution.QueryState) ErrorCode(io.trino.spi.ErrorCode) QueryError(io.trino.client.QueryError) ExecutionFailureInfo(io.trino.execution.ExecutionFailureInfo)

Aggregations

QueryState (io.trino.execution.QueryState)6 ImmutableList (com.google.common.collect.ImmutableList)2 BasicQueryInfo (io.trino.server.BasicQueryInfo)2 ResourceSecurity (io.trino.server.security.ResourceSecurity)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 GET (javax.ws.rs.GET)2 Test (org.testng.annotations.Test)2 FailureInfo (io.trino.client.FailureInfo)1 QueryError (io.trino.client.QueryError)1 DispatchManager (io.trino.dispatcher.DispatchManager)1 ExecutionFailureInfo (io.trino.execution.ExecutionFailureInfo)1 QueryInfo (io.trino.execution.QueryInfo)1 QueryManager (io.trino.execution.QueryManager)1 TestQueryRunnerUtil.waitForQueryState (io.trino.execution.TestQueryRunnerUtil.waitForQueryState)1 ErrorCode (io.trino.spi.ErrorCode)1 QueryId (io.trino.spi.QueryId)1 TrinoException (io.trino.spi.TrinoException)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1