From d9a52c359866e6d97f522fc88bad4f1ad7eb0e5e Mon Sep 17 00:00:00 2001 From: vkorukanti Date: Wed, 25 Apr 2018 11:13:04 -0700 Subject: [PATCH] ARROW-2511: [Java] Fix BaseVariableWidthVector.allocateNew to not swallow exception + Also remove the e.printStackTrace() calls --- .../arrow/vector/BaseFixedWidthVector.java | 4 +- .../arrow/vector/BaseVariableWidthVector.java | 3 +- .../vector/TestOutOfMemoryForValueVector.java | 73 +++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java diff --git a/java/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java b/java/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java index 4b47df8a450a0..b275ab22db89d 100644 --- a/java/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java +++ b/java/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java @@ -281,7 +281,6 @@ public boolean allocateNewSafe() { try { allocateBytes(curAllocationSizeValue, curAllocationSizeValidity); } catch (Exception e) { - e.printStackTrace(); clear(); return false; } @@ -314,7 +313,6 @@ public void allocateNew(int valueCount) { try { allocateBytes(valueBufferSize, validityBufferSize); } catch (Exception e) { - e.printStackTrace(); clear(); throw e; } @@ -841,4 +839,4 @@ protected void handleSafe(int index) { reAlloc(); } } -} \ No newline at end of file +} diff --git a/java/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java b/java/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java index d56da1bdd2cda..44df96b82827d 100644 --- a/java/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java +++ b/java/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java @@ -394,7 +394,6 @@ public boolean allocateNewSafe() { try { allocateBytes(curAllocationSizeValue, curAllocationSizeValidity, curAllocationSizeOffset); } catch (Exception e) { - e.printStackTrace(); clear(); return false; } @@ -427,8 +426,8 @@ public void allocateNew(int totalBytes, int valueCount) { try { allocateBytes(totalBytes, validityBufferSize, offsetBufferSize); } catch (Exception e) { - e.printStackTrace(); clear(); + throw e; } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java new file mode 100644 index 0000000000000..84c0fc33d9d4d --- /dev/null +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java @@ -0,0 +1,73 @@ +/* + * 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 org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.OutOfMemoryException; +import org.apache.arrow.memory.RootAllocator; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * This class tests cases where we expect to receive {@link OutOfMemoryException}. + */ +public class TestOutOfMemoryForValueVector { + + private final static String EMPTY_SCHEMA_PATH = ""; + + private BufferAllocator allocator; + + @Before + public void init() { + allocator = new RootAllocator(200); // Start with low memory limit + } + + @Test(expected = OutOfMemoryException.class) + public void variableWidthVectorAllocateNew() { + try (VarCharVector vector = new VarCharVector(EMPTY_SCHEMA_PATH, allocator)) { + vector.allocateNew(); + } + } + + @Test(expected = OutOfMemoryException.class) + public void variableWidthVectorAllocateNewCustom() { + try (VarCharVector vector = new VarCharVector(EMPTY_SCHEMA_PATH, allocator)) { + vector.allocateNew(2342, 234); + } + } + + @Test(expected = OutOfMemoryException.class) + public void fixedWidthVectorAllocateNew() { + try (IntVector vector = new IntVector(EMPTY_SCHEMA_PATH, allocator)) { + vector.allocateNew(); + } + } + + @Test(expected = OutOfMemoryException.class) + public void fixedWidthVectorAllocateNewCustom() { + try (IntVector vector = new IntVector(EMPTY_SCHEMA_PATH, allocator)) { + vector.allocateNew(2342); + } + } + + @After + public void terminate() { + allocator.close(); + } +}