Skip to content

Commit

Permalink
Emit ingest/input/bytes metric for index_parallel and query_controlle…
Browse files Browse the repository at this point in the history
…r tasks (#17581)
  • Loading branch information
neha-ellur authored Jan 24, 2025
1 parent a71e77c commit 0aadf1e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import org.apache.druid.java.util.common.granularity.Granularity;
import org.apache.druid.java.util.common.io.Closer;
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.msq.counters.ChannelCounters;
import org.apache.druid.msq.counters.CounterSnapshots;
import org.apache.druid.msq.counters.CounterSnapshotsTree;
import org.apache.druid.msq.indexing.InputChannelFactory;
Expand Down Expand Up @@ -162,6 +163,7 @@
import org.apache.druid.msq.querykit.scan.ScanQueryKit;
import org.apache.druid.msq.shuffle.input.DurableStorageInputChannelFactory;
import org.apache.druid.msq.shuffle.input.WorkerInputChannelFactory;
import org.apache.druid.msq.statistics.ClusterByStatisticsCollector;
import org.apache.druid.msq.statistics.PartialKeyStatisticsInformation;
import org.apache.druid.msq.util.IntervalUtils;
import org.apache.druid.msq.util.MSQFutureUtils;
Expand Down Expand Up @@ -514,7 +516,7 @@ private MSQTaskReportPayload runInternal(final QueryListener queryListener, fina
stagesReport = null;
}

return new MSQTaskReportPayload(
final MSQTaskReportPayload msqTaskReportPayload = new MSQTaskReportPayload(
makeStatusReport(
taskStateForReport,
errorForReport,
Expand All @@ -529,6 +531,44 @@ private MSQTaskReportPayload runInternal(final QueryListener queryListener, fina
countersSnapshot,
null
);
// Emit summary metrics
emitSummaryMetrics(msqTaskReportPayload, querySpec);
return msqTaskReportPayload;
}

private void emitSummaryMetrics(final MSQTaskReportPayload msqTaskReportPayload, final MSQSpec querySpec)
{
final Set<Integer> stagesToInclude = new HashSet<>();
final MSQStagesReport stagesReport = msqTaskReportPayload.getStages();
if (stagesReport != null) {
for (MSQStagesReport.Stage stage : stagesReport.getStages()) {
boolean hasParentStage = stage.getStageDefinition().getInputSpecs().stream()
.anyMatch(stageInput -> stageInput instanceof StageInputSpec);
if (!hasParentStage) {
stagesToInclude.add(stage.getStageNumber());
}
}
}
long totalProcessedBytes = 0;

if (msqTaskReportPayload.getCounters() != null) {
totalProcessedBytes = msqTaskReportPayload.getCounters()
.copyMap()
.entrySet()
.stream()
.filter(entry -> stagesReport == null || stagesToInclude.contains(entry.getKey()))
.flatMap(counterSnapshotsMap -> counterSnapshotsMap.getValue().values().stream())
.flatMap(counterSnapshots -> counterSnapshots.getMap().entrySet().stream())
.filter(entry -> entry.getKey().startsWith("input"))
.mapToLong(entry -> {
ChannelCounters.Snapshot snapshot = (ChannelCounters.Snapshot) entry.getValue();
return snapshot.getBytes() == null ? 0L : Arrays.stream(snapshot.getBytes()).sum();
})
.sum();
}

log.debug("Processed bytes[%d] for query[%s].", totalProcessedBytes, querySpec.getQuery());
context.emitMetric("ingest/input/bytes", totalProcessedBytes);
}

/**
Expand Down Expand Up @@ -2418,7 +2458,7 @@ private void startTaskLauncher()
}

/**
* Enqueues the fetching {@link org.apache.druid.msq.statistics.ClusterByStatisticsCollector}
* Enqueues the fetching {@link ClusterByStatisticsCollector}
* from each worker via {@link WorkerSketchFetcher}
*/
private void fetchStatsFromWorkers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.granularity.Granularity;
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
import org.apache.druid.rpc.HttpResponseException;
import org.apache.druid.rpc.indexing.OverlordClient;
import org.apache.druid.segment.SegmentSchemaMapping;
Expand Down Expand Up @@ -1279,11 +1280,29 @@ private void updateAndWriteCompletionReports(TaskStatus status)

private void writeCompletionReports()
{
emitCompletionMetrics();
if (!isCompactionTask) {
toolbox.getTaskReportFileWriter().write(getId(), completionReports);
}
}

private void emitCompletionMetrics()
{
final Map<String, Object> rowStats = getTaskCompletionRowStats();
if (rowStats == null) {
return;
}

final Number totalProcessedBytes = (Number) rowStats.get("processedBytes");
if (totalProcessedBytes == null) {
return;
}

final ServiceMetricEvent.Builder metricBuilder = new ServiceMetricEvent.Builder();
IndexTaskUtils.setTaskDimensions(metricBuilder, this);
toolbox.getEmitter().emit(metricBuilder.setMetric("ingest/input/bytes", totalProcessedBytes));
}

private static IndexTuningConfig convertToIndexTuningConfig(ParallelIndexTuningConfig tuningConfig)
{
return new IndexTuningConfig(
Expand Down

0 comments on commit 0aadf1e

Please sign in to comment.