Search in sources :

Example 31 with QueryId

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

the class SqlQueryManager 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) {
        QueryInfo queryInfo = expirationQueue.peek().getQueryInfo();
        // first query that's too young to expire
        if (queryInfo.getQueryStats().getEndTime().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 = queryInfo.getQueryId();
        log.debug("Remove query %s", queryId);
        queries.remove(queryId);
        expirationQueue.remove();
    }
}
Also used : QueryId(com.facebook.presto.spi.QueryId) DateTime(org.joda.time.DateTime)

Example 32 with QueryId

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

the class SqlQueryManager method createQuery.

@Override
public QueryInfo createQuery(SessionSupplier sessionSupplier, String query) {
    requireNonNull(sessionSupplier, "sessionFactory is null");
    requireNonNull(query, "query is null");
    checkArgument(!query.isEmpty(), "query must not be empty string");
    QueryId queryId = queryIdGenerator.createNextQueryId();
    Session session = null;
    QueryExecution queryExecution;
    Statement statement;
    try {
        session = sessionSupplier.createSession(queryId, transactionManager, accessControl, sessionPropertyManager);
        if (query.length() > maxQueryLength) {
            int queryLength = query.length();
            query = query.substring(0, maxQueryLength);
            throw new PrestoException(QUERY_TEXT_TOO_LARGE, format("Query text length (%s) exceeds the maximum length (%s)", queryLength, maxQueryLength));
        }
        Statement wrappedStatement = sqlParser.createStatement(query);
        statement = unwrapExecuteStatement(wrappedStatement, sqlParser, session);
        List<Expression> parameters = wrappedStatement instanceof Execute ? ((Execute) wrappedStatement).getParameters() : emptyList();
        validateParameters(statement, parameters);
        QueryExecutionFactory<?> queryExecutionFactory = executionFactories.get(statement.getClass());
        if (queryExecutionFactory == null) {
            throw new PrestoException(NOT_SUPPORTED, "Unsupported statement type: " + statement.getClass().getSimpleName());
        }
        if (statement instanceof Explain && ((Explain) statement).isAnalyze()) {
            Statement innerStatement = ((Explain) statement).getStatement();
            if (!(executionFactories.get(innerStatement.getClass()) instanceof SqlQueryExecutionFactory)) {
                throw new PrestoException(NOT_SUPPORTED, "EXPLAIN ANALYZE only supported for statements that are queries");
            }
        }
        queryExecution = queryExecutionFactory.createQueryExecution(queryId, query, session, statement, parameters);
    } catch (ParsingException | PrestoException | SemanticException e) {
        // This is intentionally not a method, since after the state change listener is registered
        // it's not safe to do any of this, and we had bugs before where people reused this code in a method
        URI self = locationFactory.createQueryLocation(queryId);
        // if session creation failed, create a minimal session object
        if (session == null) {
            session = Session.builder(new SessionPropertyManager()).setQueryId(queryId).setIdentity(sessionSupplier.getIdentity()).build();
        }
        Optional<ResourceGroupId> resourceGroup = Optional.empty();
        if (e instanceof QueryQueueFullException) {
            resourceGroup = Optional.of(((QueryQueueFullException) e).getResourceGroup());
        }
        QueryExecution execution = new FailedQueryExecution(queryId, query, resourceGroup, session, self, transactionManager, queryExecutor, metadata, e);
        QueryInfo queryInfo = null;
        try {
            queries.put(queryId, execution);
            queryInfo = execution.getQueryInfo();
            queryMonitor.queryCreatedEvent(queryInfo);
            queryMonitor.queryCompletedEvent(queryInfo);
            stats.queryFinished(queryInfo);
        } finally {
            // execution MUST be added to the expiration queue or there will be a leak
            expirationQueue.add(execution);
        }
        return queryInfo;
    }
    QueryInfo queryInfo = queryExecution.getQueryInfo();
    queryMonitor.queryCreatedEvent(queryInfo);
    queryExecution.addFinalQueryInfoListener(finalQueryInfo -> {
        try {
            QueryInfo info = queryExecution.getQueryInfo();
            stats.queryFinished(info);
            queryMonitor.queryCompletedEvent(info);
        } finally {
            expirationQueue.add(queryExecution);
        }
    });
    addStatsListener(queryExecution);
    queries.put(queryId, queryExecution);
    // start the query in the background
    queueManager.submit(statement, queryExecution, queryExecutor);
    return queryInfo;
}
Also used : Execute(com.facebook.presto.sql.tree.Execute) Optional(java.util.Optional) Statement(com.facebook.presto.sql.tree.Statement) QueryId(com.facebook.presto.spi.QueryId) Explain(com.facebook.presto.sql.tree.Explain) QueryQueueFullException(com.facebook.presto.execution.resourceGroups.QueryQueueFullException) PrestoException(com.facebook.presto.spi.PrestoException) URI(java.net.URI) SqlQueryExecutionFactory(com.facebook.presto.execution.SqlQueryExecution.SqlQueryExecutionFactory) Expression(com.facebook.presto.sql.tree.Expression) ParsingException(com.facebook.presto.sql.parser.ParsingException) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 33 with QueryId

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

the class TestSourcePartitionedScheduler method createSqlStageExecution.

private SqlStageExecution createSqlStageExecution(StageExecutionPlan tableScanPlan, NodeTaskMap nodeTaskMap) {
    StageId stageId = new StageId(new QueryId("query"), 0);
    SqlStageExecution stage = new SqlStageExecution(stageId, locationFactory.createStageLocation(stageId), tableScanPlan.getFragment(), new MockRemoteTaskFactory(executor), TEST_SESSION, true, nodeTaskMap, executor, new SplitSchedulerStats());
    stage.setOutputBuffers(createInitialEmptyOutputBuffers(PARTITIONED).withBuffer(OUT, 0).withNoMoreBufferIds());
    return stage;
}
Also used : StageId(com.facebook.presto.execution.StageId) QueryId(com.facebook.presto.spi.QueryId) SqlStageExecution(com.facebook.presto.execution.SqlStageExecution) MockRemoteTaskFactory(com.facebook.presto.execution.MockRemoteTaskFactory)

Example 34 with QueryId

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

the class TestStartTransactionTask method testStartTransactionIdleExpiration.

@Test
public void testStartTransactionIdleExpiration() throws Exception {
    Session session = sessionBuilder().setClientTransactionSupport().build();
    TransactionManager transactionManager = TransactionManager.create(new TransactionManagerConfig().setIdleTimeout(// Fast idle timeout
    new Duration(1, TimeUnit.MICROSECONDS)).setIdleCheckInterval(new Duration(10, TimeUnit.MILLISECONDS)), scheduledExecutor, new CatalogManager(), executor);
    AccessControl accessControl = new AccessControlManager(transactionManager);
    QueryStateMachine stateMachine = QueryStateMachine.begin(new QueryId("query"), "START TRANSACTION", session, URI.create("fake://uri"), true, transactionManager, accessControl, executor, metadata);
    assertFalse(stateMachine.getSession().getTransactionId().isPresent());
    getFutureValue(new StartTransactionTask().execute(new StartTransaction(ImmutableList.of()), transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList()));
    assertFalse(stateMachine.getQueryInfoWithoutDetails().isClearTransactionId());
    assertTrue(stateMachine.getQueryInfoWithoutDetails().getStartedTransactionId().isPresent());
    long start = System.nanoTime();
    while (!transactionManager.getAllTransactionInfos().isEmpty()) {
        if (Duration.nanosSince(start).toMillis() > 10_000) {
            fail("Transaction did not expire in the allotted time");
        }
        TimeUnit.MILLISECONDS.sleep(10);
    }
}
Also used : AccessControlManager(com.facebook.presto.security.AccessControlManager) QueryId(com.facebook.presto.spi.QueryId) Duration(io.airlift.units.Duration) CatalogManager(com.facebook.presto.metadata.CatalogManager) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) AccessControl(com.facebook.presto.security.AccessControl) TransactionManagerConfig(com.facebook.presto.transaction.TransactionManagerConfig) TransactionManager(com.facebook.presto.transaction.TransactionManager) TransactionManager.createTestTransactionManager(com.facebook.presto.transaction.TransactionManager.createTestTransactionManager) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) StartTransaction(com.facebook.presto.sql.tree.StartTransaction) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Example 35 with QueryId

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

the class TestStartTransactionTask method testStartTransactionTooManyAccessModes.

@Test
public void testStartTransactionTooManyAccessModes() throws Exception {
    Session session = sessionBuilder().setClientTransactionSupport().build();
    TransactionManager transactionManager = createTestTransactionManager();
    AccessControl accessControl = new AccessControlManager(transactionManager);
    QueryStateMachine stateMachine = QueryStateMachine.begin(new QueryId("query"), "START TRANSACTION", session, URI.create("fake://uri"), true, transactionManager, accessControl, executor, metadata);
    assertFalse(stateMachine.getSession().getTransactionId().isPresent());
    try {
        try {
            getFutureValue(new StartTransactionTask().execute(new StartTransaction(ImmutableList.of(new TransactionAccessMode(true), new TransactionAccessMode(true))), transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList()));
            fail();
        } catch (CompletionException e) {
            throw Throwables.propagate(e.getCause());
        }
    } catch (SemanticException e) {
        assertEquals(e.getCode(), INVALID_TRANSACTION_MODE);
    }
    assertTrue(transactionManager.getAllTransactionInfos().isEmpty());
    assertFalse(stateMachine.getQueryInfoWithoutDetails().isClearTransactionId());
    assertFalse(stateMachine.getQueryInfoWithoutDetails().getStartedTransactionId().isPresent());
}
Also used : AccessControlManager(com.facebook.presto.security.AccessControlManager) TransactionManager(com.facebook.presto.transaction.TransactionManager) TransactionManager.createTestTransactionManager(com.facebook.presto.transaction.TransactionManager.createTestTransactionManager) TransactionAccessMode(com.facebook.presto.sql.tree.TransactionAccessMode) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) QueryId(com.facebook.presto.spi.QueryId) CompletionException(java.util.concurrent.CompletionException) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) AccessControl(com.facebook.presto.security.AccessControl) StartTransaction(com.facebook.presto.sql.tree.StartTransaction) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Test(org.testng.annotations.Test)

Aggregations

QueryId (com.facebook.presto.spi.QueryId)42 Test (org.testng.annotations.Test)26 Session (com.facebook.presto.Session)20 AllowAllAccessControl (com.facebook.presto.security.AllowAllAccessControl)17 AccessControl (com.facebook.presto.security.AccessControl)15 AccessControlManager (com.facebook.presto.security.AccessControlManager)15 TransactionManager (com.facebook.presto.transaction.TransactionManager)15 TransactionManager.createTestTransactionManager (com.facebook.presto.transaction.TransactionManager.createTestTransactionManager)15 DistributedQueryRunner (com.facebook.presto.tests.DistributedQueryRunner)10 StartTransaction (com.facebook.presto.sql.tree.StartTransaction)7 CompletionException (java.util.concurrent.CompletionException)7 PrestoException (com.facebook.presto.spi.PrestoException)6 MemoryPoolId (com.facebook.presto.spi.memory.MemoryPoolId)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 DataSize (io.airlift.units.DataSize)5 QueryInfo (com.facebook.presto.execution.QueryInfo)4 MemoryPool (com.facebook.presto.memory.MemoryPool)4 QueryContext (com.facebook.presto.memory.QueryContext)4 ResourceGroupManagerPlugin (com.facebook.presto.resourceGroups.ResourceGroupManagerPlugin)4 H2ResourceGroupsDao (com.facebook.presto.resourceGroups.db.H2ResourceGroupsDao)4