jischeng opened a new pull request, #60323:
URL: https://github.com/apache/spark/pull/60323
### What changes were proposed in this pull request?
This PR improves `HybridQueue` / `HybridRowQueue` disk spill lifecycle and
execution memory isolation during Python UDF and UDTF evaluation:
1. **Bounded disk spill segment rotation & automatic in-memory recovery**:
- Adds `Queue.finishWrite()` (implemented by `DiskRowQueue` to close its
`DataOutputStream` as soon as writing to a segment finishes rather than keeping
file descriptors open until read).
- Rotates active disk queues in `HybridQueue.add` once
`currentDiskQueueBytes >= maxDiskQueueBytes` (`memManager.pageSizeBytes()` by
default, or `min(pageSizeBytes, max(maxMemoryBytes, 64KB))` when a positive
memory cap is configured).
- Because segment rotation triggers `createNewQueue`, drained disk
segments are closed and deleted incrementally while the task is still running,
and `HybridRowQueue` automatically recovers back to `InMemoryRowQueue` pages
once transient memory pressure clears.
2. **Optional per-queue execution memory cap
(`spark.sql.execution.python.udf.rowQueue.maxMemoryBytes`)**:
- Adds internal configuration
`spark.sql.execution.python.udf.rowQueue.maxMemoryBytes` (default `-1L`,
meaning no per-queue cap beyond available task execution memory; `0` forces
disk-only queuing; positive values cap per-queue in-memory bytes and allocate
sub-page blocks when `remainingBudget < pageSizeBytes`).
- Wires `SQLConf.get.pythonUDFRowQueueMaxMemoryBytes` into
`HybridRowQueue` factory methods and `InProcessArrowEvalPythonEvaluatorFactory`.
### Why are the changes needed?
Previously, once `HybridRowQueue` fell back to `DiskRowQueue` (e.g., on a
transient `SparkOutOfMemoryError`), `DiskRowQueue.add` returned `true`
indefinitely. Every subsequent row for the rest of the task was appended to a
single growing temp file that could not be deleted until the consumer caught up
to it, and the queue never attempted to return to in-memory pages even after
memory pressure subsided. In addition, workloads with fast JVM scans, large
pass-through rows, and CPU/GPU-heavy Python UDFs had no way to bound per-queue
`ExecutionMemoryPool` consumption before spilling.
### Does this PR introduce _any_ user-facing change?
No default behavioral regression:
`spark.sql.execution.python.udf.rowQueue.maxMemoryBytes` is internal and
defaults to `-1` (unlimited). When disk spilling occurs, spill data is now
split across bounded rolling segment files instead of a single monolithic file.
### How was this patch tested?
Added 5 unit tests in `RowQueueSuite`:
- `hybrid queue rotates disk segments and recovers to memory under default
unlimited cap`
- `hybrid queue enforces maxMemoryBytes cap without exhausting execution
memory`
- `hybrid queue with maxMemoryBytes = 0 uses disk only`
- `hybrid queue recovers to in-memory pages after capped memory is drained`
- `hybrid queue honors maxMemoryBytes smaller than pageSizeBytes`
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Google Antigravity
Closes #60322
--
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]