Complete a close deferred by a failed spill #16476
Non-blocking: should this only close when this thread still owns the spill? Some spill bodies clear spilling and complete the deferred close themselves, then can still throw. For example, SpillableHostColumnarBatchHandle closes the DiskHandleBuilder after publishing, so this catch would call doClose() a second time. Today's doClose implementations happen to be idempotent, so this is defensive:
val closeNow = synchronized {
val ownsSpill = spilling
spilling = false
ownsSpill && closeAfterFailedSpill
}
Non-blocking alternative: spill() removes dev under the lock but appends it to pendingRelease in a second synchronized inside executeSpill, so a failure in between drops the evicted object. Moving pendingRelease = pendingRelease ++ dev.toSeq into the same block that sets dev = None closes that window. spill() then no longer needs executeSpill, and this override becomes unnecessary.
Non-blocking: a variant where the spill fails while the handle is still open would guard against over-closing: the data should be kept and the handle should stay spillable. The disk-staged compressed-batch case also seems easy to include here.
Nit: the spill() scaladoc above still says a failed spill makes no attempt to restore state and is non-recoverable. That has been outdated since #12705, and this change also completes a deferred close on failure.
Good catch. Adopted: executeSpill now records whether spilling was still set before resetting it, and closes only in that case. I also added "failed spill does not repeat a close its block already completed", which fails against the previous revision (doClose ran twice) and passes now.
Done. The evicted object now moves to pendingRelease in the same locked block that clears dev. spill() no longer goes through executeSpill, and the closeAfterFailedSpill hook is gone along with its override.
Added both. Every failed-spill variant now also runs with the handle left open. The test checks that the data survives by materializing and verifying it, that the handle stays spillable, and that a later close() releases everything. The disk-staged compressed batch is included, with a check that no spill file is left behind.
Updated. The scaladoc now says that a failed spill propagates the exception and keeps the data, or completes the close if the handle was closed during the spill.
<a href="#"><img alt="P0" src="https://greptile-static-assets.s3.amazonaws.com/badges/p0.svg?v=9" align="top"></a> Performance exemption misses successful spills
SharedRecomputableHandle.spill() now queues the resource under the first lock and removes the executeSpill call on successful spills. This contradicts the description’s reason for marking performance testing unnecessary: that only failed spills change.
The repository requires performance results or a verifiable explanation that runtime performance cannot change. Add results for successful shared-handle spills or revise the exemption to cover this change.
Rule Used: Report Performance: Not required as a high-severity finding unless the pull request is documentation-only or test-only, or its description gives a verifiable reason the change cannot affect runtime performance. A bug-fix label, small diff, or rarel... (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
The description was updated alongside this commit to cover it (see the Performance note). On the successful path, SharedRecomputableHandle.spill() now takes its lock once instead of twice and no longer goes through executeSpill. For this handle, executeSpill is the base SpillableHandle version: a try/catch around a closure, without the spill timing or byte metrics that DeviceSpillableHandle adds (SharedRecomputableHandle extends DeviceStoreHandle directly). It performs the same state updates in the same order, without the second lock or the closure, so the change can only remove work and not add any.
You're right—the updated Performance note addresses the successful-path change, and the implementation supports that explanation. SharedRecomputableHandle uses the base executeSpill implementation, so removing that wrapper and its second lock does not remove the timing or byte metrics added by DeviceSpillableHandle. The state updates remain in order, with less work on the successful path. I retract my request for performance results.
I couldn't post this as a PR comment because permission to submit it was not granted.
when a device handle was closed during the spill, this doClose() frees dev inside trySpill, before postSpill calls synchronizeDevice(). That breaks the rule in releaseSpilled's scaladoc and reopens #16198 on the failure path. Handing it to postSpill, as a successful spill does, would fix it.
Let me know if you need help with writing tunit-tests to trigger that scenario.
This throws before underlying.copyNext opens the spill file, so the disk-staged stagingRemoved check passes whatever the cleanup does. With the throw after the write, the file is left behind on this head, which is #16205. Could you fix #16205 here and take this change, or drop the check and the description's claim?
override def copyNext(buffer: DeviceMemoryBuffer, length: Long,
stream: Cuda.Stream): Unit = {
underlying.copyNext(buffer, length, stream)
copyStarted.countDown()
await(resumeCopy)
throw new IllegalStateException("injected host copy failure")
}
Good catch, and thanks for the offer of tests. Fixed in 4a1fd23a9f and 7bf8515091: a failed device spill now keeps spilling set, as a successful one does, whether or not the handle was closed yet. The spill plan hands it to postSpill, so after synchronizeDevice() releaseSpilled resets spilling, keeps the device data, and completes a close that arrived during the spill or before the synchronize. That also covers a close that arrives after the failure. Host handles still close in the spilling thread. The failed-spill tests now run device spills through the device store and pause at the synchronize. There they check that the spill still owns the handle and its device data is intact, with the handle closed during the spill, closed after the failure, or left open. Each of those checks was confirmed to fail on the GPU with the fix broken. Two failure points outside the spill body, before it starts and after the host copy is published, predate this PR and are tracked in #16526.
You're right, the throw came before the spill file was created, so that check proved nothing. I took your change: the failure now comes after underlying.copyNext. I dropped the check and the description's claim rather than folding #16205 in. DiskHandleBuilder takes a startPos for shared files, so deleting on close needs its own design and review. The test now removes the leftover file in cleanup and points at #16205.
nit: can the spillFailed be called twice? If another thread's spill() of this handle throws while the failure waits for its sync, that plan adds the handle too, and the second releaseSpilled frees an open handle's only copy.
Good question. At 0c53d5e6c5 it can't happen: while a failed spill waits for its synchronize, spilling stays set and dev is still present. So another thread's spillable can't throw, since every override throws only when the data is missing, and its claim sees spilling and returns 0. Only the thread whose spill failed can reach that check with spillFailed set.
thirtiseven
gerashegalov
Fixes #16333.
Description
If
close()is called on a spillable handle while a spill is running, the close is deferred to the spilling thread. When that spill then failed,SpillableHandle.executeSpillresetspillingbut never completed the deferred close. The handle was leftclosedwith its resource never released, and it stayed in its store, where later spill plans could still pick it.A failed spill now completes a close that was deferred to it:
spillingwas still set. A spill that already resetspillingand handled the close itself, then threw later, does not close a second time.spillingset, as a successful one does, whether or not the handle was closed yet. The store's spill plan hands the handle topostSpill, which synchronizes the device and then callsreleaseSpilled. After a failed spill,releaseSpilledkeeps the device data, resetsspilling, and completes the close if the handle was closed during the spill or before the synchronize. Whilespillingis set no other spill can start, so the handle is released exactly once.releaseSpilledafter a successful spill. Thespill()scaladoc now says it must also synchronize the device and callreleaseSpilledafter a failed one that leftspillFailedset, which is the check the spill store uses. Only the spill store callsspill()in production code.SharedRecomputableHandle.spillnow moves the evicted object to its pending-release list in the same locked block that removes it, rather than in a second block. Nothing can fail between the two steps any more, so its spill no longer needs the failure path. The existing post-spillreleaseSpilledstill releases the object and completes any deferred close, subject to outstanding leases and rebuilds.The
spill()scaladoc said a failed spill does not restore state and is unrecoverable. It now describes the actual behavior.Apart from a
tryaround each handle's spill in the spill plan and one flag check in devicereleaseSpilled, only the failure path changes.SharedRecomputableHandlealso does its eviction in one locked block instead of two.Out of scope:
spillingset. A failure after the spill has published its host copy keeps both copies until the handle is closed. Both predate this PR and are tracked in #16526.Tests
New latch-controlled cases in
SpillLifecycleReplaySuite. Each pauses a spill and then injects a failure: a device spill after its host copy has read the device data, a host spill when it opens its spill file. Device spills run through the device store, which is also paused at its post-spill synchronize. At that point the test checks that the handle is still owned by the spill and its device data has not been released. The cases run in these modes:close()is called from another thread while the spill is paused. The spill error reaches the caller, the resource is released, the handle is no longer spillable, and the device, host and disk stores are empty.close()is called after the spill has failed but before the post-spill synchronize. The device data is kept until the synchronize and released after it.The first two modes cover these handle variants:
SpillableDeviceBufferHandle,SpillableColumnarBatchHandle,SpillableColumnarBatchFromBufferHandle,SpillableCompressedColumnarBatchHandle(host staging and disk staging) andSpillableTableHandle.SpillableHostBufferHandleandSpillableHostColumnarBatchHandle.Two single-threaded cases cover a direct
spill()of a device handle that fails, left open and closed: the handle stays owned by the spill untilreleaseSpilled, which keeps the data of the open handle and releases the closed one.Two more single-threaded cases cover error handling:
doCloseruns once, even after a secondclose().spillingand completed the close before throwing,doCloseis not called again.Validation:
mainwithout the fix, all eight original cases fail: the seven closed-during-spill variants and the suppressed-error case. The open-handle cases guard against closing too much, so they are expected to pass onmain. The double-close case fails against this PR's first revision, which closed twice.- If a failed device spill resets
spillingin the spilling thread, as this PR's first two revisions did, all 18 failed-spill device cases and both direct-spill cases fail.- If it keeps waiting for the synchronize only when the handle was already closed, the 6 closed-after-the-failure cases, the 6 left-open cases and both direct-spill cases fail.
- If it keeps
spillingset but still closes in the spilling thread, the 6 closed-during-the-spill device cases fail because the device data was released before the synchronize.- If the spill plan does not hand the failed handle to
postSpill, all 18 failed-spill device cases fail withspillingleft set, as does the existing partial-plan failure test.- If
releaseSpilledreleases the device data after a failed spill, the 6 left-open device cases and the open direct-spill case fail.SpillLifecycleReplaySuite,SpillFrameworkSuiteandSharedRecomputableHandleSuitepass 138/138 on Spark 3.5.0.sql-plugincompiles for Scala 2.13 with Spark 4.0.0.This change was developed with AI assistance (Claude Code). I reviewed the complete diff and this description before updating the PR.
Checklists
Documentation
Testing
(Please provide the names of the existing tests in the PR description.)
Performance
The successful spill path changes slightly: the spill plan wraps each handle's spill in a
try, devicereleaseSpilledchecks one flag under a lock it already holds, andSharedRecomputableHandle.spilldoes one locked block instead of two. A temporary probe, not part of this PR, timed successful device-to-host spills through the device store: 1,000 handles of 4 KiB per round, one store spill per handle, 15 rounds with the first 5 dropped as warm-up. On a Quadro RTX 6000 with Spark 3.5.0, alternating betweenmainand this PR showed no measurable difference:| Build | Median per spill, run 1 | Median per spill, run 2 | Fastest round |
|---|---|---|---|
|
main| 278.7 µs | 272.7 µs | 265.6 µs || This PR | 280.9 µs | 269.6 µs | 264.7 µs |
Round-to-round variation within a run was 265 to 330 µs, much larger than any difference between the builds.
🤖 Generated with Claude Code