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