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")));
}
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);
}
}
}
}
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();
}
}
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;
}
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;
}
Aggregations