Repository navigation
Conversation
A spill of several buffered runs merges them into one file. Before writing anything, the row-format merge converts the keys of the first batch of every run. Dictionary keys, including run-end-encoded and struct-wrapped ones, can expand far beyond the pool, so the spill fails with ResourcesExhausted before it releases any input. Convert the keys of each run's first batch one run at a time instead. When the pool refuses a run, merge and spill the runs added so far, which releases their memory, then continue with the refused run. A run whose keys do not fit alone is spilled unmerged. When all keys fit, the spill writes one file as before, so sorts that succeeded on main write the same spill files. Co-authored-by: Amp <amp@ampcode.com>
|
run benchmark sort_tpch env:
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
DATAFUSION_RUNTIME_MEMORY_LIMIT: "128M" |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing sort-spill-alloc-fallback (e16c63a) to 9d894e1 (merge-base) diff Run configurationrun benchmark sort_tpch
env:
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
DATAFUSION_RUNTIME_MEMORY_LIMIT: "128M"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing sort-spill-alloc-fallback (e16c63a) to 9d894e1 (merge-base) diff Run configurationrun benchmark sort_tpch
env:
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
DATAFUSION_RUNTIME_MEMORY_LIMIT: "128M"CPU Details (lscpu)Details
Memory Pool PeaksPeak Base:
Pool accounting vs. process RSS Max pool peak is the largest reservation any single query in the run reached; peak RSS covers the whole invocation, including data loading and allocator retention, and the two high-water marks need not coincide in time. The gap is therefore an upper bound on what the pool did not account for, not a measurement of it.
Resource Usagesort_tpch — base (merge-base)
sort_tpch — branch
File an issue against this benchmark runner |
kosiew
left a comment
There was a problem hiding this comment.
Thanks for addressing the row-format expansion problem. Grouping runs based on actual memory availability is preferable to predicting key expansion and preserves parallel run preparation.
However, successful first-batch admission does not guarantee that a merge can finish spilling. We need to address this remaining failure path before merging.
| break; | ||
| } | ||
| } | ||
| let sorted_stream = if cursors.partitions() > 0 { |
There was a problem hiding this comment.
First-batch admission cannot prevent refill failures. Spill singletons directly, preserving primed batches, and ensure multi-run merges can finish or safely fall back without losing or duplicating rows. Add a SortExec ingestion-spill regression with within-run key expansion, including multi-run refill if supported.
| async fn test_ingestion_spill_groups_cleanup(#[case] fail: bool) -> Result<()> { | ||
| let keys = EncodedKeys::new(KeyShape::Dictionary, |_| 64); | ||
| let files = Arc::new(GatedFiles { | ||
| // The fourth file of the first ingestion spill |
There was a problem hiding this comment.
Could we track finish() calls and gate the write after an earlier group completes within the same ingestion spill, rather than relying on a fixed file index? Retain the abort and resource cleanup assertions.
| /// Sorts the in-memory batches and merges them into a single sorted run, then writes | ||
| /// the result to spill files. |
There was a problem hiding this comment.
Could we update this documentation to reflect that one ingestion spill can produce multiple sorted files? Please also update spill_runs documentation to reflect the final admission and fallback policy.
Which issue does this PR close?
Rationale for this change
Rationale for this change
When a sort runs out of memory, it spills its buffered batches to disk. Before writing anything, the spill merges all buffered sorted runs into one file, and the row-format merge converts the sort keys of every run's first batch at once. Dictionary keys, including run-end-encoded and struct-wrapped ones, are decoded into their values in the row format, so these keys can be many times larger than the buffered data. The spill then fails with
ResourcesExhaustedbefore it has released any input, so a sort that should spill fails instead.#27043 avoided the merge for selected spills by predicting which keys might expand. Predictions miss some cases, such as nested dictionaries, and also catch cases that would have fit, such as short strings or low-expansion dictionaries. Those spills then pay for extra spill files, extra disk usage and lost parallel preparation.
What changes are included in this PR?
Convert the keys of each run's first batch one run at a time instead. When the pool refuses a run, merge and spill the runs added so far, which releases their memory, then continue with the refused run. A run whose keys do not fit alone is spilled unmerged. When all keys fit, the spill writes one file as before, so sorts that succeeded on main write the same spill files.
What is the testing strategy for this PR?
Unit tests.
Are there any user-facing changes?
No, just enhanced spill behavior.
@alamb @kosiew