Repository navigation
Conversation
The tasks of an executor that read a Comet broadcast at the same time share one decoded copy of it, but the copy was freed as soon as the last of them completed. The tasks of a wave finish at about the same time, so every wave of a stage decoded the broadcast again, and every task of a wave waited for that decode. Keep the copy for spark.comet.exec.broadcastExchange.shareDecoded.idleTimeout, 1 second by default, after its last task completes, so that a task that starts by then reads it. One daemon thread frees the copy once the timeout passes with no task taking it. A timeout of 0 frees it at once, as before.
andygrove
left a comment
There was a problem hiding this comment.
Thanks @comphead. Keeping the decode for the next wave is a good idea, and the benchmark shows a real gain. I read through SharedDecode and could not find a race. acquire and release run under the lock, and expire re-checks holders and idlePeriods under it. A failed decode is forgotten at once, so its other holders close it in release.
My main concern is memory. The idle copy is the whole decoded broadcast in CometArrowAllocator, which no Spark or Comet pool accounts for. 1.2 already has the shared decode, so this adds up to a second of retained container RSS for every 1.2 user by default. The 31.8 MB single-broadcast run does not show whether copies stack. Stage N's copy can still be resident while stage N+1 decodes its own, and a query with several joins in consecutive stages could hold several. Spark's broadcast threshold can also be raised to GB scale. Could you share the peak CometArrowAllocator.getAllocatedMemory for a query with several broadcast joins in consecutive stages, with the timeout at 0 and at 1s? If the copies stack, would a cap on idle bytes per executor, as #7487 suggests, be small enough to include here? If the cap stays out, could the description say Part of #7487 and link a follow-up issue for it? As written, Closes #7487 closes the issue without the cap.
On tests, CometTestBase now sets the timeout to 0 for every suite, so no suite runs the 1s default. The nested-columns test uses the real scheduler at 100ms, but it only checks that the allocator returns to its baseline. It does not check that a later task reused the decode. Could you add a test with the real scheduler and a timeout of a few hundred milliseconds, where two tasks run one after the other and the injected decode function counts decodes? It should assert 1, so the reuse this PR is about is checked end to end. The manual-scheduler test covers the entry bookkeeping well, but it does not cover the thread.
Finally, batches(context, source, idleTimeoutMs: Long = 0) has a default on both CometBroadcastValue and SharedDecode. The only production caller passes the timeout, but a new caller that forgets it silently gets no retention. Would you drop the defaults and have the tests pass the value explicitly?
Address review: the idle copies of broadcasts read in consecutive stages stack. Add `spark.comet.exec.broadcastExchange.shareDecoded.maxIdleSize` (128 MiB by default), an executor-wide cap on the bytes of idle decodes. A decode over the cap is freed at once, and keeping a decode frees the decodes idle longest until the rest fit. An expiry now frees an entry only while it is still `current`, so an expiry scheduled before another path freed the entry frees nothing. `batches` takes a `SharedDecode.Retention` with no default, so a caller cannot silently get no retention. Add a test of reuse through the real expiry thread and a test of the cap.
|
Thanks @andygrove, all three points held up when I checked them against the code. Addressed in 51ef4bc. Memory. The copies do stack. Each broadcast has its own
So without a cap, the timeout adds every broadcast that finished in the last second to the executor's peak. The cap was small enough to include here, so the PR still closes #7487:
The cap bounds only the idle copies. The copy that running tasks hold is unchanged from
Tests. Added "a task that comes after another completes reads its decode, which the timer then frees". It runs two tasks one after the other with the real expiry thread and a 500 ms timeout, asserts one decode, and waits for the thread to free the copy. The cap has its own test with an injected Defaults. |
Replace the 1 second idle timeout with the broadcast's own lifetime on the executor, which is how Spark keeps its own broadcast values. CometBroadcastValue is now AutoCloseable, and Spark's memory store closes it when the broadcast block leaves memory (SPARK-35396): when the broadcast is unpersisted or destroyed, when the driver cleans it up, when the memory store evicts it, and when the executor stops. close() frees the kept decode, and from then on the value frees each decode as soon as no task holds it. Drop spark.comet.exec.broadcastExchange.shareDecoded.idleTimeout, its expiry thread and the idle-period counter. maxIdleSize still caps the kept decodes of all broadcasts on an executor, and 0 frees a decode as soon as no task holds it. CometTestBase now sets it to 0. Also pass the retention through CometBatchRDD as an Option, None when sharing is disabled, move NoRetention into the suite, and share one decode-counting helper in the tests.
|
@andygrove one more change on the memory side, in f97aa48. There is no timer any more. The executor keeps the copy after its last task until Spark removes the broadcast from the executor, which is how Spark keeps its own broadcast values.
I did not free the copy when the query ends. Executors hear nothing from Spark at that point, and The trade-off is residency. The driver cleans up a broadcast only after it garbage-collects it, so in a long-running application the kept copies now stay until the cap evicts them, rather than for 1 s. A value that never fits in the executor's memory store is never closed, and only the cap bounds its copy. Given that, would you rather have a lower default than 128 MiB? The benchmark numbers in the description come from the timer revisions. Within a query both keep the same copies, so I expect the same task times, but I have not re-run them yet. |
andygrove
left a comment
There was a problem hiding this comment.
Thanks @comphead. Keeping the decode until Spark removes the broadcast is a cleaner design than the timer, and the gain for the next wave is clear. I went through the locking in SharedDecode and IdleDecodes and could not find a race. The lock order is consistent, eviction frees outside both locks, and freeIfIdle re-checks current so there is no double close. I also checked that nothing outside CometBroadcastExchangeExec and the suite calls CometBatchRDD or batches, so the signature changes are contained.
I have a few things I would like settled before merge.
Default for maxIdleSize. The driver cleans up a broadcast only after a GC, so a kept copy can now sit in executor RSS for as long as the application runs. With 128 MiB as the default, an executor sized with the default 10% overhead and the 384 MiB floor could be pushed past its container limit after upgrading from 1.2 with no config change. Could you say more about why 128 MiB is the right default? You asked whether a lower one would be better in your last comment. Would a smaller default keep most of the gain? The gain comes from the second and later waves reading the first wave's copy, and a typical dimension table is much smaller than the cap.
Numbers for this revision. The description says the tables come from the timer revisions and have not been re-run. The cross-query retention is new in this design, so none of the numbers cover it. Could you re-run the four-join table on this revision with maxIdleSize at 0 and at the default? Please also add a run where a second query follows the first on the same executors, and report the peak CometArrowAllocator.getAllocatedMemory. A new decode starts while the previous query's copies are still kept, so the peak is the held copy plus the kept ones before the cap evicts anything.
Test coverage of the default path. CometTestBase now sets the cap to 0, so no SQL-level suite runs with retention. "broadcast joins over nested columns release the shared decode" also runs at cap 0 now, so it no longer covers the kept path. Would you add one suite test that sets a positive cap and runs a broadcast hash join over several tasks? It should check that the allocator still holds the copy after the query and frees it after the broadcast is destroyed.
Other ways the memory store removes a value. The new test removes the broadcast with destroy. The PR also relies on the memory store closing the value on eviction and when the executor stops. Could a test drop the block from the BlockManager and check that the kept copy is freed?
Address review: each query builds its own broadcasts, so a copy kept after its query is rarely read again, and a large cap mostly keeps such copies until the driver cleans up their broadcasts. Lower the default of spark.comet.exec.broadcastExchange.shareDecoded.maxIdleSize from 128 MiB to 32 MiB, and list the kept copies in the memory tuning guide. Run the broadcast join test over nested columns with a positive cap, so that a SQL-level test checks that the copy is kept after the query and freed once the broadcast is destroyed. Drop the block through BlockManager.removeBlock in the test of the close hook, which is the memory store path that eviction takes.
|
@andygrove thanks, addressed in a232f82. Default. I lowered Numbers for this revision. Four broadcast hash joins in consecutive stages (a 4M-row fact repartitioned into 32 tasks before each join, four 1.5M-row dims of 18.4 MB decoded each,
The second query's peak is the held copy plus the kept ones before the cap evicts them, as you expected: 18.4 MB more than a cap of 0 at 32 MiB, and 73.5 MB more at 128 MiB. The 100.8 MB at a cap of 0 is JVM-side Arrow memory other than the shared copies. The median query times were between 1385 and 1486 ms in every arm, within the spread between runs. I did not profile where this query spends its time. I have not re-run the task-time benchmark, so its table is still from the timer revision. Tests. "broadcast joins over nested columns keep their decode until the broadcast is removed" now runs a hash join and a nested loop join over eight tasks with a 1g cap. It checks that the allocator still holds the copy after the query, destroys the broadcasts, and checks that the copies are freed. "dropping a broadcast's block frees the decode that the executor kept" drops the block with |
andygrove
left a comment
There was a problem hiding this comment.
Thanks @comphead. Keeping the decode until Spark removes the broadcast, with a byte cap across all broadcasts, is a clean design. I went through acquire, release, freeIfIdle, close and IdleDecodes and could not find a race. I have one question on failure isolation, in the inline comment.
| entry.close() | ||
| } | ||
| // Outside this object's lock, because each takes the lock of the decode that it frees. | ||
| evicted.foreach(_.free()) |
There was a problem hiding this comment.
Thanks for the careful lock ordering here. One thing I wonder about is evicted.foreach(_.free()). The evicted entries usually belong to other broadcasts, and Decoded.close() ends in allocator.close(), which throws if any memory is still outstanding. If one of them throws, the task that is only completing fails with a completion listener error, and the remaining evicted entries are skipped. On main a failing close was charged to the task that used that decode. Would it make sense to free each evicted entry in its own try and log a warning on failure, the way Spark's memory store does when it closes a value? A test where one evicted entry's free throws, and the completing task and the other evictions still finish, would pin it down.
There was a problem hiding this comment.
Good catch, done in 7645447. Each evicted entry is now freed in its own try, and a failure is logged as a warning with the exception, as MemoryStore.freeMemoryEntry does, so the task that is completing still completes and the other evictions still run. A failing close of the task's own decode still fails that task, as on main.
"a kept decode that fails to free does not fail the task whose decode evicts it" keeps three decodes of other broadcasts, of which the first and the last throw when freed, so that one throws first whichever order the evictions run in. It checks that the evicting task completes and that the middle one is still freed.
sunchao
left a comment
There was a problem hiding this comment.
The smaller idle cap helps the Arrow side. I found a second ownership issue on the executor when Spark stores the broadcast on disk. The earlier cleanup-error comment on freeing multiple evictions also still applies.
| val bytes = entry.bytes | ||
| if (!closed && bytes > 0 && bytes <= retention.maxIdleBytes && (current eq entry)) { | ||
| evicted = | ||
| idleDecodes.add(Idle(entry, bytes, () => freeIfIdle(entry)), retention.maxIdleBytes) |
There was a problem hiding this comment.
The executor-wide idle queue roots SharedDecode via both Entry.$outer and the callback. SharedDecode.chunks then keeps the whole compressed IPC payload on the JVM heap, while bytes charges only the Arrow allocator. If MEMORY_AND_DISK falls back to disk, no MemoryStore eviction calls close() on this value. After the weakly cached wrapper is collected, a later read deserializes another wrapper and cannot reuse this idle decode, yet the old chunks remain live. Could we pass chunks into the load path without storing them on the globally retained object, or skip retention in this case? A disk-only test can drop the wrapper and check a weak reference to its chunks.
I reproduced this with Spark 3.5.9 and a disk-only BlockManager entry (memory size 0, disk size 1,055,702 bytes). After block removal and GC the wrapper was gone, but the default 32 MiB cap retained the 1,049,797-byte compressed array alongside 2,097,152 Arrow bytes. With a zero cap the array was collected.
There was a problem hiding this comment.
Thanks for the reproduction, done in 7645447. SharedDecode no longer holds the streams. CometBroadcastValue passes its chunks to each read, which hands them to the load, so what the cap keeps reachable is the decode and its bookkeeping, not the compressed payload. Once Spark drops a value that it keeps only on disk, the value's chunks can be collected, and only the Arrow copy stays until the cap evicts it.
"a kept decode does not keep the compressed streams of its value alive" reads streams through a shared decode that keeps the decode, drops the streams, and waits for a weak reference to them to clear. Andy's point about freeing each eviction on its own is fixed in the same commit.
Address review: a decode that the cap evicts belongs to another broadcast, so a failure to free it is now logged, as Spark's memory store does when it closes a value, rather than failing the task whose decode evicted it and skipping the other evictions. SharedDecode no longer holds the compressed streams. CometBroadcastValue passes them to each read, so a kept decode, which the executor-wide cap keeps reachable, does not keep the compressed payload of a value that Spark dropped, as Spark does for a broadcast that it keeps only on disk.
Which issue does this PR close?
Closes #7487.
Part of #6013.
Rationale for this change
#7016 lets the tasks of an executor that read a Comet broadcast at the same time share one decoded copy of it, and
frees the copy when the last of them completes. The tasks of a wave finish at about the same time, so the copy is gone
before the next wave starts, and every wave decodes the broadcast again. Every task of a wave also waits for that decode.
Counters added temporarily to
SharedDecodeshow this onmain, with thebhjmode of a hash-join task-timebenchmark: a 1M-row broadcast probed by 32 tasks on
local[8], so 4 waves, lz4, 7 runs. Every run had 32 reads, 4decodes and 4 frees. 28 tasks waited about 14 ms each, which is the time of the decode they waited for. Each new decode
started 0.6 to 1.3 ms after the copy before it was freed.
What changes are included in this PR?
that starts later, such as one of the next wave of tasks of the stage, reads it instead of decoding the broadcast
again.
values.
CometBroadcastValueis nowAutoCloseable. Since SPARK-35396 (Spark 3.2), Spark's memory store closes anAutoCloseablevalue when its block leaves memory: when the broadcast is unpersisted or destroyed, when the drivercleans it up, when the memory store evicts it, and when the executor stops. After
close, the value frees each copyas soon as no task holds it, because Spark's cache of broadcast values can still hand it to a later task.
spark.comet.exec.broadcastExchange.shareDecoded.maxIdleSize, 32 MiB by default, caps the bytes ofthe kept copies of all broadcasts on an executor. A copy larger than the cap is freed as soon as no task holds it,
and keeping a copy frees the copies kept longest until the rest fit. A cap of 0 frees every copy as soon as no task
holds it, as
maindoes.SharedDecode.IdleDecodeskeeps the copies oldest first. It takes no other lock, andevicted copies are freed outside every lock. An evicted copy belongs to another broadcast, so a failure to free it is
logged, as Spark's memory store does when it closes a value, rather than failing the task whose copy evicted it.
SharedDecodedoes not hold the compressed streams.CometBroadcastValuepasses them to each read, so a kept copy,which the cap keeps reachable, does not keep the compressed payload of a value that Spark has dropped.
path freed the entry frees nothing.
CometBroadcastExchangeExecreadsshareDecoded.enabledand the cap on the driver, and passes them throughCometBatchRDDas anOption[SharedDecode.Retention], which isNonewhen sharing is disabled.batcheshas nodefault for it.
CometTestBasesets the cap to 0. Some suites compareCometArrowAllocator's memory before and after a step, andthe kept copy of an earlier test's broadcast could be freed in between, whenever Spark cleans up that broadcast.
guide describe the kept copy, which is JVM-side Arrow memory that neither Spark nor Comet's pools account for.
Keeping the copy until Spark removes the broadcast matches Spark, but Spark counts its broadcast values in storage
memory, and nothing counts this copy. The driver cleans up a broadcast only after it garbage-collects it, which can be
long after the query, so in a long-running application the cap is what bounds the kept copies. A value that never fits
in the executor's memory store is never closed, and only the cap bounds its copy, which then holds only its Arrow
memory. Freeing the copy when the query ends
would need a message from the driver to every executor.
unpersistcannot be that message: it skips the driver's blockmanager, which is the only one in local mode, and it drops the executors' copies of the broadcast as well.
Each query builds its own broadcasts, so a copy kept after its query is rarely read again, and a cap larger than the
broadcasts of the stage that is running mostly keeps such copies longer. That is why the default is 32 MiB, which is
8% of Spark's 384 MiB overhead floor. A broadcast that decodes to more than the cap decodes as on
main, and a stagewhose broadcasts together decode to more than the cap keeps only as many as fit.
The task times below come from an earlier revision of this PR, which freed a kept copy 1 s after its last task. Its
maincolumn ran with that timeout set to 0, which frees a copy as soon as no task holds it, as a cap of 0 does now.Within a run the waves are milliseconds apart, so this revision keeps the same copy, and I expect the same task times,
but I have not re-run this benchmark. Mean time per probe task in the same benchmark, median of 7 runs with the arms
interleaved:
maxIdleSize=0(asmain)The first wave still waits for its decode. A cheaper decode would shrink that wait (#2216, #3762).
The kept copies of broadcasts read in consecutive stages stack, and the copies of one query are still kept when the
next starts. On this revision, a query with four broadcast hash joins in consecutive stages (a 4M-row fact repartitioned
into 32 tasks before each join, four 1.5M-row dims of 18.4 MB decoded each,
local[8], AQE off, Spark 4.1, lz4) rantwice in each session, the second time while the copies that the first kept were still kept. A thread sampled
CometArrowAllocatorevery millisecond and counted a decode by the allocator it creates. Median of 7 runs, with thearms interleaved and a new session for each run:
main)Every cap above 0 decodes each broadcast once, because each stage reads only its own broadcast, so a cap that holds
one copy keeps the whole saving here. JVM-side Arrow memory other than the shared copies puts the peak of
CometArrowAllocatorat 100.8 MB in the first query of every arm. In the second query the copies kept from the firstadd to that peak: the copy that the running stage holds plus the kept ones before the cap evicts them, 18.4 MB at
32 MiB and 73.5 MB at 128 MiB. The median query times were between 1385 and 1486 ms in every arm, within the spread
between runs. I did not profile where this query spends its time.
How are these changes tested?
CometBroadcastValueSuitechecks that a task that comes after the last holder completes reads the keptcopy without decoding again, that closing the value while a task holds the copy frees it once that task completes,
and that a task that reads the closed value decodes again and frees its copy when it completes.
CometBatchRDD, checks that the executor keeps its copy, drops thebroadcast's block through
BlockManager.removeBlock, and checks that the memory store closed the value and freedthe copy. Eviction, unpersist, destroy and the driver's cleanup all remove the block from the memory store this way.
join over eight tasks, it checks that the allocator still holds the copy, destroys the broadcasts, and checks that
the copies are freed.
IdleDecodes, checks that keeping a copy frees the one kept longest, that a copyover the cap is freed at once, that a task taking a kept copy takes it out of the cap, and that closing the values
frees what is still kept.
the task whose copy evicts them completes and that the other one is still freed.
reference to them to clear.