andygrove commented on code in PR #7655:
URL: https://github.com/apache/datafusion-comet/pull/7655#discussion_r4745563933
##########
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)
+ } else {
+ forget(entry)
+ free = true
+ }
+ }
+ }
+ if (free) {
+ entry.close()
+ }
+ // Outside this object's lock, because each takes the lock of the decode
that it frees.
+ evicted.foreach(_.free())
Review 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.
--
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]