Skip to content

Commit b978e54

Browse files
committed
review: own the write-side compression buffers, fix the activation example
Compressing through VectorUnloader leaks on the failure path: appendNodes retains each input buffer and accumulates the compressed ones into a list local to getRecordBatch, so a buffer that fails to compress strands that retain and leaves every buffer compressed before it reachable from nothing. Closing the input batch afterwards undoes neither. Unload plain and compress in CachedBatchIpc.compressed instead, mirroring what decompressed already does on the read side, so every allocation stays reachable from an error path that owns it. The docs enabled the cache with spark.conf.set, which cannot work: the driver plugin picks spark.sql.cache.serializer while the SparkContext is initializing. Show it as a startup --conf. Also drops a redundant s interpolator that the scalafix lint rejected.
1 parent 3e74e9d commit b978e54

4 files changed

Lines changed: 173 additions & 11 deletions

File tree

‎docs/source/user-guide/latest/in-memory-cache.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ format that Comet operators read directly. Without it, a cached table is stored
2424
format and every scan of it has to convert each batch before Comet can continue, which shows up in
2525
the plan as a `CometSparkColumnarToColumnar` above the cache scan.
2626

27-
This feature is **experimental and disabled by default**.
27+
This feature is **experimental and disabled by default**. Turn it on at startup, alongside the rest
28+
of Comet's configuration:
2829

29-
```scala
30-
spark.conf.set("spark.comet.exec.inMemoryCache.enabled", "true")
30+
```shell
31+
$SPARK_HOME/bin/spark-shell \
32+
... \
33+
--conf spark.comet.exec.inMemoryCache.enabled=true
3134
```
3235

36+
It has to be set before the `SparkContext` starts. Comet's driver plugin chooses
37+
`spark.sql.cache.serializer` once, while the context is initializing, so a session that started
38+
with the default goes on using Spark's cache format however the config is set afterwards.
39+
3340
## What changes when it is enabled
3441

35-
`spark.comet.exec.inMemoryCache.enabled` is read at startup, and its value then decides whether
36-
Comet installs its cache serializer as `spark.sql.cache.serializer`. When it is installed:
42+
With Comet's serializer installed as `spark.sql.cache.serializer`:
3743

3844
- Cached data is stored as `CometCachedBatch` rather than Spark's `DefaultCachedBatch`.
3945
- Cached tables are scanned by `CometInMemoryTableScan`, which feeds Comet operators directly.

‎spark/src/main/scala/org/apache/spark/sql/comet/execution/arrow/CachedBatchIpc.scala‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,13 @@ private[comet] object CachedBatchIpc {
210210
root.setRowCount(batch.numRows())
211211
}
212212

213-
// alignBuffers=true matches the 8-byte buffer alignment Projection.load reproduces when it
214-
// repacks the selected buffers.
215-
val unloader = new VectorUnloader(root, true, codec, true)
216-
val recordBatch = unloader.getRecordBatch
213+
// Unloaded plain and compressed afterwards rather than by handing the codec to the unloader;
214+
// see compressed for why.
215+
val unloader = new VectorUnloader(root, true, NoCompressionCodec.INSTANCE, true)
216+
val plainBatch = unloader.getRecordBatch
217+
val recordBatch =
218+
try compressed(plainBatch, codec, allocator)
219+
finally plainBatch.close()
217220
try {
218221
val fields = vectors.map(_.getField)
219222
// Leaves the batch in the state serializeBatches leaves one. The record batch holds its
@@ -292,7 +295,7 @@ private[comet] object CachedBatchIpc {
292295
// for how the write path avoids producing one.
293296
if (batch.nodesLength() != totalNodes || batch.buffersLength() != totalBuffers) {
294297
throw new SparkException(
295-
s"Comet cached batch does not match the cached schema: the payload holds " +
298+
"Comet cached batch does not match the cached schema: the payload holds " +
296299
s"${batch.nodesLength()} field nodes and ${batch.buffersLength()} buffers, but the " +
297300
s"schema describes $totalNodes and $totalBuffers")
298301
}
@@ -389,6 +392,64 @@ private[comet] object CachedBatchIpc {
389392
(selectedIndices.flatMap(i => starts(i) until starts(i + 1)), starts.last)
390393
}
391394

395+
/**
396+
* The same record batch with every buffer compressed, as a new batch the caller owns.
397+
*
398+
* `VectorUnloader` would do this itself if handed the codec, but it leaks on the failure path:
399+
* `appendNodes` retains each input buffer and accumulates the compressed ones into a list local
400+
* to `getRecordBatch`, so a buffer that fails to compress -- zstd unable to allocate its
401+
* workspace, say -- strands that retain and leaves every buffer compressed before it reachable
402+
* from nothing. Closing the input batch afterwards undoes neither, so one failed cache
403+
* materialization leaks a batch's worth of off-heap for the life of the executor. Compressing
404+
* here keeps every allocation reachable from this method's own error path, as [[decompressed]]
405+
* does on the read side.
406+
*
407+
* The retain before each `compress` is where the reference on the buffer that comes back is
408+
* from. A codec that allocates consumes it and hands back a buffer of its own;
409+
* `NoCompressionCodec` hands back the input itself, and the retain is then the reference
410+
* `result` ends up owning. Releasing it again is what a throw owes.
411+
*/
412+
private def compressed(
413+
batch: ArrowRecordBatch,
414+
codec: CompressionCodec,
415+
allocator: BufferAllocator): ArrowRecordBatch = {
416+
val buffers = new java.util.ArrayList[ArrowBuf](batch.getBuffers.size)
417+
try {
418+
batch.getBuffers.asScala.foreach { buffer =>
419+
buffer.getReferenceManager.retain()
420+
val packed =
421+
try codec.compress(allocator, buffer)
422+
catch {
423+
case NonFatal(e) =>
424+
buffer.getReferenceManager.release()
425+
throw e
426+
}
427+
buffers.add(packed)
428+
}
429+
430+
val result = new ArrowRecordBatch(
431+
batch.getLength,
432+
batch.getNodes,
433+
buffers,
434+
CompressionUtil.createBodyCompression(codec),
435+
batch.getVariadicBufferCounts,
436+
// alignBuffers=true matches the 8-byte buffer alignment Projection.load reproduces when it
437+
// repacks the selected buffers. This is the layout that gets written, so the unloader's is
438+
// not the one that matters.
439+
true)
440+
// The constructor retained each buffer, so drop the references held here.
441+
buffers.asScala.foreach(_.close())
442+
result
443+
} catch {
444+
case NonFatal(e) =>
445+
buffers.asScala.foreach { buffer =>
446+
try buffer.close()
447+
catch { case NonFatal(closeError) => e.addSuppressed(closeError) }
448+
}
449+
throw e
450+
}
451+
}
452+
392453
/**
393454
* The same record batch with every buffer decompressed, as a new batch the caller owns.
394455
*

‎spark/src/test/scala/org/apache/comet/exec/CometInMemoryCacheSuite.scala‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ package org.apache.comet.exec
2121

2222
import java.{util => ju}
2323

24-
import org.apache.arrow.vector.{FixedSizeBinaryVector, VarBinaryVector}
24+
import org.apache.arrow.compression.ZstdCompressionCodec
25+
import org.apache.arrow.memory.{ArrowBuf, BufferAllocator}
26+
import org.apache.arrow.vector.{FixedSizeBinaryVector, IntVector, VarBinaryVector, VarCharVector}
27+
import org.apache.arrow.vector.compression.{CompressionCodec, CompressionUtil}
2528
import org.apache.arrow.vector.types.pojo.ArrowType
2629
import org.apache.spark.CometDriverPlugin
2730
import org.apache.spark.SparkConf
@@ -1876,6 +1879,83 @@ class CometInMemoryCacheSuite extends CometTestBase {
18761879
}
18771880
}
18781881

1882+
/**
1883+
* A zstd codec that compresses the first `succeedFor` buffers and then throws.
1884+
*
1885+
* Delegating to the real codec until it fails is the point: the buffers already compressed are
1886+
* genuine off-heap allocations reachable only from inside the writer, which is what a real
1887+
* failure -- zstd unable to allocate its workspace part way through a batch -- leaves behind.
1888+
*/
1889+
private class FailAfterCompressionCodec(succeedFor: Int) extends CompressionCodec {
1890+
private val delegate = new ZstdCompressionCodec(1)
1891+
var compressed: Int = 0
1892+
1893+
override def compress(allocator: BufferAllocator, buffer: ArrowBuf): ArrowBuf = {
1894+
if (compressed == succeedFor) {
1895+
throw new RuntimeException(FailAfterCompressionCodec.Message)
1896+
}
1897+
compressed += 1
1898+
delegate.compress(allocator, buffer)
1899+
}
1900+
1901+
override def decompress(allocator: BufferAllocator, buffer: ArrowBuf): ArrowBuf =
1902+
delegate.decompress(allocator, buffer)
1903+
1904+
override def getCodecType: CompressionUtil.CodecType = delegate.getCodecType
1905+
}
1906+
1907+
private object FailAfterCompressionCodec {
1908+
val Message: String = "injected compression failure"
1909+
}
1910+
1911+
test("Comet in-memory cache releases its buffers when a column fails to compress") {
1912+
// Writing a batch allocates a buffer per compressed buffer before any payload exists, and
1913+
// nothing outside the writer can reach them while it is still assembling the record batch they
1914+
// belong to. This is why the codec is not handed to `VectorUnloader`: it accumulates them in a
1915+
// list local to `getRecordBatch`, which is off the stack by the time a caller sees the failure,
1916+
// so a single failed materialization would leak a batch's worth of off-heap for the life of the
1917+
// executor.
1918+
val rows = 256
1919+
val ints = new IntVector("i", CometArrowAllocator)
1920+
val strings = new VarCharVector("s", CometArrowAllocator)
1921+
try {
1922+
ints.allocateNew(rows)
1923+
(0 until rows).foreach(i => ints.set(i, i))
1924+
ints.setValueCount(rows)
1925+
1926+
strings.allocateNew(rows)
1927+
(0 until rows).foreach(i => strings.setSafe(i, s"value_$i".getBytes("UTF-8")))
1928+
strings.setValueCount(rows)
1929+
1930+
val batch = new ColumnarBatch(
1931+
Array[ColumnVector](new CometPlainVector(ints), new CometPlainVector(strings)),
1932+
rows)
1933+
1934+
// An int vector is validity and data, a varchar validity, offsets and data: five buffers in
1935+
// all. Succeeding for two puts the failure at the string column's first buffer, with the int
1936+
// column's two already compressed into allocations only the writer can reach. Failing at the
1937+
// very first buffer would pass with no cleanup at all.
1938+
val codec = new FailAfterCompressionCodec(succeedFor = 2)
1939+
val before = CometArrowAllocator.getAllocatedMemory
1940+
val thrown = intercept[Exception] {
1941+
CometCachedBatchHelper.serialize(batch, codec, CometArrowAllocator)
1942+
}
1943+
1944+
assert(
1945+
causeChain(thrown).exists(t =>
1946+
Option(t.getMessage).contains(FailAfterCompressionCodec.Message)),
1947+
s"a compression failure must surface as itself: $thrown")
1948+
assert(codec.compressed == 2, "the failure must come after some buffers were compressed")
1949+
assert(
1950+
CometArrowAllocator.getAllocatedMemory == before,
1951+
"everything allocated before a write failure must be released")
1952+
} finally {
1953+
// Never reaches the writer's own clear(), which only runs once the payload is built.
1954+
ints.close()
1955+
strings.close()
1956+
}
1957+
}
1958+
18791959
/**
18801960
* Cache two low-cardinality string columns and hand the test the cached relation.
18811961
*

‎spark/src/test/scala/org/apache/spark/sql/comet/execution/arrow/CometCachedBatchHelper.scala‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import java.nio.channels.Channels
2525
import scala.jdk.CollectionConverters._
2626

2727
import org.apache.arrow.flatbuf.{MessageHeader, RecordBatch => FlatBufRecordBatch}
28+
import org.apache.arrow.memory.BufferAllocator
2829
import org.apache.arrow.vector.TypeLayout
30+
import org.apache.arrow.vector.compression.CompressionCodec
2931
import org.apache.arrow.vector.ipc.ReadChannel
3032
import org.apache.arrow.vector.ipc.message.{MessageMetadataResult, MessageSerializer}
3133
import org.apache.arrow.vector.types.pojo.Field
@@ -77,6 +79,19 @@ object CometCachedBatchHelper {
7779
Utils
7880
.isArrowBacked(batch) && CachedBatchIpc.matchesReaderLayout(batch, arrowFields(cacheSchema))
7981

82+
/**
83+
* Serialize one batch the way the cache writer does, with a caller-chosen codec.
84+
*
85+
* Another thin shim, for tests about the write path itself rather than about what a cached
86+
* relation reads back. The codec is the parameter that matters: handing in one that throws is
87+
* how a compression failure part way through a batch is reached deterministically.
88+
*/
89+
def serialize(
90+
batch: ColumnarBatch,
91+
codec: CompressionCodec,
92+
allocator: BufferAllocator): Array[Byte] =
93+
CachedBatchIpc.serialize(batch, codec, allocator)._1
94+
8095
/**
8196
* Whether the payload begins with a Schema message rather than going straight to the record
8297
* batch.

0 commit comments

Comments
 (0)