Search in sources :

Example 1 with QueryInfo

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

the class WorkerResource method getThreads.

@ResourceSecurity(WEB_UI)
@GET
@Path("{nodeId}/task/{taskId}")
public Response getThreads(@PathParam("taskId") TaskId task, @PathParam("nodeId") String nodeId, @Context HttpServletRequest servletRequest, @Context HttpHeaders httpHeaders) {
    QueryId queryId = task.getQueryId();
    Optional<QueryInfo> queryInfo = dispatchManager.getFullQueryInfo(queryId);
    if (queryInfo.isPresent()) {
        try {
            checkCanViewQueryOwnedBy(sessionContextFactory.extractAuthorizedIdentity(servletRequest, httpHeaders, alternateHeaderName), queryInfo.get().getSession().toIdentity(), accessControl);
            return proxyJsonResponse(nodeId, "v1/task/" + task);
        } catch (AccessDeniedException e) {
            throw new ForbiddenException();
        }
    }
    return Response.status(Status.GONE).build();
}
Also used : AccessDeniedException(io.trino.spi.security.AccessDeniedException) ForbiddenException(javax.ws.rs.ForbiddenException) QueryId(io.trino.spi.QueryId) QueryInfo(io.trino.execution.QueryInfo) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ResourceSecurity(io.trino.server.security.ResourceSecurity)

Example 2 with QueryInfo

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

the class Query method getNextResult.

private synchronized QueryResults getNextResult(long token, UriInfo uriInfo, DataSize targetResultSize) {
    // check if the result for the token have already been created
    Optional<QueryResults> cachedResult = getCachedResult(token);
    if (cachedResult.isPresent()) {
        return cachedResult.get();
    }
    verify(nextToken.isPresent(), "Cannot generate next result when next token is not present");
    verify(token == nextToken.getAsLong(), "Expected token to equal next token");
    // get the query info before returning
    // force update if query manager is closed
    QueryInfo queryInfo = queryManager.getFullQueryInfo(queryId);
    queryManager.recordHeartbeat(queryId);
    closeExchangeClientIfNecessary(queryInfo);
    // fetch result data from exchange
    QueryResultRows resultRows = removePagesFromExchange(queryInfo, targetResultSize.toBytes());
    if ((queryInfo.getUpdateType() != null) && (updateCount == null)) {
        // grab the update count for non-queries
        Optional<Long> updatedRowsCount = resultRows.getUpdateCount();
        updateCount = updatedRowsCount.orElse(null);
    }
    // (3) cached query result needs client acknowledgement to discard
    if (queryInfo.getState() != FAILED && (!queryInfo.isFinalQueryInfo() || !exchangeClient.isFinished() || (queryInfo.getOutputStage().isPresent() && !resultRows.isEmpty()))) {
        nextToken = OptionalLong.of(token + 1);
    } else {
        nextToken = OptionalLong.empty();
        // the client is not coming back, make sure the exchangeClient is closed
        exchangeClient.close();
    }
    URI nextResultsUri = null;
    URI partialCancelUri = null;
    if (nextToken.isPresent()) {
        long nextToken = this.nextToken.getAsLong();
        nextResultsUri = createNextResultsUri(uriInfo, nextToken);
        partialCancelUri = findCancelableLeafStage(queryInfo).map(stage -> createPartialCancelUri(stage, uriInfo, nextToken)).orElse(null);
    }
    // update catalog, schema, and path
    setCatalog = queryInfo.getSetCatalog();
    setSchema = queryInfo.getSetSchema();
    setPath = queryInfo.getSetPath();
    // update setSessionProperties
    setSessionProperties = queryInfo.getSetSessionProperties();
    resetSessionProperties = queryInfo.getResetSessionProperties();
    // update setRoles
    setRoles = queryInfo.getSetRoles();
    // update preparedStatements
    addedPreparedStatements = queryInfo.getAddedPreparedStatements();
    deallocatedPreparedStatements = queryInfo.getDeallocatedPreparedStatements();
    // update startedTransactionId
    startedTransactionId = queryInfo.getStartedTransactionId();
    clearTransactionId = queryInfo.isClearTransactionId();
    // first time through, self is null
    QueryResults queryResults = new QueryResults(queryId.toString(), getQueryInfoUri(queryInfoUrl, queryId, uriInfo), partialCancelUri, nextResultsUri, resultRows.getColumns().orElse(null), // client excepts null that indicates "no data"
    resultRows.isEmpty() ? null : resultRows, toStatementStats(queryInfo), toQueryError(queryInfo, typeSerializationException), mappedCopy(queryInfo.getWarnings(), Query::toClientWarning), queryInfo.getUpdateType(), updateCount);
    // cache the new result
    lastToken = token;
    lastResult = queryResults;
    return queryResults;
}
Also used : OptionalLong(java.util.OptionalLong) QueryInfo(io.trino.execution.QueryInfo) URI(java.net.URI) QueryResults(io.trino.client.QueryResults)

Example 3 with QueryInfo

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

the class QueryResource method getQueryInfo.

@ResourceSecurity(AUTHENTICATED_USER)
@GET
@Path("{queryId}")
public Response getQueryInfo(@PathParam("queryId") QueryId queryId, @Context HttpServletRequest servletRequest, @Context HttpHeaders httpHeaders) {
    requireNonNull(queryId, "queryId is null");
    Optional<QueryInfo> queryInfo = dispatchManager.getFullQueryInfo(queryId);
    if (queryInfo.isEmpty()) {
        return Response.status(Status.GONE).build();
    }
    try {
        checkCanViewQueryOwnedBy(sessionContextFactory.extractAuthorizedIdentity(servletRequest, httpHeaders, alternateHeaderName), queryInfo.get().getSession().toIdentity(), accessControl);
        return Response.ok(queryInfo.get()).build();
    } catch (AccessDeniedException e) {
        throw new ForbiddenException();
    }
}
Also used : AccessDeniedException(io.trino.spi.security.AccessDeniedException) ForbiddenException(javax.ws.rs.ForbiddenException) QueryInfo(io.trino.execution.QueryInfo) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ResourceSecurity(io.trino.server.security.ResourceSecurity)

Example 4 with QueryInfo

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

the class TestQueryResource method testGetQueryInfoDispatchFailure.

@Test
public void testGetQueryInfoDispatchFailure() {
    String queryId = runToCompletion("SELECT");
    QueryInfo info = getQueryInfo(queryId);
    assertFalse(info.isScheduled());
    assertNotNull(info.getFailureInfo());
    assertEquals(info.getFailureInfo().getErrorCode(), SYNTAX_ERROR.toErrorCode());
    server.getAccessControl().deny(privilege("query", VIEW_QUERY));
    try {
        assertThatThrownBy(() -> getQueryInfo(queryId)).isInstanceOf(UnexpectedResponseException.class).matches(throwable -> ((UnexpectedResponseException) throwable).getStatusCode() == 403);
    } finally {
        server.getAccessControl().reset();
    }
}
Also used : UnexpectedResponseException(io.airlift.http.client.UnexpectedResponseException) QueryInfo(io.trino.execution.QueryInfo) Test(org.testng.annotations.Test)

Example 5 with QueryInfo

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

the class TestQueryResource method testGetQueryInfoExecutionFailure.

@Test
public void testGetQueryInfoExecutionFailure() {
    String queryId = runToCompletion("SELECT cast(rand() AS integer) / 0");
    QueryInfo info = getQueryInfo(queryId);
    assertTrue(info.isScheduled());
    assertNotNull(info.getFailureInfo());
    assertEquals(info.getFailureInfo().getErrorCode(), DIVISION_BY_ZERO.toErrorCode());
}
Also used : QueryInfo(io.trino.execution.QueryInfo) Test(org.testng.annotations.Test)

Aggregations

QueryInfo (io.trino.execution.QueryInfo)16 Test (org.testng.annotations.Test)6 BasicQueryInfo (io.trino.server.BasicQueryInfo)5 QueryManager (io.trino.execution.QueryManager)3 ResourceSecurity (io.trino.server.security.ResourceSecurity)3 QueryId (io.trino.spi.QueryId)3 AccessDeniedException (io.trino.spi.security.AccessDeniedException)3 ForbiddenException (javax.ws.rs.ForbiddenException)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 QueryStats (io.trino.execution.QueryStats)2 BlockBuilder (io.trino.spi.block.BlockBuilder)2 URI (java.net.URI)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 Request (io.airlift.http.client.Request)1 UnexpectedResponseException (io.airlift.http.client.UnexpectedResponseException)1 Duration (io.airlift.units.Duration)1 Session (io.trino.Session)1 QueryResults (io.trino.client.QueryResults)1 DispatchManager (io.trino.dispatcher.DispatchManager)1