venkata91 opened a new pull request, #19298: URL: https://github.com/apache/iceberg/pull/19298
## Intent Fix `https://github.com/apache/iceberg/issues/19294` on main. scanDuration should be timed once per task and never per row. Context from the issue: `BaseReader.next()` calls `System.nanoTime()` at the start and again in `finally` to add to `scanDurationNanos`. On the batch path that is twice per batch, which is fine. On the row path `next()` runs once per row, so every row pays two clock reads. gerashegalov measured about 37 ns/row, +58% on a narrow `SELECT sum(id)` with vectorization off, while reviewing the 3.5 backport (#19022). Spark's `DataSourceScanExec` avoids per-row scan timing for the same reason. scanDuration is not released yet (not in 1.12.0), so fixing it before 1.13 means no release ever has the per-row cost. Per-row timing was never the intended design. Validate and open the PR for the once-per-task scanDuration fix, and also fix the same multi-task test gap in the Spark 4.1 and 4.2 versions of TestSparkReadMetrics.scanDurationAggregatedAcrossTasks in that PR: yes go ahead. ## What Changed - In Spark 4.1 and 4.2, `BaseReader` stops reading the clock on every `next()` call. Row readers now time each task as a whole, from opening the first file until the reader is exhausted or closed, using a new `ScanTimer`. `BaseBatchReader` overrides `timeEachCall()`, so batch reads are still timed once per `next()`. - `MergingSortedRowDataReader` (Spark 4.1) used to sum the durations of its per-file readers. It now times the whole merge with one timer, so files that are open at the same time are no longer counted twice. - New unit tests in `TestBaseReader` and `TestMergingSortedRowDataReader` cover the new timing behavior. `TestSparkReadMetrics.scanDurationAggregatedAcrossTasks` (4.1 and 4.2) now forces two tasks and checks that the driver's `scanDuration` equals the sum of the two task values. ## Risk Assessment ✅ Low: This is a small metrics-only change in Spark 4.1 and 4.2. The row path now reads the clock once per task (start on first open, stop on exhaustion or close), and the batch path keeps per-call timing as the user chose in round 1. MergingSortedRowDataReader times the merge window once instead of summing overlapping per-file windows. The tests compare against independent oracles: wall-clock bounds, and Spark per-task accumulator updates summed against the driver total. ## Testing Ran only the targeted reader and read-metrics test classes for Spark 4.1 and 4.2, not the full suite. TestSparkReadMetrics runs real local Spark SQL reads against disposable Iceberg tables, with vectorization both on and off, and checks the scanDuration SQL metric and the per-task accumulator updates. TestBaseReader and TestMergingSortedRowDataReader cover the timer behavior: on the row path, time between rows is included (which is only possible if the timer runs once per task rather than per row); per-call timing excludes time between calls; and files open at the same time are not counted twice. Everything passed. My first combined Gradle invocation attached the --tests filters only to the 4.2 task, so 4.1 started running its full suite. I stopped it and reran 4.1 with its own filters; that rerun is the 4.1 result reported here. There is no UI surface, so no screenshots. - Live validation: ✅ go - 6 of 6 scenarios driven live against the product | Scenario | Result | Live | Evidence | | --- | --- | --- | --- | | Multi-task local Spark SQL read (2 files, split-size=1): each of the two tasks reports a positive scanDuration and the SQL metric equals their sum (Spark 4.1 and 4.2) | ✅ pass | live | TestSparkReadMetrics.scanDurationAggregatedAcrossTasks passed in spark-4.1-results and spark-4.2-results XML | | Non-vectorized local Spark read (RowDataReader) reports a positive scanDuration in the SQL metric (Spark 4.1 and 4.2) | ✅ pass | live | TestSparkReadMetrics.scanDurationForNonVectorizedRead passed on both versions | | Row path times the task as a whole, not each row: time spent between next() calls is included in scanDuration | ✅ pass | live | TestBaseReader.scanDurationIncludesTimeBetweenRows passed on 4.1 and 4.2 | | Adversarial: task stopped early (e.g. by LIMIT) reports elapsed time while still open; time after the reader is exhausted is not counted | ✅ pass | live | TestBaseReader.scanDurationIncludesTaskStillBeingRead and scanDurationStopsWhenReaderIsExhausted passed on both versions | | Batch path keeps per-call timing (review round 1 decision): time between calls is excluded | ✅ pass | live | TestBaseReader.scanDurationExcludesTimeBetweenCallsWhenTimingEachCall passed on both versions | | Merging sorted row reader (Spark 4.1) does not count concurrently open files twice | ✅ pass | live | TestMergingSortedRowDataReader.scanDurationDoesNotCountConcurrentlyOpenFilesTwice passed | <details> <summary>Evidence: Spark 4.1 targeted Gradle run log</summary> ```text Configuration on demand is an incubating feature. > Task :iceberg-common:processResources NO-SOURCE > Task :iceberg-arrow:processResources NO-SOURCE > Task :iceberg-azure:processResources NO-SOURCE > Task :iceberg-api:processResources NO-SOURCE > Task :iceberg-gcp-bundle:compileJava NO-SOURCE > Task :iceberg-hive-metastore:processResources NO-SOURCE > Task :iceberg-gcp:processResources NO-SOURCE > Task :iceberg-aws-bundle:compileJava NO-SOURCE > Task :iceberg-azure-bundle:compileJava NO-SOURCE > Task :iceberg-orc:processResources NO-SOURCE > Task :iceberg-core:processResources NO-SOURCE > Task :iceberg-data:processResources NO-SOURCE > Task :iceberg-aws:processResources UP-TO-DATE > Task :iceberg-open-api:processTestFixturesResources UP-TO-DATE > Task :iceberg-hive-metastore:processTestResources UP-TO-DATE > Task :iceberg-orc:processTestResources NO-SOURCE > Task :iceberg-aws-bundle:processResources NO-SOURCE > Task :iceberg-azure-bundle:processResources NO-SOURCE > Task :iceberg-aws-bundle:classes UP-TO-DATE > Task :iceberg-parquet:processResources NO-SOURCE > Task :iceberg-gcp-bundle:processResources NO-SOURCE > Task :iceberg-azure-bundle:classes UP-TO-DATE > Task :iceberg-data:processTestResources NO-SOURCE > Task :iceberg-spark:iceberg-spark-4.1_2.13:processResources UP-TO-DATE > Task :iceberg-gcp-bundle:classes UP-TO-DATE > Task :iceberg-core:processTestResources UP-TO-DATE > Task :iceberg-spark:iceberg-spark-4.1_2.13:processTestResources UP-TO-DATE > Task :iceberg-parquet:processTestFixturesResources UP-TO-DATE > Task :generateGitProperties > Task :buildInfo > Task :iceberg-bundled-guava:compileJava UP-TO-DATE > Task :iceberg-bundled-guava:processResources NO-SOURCE > Task :iceberg-bundled-guava:classes UP-TO-DATE > Task :iceberg-bundled-guava:compileTestJava NO-SOURCE > Task :iceberg-bundled-guava:shadowJar UP-TO-DATE > Task :iceberg-common:compileJava UP-TO-DATE > Task :iceberg-common:classes UP-TO-DATE > Task :iceberg-common:jar UP-TO-DATE > Task :iceberg-azure-bundle:shadowJar UP-TO-DATE > Task :iceberg-azure-bundle:jar SKIPPED > Task :iceberg-api:processTestResources > Task :iceberg-api:compileJava UP-TO-DATE > Task :iceberg-api:classes UP-TO-DATE > Task :iceberg-api:jar UP-TO-DATE > Task :iceberg-aws-bundle:shadowJar UP-TO-DATE > Task :iceberg-aws-bundle:jar SKIPPED > Task :iceberg-api:compileTestJava UP-TO-DATE > Task :iceberg-api:testClasses > Task :iceberg-gcp-bundle:shadowJar UP-TO-DATE > Task :iceberg-gcp-bundle:jar SKIPPED > Task :iceberg-core:compileJava UP-TO-DATE > Task :iceberg-core:classes UP-TO-DATE > Task :iceberg-core:jar UP-TO-DATE > Task :iceberg-azure:compileJava UP-TO-DATE > Task :iceberg-azure:classes UP-TO-DATE > Task :iceberg-azure:jar UP-TO-DATE > Task :iceberg-api:testJar file '{run-worktree}/build/iceberg-build.properties' will be copied to 'iceberg-build.properties', overwriting file '{run-worktree}/api/build/resources/test/iceberg-build.properties', which has already been copied there. > Task :iceberg-gcp:compileJava UP-TO-DATE > Task :iceberg-gcp:classes UP-TO-DATE > Task :iceberg-aws:compileJava UP-TO-DATE > Task :iceberg-aws:classes UP-TO-DATE > Task :iceberg-gcp:jar UP-TO-DATE > Task :iceberg-parquet:compileJava UP-TO-DATE > Task :iceberg-aws:jar UP-TO-DATE > Task :iceberg-orc:compileJava UP-TO-DATE > Task :iceberg-orc:classes UP-TO-DATE > Task :iceberg-parquet:compileTestFixturesJava UP-TO-DATE > Task :iceberg-parquet:classes UP-TO-DATE > Task :iceberg-orc:jar UP-TO-DATE > Task :iceberg-parquet:jar UP-TO-DATE > Task :iceberg-parquet:testFixturesClasses UP-TO-DATE > Task :iceberg-parquet:testFixturesJar UP-TO-DATE > Task :iceberg-hive-metastore:compileJava UP-TO-DATE > Task :iceberg-hive-metastore:classes UP-TO-DATE > Task :iceberg-data:compileJava UP-TO-DATE > Task :iceberg-data:classes UP-TO-DATE > Task :iceberg-hive-metastore:jar UP-TO-DATE > Task :iceberg-arrow:compileJava UP-TO-DATE > Task :iceberg-arrow:classes UP-TO-DATE > Task :iceberg-spark:iceberg-spark-4.1_2.13:compileJava NO-SOURCE > Task :iceberg-data:jar UP-TO-DATE > Task :iceberg-arrow:jar UP-TO-DATE > Task :iceberg-core:compileTestJava UP-TO-DATE > Task :iceberg-core:testClasses UP-TO-DATE > Task :iceberg-core:testJar UP-TO-DATE > Task :iceberg-open-api:compileTestFixturesJava UP-TO-DATE > Task :iceberg-open-api:testFixturesClasses UP-TO-DATE > Task :iceberg-open-api:testFixturesJar UP-TO-DATE > Task :iceberg-orc:compileTestJava UP-TO-DATE > Task :iceberg-orc:testClasses UP-TO-DATE > Task :iceberg-orc:testJar UP-TO-DATE > Task :iceberg-hive-metastore:compileTestJava UP-TO-DATE > Task :iceberg-spark:iceberg-spark-4.1_2.13:compileScala UP-TO-DATE > Task :iceberg-spark:iceberg-spark-4.1_2.13:classes UP-TO-DATE > Task :iceberg-hive-metastore:testClasses UP-TO-DATE > Task :iceberg-hive-metastore:testJar UP-TO-DATE > Task :iceberg-data:compileTestJava UP-TO-DATE > Task :iceberg-data:testClasses UP-TO-DATE > Task :iceberg-data:testJar UP-TO-DATE > Task :iceberg-spark:iceberg-spark-4.1_2.13:compileTestJava UP-TO-DATE > Task :iceberg-spark:iceberg-spark-4.1_2.13:compileTestScala NO-SOURCE > Task :iceberg-spark:iceberg-spark-4.1_2.13:testClasses UP-TO-DATE OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended > Task :iceberg-spark:iceberg-spark-4.1_2.13:test [Incubating] Problems report is available at: file://{run-worktree}/build/reports/problems/problems-report.html Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. For more on this, please refer to https://docs.gradle.org/8.14.8/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation. BUILD SUCCESSFUL in 39s 53 actionable tasks: 4 executed, 49 up-to-date ``` </details> - Evidence: Spark 4.1 JUnit XML results (local file: <code>~/.no-mistakes/evidence/01M8X8WBP3ASDK7R8KA8QPT8Q8/spark-4.1-results</code>) - Evidence: Spark 4.2 JUnit XML results (local file: <code>~/.no-mistakes/evidence/01M8X8WBP3ASDK7R8KA8QPT8Q8/spark-4.2-results</code>) <details> <summary>Evidence: scanDuration-related test cases executed</summary> ```text == Spark 4.1 scanDurationIncludesTimeBetweenRows() scanDurationExcludesTimeBetweenCallsWhenTimingEachCall() scanDurationIncludesTaskStillBeingRead() scanDurationStopsWhenReaderIsExhausted() scanDurationAggregatedAcrossTasks() testDeleteMetrics() scanDurationForNonVectorizedRead() metricsWhenTaskReadsSeveralPartitions() testReadMetricsForV1Table() testReadMetricsForV2Table() scanDurationDoesNotCountConcurrentlyOpenFilesTwice() metricsIncludeValuesFromPreviousReader() metricsReportTasksAndDeletes() == Spark 4.2 scanDurationIncludesTimeBetweenRows() scanDurationExcludesTimeBetweenCallsWhenTimingEachCall() scanDurationIncludesTaskStillBeingRead() scanDurationStopsWhenReaderIsExhausted() scanDurationAggregatedAcrossTasks() testDeleteMetrics() scanDurationForNonVectorizedRead() testReadMetricsForV1Table() testReadMetricsForV2Table() metricsReportTasksAndDeletes() ``` </details> ## Pipeline Updates from [git push no-mistakes](https://github.com/kunchenguid/no-mistakes) <!-- no-mistakes-pipeline-attestation:v1 {"head_sha":"9b31ea41edabf5d3c530d3c7b7102b27a66e3d3b","steps":[{"step":"intent","status":"completed"},{"step":"rebase","status":"completed"},{"step":"review","status":"completed"},{"step":"test","status":"completed"},{"step":"document","status":"completed"},{"step":"lint","status":"completed"},{"step":"push","status":"completed"},{"step":"pr","status":"running"},{"step":"ci","status":"pending"}],"live_validation":{"verdict":"go","live":6,"total":6}} --> <details> <summary>✅ **Intent** - passed</summary> ✅ No issues found. </details> <details> <summary>✅ **Rebase** - passed</summary> ✅ No issues found. </details> <details> <summary>🔧 **Review** - 1 issue found → auto-fixed ✅</summary> - ⚠️ `spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/BaseReader.java:153` - Once-per-task timing changes what scanDuration means on the vectorized batch path, not just the row path. The issue says per-batch timing was "fine". Before this change, BatchDataReader reported only the time spent inside next(), which is pure read time. Now ScanTimer runs from the first open() until the reader is exhausted or closed, so it also counts every downstream operator in the same whole-stage pipeline (aggregation, shuffle write, and so on) between batches. Example: `SELECT key, sum(v) ... GROUP BY key` with vectorization on now reports roughly the whole task's wall time as "total scan duration" instead of the Parquet decode time. This matches the literal requirement "timed once per task and never per row", but the batch path didn't need the change: Spark's FileSourceScanExec keeps timing per batch and only drops per-row timing. Please confir m the batch path should also switch to task wall time. The narrower alternative is to keep per-call timing in next() for batch readers and use ScanTimer only on the row path (RowDataReader, MergingSortedRowDataReader). Same sites: spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/BaseReader.java:153 and :157 (start/stop shared by BatchDataReader and RowDataReader); spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/BaseReader.java:157 and :183. 🔧 Fix applied. ✅ Re-checked - no issues remain. </details> <details> <summary>✅ **Test** - passed</summary> ✅ No issues found. - Live validation: ✅ go - 6 of 6 scenarios driven live against the product | Scenario | Result | Live | Evidence | | --- | --- | --- | --- | | Multi-task local Spark SQL read (2 files, split-size=1): each of the two tasks reports a positive scanDuration and the SQL metric equals their sum (Spark 4.1 and 4.2) | ✅ pass | live | TestSparkReadMetrics.scanDurationAggregatedAcrossTasks passed in spark-4.1-results and spark-4.2-results XML | | Non-vectorized local Spark read (RowDataReader) reports a positive scanDuration in the SQL metric (Spark 4.1 and 4.2) | ✅ pass | live | TestSparkReadMetrics.scanDurationForNonVectorizedRead passed on both versions | | Row path times the task as a whole, not each row: time spent between next() calls is included in scanDuration | ✅ pass | live | TestBaseReader.scanDurationIncludesTimeBetweenRows passed on 4.1 and 4.2 | | Adversarial: task stopped early (e.g. by LIMIT) reports elapsed time while still open; time after the reader is exhausted is not counted | ✅ pass | live | TestBaseReader.scanDurationIncludesTaskStillBeingRead and scanDurationStopsWhenReaderIsExhausted passed on both versions | | Batch path keeps per-call timing (review round 1 decision): time between calls is excluded | ✅ pass | live | TestBaseReader.scanDurationExcludesTimeBetweenCallsWhenTimingEachCall passed on both versions | | Merging sorted row reader (Spark 4.1) does not count concurrently open files twice | ✅ pass | live | TestMergingSortedRowDataReader.scanDurationDoesNotCountConcurrentlyOpenFilesTwice passed | - <code>`./gradlew -DsparkVersions=4.1 :iceberg-spark:iceberg-spark-4.1_2.13:test --tests org.apache.iceberg.spark.source.TestBaseReader --tests org.apache.iceberg.spark.source.TestSparkReadMetrics --tests org.apache.iceberg.spark.source.TestMergingSortedRowDataReader` (BUILD SUCCESSFUL; 9 + 36 + 9 tests, 0 failures)</code> - <code>`:iceberg-spark:iceberg-spark-4.2_2.13:test` with the same three --tests filters (9 + 34 + 8 tests, 0 failures)</code> - <code>Spark 4.1/4.2 `TestSparkReadMetrics.scanDurationAggregatedAcrossTasks`: a real local Spark SQL read of 2 files with split-size=1; a SparkListener records the per-task scanDuration accumulator updates</code> - <code>Spark 4.1/4.2 `TestSparkReadMetrics.scanDurationForNonVectorizedRead`: a local Spark read with vectorization off (RowDataReader)</code> - <code>Spark 4.1/4.2 `TestBaseReader.scanDuration*`: timing on the row path vs per-call timing</code> - <code>Spark 4.1 `TestMergingSortedRowDataReader.scanDurationDoesNotCountConcurrentlyOpenFilesTwice`</code> </details> <details> <summary>✅ **Document** - passed</summary> ✅ No issues found. </details> <details> <summary>✅ **Lint** - passed</summary> ✅ No issues found. </details> <details> <summary>✅ **Push** - passed</summary> ✅ No issues found. </details> ## Issues Closes #19294 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
