use of io.prestosql.execution.QueryInfo in project hetu-core by openlookeng.
the class Query method getNextResult.
private synchronized DataCenterQueryResults getNextResult(long token, DataSize targetResultSize) {
// check if the result for the token have already been created
Optional<DataCenterQueryResults> cachedResult = getCachedDCQueryResult(token);
if (cachedResult.isPresent()) {
return cachedResult.get();
}
verify(nextToken.isPresent(), "Can not generate next result when next token is not present");
verify(token == nextToken.getAsLong(), "Expected token to equal next token");
URI queryHtmlUri = null;
try {
queryHtmlUri = new URI("http://localhost");
} catch (URISyntaxException e) {
log.error("get uri error: %s", e.getMessage());
}
// Remove as many pages as possible from the exchange until just greater than DESIRED_RESULT_BYTES
// NOTE: it is critical that query results are created for the pages removed from the exchange
// client while holding the lock because the query may transition to the finished state when the
// last page is removed. If another thread observes this state before the response is cached
// the pages will be lost.
List<SerializedPage> data = null;
try {
ImmutableList.Builder<SerializedPage> builder = new ImmutableList.Builder<>();
long bytes = 0;
long rows = 0;
long targetResultBytes = targetResultSize.toBytes();
while (bytes < targetResultBytes) {
// at this point, origin is irrelevant, so we can safely ignore it
SerializedPage serializedPage = exchangeClient.pollPage(null).getLeft();
if (serializedPage == null) {
break;
}
builder.add(new SerializedPage(serializedPage.getSlice().getBytes(), serializedPage.getPageCodecMarkers(), serializedPage.getPositionCount(), serializedPage.getUncompressedSizeInBytes()));
bytes += serializedPage.getUncompressedSizeInBytes();
rows += serializedPage.getPositionCount();
}
if (rows > 0) {
// client implementations do not properly handle empty list of data
data = builder.build();
}
} catch (Throwable cause) {
queryManager.failQuery(queryId, cause);
}
// get the query info before returning
// force update if query manager is closed
QueryInfo queryInfo = queryManager.getFullQueryInfo(queryId);
queryManager.recordHeartbeat(queryId);
closeExchangeClientIfNecessary(queryInfo);
// for queries with no output, return a fake result for clients that require it
if ((queryInfo.getState() == QueryState.FINISHED) && !queryInfo.getOutputStage().isPresent()) {
columns = ImmutableList.of(createColumn("result", BooleanType.BOOLEAN));
PageBuilder pageBuilder = new PageBuilder(Collections.singletonList(BooleanType.BOOLEAN));
pageBuilder.declarePosition();
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
BooleanType.BOOLEAN.writeBoolean(blockBuilder, true);
Page page = pageBuilder.build();
SerializedPage serializedPage = serde.serialize(page);
data = Collections.singletonList(new SerializedPage(serializedPage.getSlice().getBytes(), serializedPage.getPageCodecMarkers(), serializedPage.getPositionCount(), serializedPage.getUncompressedSizeInBytes()));
} else if (queryInfo.isRunningAsync()) {
columns = ImmutableList.of(createColumn("result", BooleanType.BOOLEAN), createColumn("runningAsync", BooleanType.BOOLEAN));
PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(BooleanType.BOOLEAN, BooleanType.BOOLEAN));
pageBuilder.declarePosition();
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
BooleanType.BOOLEAN.writeBoolean(blockBuilder, true);
BooleanType.BOOLEAN.writeBoolean(blockBuilder, true);
Page page = pageBuilder.build();
SerializedPage serializedPage = serde.serialize(page);
data = Collections.singletonList(new SerializedPage(serializedPage.getSlice().getBytes(), serializedPage.getPageCodecMarkers(), serializedPage.getPositionCount(), serializedPage.getUncompressedSizeInBytes()));
}
// (3) Query supports and started running Async
if ((!queryInfo.isFinalQueryInfo() && queryInfo.getState() != FAILED && !queryInfo.isRunningAsync()) || !exchangeClient.isClosed()) {
nextToken = OptionalLong.of(token + 1);
} else {
nextToken = OptionalLong.empty();
}
URI nextResultsUri = null;
if (nextToken.isPresent()) {
try {
nextResultsUri = new URI(Long.toString(nextToken.getAsLong()));
} catch (URISyntaxException e) {
log.error("get uri error: %s", e.getMessage());
}
}
// update catalog, schema, and path
setCatalog = queryInfo.getSetCatalog();
setSchema = queryInfo.getSetSchema();
setPath = queryInfo.getSetPath();
// update setSessionProperties
setSessionProperties = queryInfo.getSetSessionProperties();
resetSessionProperties = queryInfo.getResetSessionProperties();
// update setRoles
setRoles = queryInfo.getSetRoles();
// update preparedStatements
addedPreparedStatements = queryInfo.getAddedPreparedStatements();
deallocatedPreparedStatements = queryInfo.getDeallocatedPreparedStatements();
// update startedTransactionId
startedTransactionId = queryInfo.getStartedTransactionId();
clearTransactionId = queryInfo.isClearTransactionId();
// first time through, self is null
DataCenterQueryResults queryResults = new DataCenterQueryResults(queryId.toString(), queryHtmlUri, findCancelableLeafStage(queryInfo), nextResultsUri, columns, data, toStatementStats(queryInfo), toQueryError(queryInfo), mappedCopy(queryInfo.getWarnings(), Query::toClientWarning), queryInfo.getUpdateType(), true);
// cache the new result
lastToken = token;
lastDCQueryResult = queryResults;
return queryResults;
}
use of io.prestosql.execution.QueryInfo in project hetu-core by openlookeng.
the class Query method create.
public static Query create(Session session, String slug, QueryManager queryManager, ExchangeClient exchangeClient, Executor dataProcessorExecutor, ScheduledExecutorService timeoutExecutor, BlockEncodingSerde blockEncodingSerde) {
Query result = new Query(session, slug, queryManager, exchangeClient, dataProcessorExecutor, timeoutExecutor, blockEncodingSerde);
result.queryManager.addOutputInfoListener(result.getQueryId(), result::setQueryOutputInfo);
result.queryManager.addStateChangeListener(result.getQueryId(), result::updateQueryState);
result.queryManager.addStateChangeListener(result.getQueryId(), state -> {
if (state.isDone()) {
QueryInfo queryInfo = queryManager.getFullQueryInfo(result.getQueryId());
result.closeExchangeClientIfNecessary(queryInfo);
}
});
return result;
}
use of io.prestosql.execution.QueryInfo in project hetu-core by openlookeng.
the class Query method getNextResult.
private synchronized QueryResults getNextResult(long token, UriInfo uriInfo, String scheme, DataSize targetResultSize) {
// check if the result for the token have already been created
Optional<QueryResults> cachedResult = getCachedResult(token);
if (cachedResult.isPresent()) {
return cachedResult.get();
}
verify(nextToken.isPresent(), "Can not generate next result when next token is not present");
verify(token == nextToken.getAsLong(), "Expected token to equal next token");
URI queryHtmlUri = uriInfo.getRequestUriBuilder().scheme(scheme).replacePath("ui/query.html").replaceQuery(queryId.toString()).build();
// Remove as many pages as possible from the exchange until just greater than DESIRED_RESULT_BYTES
// NOTE: it is critical that query results are created for the pages removed from the exchange
// client while holding the lock because the query may transition to the finished state when the
// last page is removed. If another thread observes this state before the response is cached
// the pages will be lost.
Iterable<List<Object>> data = null;
try {
ImmutableList.Builder<RowIterable> pages = ImmutableList.builder();
long bytes = 0;
long rows = 0;
long targetResultBytes = targetResultSize.toBytes();
while (bytes < targetResultBytes) {
// at this point, origin is irrelevant, so we can safely ignore it
SerializedPage serializedPage = exchangeClient.pollPage(null).getLeft();
if (serializedPage == null) {
break;
}
Page page = serde.deserialize(serializedPage);
bytes += page.getLogicalSizeInBytes();
rows += page.getPositionCount();
pages.add(new RowIterable(session.toConnectorSession(), types, page));
}
if (rows > 0) {
// client implementations do not properly handle empty list of data
data = Iterables.concat(pages.build());
}
} catch (Throwable cause) {
queryManager.failQuery(queryId, cause);
}
// get the query info before returning
// force update if query manager is closed
QueryInfo queryInfo = queryManager.getFullQueryInfo(queryId);
queryManager.recordHeartbeat(queryId);
// grab the update count for non-queries
if ((data != null) && (queryInfo.getUpdateType() != null) && (updateCount == null) && (columns.size() == 1) && (columns.get(0).getType().equals(StandardTypes.BIGINT))) {
Iterator<List<Object>> iterator = data.iterator();
if (iterator.hasNext()) {
Number number = (Number) iterator.next().get(0);
if (number != null) {
updateCount = number.longValue();
}
}
}
closeExchangeClientIfNecessary(queryInfo);
// for queries with no output, return a fake result for clients that require it
if ((queryInfo.getState() == QueryState.FINISHED) && !queryInfo.getOutputStage().isPresent()) {
columns = ImmutableList.of(createColumn("result", BooleanType.BOOLEAN));
data = ImmutableSet.of(ImmutableList.of(true));
} else if (queryInfo.isRunningAsync()) {
columns = ImmutableList.of(createColumn("result", BooleanType.BOOLEAN), createColumn("runningAsync", BooleanType.BOOLEAN));
data = ImmutableSet.of(ImmutableList.of(true, true));
}
// (3) Query supports and started running Async
if ((!queryInfo.isFinalQueryInfo() && queryInfo.getState() != FAILED && !queryInfo.isRunningAsync()) || !exchangeClient.isClosed()) {
nextToken = OptionalLong.of(token + 1);
} else {
nextToken = OptionalLong.empty();
queryManager.checkForQueryPruning(queryId, queryInfo);
}
URI nextResultsUri = null;
if (nextToken.isPresent()) {
nextResultsUri = createNextResultsUri(scheme, uriInfo, nextToken.getAsLong());
}
// update catalog, schema, and path
setCatalog = queryInfo.getSetCatalog();
setSchema = queryInfo.getSetSchema();
setPath = queryInfo.getSetPath();
// update setSessionProperties
setSessionProperties = queryInfo.getSetSessionProperties();
resetSessionProperties = queryInfo.getResetSessionProperties();
// update setRoles
setRoles = queryInfo.getSetRoles();
// update preparedStatements
addedPreparedStatements = queryInfo.getAddedPreparedStatements();
deallocatedPreparedStatements = queryInfo.getDeallocatedPreparedStatements();
// update startedTransactionId
startedTransactionId = queryInfo.getStartedTransactionId();
clearTransactionId = queryInfo.isClearTransactionId();
// first time through, self is null
QueryResults queryResults = new QueryResults(queryId.toString(), queryHtmlUri, findCancelableLeafStage(queryInfo), nextResultsUri, columns, data, toStatementStats(queryInfo), toQueryError(queryInfo), mappedCopy(queryInfo.getWarnings(), Query::toClientWarning), queryInfo.getUpdateType(), (queryInfo.isRunningAsync() ? null : updateCount));
// cache the new result
lastToken = token;
lastResult = queryResults;
return queryResults;
}
use of io.prestosql.execution.QueryInfo in project hetu-core by openlookeng.
the class TestOracleDistributedQueries method testWrittenStats.
/**
* testWrittenStats
*/
@Override
public void testWrittenStats() {
String sql = "CREATE TABLE test_written_stats AS SELECT * FROM nation";
DistributedQueryRunner distributedQueryRunner = (DistributedQueryRunner) getQueryRunner();
ResultWithQueryId<MaterializedResult> resultResultWithQueryId = distributedQueryRunner.executeWithQueryId(getSession(), sql);
QueryInfo queryInfo = distributedQueryRunner.getCoordinator().getQueryManager().getFullQueryInfo(resultResultWithQueryId.getQueryId());
assertEquals(queryInfo.getQueryStats().getOutputPositions(), 1L);
assertEquals(queryInfo.getQueryStats().getWrittenPositions(), LONG_NUMBER_25);
assertTrue(queryInfo.getQueryStats().getLogicalWrittenDataSize().toBytes() > 0L);
sql = "INSERT INTO test_written_stats SELECT * FROM nation LIMIT 10";
resultResultWithQueryId = distributedQueryRunner.executeWithQueryId(getSession(), sql);
queryInfo = distributedQueryRunner.getCoordinator().getQueryManager().getFullQueryInfo(resultResultWithQueryId.getQueryId());
assertEquals(queryInfo.getQueryStats().getOutputPositions(), 1L);
assertEquals(queryInfo.getQueryStats().getWrittenPositions(), LONG_NUMBER_10);
assertTrue(queryInfo.getQueryStats().getLogicalWrittenDataSize().toBytes() > 0L);
assertUpdate("DROP TABLE test_written_stats");
}
use of io.prestosql.execution.QueryInfo in project hetu-core by openlookeng.
the class ExplainAnalyzeOperator method getOutput.
@Override
public Page getOutput() {
if (snapshotState != null) {
Page marker = snapshotState.nextMarker();
if (marker != null) {
return marker;
}
}
if (!finishing) {
return null;
}
QueryInfo queryInfo = queryPerformanceFetcher.getQueryInfo(operatorContext.getDriverContext().getTaskId().getQueryId());
checkState(queryInfo.getOutputStage().isPresent(), "Output stage is missing");
checkState(queryInfo.getOutputStage().get().getSubStages().size() == 1, "Expected one sub stage of explain node");
if (!hasFinalStageInfo(queryInfo.getOutputStage().get())) {
return null;
}
String plan = textDistributedPlan(queryInfo.getOutputStage().get().getSubStages().get(0), metadata, operatorContext.getSession(), verbose);
BlockBuilder builder = VARCHAR.createBlockBuilder(null, 1);
VARCHAR.writeString(builder, plan);
outputConsumed = true;
return new Page(builder.build());
}
Aggregations