rahil-c commented on code in PR #17632: URL: https://github.com/apache/hudi/pull/17632#discussion_r2644199798
########## hudi-common/src/main/java/org/apache/hudi/common/util/HoodieArrowAllocator.java: ########## @@ -0,0 +1,81 @@ +/* + * 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.hudi.common.util; + +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; + +/** + * Manages Arrow BufferAllocator lifecycle for Arrow-based file format operations. + * + * <p>Following Arrow best practices: + * <ul> + * <li>Single RootAllocator per application</li> + * <li>Named child allocators for debugging and isolation</li> + * <li>Caller-specified memory limits per child allocator</li> + * </ul> + * + * <p>The root allocator is hardcoded to Long.MAX_VALUE and acts as a + * bookkeeper. Memory limits are enforced at the child allocator level, with each + * caller specifying an appropriate limit for their use case. + * + * @see <a href="https://arrow.apache.org/docs/java/memory.html">Arrow Memory Management</a> + */ +public class HoodieArrowAllocator { + + private HoodieArrowAllocator() { + // Utility class + } + + /** + * Initialization-on-demand holder idiom for thread-safe lazy initialization. + * The root allocator is created when first accessed and has a maximum size of Long.MAX_VALUE. + */ + private static class RootAllocatorHolder { + static final BufferAllocator INSTANCE = new RootAllocator(Long.MAX_VALUE); + + static { + Runtime.getRuntime().addShutdownHook(new Thread( + INSTANCE::close, "hudi-arrow-root-allocator-shutdown")); + } + } + + /** + * Get the shared root allocator. + * Thread-safe lazy initialization using the holder idiom. + * + * @return The singleton root allocator instance + */ + private static BufferAllocator getRootAllocator() { + return RootAllocatorHolder.INSTANCE; + } + + /** + * Create a named child allocator for Arrow operations. + * Caller is responsible for closing the returned allocator. + * + * @param name Descriptive name for debugging (e.g., "HoodieSparkLanceReader-data-file.lance") + * @param childSizeBytes Maximum memory size in bytes for this child allocator + * @return A new child allocator with the specified size limit + */ + public static BufferAllocator newChildAllocator(String name, long childSizeBytes) { Review Comment: @the-other-tim-brown When checking the arrow docs https://arrow.apache.org/docs/java/memory.html did not see anything around a size limit for the name. When checking the arrow java impl, I also dont see any logic around size naming restraints atleast when checking the super classes of ChildAllocator: https://github.com/apache/arrow-java/blob/main/memory/memory-core/src/main/java/org/apache/arrow/memory/ChildAllocator.java#L36 https://github.com/apache/arrow-java/blob/main/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java#L121 https://github.com/apache/arrow-java/blob/main/memory/memory-core/src/main/java/org/apache/arrow/memory/Accountant.java#L65 Just curious if the concern was due to me appending a file path (for some of the naming) for these child allocators like so https://github.com/apache/hudi/pull/17632/files#diff-ae5484bd256dc2ac54f998bd0a2952a2ef3789058fc87331343d539dff12fdb7R77. I did this for debuggability but its not essential and I can remove it if needed. -- 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]
