Skip to content

Commit 1b48862

Browse files
authored
fix: publish metrics on the update interval for native blocks without a JVM input (apache#6314)
* fix: publish metrics on the update interval for native blocks without a JVM input Native blocks whose leaves are all native scans drain their stream through a channel (apache#3553). That branch of executePlan published the whole metric tree to the JVM after every output batch, ignoring spark.comet.metrics.updateInterval. Each publish walks the plan, aggregates every metric by name, encodes a protobuf and calls into the JVM, which cost 19-38 us per batch locally. Use the same interval check as the ScanExec path. releasePlan still publishes the final values. * test: check that a native block without a JVM input publishes once its metrics interval passes The test covered only a disabled interval, so it would also pass if this branch never published mid-stream. Run it with a 1 ms interval too, sleeping before the first batch, and expect output_rows to be published right after that batch. * test: keep the metrics interval check compiling on Spark 3.x withSQLConf returns Unit before Spark 4.0, so run the assertions inside the block instead of returning the metric values from it.
1 parent 33ea8c0 commit 1b48862

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎native/core/src/execution/jni_api.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,10 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_executePlan(
13121312
if let Some(producer) = &mut exec_context.batch_producer {
13131313
match producer.next_batch()? {
13141314
Some(batch) => {
1315-
update_metrics(env, exec_context)?;
1315+
// Publish on the configured interval, as the ScanExec path below does,
1316+
// since each publish walks the whole metric tree and calls into the JVM.
1317+
// `releasePlan` publishes the final values.
1318+
update_metrics_on_interval(env, exec_context)?;
13161319
return prepare_output(
13171320
env,
13181321
array_addrs,

‎spark/src/test/scala/org/apache/spark/sql/comet/CometTaskMetricsSuite.scala‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import org.apache.spark.sql.comet.execution.shuffle.CometBypassMergeSortShuffleH
3636
import org.apache.spark.sql.comet.execution.shuffle.CometColumnarShuffle
3737
import org.apache.spark.sql.comet.execution.shuffle.CometNativeShuffle
3838
import org.apache.spark.sql.comet.execution.shuffle.CometShuffleExchangeExec
39-
import org.apache.spark.sql.execution.SparkPlan
39+
import org.apache.spark.sql.execution.{SparkPlan, SQLExecution}
4040
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
4141
import org.apache.spark.sql.execution.exchange.ShuffleExchangeLike
4242
import org.apache.spark.sql.execution.metric.SQLMetric
@@ -1154,6 +1154,55 @@ class CometTaskMetricsSuite extends CometTestBase with AdaptiveSparkPlanHelper {
11541154
}
11551155
}
11561156

1157+
test("native block without a JVM input publishes SQL metrics on the update interval") {
1158+
withTempPath { dir =>
1159+
spark.range(0, 10000, 1, 1).write.parquet(dir.getAbsolutePath)
1160+
// Ten output batches from one task. Passes `check` the output_rows the task sees right after
1161+
// the first batch, and again after the last. The task creates the native plan, which starts
1162+
// the interval's clock, when it builds the iterator, so the sleep before the first batch runs
1163+
// after that.
1164+
def checkOutputRows(interval: String)(check: (Long, Long) => Unit): Unit =
1165+
withSQLConf(
1166+
CometConf.COMET_BATCH_SIZE.key -> "1000",
1167+
CometConf.COMET_METRICS_UPDATE_INTERVAL.key -> interval) {
1168+
val plan = spark.read.parquet(dir.getAbsolutePath).queryExecution.executedPlan
1169+
val scan = find(plan)(_.isInstanceOf[CometNativeScanExec])
1170+
.getOrElse(fail(s"Expected CometNativeScanExec in plan:\n${plan.treeString}"))
1171+
.asInstanceOf[CometNativeScanExec]
1172+
// The task deserializes this metric together with the scan's metric node, so both
1173+
// refer to the task-side copy that native execution publishes into.
1174+
val outputRows = scan.metrics("output_rows")
1175+
val (midStream, atEnd) = SQLExecution.withSQLConfPropagated(spark) {
1176+
scan
1177+
.executeColumnar()
1178+
.mapPartitions { batches =>
1179+
Thread.sleep(100)
1180+
batches.next()
1181+
val mid = outputRows.value
1182+
batches.foreach(_ => ())
1183+
Iterator((mid, outputRows.value))
1184+
}
1185+
.collect()
1186+
.head
1187+
}
1188+
check(midStream, atEnd)
1189+
}
1190+
1191+
// With the interval disabled, the only publish is the one in releasePlan, which a
1192+
// per-batch publish would break after the first batch.
1193+
checkOutputRows("-1") { (midStream, atEnd) =>
1194+
assert(midStream == 0, s"output_rows was published mid-stream: $midStream")
1195+
assert(atEnd == 10000, s"releasePlan should publish the final output_rows, got $atEnd")
1196+
}
1197+
1198+
// A 1 ms interval has passed by the time the first batch arrives, so that batch publishes.
1199+
checkOutputRows("1") { (midStream, atEnd) =>
1200+
assert(midStream > 0, "output_rows was not published mid-stream once the interval passed")
1201+
assert(atEnd == 10000, s"releasePlan should publish the final output_rows, got $atEnd")
1202+
}
1203+
}
1204+
}
1205+
11571206
test("native scan left unconsumed by a limit still reports task input metrics") {
11581207
withTempPath { dir =>
11591208
spark

0 commit comments

Comments
 (0)