Repository navigation
fix: count every hash-based JVM shuffle batch as shuffle bytes written - #6369
Conversation
…ot spill CometDiskBlockWriter appends every batch, whether written at the batch size, under memory pressure, or on close, to the partition file that CometBypassMergeSortShuffleWriter concatenates into the map output. It counted only the final batch as shuffle bytes written, reported the earlier batches as disk spill, and dropped their write time. Count every batch with the task's shuffle write metrics, as Spark's BypassMergeSortShuffleWriter does for its partition files, and report nothing as spill.
…pill A flush that CometDiskBlockWriter makes to relieve memory pressure now adds its bytes to the task's diskBytesSpilled and the memory it freed to memoryBytesSpilled, like a sort-based spill. The flush still counts as shuffle bytes and records written, since it lands in the partition file that becomes the map output. Flushes on reaching the batch size and the final flush on close count only as writes. The log line for a batch-size flush no longer calls it a spill and moves to debug level.
sunchao
left a comment
There was a problem hiding this comment.
Summary
- Prior state and problem: Intermediate JVM hash-shuffle batches were reported as disk spill instead of shuffle output, understating bytes written and write time.
- Design approach: Pass the task’s
ShuffleWriteMetricsReporterto every batch write. Separately record disk bytes and freed memory for allocation-pressure spills. - Correctness / compatibility analysis: Every batch is appended to a partition file that becomes map output. Counting those bytes once matches Spark’s bypass-writer accounting. Checked the relevant Spark sources for 3.4.3, 3.5.9, 4.0.4, 4.1.3 and 4.2.0. The final concatenation does not increment bytes again.
- Key design decisions:
underMemoryPressuredistinguishes spill accounting from ordinary batch writes without changing allocation or spill selection. Removing temporary metrics objects and per-batch INFO logging avoids that existing overhead. Added spill counters run per pressure-triggered flush. - Implementation sketch:
doSpilling()always updates write metrics.doSpill(true)additionally records the bytes written and pages actually freed. Tests cover repeated batch writes and pressure-driven flushing of sibling writers. - Behavioral changes worth calling out: Batch-size and final writes report no spill. Memory-pressure writes contribute to both shuffle output and spill metrics. Output data and the sort-based writer are unchanged.
- Suggested improvements: None at the P1/P2 bar. No introduced P1/P2 issues found within this review.
Reviewed full SHA b4d7f3c323bdb810ef27386b710e1acc90c22f0c against base 8805e3f066440752ac98d5b48e197b43e4fb9ebd, covering both PR commits and all three files in the full merge-base diff. The additional main-only commit is not a PR change. The PR is not a draft. No existing reviews, issue comments, inline comments or review threads were present.
Routed skills: review-comet-pr, review-comet-shuffle-pr, and review-comet-memory-pr.
Exact-head CI: Required checks passed, including Linux builds, Rust tests, Spark 4.1 Comet suites and TPC-H/TPC-DS checks. Downloaded reports confirm both changed tests passed, alongside all 4 CometDiskBlockWriterSuite, 25 CometTaskMetricsSuite and 46 CometShuffleSuite tests.
Validation limits: No local JVM/native build or test execution was completed. Runtime evidence comes from exact-head CI. Spark SQL, Iceberg and macOS suites were skipped, and non-4.1 runtime profiles were not exercised. No performance benchmark was run.
Which issue does this PR close?
Closes #6258.
Rationale for this change
In the hash-based JVM columnar shuffle (
CometBypassMergeSortShuffleWriter), each partition'sCometDiskBlockWriterbuffers rows and appends them to the partition's file one Arrow IPC batch at a time. A batch is written in three cases:spark.comet.shuffle.jvm.batchSizerows,close().writePartitionedDatathen concatenates the partition files into the map output.CometDiskBlockWriter.ArrowIPCWriter.doSpilling(boolean isLast)gave native a throwawayShuffleWriteMetricsfor every batch except the one written inclose(), and added those bytes to the task'sdiskBytesSpilled. As a result:memoryBytesSpilled.The map output itself was correct.
A batch that fills up is an ordinary write, as it is in Spark's
BypassMergeSortShuffleWriter. A batch written because memory ran out is a spill, but it goes into the same partition file and becomes part of the map output. This writer has no merge phase that would count it as written later, so it has to count as both a spill and a write.What changes are included in this PR?
CometDiskBlockWriternow reports its metrics as follows:ShuffleWriteMetricsReporteris passed toSpillWriter.doSpillingfor every batch.ArrowIPCWriter.spill, which flushes the requesting writer and, if needed, the task's other partition writers. Its bytes go todiskBytesSpilled, and the memory pages it freed go tomemoryBytesSpilled, as inCometShuffleExternalSorter.spilland Spark'sShuffleExternalSorter.spill. So its bytes show up in both the shuffle write size and "Spill (Disk)".spark.comet.shuffle.jvm.spillThreshold) and the final batch inclose()count only as writes.ShuffleExternalSorter.writeSortedFile,ExternalSorter). Its bypass writer counts every write to its partition files, and a spilled batch here is written once, into its partition file.The sort-based writer (
CometUnsafeShuffleWriter/SpillSorter) writes real spill files and merges them into the output. Its accounting already matches Spark'sUnsafeShuffleWriterand is unchanged.How are these changes tested?
A new test in
CometTaskMetricsSuite,JVM hash shuffle counts batch-size writes as shuffle bytes written, not spill, runs ajvmmode shuffle of 20,000 rows into 4 partitions withspark.comet.shuffle.jvm.batchSize=100, so each partition writer writes many batches. It asserts that the plan has a Comet columnar shuffle exchange whose shuffle handle isCometBypassMergeSortShuffleHandle, so the hash-based writer ran. It then checks that:shuffleBytesWrittenSQL metric and the stages' shuffle write bytes in the status store both equal the total size of the shuffle's.datafiles,The memory pressure test in
CometDiskBlockWriterSuite,memory pressure spills only writers of the requesting task, now checks the spill and write metrics together:diskBytesSpilledequals exactly the bytes in its partition files before they are closed (all written by spills), andmemoryBytesSpilledis greater than 0.I ran the new end-to-end test and the memory pressure test before changing the main code. The end-to-end test failed with
17523 did not equal 316630: the reported bytes written covered about 5.5% of the map output. The memory pressure test's bytes-written check failed because the task reported 0 bytes written after its spills.With the fix, on the default Spark 4.1 profile,
CometDiskBlockWriterSuite(4 tests),CometTaskMetricsSuite(25 tests), andCometShuffleSuite(46 tests) all pass.