Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy computation of some fields in QueryMetadata and QueryStatistics #21445

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions core/trino-main/src/main/java/io/trino/event/QueryMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -86,6 +87,7 @@
import java.util.OptionalLong;
import java.util.stream.Collectors;

import static com.google.common.base.Suppliers.memoize;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.execution.QueryState.QUEUED;
Expand Down Expand Up @@ -166,7 +168,7 @@ public void queryCreatedEvent(BasicQueryInfo queryInfo)
queryInfo.getSelf(),
Optional.empty(),
Optional.empty(),
Optional.empty())));
Optional::empty)));
}

public void queryImmediateFailureEvent(BasicQueryInfo queryInfo, ExecutionFailureInfo failure)
Expand All @@ -184,7 +186,7 @@ public void queryImmediateFailureEvent(BasicQueryInfo queryInfo, ExecutionFailur
queryInfo.getSelf(),
Optional.empty(),
Optional.empty(),
Optional.empty()),
Optional::empty),
new QueryStatistics(
ofMillis(0),
ofMillis(0),
Expand Down Expand Up @@ -245,9 +247,13 @@ public void queryImmediateFailureEvent(BasicQueryInfo queryInfo, ExecutionFailur

public void queryCompletedEvent(QueryInfo queryInfo)
{
Map<Boolean, QueryCompletedEvent> memo = new HashMap<>();
QueryStats queryStats = queryInfo.getQueryStats();
eventListenerManager.queryCompleted(requiresAnonymizedPlan ->
new QueryCompletedEvent(

eventListenerManager.queryCompleted(requiresAnonymizedPlan -> {
QueryCompletedEvent queryCompletedEvent = memo.get(requiresAnonymizedPlan);
if (queryCompletedEvent == null) {
queryCompletedEvent = new QueryCompletedEvent(
createQueryMetadata(queryInfo, requiresAnonymizedPlan),
createQueryStatistics(queryInfo),
createQueryContext(
Expand All @@ -260,8 +266,11 @@ public void queryCompletedEvent(QueryInfo queryInfo)
queryInfo.getWarnings(),
ofEpochMilli(queryStats.getCreateTime().getMillis()),
ofEpochMilli(queryStats.getExecutionStartTime().getMillis()),
ofEpochMilli(queryStats.getEndTime() != null ? queryStats.getEndTime().getMillis() : 0)));

ofEpochMilli(queryStats.getEndTime() != null ? queryStats.getEndTime().getMillis() : 0));
memo.put(requiresAnonymizedPlan, queryCompletedEvent);
}
return queryCompletedEvent;
});
logQueryTimeline(queryInfo);
}

Expand All @@ -280,7 +289,7 @@ private QueryMetadata createQueryMetadata(QueryInfo queryInfo, boolean requiresA
queryInfo.getSelf(),
createTextQueryPlan(queryInfo, anonymizer),
createJsonQueryPlan(queryInfo, anonymizer),
queryInfo.getOutputStage().flatMap(stage -> stageInfoCodec.toJsonWithLengthLimit(stage, maxJsonLimit)));
memoize(() -> queryInfo.getOutputStage().flatMap(stage -> stageInfoCodec.toJsonWithLengthLimit(stage, maxJsonLimit))));
}

private QueryStatistics createQueryStatistics(QueryInfo queryInfo)
Expand Down Expand Up @@ -335,7 +344,7 @@ private QueryStatistics createQueryStatistics(QueryInfo queryInfo)
queryInfo.isFinalQueryInfo(),
getCpuDistributions(queryInfo),
getStageOutputBufferUtilizations(queryInfo),
operatorSummaries.build(),
memoize(() -> operatorStats.stream().map(operatorStatsCodec::toJson).toList()),
ImmutableList.copyOf(queryInfo.getQueryStats().getOptimizerRulesSummaries()),
serializedPlanNodeStatsAndCosts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

Expand All @@ -44,7 +45,7 @@ public class QueryMetadata
private final Optional<String> plan;
private final Optional<String> jsonPlan;

private final Optional<String> payload;
private final Supplier<Optional<String>> payloadProvider;

@JsonCreator
@Unstable
Expand All @@ -61,6 +62,35 @@ public QueryMetadata(
Optional<String> plan,
Optional<String> jsonPlan,
Optional<String> payload)
{
this(
queryId,
transactionId,
query,
updateType,
preparedQuery,
queryState,
tables,
routines,
uri,
plan,
jsonPlan,
() -> payload);
}

public QueryMetadata(
String queryId,
Optional<String> transactionId,
String query,
Optional<String> updateType,
Optional<String> preparedQuery,
String queryState,
List<TableInfo> tables,
List<RoutineInfo> routines,
URI uri,
Optional<String> plan,
Optional<String> jsonPlan,
Supplier<Optional<String>> payloadProvider)
{
this.queryId = requireNonNull(queryId, "queryId is null");
this.transactionId = requireNonNull(transactionId, "transactionId is null");
Expand All @@ -73,7 +103,7 @@ public QueryMetadata(
this.uri = requireNonNull(uri, "uri is null");
this.plan = requireNonNull(plan, "plan is null");
this.jsonPlan = requireNonNull(jsonPlan, "jsonPlan is null");
this.payload = requireNonNull(payload, "payload is null");
this.payloadProvider = requireNonNull(payloadProvider, "payloadProvider is null");
}

@JsonProperty
Expand Down Expand Up @@ -145,6 +175,6 @@ public Optional<String> getJsonPlan()
@JsonProperty
public Optional<String> getPayload()
{
return payload;
return payloadProvider.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -77,7 +78,7 @@ public class QueryStatistics
* Operator summaries serialized to JSON. Serialization format and structure
* can change without preserving backward compatibility.
*/
private final List<String> operatorSummaries;
private final Supplier<List<String>> operatorSummariesProvider;
private final List<QueryPlanOptimizerStatistics> optimizerRulesSummaries;
/**
* Plan node stats and costs serialized to JSON. Serialization format and structure
Expand Down Expand Up @@ -130,6 +131,95 @@ public QueryStatistics(
List<String> operatorSummaries,
List<QueryPlanOptimizerStatistics> optimizerRulesSummaries,
Optional<String> planNodeStatsAndCosts)
{
this(
cpuTime,
failedCpuTime,
wallTime,
queuedTime,
scheduledTime,
failedScheduledTime,
resourceWaitingTime,
analysisTime,
planningTime,
planningCpuTime,
executionTime,
inputBlockedTime,
failedInputBlockedTime,
outputBlockedTime,
failedOutputBlockedTime,
physicalInputReadTime,
peakUserMemoryBytes,
peakTaskUserMemory,
peakTaskTotalMemory,
physicalInputBytes,
physicalInputRows,
processedInputBytes,
processedInputRows,
internalNetworkBytes,
internalNetworkRows,
totalBytes,
totalRows,
outputBytes,
outputRows,
writtenBytes,
writtenRows,
spilledBytes,
cumulativeMemory,
failedCumulativeMemory,
stageGcStatistics,
completedSplits,
complete,
cpuTimeDistribution,
outputBufferUtilization,
() -> operatorSummaries,
optimizerRulesSummaries,
planNodeStatsAndCosts);
}

public QueryStatistics(
Duration cpuTime,
Duration failedCpuTime,
Duration wallTime,
Duration queuedTime,
Optional<Duration> scheduledTime,
Optional<Duration> failedScheduledTime,
Optional<Duration> resourceWaitingTime,
Optional<Duration> analysisTime,
Optional<Duration> planningTime,
Optional<Duration> planningCpuTime,
Optional<Duration> executionTime,
Optional<Duration> inputBlockedTime,
Optional<Duration> failedInputBlockedTime,
Optional<Duration> outputBlockedTime,
Optional<Duration> failedOutputBlockedTime,
Optional<Duration> physicalInputReadTime,
long peakUserMemoryBytes,
long peakTaskUserMemory,
long peakTaskTotalMemory,
long physicalInputBytes,
long physicalInputRows,
long processedInputBytes,
long processedInputRows,
long internalNetworkBytes,
long internalNetworkRows,
long totalBytes,
long totalRows,
long outputBytes,
long outputRows,
long writtenBytes,
long writtenRows,
long spilledBytes,
double cumulativeMemory,
double failedCumulativeMemory,
List<StageGcStatistics> stageGcStatistics,
int completedSplits,
boolean complete,
List<StageCpuDistribution> cpuTimeDistribution,
List<StageOutputBufferUtilization> outputBufferUtilization,
Supplier<List<String>> operatorSummariesProvider,
List<QueryPlanOptimizerStatistics> optimizerRulesSummaries,
Optional<String> planNodeStatsAndCosts)
{
this.cpuTime = requireNonNull(cpuTime, "cpuTime is null");
this.failedCpuTime = requireNonNull(failedCpuTime, "failedCpuTime is null");
Expand Down Expand Up @@ -170,7 +260,7 @@ public QueryStatistics(
this.complete = complete;
this.cpuTimeDistribution = requireNonNull(cpuTimeDistribution, "cpuTimeDistribution is null");
this.outputBufferUtilization = requireNonNull(outputBufferUtilization, "outputBufferUtilization is null");
this.operatorSummaries = requireNonNull(operatorSummaries, "operatorSummaries is null");
this.operatorSummariesProvider = requireNonNull(operatorSummariesProvider, "operatorSummariesProvider is null");
this.optimizerRulesSummaries = requireNonNull(optimizerRulesSummaries, "optimizerRulesSummaries is null");
this.planNodeStatsAndCosts = requireNonNull(planNodeStatsAndCosts, "planNodeStatsAndCosts is null");
}
Expand Down Expand Up @@ -412,7 +502,7 @@ public List<StageOutputBufferUtilization> getOutputBufferUtilization()
@JsonProperty
public List<String> getOperatorSummaries()
{
return operatorSummaries;
return operatorSummariesProvider.get();
}

@JsonProperty
Expand Down
Loading