Search in sources :

Example 26 with QueryId

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

the class TestServer method testQuery.

@Test
public void testQuery() throws Exception {
    // 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, DISTRIBUTED_JOIN + "=true," + HASH_PARTITION_COUNT + " = 43").addHeader(PRESTO_PREPARED_STATEMENT, "foo=select * from bar").build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class)));
    // get the query info
    QueryInfo queryInfo = server.getQueryManager().getQueryInfo(new QueryId(queryResults.getId()));
    // verify session properties
    assertEquals(queryInfo.getSession().getSystemProperties(), ImmutableMap.builder().put(QUERY_MAX_MEMORY, "1GB").put(DISTRIBUTED_JOIN, "true").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());
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    if (queryResults.getData() != null) {
        data.addAll(queryResults.getData());
    }
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(jsonCodec(QueryResults.class)));
        if (queryResults.getData() != null) {
            data.addAll(queryResults.getData());
        }
    }
    assertNull(queryResults.getError());
    // 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(io.airlift.http.client.Request) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryInfo(com.facebook.presto.execution.QueryInfo) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 27 with QueryId

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

the class ClusterMemoryPool method update.

public synchronized void update(List<MemoryInfo> memoryInfos, int assignedQueries) {
    nodes = 0;
    blockedNodes = 0;
    totalDistributedBytes = 0;
    freeDistributedBytes = 0;
    this.assignedQueries = assignedQueries;
    this.queryMemoryReservations.clear();
    for (MemoryInfo info : memoryInfos) {
        MemoryPoolInfo poolInfo = info.getPools().get(id);
        if (poolInfo != null) {
            nodes++;
            if (poolInfo.getFreeBytes() <= 0) {
                blockedNodes++;
            }
            totalDistributedBytes += poolInfo.getMaxBytes();
            freeDistributedBytes += poolInfo.getFreeBytes();
            for (Map.Entry<QueryId, Long> entry : poolInfo.getQueryMemoryReservations().entrySet()) {
                queryMemoryReservations.merge(entry.getKey(), entry.getValue(), Long::sum);
            }
        }
    }
}
Also used : QueryId(com.facebook.presto.spi.QueryId) MemoryPoolInfo(com.facebook.presto.spi.memory.MemoryPoolInfo) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 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 29 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 30 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)

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