Search in sources :

Example 51 with QueryId

use of com.facebook.presto.spi.QueryId in project presto by prestodb.

the class QueryStateMachine method getQueryInfo.

public QueryInfo getQueryInfo(Optional<StageInfo> rootStage) {
    // Query state must be captured first in order to provide a
    // correct view of the query.  For example, building this
    // information, the query could finish, and the task states would
    // never be visible.
    QueryState state = queryState.get();
    ExecutionFailureInfo failureCause = null;
    ErrorCode errorCode = null;
    if (state == QueryState.FAILED) {
        failureCause = this.failureCause.get();
        if (failureCause != null) {
            errorCode = failureCause.getErrorCode();
        }
    }
    boolean completeInfo = getAllStages(rootStage).stream().allMatch(StageInfo::isFinalStageInfo);
    Optional<List<TaskId>> failedTasks;
    // Traversing all tasks is expensive, thus only construct failedTasks list when query finished.
    if (state.isDone()) {
        failedTasks = Optional.of(getAllStages(rootStage).stream().flatMap(stageInfo -> Streams.concat(ImmutableList.of(stageInfo.getLatestAttemptExecutionInfo()).stream(), stageInfo.getPreviousAttemptsExecutionInfos().stream())).flatMap(execution -> execution.getTasks().stream()).filter(taskInfo -> taskInfo.getTaskStatus().getState() == TaskState.FAILED).map(TaskInfo::getTaskId).collect(toImmutableList()));
    } else {
        failedTasks = Optional.empty();
    }
    List<StageId> runtimeOptimizedStages = getAllStages(rootStage).stream().filter(StageInfo::isRuntimeOptimized).map(StageInfo::getStageId).collect(toImmutableList());
    QueryStats queryStats = getQueryStats(rootStage);
    return new QueryInfo(queryId, session.toSessionRepresentation(), state, memoryPool.get().getId(), queryStats.isScheduled(), self, outputManager.getQueryOutputInfo().map(QueryOutputInfo::getColumnNames).orElse(ImmutableList.of()), query, expandedQuery.get(), preparedQuery, queryStats, Optional.ofNullable(setCatalog.get()), Optional.ofNullable(setSchema.get()), setSessionProperties, resetSessionProperties, setRoles, addedPreparedStatements, deallocatedPreparedStatements, Optional.ofNullable(startedTransactionId.get()), clearTransactionId.get(), updateType.get(), rootStage, failureCause, errorCode, warningCollector.getWarnings(), inputs.get(), output.get(), completeInfo, Optional.of(resourceGroup), queryType, failedTasks, runtimeOptimizedStages.isEmpty() ? Optional.empty() : Optional.of(runtimeOptimizedStages), addedSessionFunctions, removedSessionFunctions);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) FINISHING(com.facebook.presto.execution.QueryState.FINISHING) PLANNING(com.facebook.presto.execution.QueryState.PLANNING) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) EMPTY_STAGE_STATS(com.facebook.presto.execution.BasicStageExecutionStats.EMPTY_STAGE_STATS) GENERAL_POOL(com.facebook.presto.memory.LocalMemoryManager.GENERAL_POOL) TransactionId(com.facebook.presto.transaction.TransactionId) BasicQueryInfo(com.facebook.presto.server.BasicQueryInfo) URI(java.net.URI) StageInfo.getAllStages(com.facebook.presto.execution.StageInfo.getAllStages) BasicQueryStats(com.facebook.presto.server.BasicQueryStats) ImmutableSet(com.google.common.collect.ImmutableSet) Predicate(java.util.function.Predicate) DISPATCHING(com.facebook.presto.execution.QueryState.DISPATCHING) WAITING_FOR_RESOURCES(com.facebook.presto.execution.QueryState.WAITING_FOR_RESOURCES) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Failures.toFailure(com.facebook.presto.util.Failures.toFailure) ResourceGroupId(com.facebook.presto.spi.resourceGroups.ResourceGroupId) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Ticker(com.google.common.base.Ticker) Streams(com.google.common.collect.Streams) GuardedBy(javax.annotation.concurrent.GuardedBy) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) List(java.util.List) USER_CANCELED(com.facebook.presto.spi.StandardErrorCode.USER_CANCELED) Optional(java.util.Optional) QueryOutputInfo(com.facebook.presto.execution.QueryExecution.QueryOutputInfo) WAITING_FOR_PREREQUISITES(com.facebook.presto.execution.QueryState.WAITING_FOR_PREREQUISITES) Logger(com.facebook.airlift.log.Logger) QUEUED(com.facebook.presto.execution.QueryState.QUEUED) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ErrorCode(com.facebook.presto.spi.ErrorCode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSize.succinctBytes(io.airlift.units.DataSize.succinctBytes) PrestoException(com.facebook.presto.spi.PrestoException) VersionedMemoryPoolId(com.facebook.presto.memory.VersionedMemoryPoolId) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) TERMINAL_QUERY_STATES(com.facebook.presto.execution.QueryState.TERMINAL_QUERY_STATES) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) Objects.requireNonNull(java.util.Objects.requireNonNull) TransactionManager(com.facebook.presto.transaction.TransactionManager) Type(com.facebook.presto.common.type.Type) Nullable(javax.annotation.Nullable) SelectedRole(com.facebook.presto.spi.security.SelectedRole) RUNNING(com.facebook.presto.execution.QueryState.RUNNING) Executor(java.util.concurrent.Executor) Session(com.facebook.presto.Session) DateTime(org.joda.time.DateTime) FutureCallback(com.google.common.util.concurrent.FutureCallback) FINISHED(com.facebook.presto.execution.QueryState.FINISHED) TransactionInfo(com.facebook.presto.transaction.TransactionInfo) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Futures(com.google.common.util.concurrent.Futures) StateChangeListener(com.facebook.presto.execution.StateMachine.StateChangeListener) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) QueryType(com.facebook.presto.spi.resourceGroups.QueryType) QueryId(com.facebook.presto.spi.QueryId) STARTING(com.facebook.presto.execution.QueryState.STARTING) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) BasicQueryInfo(com.facebook.presto.server.BasicQueryInfo) BasicQueryStats(com.facebook.presto.server.BasicQueryStats) QueryOutputInfo(com.facebook.presto.execution.QueryExecution.QueryOutputInfo) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ErrorCode(com.facebook.presto.spi.ErrorCode)

Example 52 with QueryId

use of com.facebook.presto.spi.QueryId in project presto by prestodb.

the class QueryTracker method removeExpiredQueries.

/**
 * Remove completed queries after a waiting period
 */
private void removeExpiredQueries() {
    DateTime timeHorizon = DateTime.now().minus(minQueryExpireAge.toMillis());
    // we're willing to keep queries beyond timeHorizon as long as we have fewer than maxQueryHistory
    while (expirationQueue.size() > maxQueryHistory) {
        T query = expirationQueue.peek();
        if (query == null) {
            return;
        }
        // expirationQueue is FIFO based on query end time. Stop when we see the
        // first query that's too young to expire
        Optional<DateTime> endTime = query.getEndTime();
        if (!endTime.isPresent()) {
            // this shouldn't happen but it is better to be safe here
            continue;
        }
        if (endTime.get().isAfter(timeHorizon)) {
            return;
        }
        // only expire them if they are older than minQueryExpireAge. We need to keep them
        // around for a while in case clients come back asking for status
        QueryId queryId = query.getQueryId();
        log.debug("Remove query %s", queryId);
        queries.remove(queryId);
        expirationQueue.remove(query);
    }
}
Also used : EXCEEDED_TIME_LIMIT(com.facebook.presto.spi.StandardErrorCode.EXCEEDED_TIME_LIMIT) QueryId(com.facebook.presto.spi.QueryId) DateTime(org.joda.time.DateTime)

Example 53 with QueryId

use of com.facebook.presto.spi.QueryId in project presto by prestodb.

the class QueryStateInfoResource method getQueryStateInfo.

@GET
@Path("{queryId}")
@Produces(MediaType.APPLICATION_JSON)
public void getQueryStateInfo(@PathParam("queryId") String queryId, @HeaderParam(X_FORWARDED_PROTO) String xForwardedProto, @Context UriInfo uriInfo, @Context HttpServletRequest servletRequest, @Suspended AsyncResponse asyncResponse) throws WebApplicationException {
    try {
        QueryId queryID = new QueryId(queryId);
        if (resourceManagerEnabled && !dispatchManager.isQueryPresent(queryID)) {
            proxyQueryStateInfo(servletRequest, asyncResponse, xForwardedProto, uriInfo);
        } else {
            BasicQueryInfo queryInfo = dispatchManager.getQueryInfo(queryID);
            asyncResponse.resume(Response.ok(getQueryStateInfo(queryInfo, false, true, OptionalInt.empty())).build());
        }
    } catch (NoSuchElementException e) {
        asyncResponse.resume(Response.status(NOT_FOUND).build());
    }
}
Also used : QueryId(com.facebook.presto.spi.QueryId) NoSuchElementException(java.util.NoSuchElementException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 54 with QueryId

use of com.facebook.presto.spi.QueryId in project presto by prestodb.

the class TestServer method testQuery.

@Test
public void testQuery() {
    // start query
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_CATALOG, "catalog").setHeader(PRESTO_SCHEMA, "schema").setHeader(PRESTO_CLIENT_INFO, "{\"clientVersion\":\"testVersion\"}").addHeader(PRESTO_SESSION, QUERY_MAX_MEMORY + "=1GB").addHeader(PRESTO_SESSION, JOIN_DISTRIBUTION_TYPE + "=partitioned," + HASH_PARTITION_COUNT + " = 43").addHeader(PRESTO_PREPARED_STATEMENT, "foo=select * from bar").addHeader(PRESTO_SESSION_FUNCTION, format("%s=%s", urlEncode(SERIALIZED_SQL_FUNCTION_ID_ADD), urlEncode(SERIALIZED_SQL_FUNCTION_ADD))).build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC));
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC));
        if (queryResults.getData() != null) {
            data.addAll(queryResults.getData());
        }
    }
    assertNull(queryResults.getError());
    // get the query info
    BasicQueryInfo queryInfo = server.getQueryManager().getQueryInfo(new QueryId(queryResults.getId()));
    // verify session properties
    assertEquals(queryInfo.getSession().getSystemProperties(), ImmutableMap.builder().put(QUERY_MAX_MEMORY, "1GB").put(JOIN_DISTRIBUTION_TYPE, "partitioned").put(HASH_PARTITION_COUNT, "43").build());
    // verify client info in session
    assertEquals(queryInfo.getSession().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    // verify prepared statements
    assertEquals(queryInfo.getSession().getPreparedStatements(), ImmutableMap.builder().put("foo", "select * from bar").build());
    // verify session functions
    assertEquals(queryInfo.getSession().getSessionFunctions(), ImmutableMap.of(SQL_FUNCTION_ID_ADD, SQL_FUNCTION_ADD));
    // only the system catalog exists by default
    List<List<Object>> rows = data.build();
    assertEquals(rows, ImmutableList.of(ImmutableList.of("system")));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) QueryId(com.facebook.presto.spi.QueryId) Request(com.facebook.airlift.http.client.Request) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 55 with QueryId

use of com.facebook.presto.spi.QueryId in project presto by prestodb.

the class TestSessionPropertyDefaults method testApplyDefaultProperties.

@Test
public void testApplyDefaultProperties() {
    SessionPropertyDefaults sessionPropertyDefaults = new SessionPropertyDefaults(TEST_NODE_INFO);
    SessionPropertyConfigurationManagerFactory factory = new TestingSessionPropertyConfigurationManagerFactory(new SystemSessionPropertyConfiguration(ImmutableMap.<String, String>builder().put(QUERY_MAX_MEMORY, "override").put("system_default", "system_default").build(), ImmutableMap.of("override", "overridden")), ImmutableMap.of("testCatalog", ImmutableMap.<String, String>builder().put("explicit_set", "override").put("catalog_default", "catalog_default").build()));
    sessionPropertyDefaults.addConfigurationManagerFactory(factory);
    sessionPropertyDefaults.setConfigurationManager(factory.getName(), ImmutableMap.of());
    Session session = Session.builder(new SessionPropertyManager()).setQueryId(new QueryId("test_query_id")).setIdentity(new Identity("testUser", Optional.empty())).setSystemProperty(QUERY_MAX_MEMORY, "1GB").setSystemProperty(JOIN_DISTRIBUTION_TYPE, "partitioned").setSystemProperty(HASH_PARTITION_COUNT, "43").setSystemProperty("override", "should be overridden").setCatalogSessionProperty("testCatalog", "explicit_set", "explicit_set").build();
    assertEquals(session.getSystemProperties(), ImmutableMap.<String, String>builder().put(QUERY_MAX_MEMORY, "1GB").put(JOIN_DISTRIBUTION_TYPE, "partitioned").put(HASH_PARTITION_COUNT, "43").put("override", "should be overridden").build());
    assertEquals(session.getUnprocessedCatalogProperties(), ImmutableMap.of("testCatalog", ImmutableMap.<String, String>builder().put("explicit_set", "explicit_set").build()));
    session = sessionPropertyDefaults.newSessionWithDefaultProperties(session, Optional.empty(), Optional.of(TEST_RESOURCE_GROUP_ID));
    assertEquals(session.getSystemProperties(), ImmutableMap.<String, String>builder().put(QUERY_MAX_MEMORY, "1GB").put(JOIN_DISTRIBUTION_TYPE, "partitioned").put(HASH_PARTITION_COUNT, "43").put("system_default", "system_default").put("override", "overridden").build());
    assertEquals(session.getUnprocessedCatalogProperties(), ImmutableMap.of("testCatalog", ImmutableMap.<String, String>builder().put("explicit_set", "explicit_set").put("catalog_default", "catalog_default").build()));
}
Also used : SystemSessionPropertyConfiguration(com.facebook.presto.spi.session.SessionPropertyConfigurationManager.SystemSessionPropertyConfiguration) QueryId(com.facebook.presto.spi.QueryId) TestingSessionPropertyConfigurationManagerFactory(com.facebook.presto.spi.session.TestingSessionPropertyConfigurationManagerFactory) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) SessionPropertyConfigurationManagerFactory(com.facebook.presto.spi.session.SessionPropertyConfigurationManagerFactory) TestingSessionPropertyConfigurationManagerFactory(com.facebook.presto.spi.session.TestingSessionPropertyConfigurationManagerFactory) Identity(com.facebook.presto.spi.security.Identity) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Aggregations

QueryId (com.facebook.presto.spi.QueryId)121 Test (org.testng.annotations.Test)79 DistributedQueryRunner (com.facebook.presto.tests.DistributedQueryRunner)19 DataSize (io.airlift.units.DataSize)18 MemoryPoolId (com.facebook.presto.spi.memory.MemoryPoolId)17 Session (com.facebook.presto.Session)16 BasicQueryInfo (com.facebook.presto.server.BasicQueryInfo)16 QueryManager (com.facebook.presto.execution.QueryManager)15 Identity (com.facebook.presto.spi.security.Identity)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 ArrayList (java.util.ArrayList)11 ResourceGroupManagerPlugin (com.facebook.presto.resourceGroups.ResourceGroupManagerPlugin)10 PrestoException (com.facebook.presto.spi.PrestoException)10 ConnectorIdentity (com.facebook.presto.spi.security.ConnectorIdentity)10 List (java.util.List)10 SqlTask.createSqlTask (com.facebook.presto.execution.SqlTask.createSqlTask)9 MemoryPool (com.facebook.presto.memory.MemoryPool)9 ImmutableList (com.google.common.collect.ImmutableList)9 ResourceGroupId (com.facebook.presto.spi.resourceGroups.ResourceGroupId)8 DispatchManager (com.facebook.presto.dispatcher.DispatchManager)7