sunchao commented on code in PR #7655:
URL: https://github.com/apache/datafusion-comet/pull/7655#discussion_r4746927459
##########
spark/src/main/scala/org/apache/spark/sql/comet/CometBroadcastValue.scala:
##########
@@ -76,59 +89,122 @@ private[comet] final class CometBroadcastValue(val chunks:
Array[ChunkedByteBuff
* [[org.apache.spark.sql.comet.execution.arrow.ColumnarBatchArrowReader]]
does, without affecting
* another task.
*
- * The decode is freed when the last task holding it completes, and a task
that reads the value
- * after that decodes it again. A native join keeps a build side of one batch,
which is what
- * coalescing makes of a broadcast, for as long as its task runs, so for such
a broadcast the
- * decode holds no memory that the tasks would not hold anyway. A native join
copies a build side
- * of several batches, so the decode keeps one copy of such a broadcast that
the tasks would have
- * freed once they built their tables.
+ * When the last task holding the decode completes, the decode is kept, so
that a task that comes
+ * later, such as one of the next wave of tasks of the stage, reads it too,
until [[close]] frees
+ * it. The kept decodes of all broadcasts on the executor are capped in bytes:
a decode larger
+ * than the cap is freed at once, and keeping a decode frees the decodes that
have been kept
+ * longest, until the kept decodes fit the cap again. A value that Spark does
not hold in its
+ * memory store, as when the broadcast does not fit there, is never closed, so
only the cap bounds
+ * its kept decode. A native join keeps a build side of one batch, which is
what coalescing makes
+ * of a broadcast, for as long as its task runs, so for such a broadcast the
decode holds no
+ * memory that running tasks would not hold anyway, apart from while it is
kept. A native join
+ * copies a build side of several batches, so the decode keeps one copy of
such a broadcast that
+ * the tasks would have freed once they built their tables.
*
* @param chunks
* the compressed Arrow IPC streams of the broadcast value
* @param decode
* decodes the streams; `None` makes each task decode them for itself
+ * @param idleDecodes
+ * the kept decodes that count against the cap, shared with the other
broadcasts
*/
private[comet] final class SharedDecode(
chunks: Array[ChunkedByteBuffer],
- decode: (Array[ChunkedByteBuffer], String) =>
Option[SharedDecode.Decoded]) {
- import SharedDecode.{Decoded, KillCheckIntervalMs}
+ decode: (Array[ChunkedByteBuffer], String) => Option[SharedDecode.Decoded],
+ idleDecodes: SharedDecode.IdleDecodes = SharedDecode.idleDecodes) {
+ import SharedDecode.{Decoded, Idle, KillCheckIntervalMs, Retention}
- // The decode that running tasks hold, if there are any. Guarded by `this`.
+ // The decode that running tasks hold, or that the executor keeps for later
tasks. Guarded by
+ // `this`. An entry is freed only once it is no longer `current`.
private var current: Entry = _
+ // Set by `close`, after which a decode is freed as soon as no task holds
it. Guarded by `this`.
+ private var closed = false
+
/**
* The batches of the broadcast value for the task of `context`. As with
* [[Utils.decodeBatches]], each batch is valid until the next call to
`hasNext` or `next`, or
* until the task completes. A task must read them on its own thread,
because that is where its
- * completion listener runs and gives back the buffers.
+ * completion listener runs and gives back the buffers. If the task is the
last to hold the
+ * decode, the decode is kept as `retention` says after the task completes.
*/
- def batches(context: TaskContext, source: String): Iterator[ColumnarBatch] =
- new SharedBatchIterator(context, source)
+ def batches(
+ context: TaskContext,
+ source: String,
+ retention: Retention): Iterator[ColumnarBatch] =
+ new SharedBatchIterator(context, source, retention)
/** The number of holds on the decode, one for each read by a running task.
*/
def holders: Int = synchronized(if (current == null) 0 else current.holders)
- /** Hands out the entry that tasks hold, creating it if none does. True if
it did. */
+ /**
+ * Frees the kept decode, and from then on frees each decode as soon as no
task holds it. Tasks
+ * can still read the value after this, through Spark's cache of broadcast
values, and the tasks
+ * that read it at the same time still share one decode.
+ */
+ def close(): Unit = {
+ val entry = synchronized {
+ closed = true
+ current
+ }
+ if (entry != null) {
+ freeIfIdle(entry)
+ }
+ }
+
+ /** Hands out the entry, held or kept, creating it if there is none. True if
it did. */
private def acquire(): (Entry, Boolean) = synchronized {
val loading = current == null
if (loading) {
current = new Entry
+ } else if (current.holders == 0) {
+ idleDecodes.remove(current)
}
current.holders += 1
(current, loading)
}
- /** Gives back a task's hold on `entry`, and frees the entry once no task
holds it. */
- private def release(entry: Entry): Unit = {
- val unheld = synchronized {
+ /**
+ * Gives back a task's hold on `entry`. Once no task holds the entry, it is
kept as `retention`
+ * says, or freed at once if it may not be kept.
+ */
+ private def release(entry: Entry, retention: Retention): Unit = {
+ var free = false
+ var evicted = List.empty[Idle]
+ synchronized {
entry.holders -= 1
- val unheld = entry.holders == 0
- if (unheld) {
- forget(entry)
+ if (entry.holders == 0) {
+ val bytes = entry.bytes
+ if (!closed && bytes > 0 && bytes <= retention.maxIdleBytes &&
(current eq entry)) {
+ evicted =
+ idleDecodes.add(Idle(entry, bytes, () => freeIfIdle(entry)),
retention.maxIdleBytes)
Review 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.
--
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]