|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.arrow.vector; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import org.apache.arrow.memory.ArrowBuf; |
| 26 | +import org.apache.arrow.memory.BufferAllocator; |
| 27 | +import org.apache.arrow.memory.OutOfMemoryException; |
| 28 | +import org.apache.arrow.memory.RootAllocator; |
| 29 | +import org.apache.arrow.vector.compression.AbstractCompressionCodec; |
| 30 | +import org.apache.arrow.vector.compression.CompressionUtil; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.ValueSource; |
| 34 | + |
| 35 | +/** Exception-safety tests for record batch serialization. */ |
| 36 | +class TestVectorUnloaderFailure { |
| 37 | + |
| 38 | + @ParameterizedTest |
| 39 | + @ValueSource(longs = {0, 16}) |
| 40 | + void compressionAllocationFailureReleasesBuffers(long availableBytes) { |
| 41 | + try (BufferAllocator allocator = new RootAllocator(); |
| 42 | + IntVector vector = new IntVector("values", allocator); |
| 43 | + VectorSchemaRoot root = VectorSchemaRoot.of(vector)) { |
| 44 | + vector.allocateNew(1); |
| 45 | + vector.set(0, 42); |
| 46 | + root.setRowCount(1); |
| 47 | + long allocatedBefore = allocator.getAllocatedMemory(); |
| 48 | + int referencesBefore = vector.getDataBuffer().getReferenceManager().getRefCount(); |
| 49 | + allocator.setLimit(allocatedBefore + availableBytes); |
| 50 | + VectorUnloader unloader = new VectorUnloader(root, true, new CopyCodec(), true); |
| 51 | + |
| 52 | + assertThrows(OutOfMemoryException.class, unloader::getRecordBatch); |
| 53 | + assertEquals(allocatedBefore, allocator.getAllocatedMemory()); |
| 54 | + assertEquals(referencesBefore, vector.getDataBuffer().getReferenceManager().getRefCount()); |
| 55 | + assertEquals(42, vector.get(0)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @ParameterizedTest |
| 60 | + @ValueSource(booleans = {false, true}) |
| 61 | + void compressionFailurePreservesExceptionAndSource(boolean throwError) { |
| 62 | + Throwable failure = |
| 63 | + throwError |
| 64 | + ? new OutOfMemoryError("compression failed") |
| 65 | + : new IllegalStateException("compression failed"); |
| 66 | + try (BufferAllocator allocator = new RootAllocator(); |
| 67 | + IntVector vector = new IntVector("values", allocator); |
| 68 | + VectorSchemaRoot root = VectorSchemaRoot.of(vector)) { |
| 69 | + vector.allocateNew(1); |
| 70 | + vector.set(0, 42); |
| 71 | + root.setRowCount(1); |
| 72 | + long allocatedBefore = allocator.getAllocatedMemory(); |
| 73 | + int referencesBefore = vector.getDataBuffer().getReferenceManager().getRefCount(); |
| 74 | + CopyCodec codec = |
| 75 | + new CopyCodec() { |
| 76 | + private int calls; |
| 77 | + |
| 78 | + @Override |
| 79 | + protected ArrowBuf doCompress(BufferAllocator allocator, ArrowBuf input) { |
| 80 | + if (++calls == 2) { |
| 81 | + if (failure instanceof Error) { |
| 82 | + throw (Error) failure; |
| 83 | + } |
| 84 | + throw (RuntimeException) failure; |
| 85 | + } |
| 86 | + return super.doCompress(allocator, input); |
| 87 | + } |
| 88 | + }; |
| 89 | + VectorUnloader unloader = new VectorUnloader(root, true, codec, true); |
| 90 | + |
| 91 | + assertSame(failure, assertThrows(failure.getClass(), unloader::getRecordBatch)); |
| 92 | + assertEquals(allocatedBefore, allocator.getAllocatedMemory()); |
| 93 | + assertEquals(referencesBefore, vector.getDataBuffer().getReferenceManager().getRefCount()); |
| 94 | + assertEquals(42, vector.get(0)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void emptyBufferAllocationFailureReleasesInput() { |
| 100 | + try (BufferAllocator allocator = new RootAllocator()) { |
| 101 | + ArrowBuf input = allocator.buffer(8); |
| 102 | + allocator.setLimit(allocator.getAllocatedMemory()); |
| 103 | + |
| 104 | + assertThrows(OutOfMemoryException.class, () -> new CopyCodec().compress(allocator, input)); |
| 105 | + assertEquals(0, input.getReferenceManager().getRefCount()); |
| 106 | + assertEquals(0, allocator.getAllocatedMemory()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void invalidLaterVectorReleasesPreviouslyRetainedBuffers() { |
| 112 | + try (BufferAllocator allocator = new RootAllocator(); |
| 113 | + IntVector first = new IntVector("first", allocator)) { |
| 114 | + FieldVector invalid = |
| 115 | + new NullVector("invalid") { |
| 116 | + @Override |
| 117 | + public List<ArrowBuf> getFieldBuffers() { |
| 118 | + return Collections.singletonList(allocator.getEmpty()); |
| 119 | + } |
| 120 | + }; |
| 121 | + try (VectorSchemaRoot root = VectorSchemaRoot.of(first, invalid)) { |
| 122 | + first.allocateNew(1); |
| 123 | + first.set(0, 42); |
| 124 | + root.setRowCount(1); |
| 125 | + int referencesBefore = first.getDataBuffer().getReferenceManager().getRefCount(); |
| 126 | + |
| 127 | + assertThrows(IllegalArgumentException.class, new VectorUnloader(root)::getRecordBatch); |
| 128 | + assertEquals(referencesBefore, first.getDataBuffer().getReferenceManager().getRefCount()); |
| 129 | + assertEquals(42, first.get(0)); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static class CopyCodec extends AbstractCompressionCodec { |
| 135 | + @Override |
| 136 | + protected ArrowBuf doCompress(BufferAllocator allocator, ArrowBuf input) { |
| 137 | + return CompressionUtil.packageRawBuffer(allocator, input); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected ArrowBuf doDecompress(BufferAllocator allocator, ArrowBuf input) { |
| 142 | + throw new UnsupportedOperationException(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public CompressionUtil.CodecType getCodecType() { |
| 147 | + return CompressionUtil.CodecType.LZ4_FRAME; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments