platinumhamburg commented on code in PR #2647:
URL: https://github.com/apache/fluss/pull/2647#discussion_r2799185370


##########
fluss-common/src/main/java/org/apache/fluss/shaded/arrow/org/apache/arrow/vector/VectorLoader.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.fluss.shaded.arrow.org.apache.arrow.vector;
+
+import org.apache.fluss.shaded.arrow.org.apache.arrow.memory.ArrowBuf;
+import org.apache.fluss.shaded.arrow.org.apache.arrow.util.Collections2;
+import org.apache.fluss.shaded.arrow.org.apache.arrow.util.Preconditions;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.compression.CompressionCodec;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.compression.CompressionUtil;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.compression.CompressionUtil.CodecType;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.compression.NoCompressionCodec;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.compression.NoCompressionCodec.Factory;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.ipc.message.ArrowFieldNode;
+import 
org.apache.fluss.shaded.arrow.org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+import org.apache.fluss.shaded.arrow.org.apache.arrow.vector.types.pojo.Field;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * A patched version of Arrow's {@code VectorLoader} that ensures decompressed 
buffers are properly
+ * released when an error (e.g. OOM) occurs during {@link 
#load(ArrowRecordBatch)}.
+ *
+ * <p>In the original Arrow implementation, the decompression loop runs 
<b>outside</b> the
+ * try-finally block that guards {@code loadFieldBuffers}. This means if 
decompression succeeds for
+ * the first N buffers of a field but fails on the (N+1)-th buffer, the 
already-decompressed buffers
+ * in {@code ownBuffers} are never closed, leaking Direct Memory.
+ *
+ * <p>This workaround moves the decompression loop <b>inside</b> the try block 
so that the finally
+ * clause always closes every buffer in {@code ownBuffers}, regardless of 
whether the load succeeds
+ * or fails:
+ *
+ * <ul>
+ *   <li><b>Success path:</b> {@code loadFieldBuffers} retains each buffer 
(ref count +1), then the
+ *       finally close decrements it back (ref count -1). The field vector 
still holds the buffer.
+ *   <li><b>Error path:</b> The finally close decrements each 
already-decompressed buffer's ref
+ *       count to 0, immediately freeing the Direct Memory.
+ * </ul>
+ *
+ * @see <a href="https://github.com/apache/fluss/issues/2646";>FLUSS-2646</a>
+ */
+public class VectorLoader {

Review Comment:
   I'd suggest changing the approach from class-shadowing to an explicitly 
named class under a
   Fluss-owned package. Specifically: rename this to `FlussVectorLoader` and 
move it to
   `org.apache.fluss.record`, then update the call site in 
`ArrowUtils.createArrowReader()`.
   
   I've verified locally that all Arrow APIs used in loadBuffers() — 
TypeLayout, FieldVector,
   Collections2, Preconditions, etc. — are public in the shaded JAR, so there's 
no package-visibility
   blocker. Compilation and all 3 tests pass with this change.
   
   Why this is better than class-shadowing:
   
   1. **Explicit over implicit.** The current approach relies on classpath 
ordering to silently
      override the shaded Arrow JAR's VectorLoader. With `FlussVectorLoader`, 
the call site in
      ArrowUtils makes it obvious that a patched loader is being used — no 
hidden magic.
   
   2. **Compile-time safety on Arrow upgrades.** If Arrow's VectorLoader API 
changes in a future
      version (e.g. the upstream `main` branch already added a 
`variadicBufferCounts` parameter),
      the shadowed class would silently diverge. With an explicit class, any 
API incompatibility
      surfaces as a compile error immediately.
   
   3. **No risk of classpath conflicts.** Class-shadowing behavior depends on 
JAR ordering, which
      can vary across build tools, IDEs, and deployment environments. An 
explicitly named class
      eliminates this uncertainty entirely.
   
   The change is straightforward:
   - Rename `VectorLoader` → `FlussVectorLoader` in `org.apache.fluss.record`
   - Update import + usage in `ArrowUtils.createArrowReader()`
   - Update import + usage in `VectorLoaderTest`
   
   Please also add a TODO in the class Javadoc for future cleanup:
   
      // TODO(FLUSS-2646): Remove this patched VectorLoader once Apache Arrow 
fixes
      // the buffer leak in loadBuffers(). Current Arrow version: 15.0.0
   
   Additionally, I'd recommend filing an issue in 
https://github.com/apache/arrow-java/issues to
   push for an upstream fix — that way we have a clear signal for when this 
patch can be retired.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to