Repository navigation
GH-1464: Clear vectors before loading their buffers #1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,9 +255,14 @@ public void loadFieldBuffers(ArrowFieldNode fieldNode, List<ArrowBuf> ownBuffers | |
| ArrowBuf bitBuffer = ownBuffers.get(0); | ||
| ArrowBuf offBuffer = ownBuffers.get(1); | ||
|
|
||
| validityBuffer.getReferenceManager().release(); | ||
| // Release this vector's buffers first, so that a failure while loading leaves it empty but | ||
| // usable. Unlike clear(), this keeps the child vectors. | ||
| validityBuffer = releaseBuffer(validityBuffer); | ||
| offsetBuffer = releaseBuffer(offsetBuffer); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this runs into the question of whether an empty offset buffer is valid for a list vector...but it's consistent with before so we can defer that discussion. |
||
| lastSet = -1; | ||
| valueCount = 0; | ||
|
|
||
| validityBuffer = BitVectorHelper.loadValidityBuffer(fieldNode, bitBuffer, allocator); | ||
| offsetBuffer.getReferenceManager().release(); | ||
| offsetBuffer = offBuffer.getReferenceManager().retain(offBuffer, allocator); | ||
|
|
||
| validityAllocationSizeInBytes = checkedCastToInt(validityBuffer.capacity()); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
vector/src/test/java/org/apache/arrow/vector/TestLoadFieldBuffers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.arrow.vector; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Stream; | ||
| import org.apache.arrow.memory.ArrowBuf; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.memory.OutOfMemoryException; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.complex.FixedSizeListVector; | ||
| import org.apache.arrow.vector.complex.LargeListVector; | ||
| import org.apache.arrow.vector.complex.LargeListViewVector; | ||
| import org.apache.arrow.vector.complex.ListVector; | ||
| import org.apache.arrow.vector.complex.ListViewVector; | ||
| import org.apache.arrow.vector.complex.StructVector; | ||
| import org.apache.arrow.vector.ipc.message.ArrowFieldNode; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| /** Checks that a vector whose buffers fail to load is left empty, and can still be closed. */ | ||
| public class TestLoadFieldBuffers { | ||
|
|
||
| static Stream<Arguments> vectors() { | ||
| return Stream.of( | ||
| vector(a -> new IntVector("v", a), 2), | ||
| vector(a -> new VarCharVector("v", a), 3), | ||
| vector(a -> new LargeVarCharVector("v", a), 3), | ||
| vector(a -> new ViewVarCharVector("v", a), 2), | ||
| vector(a -> ListVector.empty("v", a), 2), | ||
| vector(a -> LargeListVector.empty("v", a), 2), | ||
| vector(a -> FixedSizeListVector.empty("v", 2, a), 1), | ||
| vector(a -> ListViewVector.empty("v", a), 3), | ||
| vector(a -> LargeListViewVector.empty("v", a), 3), | ||
| vector(a -> StructVector.empty("v", a), 1)); | ||
| } | ||
|
|
||
| static Stream<Arguments> vectorsWithRetainedBuffers() { | ||
| // The validity buffer is the only buffer of a fixed size list or a struct vector | ||
| return vectors().filter(arguments -> (int) arguments.get()[1] > 1); | ||
| } | ||
|
|
||
| private static Arguments vector(Function<BufferAllocator, FieldVector> create, int bufferCount) { | ||
| return Arguments.of(create, bufferCount); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("vectors") | ||
| public void testFailedValidityLoadClearsVector( | ||
| Function<BufferAllocator, FieldVector> create, int bufferCount) { | ||
| try (BufferAllocator allocator = new RootAllocator(64 * 1024)) { | ||
| FieldVector vector = create.apply(allocator); | ||
| vector.setInitialCapacity(16); | ||
| vector.allocateNew(); | ||
| vector.setValueCount(16); | ||
| List<ArrowBuf> buffers = Collections.nCopies(bufferCount, allocator.getEmpty()); | ||
|
|
||
| // Without a validity buffer, 1,000,000 values need a 125 KB validity buffer, which is more | ||
| // than the allocator allows | ||
| assertThrows( | ||
| OutOfMemoryException.class, | ||
| () -> vector.loadFieldBuffers(new ArrowFieldNode(1_000_000, 0), buffers)); | ||
| assertEquals(0, vector.getValueCount()); | ||
| // A missing validity buffer is rejected when only some values are null | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> vector.loadFieldBuffers(new ArrowFieldNode(16, 3), buffers)); | ||
| assertEquals(0, vector.getValueCount()); | ||
|
|
||
| // Closing the vector releases each of its buffers once | ||
| vector.close(); | ||
| assertEquals(0, allocator.getAllocatedMemory()); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("vectorsWithRetainedBuffers") | ||
| public void testFailedBufferRetainClearsVector( | ||
| Function<BufferAllocator, FieldVector> create, int bufferCount) { | ||
| try (BufferAllocator allocator = new RootAllocator(); | ||
| BufferAllocator otherAllocator = new RootAllocator(); | ||
| ArrowBuf otherBuffer = otherAllocator.buffer(64)) { | ||
| FieldVector vector = create.apply(allocator); | ||
| vector.setInitialCapacity(16); | ||
| vector.allocateNew(); | ||
| vector.setValueCount(16); | ||
| // The validity buffer is created by the vector, but the next buffer belongs to another root | ||
| // allocator, so the vector's allocator can't retain it | ||
| List<ArrowBuf> buffers = new ArrayList<>(Collections.nCopies(bufferCount, otherBuffer)); | ||
| buffers.set(0, allocator.getEmpty()); | ||
|
|
||
| assertThrows( | ||
| IllegalStateException.class, | ||
| () -> vector.loadFieldBuffers(new ArrowFieldNode(16, 0), buffers)); | ||
| assertEquals(0, vector.getValueCount()); | ||
|
|
||
| // Closing the vector releases each of its buffers once | ||
| vector.close(); | ||
| assertEquals(0, allocator.getAllocatedMemory()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we be doing the same thing for the value buffer? Otherwise you still have an inconsistent vector.
Also, this would mean that you temporarily have 2x memory usage instead of 1x as before. Is it necessarily wrong to say that failing a load means the vector is cleared? After all, if you're loading data, presumably you didn't care about the previous contents of the vector anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points, thanks. You're right: the offset, size and value buffers had the same problem (
retaincan still throw, for example for a buffer from another root allocator), and there's no reason to keep the old contents of a vector we're loading into.I changed
loadFieldBuffersto release the vector's own buffers and reset its value count before loading anything. A failed load now leaves the vector empty but closeable, with no extra allocation. The leaf vectors just callclear(), asBaseVariableWidthViewVectoralready did. The list and struct vectors only release their own buffers, so their child vectors are left alone.I also added a test where retaining a buffer after the validity buffer fails. On the previous commit it hit
RefCnt has gone negativeon close for every vector exceptBaseVariableWidthViewVector, which instead leaked the new validity buffer, so I reverted that one tomain.