Skip to content

Commit a06a59a

Browse files
committed
address review: release every vector a failed write made, and tidy the large-offset tests
1 parent b3630a8 commit a06a59a

4 files changed

Lines changed: 190 additions & 54 deletions

File tree

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private[comet] object CachedBatchIpc {
209209
codec: CompressionCodec,
210210
allocator: BufferAllocator,
211211
chunkSize: Int): (ChunkedByteBuffer, Array[Long]) = {
212-
val (vectors, decoded) = decodeDictionaries(batch, allocator)
212+
val (vectors, allocated) = decodeDictionaries(batch, allocator)
213213
try {
214214
val root = new VectorSchemaRoot(vectors.asJava)
215215
// A batch of zero columns carries only a row count, which a VectorSchemaRoot cannot infer
@@ -246,8 +246,8 @@ private[comet] object CachedBatchIpc {
246246
recordBatch.close()
247247
}
248248
} finally {
249-
// Only the vectors this method allocated. The rest belong to the input batch.
250-
decoded.foreach(v =>
249+
// Only the vectors allocated for this write. The rest belong to the input batch.
250+
allocated.foreach(v =>
251251
try v.close()
252252
catch { case NonFatal(_) => () })
253253
}
@@ -641,32 +641,34 @@ private[comet] object CachedBatchIpc {
641641
/**
642642
* Replace every dictionary-encoded column of `batch` with its decoded form.
643643
*
644-
* Returns the vectors to write and, separately, the ones allocated here so the caller can close
645-
* exactly those. Columns that needed no decoding are returned as they are and stay owned by
644+
* Returns the vectors to write and, separately, the ones allocated for the write, so the caller
645+
* can close exactly those: the decoded columns, and any that
646+
* [[Utils.getBatchFieldVectorsWithProviders]] allocated, such as a 32-bit copy of a
647+
* large-offset column. Columns that needed neither are returned as they are and stay owned by
646648
* `batch`.
647649
*/
648650
private def decodeDictionaries(
649651
batch: ColumnarBatch,
650652
allocator: BufferAllocator): (Seq[FieldVector], Seq[ValueVector]) = {
651-
val decoded = mutable.ArrayBuffer.empty[ValueVector]
653+
val columns = Utils.getBatchFieldVectorsWithProviders(batch)
654+
val allocated = mutable.ArrayBuffer.empty[ValueVector]
655+
allocated ++= Utils.allocatedFor(batch, columns.map(_._1))
652656
try {
653657
val vectors =
654-
Utils.getBatchFieldVectorsWithProviders(batch).map { case (vector, providerOpt) =>
658+
columns.map { case (vector, providerOpt) =>
655659
if (vector.getField.getDictionary == null) {
656660
vector
657661
} else {
658662
val dictionary = Utils.lookupDictionary(vector, providerOpt)
659663
val plain = DictionaryEncoder.decode(vector, dictionary, allocator)
660-
decoded += plain
664+
allocated += plain
661665
plain.asInstanceOf[FieldVector]
662666
}
663667
}
664-
(vectors, decoded.toSeq)
668+
(vectors, allocated.toSeq)
665669
} catch {
666670
case NonFatal(e) =>
667-
decoded.foreach(v =>
668-
try v.close()
669-
catch { case NonFatal(closeError) => e.addSuppressed(closeError) })
671+
Utils.closeAfterFailure(allocated.toSeq, e)
670672
throw e
671673
}
672674
}

‎spark/src/main/scala/org/apache/spark/sql/comet/util/Utils.scala‎

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -261,28 +261,36 @@ object Utils extends CometTypeShim with Logging {
261261
val out = new DataOutputStream(codec.compressedOutputStream(cbbos))
262262

263263
val (fieldVectors, batchProviderOpt) = getBatchFieldVectors(batch)
264-
val root = new VectorSchemaRoot(fieldVectors.asJava)
265-
if (fieldVectors.isEmpty) {
266-
// VSR cannot infer rowCount without field vectors
267-
root.setRowCount(batch.numRows())
268-
}
269-
val provider = batchProviderOpt.getOrElse(dictionaryProvider)
270-
271-
// VectorSchemaRootAppender, which coalesces broadcast batches, assumes offsets that start
272-
// at zero, and Arrow Java writes the bytes before the first offset too. A slice of the root
273-
// rebases the offsets to zero and leaves those bytes out.
274-
val written =
275-
if (fieldVectors.forall(offsetsStartAtZero)) root else root.slice(0, root.getRowCount)
276264
try {
277-
val writer = new ArrowStreamWriter(written, provider, Channels.newChannel(out))
278-
writer.start()
279-
writer.writeBatch()
280-
root.clear()
281-
writer.close()
282-
} finally {
283-
// The slice holds its own references on the batch's buffers, so it is closed even when the
284-
// write fails, as it does when a cancelled broadcast interrupts the task.
285-
if (written ne root) written.close()
265+
val root = new VectorSchemaRoot(fieldVectors.asJava)
266+
if (fieldVectors.isEmpty) {
267+
// VSR cannot infer rowCount without field vectors
268+
root.setRowCount(batch.numRows())
269+
}
270+
val provider = batchProviderOpt.getOrElse(dictionaryProvider)
271+
272+
// VectorSchemaRootAppender, which coalesces broadcast batches, assumes offsets that start
273+
// at zero, and Arrow Java writes the bytes before the first offset too. A slice of the root
274+
// rebases the offsets to zero and leaves those bytes out.
275+
val written =
276+
if (fieldVectors.forall(offsetsStartAtZero)) root else root.slice(0, root.getRowCount)
277+
try {
278+
val writer = new ArrowStreamWriter(written, provider, Channels.newChannel(out))
279+
writer.start()
280+
writer.writeBatch()
281+
root.clear()
282+
writer.close()
283+
} finally {
284+
// The slice holds its own references on the batch's buffers, so it is closed even when
285+
// the write fails, as it does when a cancelled broadcast interrupts the task.
286+
if (written ne root) written.close()
287+
}
288+
} catch {
289+
case NonFatal(e) =>
290+
// The batch's own vectors stay with their owner. The ones made for this write are held
291+
// by nothing else, and a narrowed copy can be close to 2 GiB.
292+
closeAfterFailure(allocatedFor(batch, fieldVectors), e)
293+
throw e
286294
}
287295

288296
if (out.size() > 0) {
@@ -490,12 +498,49 @@ object Utils extends CometTypeShim with Logging {
490498
}
491499
}
492500

501+
/**
502+
* Field vectors of `batch` and the dictionaries they refer to, as one provider.
503+
*
504+
* Some of the vectors may have been allocated here rather than taken from `batch`; see
505+
* [[getBatchFieldVectorsWithProviders]]. They are released with the rest when the caller clears
506+
* the vectors it wrote, or by [[closeAfterFailure]] if the write fails.
507+
*/
493508
def getBatchFieldVectors(
494509
batch: ColumnarBatch): (Seq[FieldVector], Option[DictionaryProvider]) = {
495510
val columns = getBatchFieldVectorsWithProviders(batch)
496-
(columns.map(_._1), combineDictionaryProviders(columns))
511+
val vectors = columns.map(_._1)
512+
try {
513+
(vectors, combineDictionaryProviders(columns))
514+
} catch {
515+
case NonFatal(e) =>
516+
closeAfterFailure(allocatedFor(batch, vectors), e)
517+
throw e
518+
}
519+
}
520+
521+
/**
522+
* The vectors of `vectors`, which were returned for `batch`, that were allocated for it rather
523+
* than taken from it: a 32-bit copy of a large-offset column, or a materialized
524+
* `ConstantColumnVector`. Whoever holds the returned vectors is the only one who can release
525+
* them, and `batch` still owns the rest.
526+
*/
527+
def allocatedFor(batch: ColumnarBatch, vectors: Seq[FieldVector]): Seq[FieldVector] = {
528+
val borrowed = (0 until batch.numCols()).flatMap { i =>
529+
batch.column(i) match {
530+
case v: CometVector => Some(v.getValueVector)
531+
case _ => None
532+
}
533+
}
534+
vectors.filterNot(v => borrowed.exists(_ eq v))
497535
}
498536

537+
/** Closes `vectors` for a caller that is failing with `failure`, which keeps any error. */
538+
def closeAfterFailure(vectors: Seq[ValueVector], failure: Throwable): Unit =
539+
vectors.foreach { v =>
540+
try v.close()
541+
catch { case NonFatal(closeError) => failure.addSuppressed(closeError) }
542+
}
543+
499544
/**
500545
* The dictionaries every dictionary-encoded column of `columns` refers to, as one provider.
501546
*
@@ -558,6 +603,12 @@ object Utils extends CometTypeShim with Logging {
558603
* what a single stream over every column needs. Comet's cache serializer keeps the pairing
559604
* instead: its payload has no schema message to describe a dictionary encoding, so it decodes
560605
* each such column against the provider that column arrived with.
606+
*
607+
* Not every returned vector is `batch`'s own. A large-offset column is replaced by a 32-bit
608+
* copy and a `ConstantColumnVector` by a materialized one, and the caller releases those, as
609+
* [[allocatedFor]] tells them apart. A caller that only unloads or closes the vectors it
610+
* decoded itself would leak them. If a later column fails, the ones already made are closed
611+
* here before the failure is rethrown.
561612
*/
562613
def getBatchFieldVectorsWithProviders(
563614
batch: ColumnarBatch): Seq[(FieldVector, Option[DictionaryProvider])] = {
@@ -614,9 +665,7 @@ object Utils extends CometTypeShim with Logging {
614665
}
615666
} catch {
616667
case NonFatal(e) =>
617-
made.foreach(v =>
618-
try v.close()
619-
catch { case NonFatal(closeError) => e.addSuppressed(closeError) })
668+
closeAfterFailure(made.toSeq, e)
620669
throw e
621670
}
622671
}

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import scala.jdk.CollectionConverters._
3030

3131
import org.apache.arrow.compression.ZstdCompressionCodec
3232
import org.apache.arrow.memory.{ArrowBuf, BufferAllocator}
33-
import org.apache.arrow.vector.{BitVector, FixedSizeBinaryVector, IntVector, VarBinaryVector, VarCharVector}
33+
import org.apache.arrow.vector.{BitVector, FixedSizeBinaryVector, IntVector, LargeVarCharVector, VarBinaryVector, VarCharVector}
3434
import org.apache.arrow.vector.compression.{CompressionCodec, CompressionUtil, NoCompressionCodec}
3535
import org.apache.arrow.vector.types.pojo.ArrowType
3636
import org.apache.spark.{SparkConf, SparkException}
@@ -3039,6 +3039,57 @@ class CometInMemoryCacheSuite extends CometTestBase {
30393039
}
30403040
}
30413041

3042+
test("Comet in-memory cache releases the 32-bit copy it makes of a large-offset column") {
3043+
// A large-offset column is written as a copy with 32-bit offsets, which nothing outside the
3044+
// writer ever holds. It has to be closed whether the write succeeds or fails, and a copy can
3045+
// be close to 2 GiB.
3046+
val rows = 16
3047+
// Returns the failure of the write, if any, once the batch's own vectors are closed and the
3048+
// allocator is back where it started.
3049+
def write(codec: CompressionCodec): Option[Throwable] = {
3050+
val baseline = CometArrowAllocator.getAllocatedMemory
3051+
val ints = new IntVector("i", CometArrowAllocator)
3052+
val large = new LargeVarCharVector("s", CometArrowAllocator)
3053+
val failure =
3054+
try {
3055+
ints.allocateNew(rows)
3056+
(0 until rows).foreach(i => ints.set(i, i))
3057+
ints.setValueCount(rows)
3058+
large.allocateNew(rows)
3059+
(0 until rows).foreach(i => large.setSafe(i, s"value_$i".getBytes("UTF-8")))
3060+
large.setValueCount(rows)
3061+
3062+
val batch = new ColumnarBatch(
3063+
Array[ColumnVector](new CometPlainVector(ints), new CometPlainVector(large)),
3064+
rows)
3065+
try {
3066+
CometCachedBatchHelper.serialize(batch, codec, CometArrowAllocator)
3067+
None
3068+
} catch {
3069+
case e: Exception => Some(e)
3070+
}
3071+
} finally {
3072+
ints.close()
3073+
large.close()
3074+
}
3075+
assert(
3076+
CometArrowAllocator.getAllocatedMemory == baseline,
3077+
"the copy of the large-offset column must be released")
3078+
failure
3079+
}
3080+
3081+
assert(write(new ZstdCompressionCodec(1)).isEmpty)
3082+
3083+
// The int column's two buffers compress, then the copy's first one fails.
3084+
val codec = new FailAfterCompressionCodec(succeedFor = 2)
3085+
val thrown = write(codec).getOrElse(fail("the injected failure must surface"))
3086+
assert(
3087+
causeChain(thrown).exists(t =>
3088+
Option(t.getMessage).contains(FailAfterCompressionCodec.Message)),
3089+
s"a compression failure must surface as itself: $thrown")
3090+
assert(codec.compressed == 2, "the failure must come after some buffers were compressed")
3091+
}
3092+
30423093
test("Comet in-memory cache keeps no compressed bytes alive behind a buffer stored raw") {
30433094
// Arrow stores a buffer verbatim when compressing it would not make it smaller -- a bitmap of
30443095
// random bits, say -- and decompressing hands it back as a slice of whatever holds it. Copied

‎spark/src/test/scala/org/apache/spark/sql/comet/util/UtilsSuite.scala‎

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import scala.jdk.CollectionConverters._
2626

2727
import org.apache.arrow.c.CDataDictionaryProvider
2828
import org.apache.arrow.memory.BufferAllocator
29-
import org.apache.arrow.vector.{BaseLargeVariableWidthVector, BitVectorHelper, LargeVarBinaryVector, LargeVarCharVector, ValueVector, VarBinaryVector, VarCharVector, VariableWidthFieldVector}
29+
import org.apache.arrow.vector.{BaseLargeVariableWidthVector, BitVectorHelper, IntVector, LargeVarBinaryVector, LargeVarCharVector, ValueVector, VarBinaryVector, VarCharVector, VariableWidthFieldVector}
30+
import org.apache.arrow.vector.dictionary.{Dictionary, DictionaryProvider}
3031
import org.apache.arrow.vector.ipc.message.ArrowFieldNode
32+
import org.apache.arrow.vector.types.pojo.{ArrowType, DictionaryEncoding, Field, FieldType}
3133
import org.apache.spark.SparkException
3234
import org.apache.spark.sql.CometTestBase
3335
import org.apache.spark.sql.execution.vectorized.ConstantColumnVector
@@ -237,7 +239,9 @@ class UtilsSuite extends CometTestBase {
237239
// mapInArrow returning pa.large_string() produces one -- but getFieldVector rejects both, and
238240
// getBatchFieldVectors writes a 32-bit copy of them rather than the vector itself. A caller
239241
// taking the direct write path expects the batch's own vectors, so isArrowBacked must report
240-
// false and let the caller convert the batch instead.
242+
// false and let the caller convert the batch instead. The cache's direct write path also asks
243+
// CachedBatchIpc.matchesReaderLayout, which refuses LargeUtf8 against the reader's Utf8, so
244+
// either check alone keeps such a batch off it.
241245
val values = Seq("hello", "world")
242246
Seq[ValueVector](
243247
fill(new LargeVarCharVector("s", CometArrowAllocator), values),
@@ -314,7 +318,7 @@ class UtilsSuite extends CometTestBase {
314318
}
315319
}
316320

317-
test("coalesceBroadcastBatches appends a large-offset batch to a 32-bit one") {
321+
test("coalesceBroadcastBatches coalesces batches of mixed offset widths") {
318322
// The coalescer appends every batch to a root built from the first one's schema, and leaves
319323
// the broadcast uncoalesced when a batch does not match it. A producer can hand back
320324
// large_string for one batch and string for the next, so both must serialize alike.
@@ -339,19 +343,6 @@ class UtilsSuite extends CometTestBase {
339343
}
340344
}
341345

342-
test("serializeBatches refuses a large-offset column too big for 32-bit offsets") {
343-
// The offsets alone claim 2 GiB of data. The size is checked before anything is copied, so
344-
// the data itself never has to exist.
345-
val strings = tooBigForIntOffsets("s")
346-
try {
347-
val batch = new ColumnarBatch(Array(cometVector(strings)), 1)
348-
val e = intercept[SparkException](Utils.serializeBatches(Iterator(batch)).next())
349-
assert(e.getMessage.contains("more than 32-bit offsets can address"), e.getMessage)
350-
} finally {
351-
strings.close()
352-
}
353-
}
354-
355346
test("serializeBatches releases the vectors it made when a later column fails") {
356347
// A materialized constant and a 32-bit copy of a large-offset column are both made before the
357348
// last column is refused. Nothing outside serializeBatches ever holds them, so it has to
@@ -374,6 +365,49 @@ class UtilsSuite extends CometTestBase {
374365
assert(CometArrowAllocator.getAllocatedMemory == before)
375366
}
376367

368+
test("serializeBatches releases the vectors it made when the dictionaries of a batch clash") {
369+
// The vectors are all made before the dictionaries of the columns are combined, which is where
370+
// two columns sharing an ID without sharing a dictionary are refused. By then the caller has
371+
// not got them either, and a copy of a large-offset column can be close to 2 GiB.
372+
val before = CometArrowAllocator.getAllocatedMemory
373+
val large = fill(new LargeVarCharVector("large", CometArrowAllocator), Seq("a"))
374+
val (first, firstProvider) = dictionaryEncoded("first", 7L, "x")
375+
val (second, secondProvider) = dictionaryEncoded("second", 7L, "y")
376+
try {
377+
val batch = new ColumnarBatch(
378+
Array(
379+
cometVector(large),
380+
CometVector.getVector(first, firstProvider),
381+
CometVector.getVector(second, secondProvider)),
382+
1)
383+
val e = intercept[SparkException](Utils.serializeBatches(Iterator(batch)).next())
384+
assert(e.getMessage.contains("different dictionaries under ID 7"), e.getMessage)
385+
assert(large.getValueCount == 1 && large.getObject(0).toString == "a")
386+
} finally {
387+
large.close()
388+
Seq(first, second).foreach(_.close())
389+
Seq(firstProvider, secondProvider).foreach(_.lookup(7L).getVector.close())
390+
}
391+
assert(CometArrowAllocator.getAllocatedMemory == before)
392+
}
393+
394+
/** A dictionary-encoded index column holding the one entry of a dictionary holding `value`. */
395+
private def dictionaryEncoded(
396+
name: String,
397+
id: Long,
398+
value: String): (IntVector, DictionaryProvider) = {
399+
val encoding = new DictionaryEncoding(id, false, null)
400+
val dictionary =
401+
new Dictionary(fill(new VarCharVector("dict", CometArrowAllocator), Seq(value)), encoding)
402+
val indices = new IntVector(
403+
new Field(name, new FieldType(true, new ArrowType.Int(32, true), encoding), null),
404+
CometArrowAllocator)
405+
indices.allocateNew(1)
406+
indices.set(0, 0)
407+
indices.setValueCount(1)
408+
(indices, new DictionaryProvider.MapDictionaryProvider(dictionary))
409+
}
410+
377411
/** Writes `values` into `vector`, leaving a null entry unset so that it reads back as null. */
378412
private def fill[V <: VariableWidthFieldVector](vector: V, values: Seq[String]): V = {
379413
vector.allocateNew(values.length)

0 commit comments

Comments
 (0)