Skip to content

Commit

Permalink
Expose output buffer metrics in query completion event
Browse files Browse the repository at this point in the history
  • Loading branch information
losipiuk committed Dec 12, 2024
1 parent 2331e11 commit 98d7c13
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions core/trino-main/src/main/java/io/trino/event/QueryMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import io.trino.spi.eventlistener.QueryOutputMetadata;
import io.trino.spi.eventlistener.QueryStatistics;
import io.trino.spi.eventlistener.StageCpuDistribution;
import io.trino.spi.eventlistener.StageOutputBufferMetrics;
import io.trino.spi.eventlistener.StageOutputBufferUtilization;
import io.trino.spi.eventlistener.StageTaskStatistics;
import io.trino.spi.metrics.Metrics;
Expand Down Expand Up @@ -238,6 +239,7 @@ public void queryImmediateFailureEvent(BasicQueryInfo queryInfo, ExecutionFailur
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(),
Optional.empty()),
createQueryContext(
queryInfo.getSession(),
Expand Down Expand Up @@ -351,6 +353,7 @@ private QueryStatistics createQueryStatistics(QueryInfo queryInfo)
queryInfo.isFinalQueryInfo(),
getCpuDistributions(queryInfo),
getStageOutputBufferUtilizations(queryInfo),
getStageOutputBufferMetrics(queryInfo),
getStageTaskStatistics(queryInfo),
memoize(() -> operatorStats.stream().map(operatorStatsCodec::toJson).toList()),
ImmutableList.copyOf(queryInfo.getQueryStats().getOptimizerRulesSummaries()),
Expand Down Expand Up @@ -780,6 +783,29 @@ private static void populateStageOutputBufferUtilization(StageInfo stageInfo, Im
}
}

private static List<StageOutputBufferMetrics> getStageOutputBufferMetrics(QueryInfo queryInfo)
{
if (queryInfo.getOutputStage().isEmpty()) {
return ImmutableList.of();
}
ImmutableList.Builder<StageOutputBufferMetrics> builder = ImmutableList.builder();
populateStageOutputBufferMetrics(queryInfo.getOutputStage().get(), builder);

return builder.build();
}

private static void populateStageOutputBufferMetrics(StageInfo stageInfo, ImmutableList.Builder<StageOutputBufferMetrics> accumulator)
{
Metrics metrics = stageInfo.getStageStats().getOutputBufferMetrics();
if (!metrics.getMetrics().isEmpty()) {
accumulator.add(new StageOutputBufferMetrics(stageInfo.getStageId().getId(), metrics));
}

for (StageInfo subStage : stageInfo.getSubStages()) {
populateStageOutputBufferMetrics(subStage, accumulator);
}
}

private List<StageTaskStatistics> getStageTaskStatistics(QueryInfo queryInfo)
{
if (queryInfo.getOutputStage().isEmpty()) {
Expand Down
7 changes: 7 additions & 0 deletions core/trino-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@
<code>java.method.removed</code>
<old>method io.trino.spi.connector.ConnectorTableHandle io.trino.spi.connector.ConnectorMetadata::makeCompatiblePartitioning(io.trino.spi.connector.ConnectorSession, io.trino.spi.connector.ConnectorTableHandle, io.trino.spi.connector.ConnectorPartitioningHandle)</old>
</item>
<item>
<ignore>true</ignore>
<code>java.method.numberOfParametersChanged</code>
<old>method void io.trino.spi.eventlistener.QueryStatistics::&lt;init&gt;(java.time.Duration, java.time.Duration, java.time.Duration, java.time.Duration, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, double, double, java.util.List&lt;io.trino.spi.eventlistener.StageGcStatistics&gt;, int, boolean, java.util.List&lt;io.trino.spi.eventlistener.StageCpuDistribution&gt;, java.util.List&lt;io.trino.spi.eventlistener.StageOutputBufferUtilization&gt;, java.util.List&lt;io.trino.spi.eventlistener.StageTaskStatistics&gt;, java.util.function.Supplier&lt;java.util.List&lt;java.lang.String&gt;&gt;, java.util.List&lt;io.trino.spi.eventlistener.QueryPlanOptimizerStatistics&gt;, java.util.Optional&lt;java.lang.String&gt;)</old>
<new>method void io.trino.spi.eventlistener.QueryStatistics::&lt;init&gt;(java.time.Duration, java.time.Duration, java.time.Duration, java.time.Duration, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, java.util.Optional&lt;java.time.Duration&gt;, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, long, double, double, java.util.List&lt;io.trino.spi.eventlistener.StageGcStatistics&gt;, int, boolean, java.util.List&lt;io.trino.spi.eventlistener.StageCpuDistribution&gt;, java.util.List&lt;io.trino.spi.eventlistener.StageOutputBufferUtilization&gt;, java.util.List&lt;io.trino.spi.eventlistener.StageOutputBufferMetrics&gt;, java.util.List&lt;io.trino.spi.eventlistener.StageTaskStatistics&gt;, java.util.function.Supplier&lt;java.util.List&lt;java.lang.String&gt;&gt;, java.util.List&lt;io.trino.spi.eventlistener.QueryPlanOptimizerStatistics&gt;, java.util.Optional&lt;java.lang.String&gt;)</new>
<justification>Added output buffer metrics</justification>
</item>
</differences>
</revapi.differences>
</analysisConfiguration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class QueryStatistics
private final List<StageCpuDistribution> cpuTimeDistribution;
private final List<StageOutputBufferUtilization> outputBufferUtilization;
private final List<StageTaskStatistics> taskStatistics;
private final List<StageOutputBufferMetrics> outputBufferMetrics;

/**
* Operator summaries serialized to JSON. Serialization format and structure
Expand Down Expand Up @@ -131,6 +132,7 @@ public QueryStatistics(
boolean complete,
List<StageCpuDistribution> cpuTimeDistribution,
List<StageOutputBufferUtilization> outputBufferUtilization,
List<StageOutputBufferMetrics> outputBufferMetrics,
List<StageTaskStatistics> taskStatistics,
List<String> operatorSummaries,
List<QueryPlanOptimizerStatistics> optimizerRulesSummaries,
Expand Down Expand Up @@ -177,6 +179,7 @@ public QueryStatistics(
complete,
cpuTimeDistribution,
outputBufferUtilization,
outputBufferMetrics,
taskStatistics,
() -> operatorSummaries,
optimizerRulesSummaries,
Expand Down Expand Up @@ -224,6 +227,7 @@ public QueryStatistics(
boolean complete,
List<StageCpuDistribution> cpuTimeDistribution,
List<StageOutputBufferUtilization> outputBufferUtilization,
List<StageOutputBufferMetrics> outputBufferMetrics,
List<StageTaskStatistics> taskStatistics,
Supplier<List<String>> operatorSummariesProvider,
List<QueryPlanOptimizerStatistics> optimizerRulesSummaries,
Expand Down Expand Up @@ -269,6 +273,7 @@ public QueryStatistics(
this.complete = complete;
this.cpuTimeDistribution = requireNonNull(cpuTimeDistribution, "cpuTimeDistribution is null");
this.outputBufferUtilization = requireNonNull(outputBufferUtilization, "outputBufferUtilization is null");
this.outputBufferMetrics = requireNonNull(outputBufferMetrics, "outputBufferMetrics is null");
this.taskStatistics = requireNonNull(taskStatistics, "taskStatistics is null");
this.operatorSummariesProvider = requireNonNull(operatorSummariesProvider, "operatorSummariesProvider is null");
this.optimizerRulesSummaries = requireNonNull(optimizerRulesSummaries, "optimizerRulesSummaries is null");
Expand Down Expand Up @@ -515,6 +520,12 @@ public List<StageOutputBufferUtilization> getOutputBufferUtilization()
return outputBufferUtilization;
}

@JsonProperty
public List<StageOutputBufferMetrics> getOutputBufferMetrics()
{
return outputBufferMetrics;
}

@JsonProperty
public List<StageTaskStatistics> getTaskStatistics()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.spi.eventlistener;

import io.trino.spi.metrics.Metrics;

/**
* This class is JSON serializable for convenience and serialization compatibility is not guaranteed across versions.
*/
public record StageOutputBufferMetrics(int stageId, Metrics metrics) {}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ final class TestHttpEventListener
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Optional.empty());

splitCompleteEvent = new SplitCompletedEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ final class TestHttpServerEventListener
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Optional.empty());

queryCompleteEvent = new QueryCompletedEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private TestUtils() {}
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Optional.empty());

queryFailureInfo = Optional.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ final class TestMysqlEventListener
// not stored
Collections.emptyList(),
// not stored
Collections.emptyList(),
// not stored
List.of("{operator: \"operator1\"}", "{operator: \"operator2\"}"),
// not stored
Collections.emptyList(),
Expand Down Expand Up @@ -293,6 +295,8 @@ final class TestMysqlEventListener
Collections.emptyList(),
// not stored
Collections.emptyList(),
// not stored
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
// not stored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private TrinoEventData()
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Optional.empty());

queryCompleteEvent = new QueryCompletedEvent(
Expand Down

0 comments on commit 98d7c13

Please sign in to comment.