Skip to content

Commit

Permalink
Allow getting metrics after the operator is closed
Browse files Browse the repository at this point in the history
In case the metrics are updated when close() is called, the metrics' handling code would be simpler.
  • Loading branch information
rzeyde-varada committed Nov 7, 2021
1 parent 43e5836 commit d49ed1c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ private void closeOperators(int lastOperatorIndex)
operatorContext.getDriverContext().getTaskId());
}
finally {
workProcessorOperatorContext.metrics.set(operator.getMetrics());
if (operator instanceof WorkProcessorSourceOperator) {
WorkProcessorSourceOperator sourceOperator = (WorkProcessorSourceOperator) operator;
workProcessorOperatorContext.connectorMetrics.set(sourceOperator.getConnectorMetrics());
}
workProcessorOperatorContext.memoryTrackingContext.close();
workProcessorOperatorContext.finalOperatorInfo = operator.getOperatorInfo().orElse(null);
workProcessorOperatorContext.operator = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ public boolean isFinished()
public void close()
throws Exception
{
sourceOperator.close();
operatorContext.setLatestMetrics(sourceOperator.getMetrics());
operatorContext.setLatestConnectorMetrics(sourceOperator.getConnectorMetrics());
sourceOperator.close();
}

private void updateOperatorStats()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ public void testWorkProcessorPipelineSourceOperator()
// assert source operator stats are correct
OperatorStats sourceOperatorStats = operatorStats.get(0);

assertEquals(sourceOperatorStats.getMetrics().getMetrics(), ImmutableMap.of("testSourceMetric", new LongCount(1)));
assertEquals(sourceOperatorStats.getConnectorMetrics().getMetrics(), ImmutableMap.of("testSourceConnectorMetric", new LongCount(2)));
assertEquals(sourceOperatorStats.getMetrics().getMetrics(), ImmutableMap.of(
"testSourceMetric", new LongCount(1),
"testSourceClosed", new LongCount(1)));
assertEquals(sourceOperatorStats.getConnectorMetrics().getMetrics(), ImmutableMap.of(
"testSourceConnectorMetric", new LongCount(2),
"testSourceConnectorClosed", new LongCount(1)));

assertEquals(sourceOperatorStats.getDynamicFilterSplitsProcessed(), 42L);

Expand Down Expand Up @@ -239,8 +243,12 @@ public void testWorkProcessorPipelineSourceOperator()

// assert pipeline metrics
List<OperatorStats> operatorSummaries = pipelineStats.getOperatorSummaries();
assertEquals(operatorSummaries.get(0).getMetrics().getMetrics(), ImmutableMap.of("testSourceMetric", new LongCount(1)));
assertEquals(operatorSummaries.get(0).getConnectorMetrics().getMetrics(), ImmutableMap.of("testSourceConnectorMetric", new LongCount(2)));
assertEquals(operatorSummaries.get(0).getMetrics().getMetrics(), ImmutableMap.of(
"testSourceMetric", new LongCount(1),
"testSourceClosed", new LongCount(1)));
assertEquals(operatorSummaries.get(0).getConnectorMetrics().getMetrics(), ImmutableMap.of(
"testSourceConnectorMetric", new LongCount(2),
"testSourceConnectorClosed", new LongCount(1)));
assertEquals(operatorSummaries.get(1).getMetrics().getMetrics(), ImmutableMap.of("testOperatorMetric", new LongCount(1)));
}

Expand Down Expand Up @@ -425,13 +433,18 @@ public long getDynamicFilterSplitsProcessed()
@Override
public Metrics getMetrics()
{
return new Metrics(ImmutableMap.of("testSourceMetric", new LongCount(1)));
System.err.println("closed: " + closed);
return new Metrics(ImmutableMap.of(
"testSourceMetric", new LongCount(1),
"testSourceClosed", new LongCount(closed ? 1 : 0)));
}

@Override
public Metrics getConnectorMetrics()
{
return new Metrics(ImmutableMap.of("testSourceConnectorMetric", new LongCount(2)));
return new Metrics(ImmutableMap.of(
"testSourceConnectorMetric", new LongCount(2),
"testSourceConnectorClosed", new LongCount(closed ? 1 : 0)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ default CompletableFuture<?> isBlocked()
* Returns the connector's metrics, mapping a metric ID to its latest value.
* Each call must return an immutable snapshot of available metrics.
* Same ID metrics are merged across all tasks and exposed via OperatorStats.
* This method can be called after the page source is closed.
*/
default Metrics getMetrics()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static class DynamicFilteringPageSource
private final boolean enableLazyDynamicFiltering;
private long rows;
private long completedPositions;
private boolean closed;

private DynamicFilteringPageSource(FixedPageSource delegate, List<ColumnHandle> columns, DynamicFilter dynamicFilter, boolean enableLazyDynamicFiltering)
{
Expand Down Expand Up @@ -171,14 +172,15 @@ public long getSystemMemoryUsage()
public void close()
{
delegate.close();
closed = true;
}

@Override
public Metrics getMetrics()
{
return new Metrics(ImmutableMap.of(
"rows", new LongCount(rows),
"finished", new LongCount(isFinished() ? 1 : 0),
"finished", new LongCount(closed ? 1 : 0),
"started", new LongCount(1)));
}
}
Expand Down

0 comments on commit d49ed1c

Please sign in to comment.