From 749772cb0bd83c1a8fe455410ec80b1e5a9bf239 Mon Sep 17 00:00:00 2001 From: shfshihuafeng Date: Mon, 25 Mar 2024 08:29:51 +0800 Subject: [PATCH] DRILL-8485: HashJoinPOP memory leak is caused by an oom exception when read data from InputStream (#2891) --- .../cache/VectorAccessibleSerializable.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/cache/VectorAccessibleSerializable.java b/exec/java-exec/src/main/java/org/apache/drill/exec/cache/VectorAccessibleSerializable.java index d0f4c0345f4..2b85d7912bd 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/cache/VectorAccessibleSerializable.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/cache/VectorAccessibleSerializable.java @@ -24,6 +24,7 @@ import java.io.OutputStream; import java.util.List; +import org.apache.drill.common.exceptions.UserException; import org.apache.drill.exec.expr.TypeHelper; import org.apache.drill.exec.memory.BufferAllocator; import org.apache.drill.exec.metrics.DrillMetrics; @@ -124,11 +125,19 @@ private void readVectors(InputStream input, RecordBatchDef batchDef) throws IOEx for (SerializedField metaData : fieldList) { final int dataLength = metaData.getBufferLength(); final MaterializedField field = MaterializedField.create(metaData); - final DrillBuf buf = allocator.read(dataLength, input); - final ValueVector vector = TypeHelper.getNewVector(field, allocator); - vector.load(metaData, buf); - buf.release(); // Vector now owns the buffer - vectorList.add(vector); + DrillBuf buf = null; + try { + buf = allocator.read(dataLength, input); + final ValueVector vector = TypeHelper.getNewVector(field, allocator); + vector.load(metaData, buf); + buf.release(); // Vector now owns the buffer + vectorList.add(vector); + } catch (OutOfMemoryError oom) { + for (ValueVector valueVector : vectorList) { + valueVector.clear(); + } + throw UserException.memoryError(oom).message("Allocator memory failed").build(logger); + } } container.addCollection(vectorList); container.buildSchema(svMode);