BryanCutler commented on a change in pull request #6323:
URL: https://github.com/apache/arrow/pull/6323#discussion_r414695756



##########
File path: 
java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java
##########
@@ -17,48 +17,97 @@
 
 package org.apache.arrow.memory;
 
-import org.apache.arrow.memory.util.LargeMemoryUtil;
-
 import io.netty.buffer.PooledByteBufAllocatorL;
 import io.netty.buffer.UnsafeDirectLittleEndian;
+import io.netty.util.internal.PlatformDependent;
 
 /**
- * The default implementation of AllocationManagerBase. The implementation is 
responsible for managing when memory
+ * The default implementation of {@link AllocationManager}. The implementation 
is responsible for managing when memory
  * is allocated and returned to the Netty-based PooledByteBufAllocatorL.
  */
 public class NettyAllocationManager extends AllocationManager {
 
   public static final Factory FACTORY = new Factory();
 
+  /**
+   * The default cut-off value for switching allocation strategies.
+   * If the request size is not greater than the cut-off value, we will 
allocate memory by
+   * {@link PooledByteBufAllocatorL} APIs,
+   * otherwise, we will use {@link PlatformDependent} APIs.
+   */
+  public static final long DEFAULT_ALLOCATION_CUTOFF_VALUE;
+
+  public static final String DEFAULT_ALLOCATION_CUTOFF_NAME = 
"default.allocation.cutoff.name";
+
+  static {
+    long cutOffValue;
+    try {
+      cutOffValue = 
Long.parseLong(System.getProperty(DEFAULT_ALLOCATION_CUTOFF_NAME));
+    } catch (Exception e) {
+      cutOffValue = Integer.MAX_VALUE;
+    }
+    DEFAULT_ALLOCATION_CUTOFF_VALUE = cutOffValue;
+  }
+
   private static final PooledByteBufAllocatorL INNER_ALLOCATOR = new 
PooledByteBufAllocatorL();
   static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty;
   static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize();
 
-  private final int allocatedSize;
+  private final long allocatedSize;
   private final UnsafeDirectLittleEndian memoryChunk;
+  private final long allocatedAddress;
+
+  /**
+   * The cut-off value for switching allocation strategies.
+   */
+  private final long allocationCutOffValue;

Review comment:
       This should be an int because it can't go above `Int.MAX_VALUE`

##########
File path: 
java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java
##########
@@ -17,48 +17,97 @@
 
 package org.apache.arrow.memory;
 
-import org.apache.arrow.memory.util.LargeMemoryUtil;
-
 import io.netty.buffer.PooledByteBufAllocatorL;
 import io.netty.buffer.UnsafeDirectLittleEndian;
+import io.netty.util.internal.PlatformDependent;
 
 /**
- * The default implementation of AllocationManagerBase. The implementation is 
responsible for managing when memory
+ * The default implementation of {@link AllocationManager}. The implementation 
is responsible for managing when memory
  * is allocated and returned to the Netty-based PooledByteBufAllocatorL.
  */
 public class NettyAllocationManager extends AllocationManager {
 
   public static final Factory FACTORY = new Factory();
 
+  /**
+   * The default cut-off value for switching allocation strategies.
+   * If the request size is not greater than the cut-off value, we will 
allocate memory by
+   * {@link PooledByteBufAllocatorL} APIs,
+   * otherwise, we will use {@link PlatformDependent} APIs.
+   */
+  public static final long DEFAULT_ALLOCATION_CUTOFF_VALUE;
+
+  public static final String DEFAULT_ALLOCATION_CUTOFF_NAME = 
"default.allocation.cutoff.name";
+
+  static {
+    long cutOffValue;
+    try {
+      cutOffValue = 
Long.parseLong(System.getProperty(DEFAULT_ALLOCATION_CUTOFF_NAME));
+    } catch (Exception e) {
+      cutOffValue = Integer.MAX_VALUE;
+    }
+    DEFAULT_ALLOCATION_CUTOFF_VALUE = cutOffValue;
+  }
+
   private static final PooledByteBufAllocatorL INNER_ALLOCATOR = new 
PooledByteBufAllocatorL();
   static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty;
   static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize();
 
-  private final int allocatedSize;
+  private final long allocatedSize;
   private final UnsafeDirectLittleEndian memoryChunk;
+  private final long allocatedAddress;
+
+  /**
+   * The cut-off value for switching allocation strategies.
+   */
+  private final long allocationCutOffValue;
 
-  NettyAllocationManager(BaseAllocator accountingAllocator, int requestedSize) 
{
+  NettyAllocationManager(BaseAllocator accountingAllocator, long 
requestedSize, long allocationCutOffValue) {
     super(accountingAllocator);
-    this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize);
-    this.allocatedSize = memoryChunk.capacity();
+    if (allocationCutOffValue > Integer.MAX_VALUE) {
+      throw new IllegalArgumentException("The cut-off value cannot be larger 
than Integer.MAX_VALUE");
+    }
+    this.allocationCutOffValue = allocationCutOffValue;
+
+    if (requestedSize > allocationCutOffValue) {
+      this.memoryChunk = null;
+      this.allocatedAddress = PlatformDependent.allocateMemory(requestedSize);
+      this.allocatedSize = requestedSize;
+    } else {
+      this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize);
+      this.allocatedAddress = memoryChunk.memoryAddress();
+      this.allocatedSize = memoryChunk.capacity();
+    }
+  }
+
+  NettyAllocationManager(BaseAllocator accountingAllocator, long 
requestedSize) {
+    this(accountingAllocator, requestedSize, DEFAULT_ALLOCATION_CUTOFF_VALUE);
   }
 
   /**
    * Get the underlying memory chunk managed by this AllocationManager.
-   * @return buffer
+   * @return the underlying memory chunk if the request size is not greater 
than the
+   *   {@link NettyAllocationManager#allocationCutOffValue}, or null otherwise.
+   *
+   * @deprecated this method will be removed in a future release.
    */
+  @Deprecated
   UnsafeDirectLittleEndian getMemoryChunk() {
-    return memoryChunk;
+    return allocatedSize > allocationCutOffValue ? null : memoryChunk;
   }
 
   @Override
   protected long memoryAddress() {
-    return memoryChunk.memoryAddress();
+    return allocatedAddress;
   }
 
   @Override
   protected void release0() {
-    memoryChunk.release();
+    if (allocatedSize > allocationCutOffValue) {

Review comment:
       just check if `memoryChunk` is null here

##########
File path: 
java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java
##########
@@ -17,48 +17,97 @@
 
 package org.apache.arrow.memory;
 
-import org.apache.arrow.memory.util.LargeMemoryUtil;
-
 import io.netty.buffer.PooledByteBufAllocatorL;
 import io.netty.buffer.UnsafeDirectLittleEndian;
+import io.netty.util.internal.PlatformDependent;
 
 /**
- * The default implementation of AllocationManagerBase. The implementation is 
responsible for managing when memory
+ * The default implementation of {@link AllocationManager}. The implementation 
is responsible for managing when memory
  * is allocated and returned to the Netty-based PooledByteBufAllocatorL.
  */
 public class NettyAllocationManager extends AllocationManager {
 
   public static final Factory FACTORY = new Factory();
 
+  /**
+   * The default cut-off value for switching allocation strategies.
+   * If the request size is not greater than the cut-off value, we will 
allocate memory by
+   * {@link PooledByteBufAllocatorL} APIs,
+   * otherwise, we will use {@link PlatformDependent} APIs.
+   */
+  public static final long DEFAULT_ALLOCATION_CUTOFF_VALUE;
+
+  public static final String DEFAULT_ALLOCATION_CUTOFF_NAME = 
"default.allocation.cutoff.name";
+
+  static {
+    long cutOffValue;
+    try {
+      cutOffValue = 
Long.parseLong(System.getProperty(DEFAULT_ALLOCATION_CUTOFF_NAME));
+    } catch (Exception e) {
+      cutOffValue = Integer.MAX_VALUE;
+    }
+    DEFAULT_ALLOCATION_CUTOFF_VALUE = cutOffValue;
+  }
+
   private static final PooledByteBufAllocatorL INNER_ALLOCATOR = new 
PooledByteBufAllocatorL();
   static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty;
   static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize();
 
-  private final int allocatedSize;
+  private final long allocatedSize;
   private final UnsafeDirectLittleEndian memoryChunk;
+  private final long allocatedAddress;
+
+  /**
+   * The cut-off value for switching allocation strategies.
+   */
+  private final long allocationCutOffValue;
 
-  NettyAllocationManager(BaseAllocator accountingAllocator, int requestedSize) 
{
+  NettyAllocationManager(BaseAllocator accountingAllocator, long 
requestedSize, long allocationCutOffValue) {
     super(accountingAllocator);
-    this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize);
-    this.allocatedSize = memoryChunk.capacity();
+    if (allocationCutOffValue > Integer.MAX_VALUE) {
+      throw new IllegalArgumentException("The cut-off value cannot be larger 
than Integer.MAX_VALUE");
+    }
+    this.allocationCutOffValue = allocationCutOffValue;
+
+    if (requestedSize > allocationCutOffValue) {
+      this.memoryChunk = null;
+      this.allocatedAddress = PlatformDependent.allocateMemory(requestedSize);
+      this.allocatedSize = requestedSize;
+    } else {
+      this.memoryChunk = INNER_ALLOCATOR.allocate(requestedSize);
+      this.allocatedAddress = memoryChunk.memoryAddress();
+      this.allocatedSize = memoryChunk.capacity();
+    }
+  }
+
+  NettyAllocationManager(BaseAllocator accountingAllocator, long 
requestedSize) {
+    this(accountingAllocator, requestedSize, DEFAULT_ALLOCATION_CUTOFF_VALUE);
   }
 
   /**
    * Get the underlying memory chunk managed by this AllocationManager.
-   * @return buffer
+   * @return the underlying memory chunk if the request size is not greater 
than the
+   *   {@link NettyAllocationManager#allocationCutOffValue}, or null otherwise.
+   *
+   * @deprecated this method will be removed in a future release.
    */
+  @Deprecated
   UnsafeDirectLittleEndian getMemoryChunk() {
-    return memoryChunk;
+    return allocatedSize > allocationCutOffValue ? null : memoryChunk;

Review comment:
       why add this? just return `memoryChunk`, it with either be null or a 
value

##########
File path: 
java/memory/src/test/java/org/apache/arrow/memory/TestNettyAllocationManager.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.memory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import io.netty.buffer.ArrowBuf;
+
+/**
+ * Test cases for {@link NettyAllocationManager}.
+ */
+public class TestNettyAllocationManager {
+
+  private void readWriteArrowBuf(ArrowBuf buffer) {
+    // write buffer
+    for (long i = 0; i < buffer.capacity() / 8; i++) {
+      buffer.setLong(i * 8, i);
+    }
+
+    // read buffer
+    for (long i = 0; i < buffer.capacity() / 8; i++) {
+      long val = buffer.getLong(i * 8);
+      assertEquals(i, val);
+    }
+  }
+
+  /**
+   * Test the allocation strategy for small buffers..
+   */
+  @Test
+  public void testSmallBufferAllocation() {
+    final long bufSize = 512L;
+    try (RootAllocator allocator = new RootAllocator(bufSize);
+         ArrowBuf buffer = allocator.buffer(bufSize)) {
+      // make sure the buffer is small enough, so we wil use the allocation 
strategy for small buffers
+      assertTrue(bufSize < 
NettyAllocationManager.DEFAULT_ALLOCATION_CUTOFF_VALUE);
+
+      assertTrue(buffer.getReferenceManager() instanceof BufferLedger);
+      BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager();
+
+      // make sure we are using netty allocation manager
+      AllocationManager allocMgr = bufferLedger.getAllocationManager();
+      assertTrue(allocMgr instanceof NettyAllocationManager);
+      NettyAllocationManager nettyMgr = (NettyAllocationManager) allocMgr;
+
+      // for the small buffer allocation strategy, the chunk is not null
+      assertNotNull(nettyMgr.getMemoryChunk());
+
+      readWriteArrowBuf(buffer);
+    }
+  }
+
+  /**
+   * Test the allocation strategy for large buffers..
+   */
+  @Test
+  public void testLargeBufferAllocation() {
+    final long bufSize = 2048L;
+    try (RootAllocator allocator = new RootAllocator(bufSize);
+         ArrowBuf buffer = allocator.buffer(bufSize)) {
+      // make sure the buffer is large enough, so we wil use the allocation 
strategy for large buffers

Review comment:
       misspelled `wil`

##########
File path: 
java/memory/src/test/java/org/apache/arrow/memory/TestNettyAllocationManager.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.memory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import io.netty.buffer.ArrowBuf;
+
+/**
+ * Test cases for {@link NettyAllocationManager}.
+ */
+public class TestNettyAllocationManager {
+
+  private void readWriteArrowBuf(ArrowBuf buffer) {
+    // write buffer
+    for (long i = 0; i < buffer.capacity() / 8; i++) {
+      buffer.setLong(i * 8, i);
+    }
+
+    // read buffer
+    for (long i = 0; i < buffer.capacity() / 8; i++) {
+      long val = buffer.getLong(i * 8);
+      assertEquals(i, val);
+    }
+  }
+
+  /**
+   * Test the allocation strategy for small buffers..
+   */
+  @Test
+  public void testSmallBufferAllocation() {
+    final long bufSize = 512L;
+    try (RootAllocator allocator = new RootAllocator(bufSize);
+         ArrowBuf buffer = allocator.buffer(bufSize)) {
+      // make sure the buffer is small enough, so we wil use the allocation 
strategy for small buffers
+      assertTrue(bufSize < 
NettyAllocationManager.DEFAULT_ALLOCATION_CUTOFF_VALUE);
+
+      assertTrue(buffer.getReferenceManager() instanceof BufferLedger);
+      BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager();
+
+      // make sure we are using netty allocation manager
+      AllocationManager allocMgr = bufferLedger.getAllocationManager();
+      assertTrue(allocMgr instanceof NettyAllocationManager);
+      NettyAllocationManager nettyMgr = (NettyAllocationManager) allocMgr;
+
+      // for the small buffer allocation strategy, the chunk is not null
+      assertNotNull(nettyMgr.getMemoryChunk());
+
+      readWriteArrowBuf(buffer);
+    }
+  }
+
+  /**
+   * Test the allocation strategy for large buffers..
+   */
+  @Test
+  public void testLargeBufferAllocation() {
+    final long bufSize = 2048L;

Review comment:
       how about set this to 
`NettyAllocationManager.DEFAULT_ALLOCATION_CUTOFF_VALUE` + someval

##########
File path: 
java/memory/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java
##########
@@ -17,48 +17,97 @@
 
 package org.apache.arrow.memory;
 
-import org.apache.arrow.memory.util.LargeMemoryUtil;
-
 import io.netty.buffer.PooledByteBufAllocatorL;
 import io.netty.buffer.UnsafeDirectLittleEndian;
+import io.netty.util.internal.PlatformDependent;
 
 /**
- * The default implementation of AllocationManagerBase. The implementation is 
responsible for managing when memory
+ * The default implementation of {@link AllocationManager}. The implementation 
is responsible for managing when memory
  * is allocated and returned to the Netty-based PooledByteBufAllocatorL.
  */
 public class NettyAllocationManager extends AllocationManager {
 
   public static final Factory FACTORY = new Factory();
 
+  /**
+   * The default cut-off value for switching allocation strategies.
+   * If the request size is not greater than the cut-off value, we will 
allocate memory by
+   * {@link PooledByteBufAllocatorL} APIs,
+   * otherwise, we will use {@link PlatformDependent} APIs.
+   */
+  public static final long DEFAULT_ALLOCATION_CUTOFF_VALUE;
+
+  public static final String DEFAULT_ALLOCATION_CUTOFF_NAME = 
"default.allocation.cutoff.name";
+
+  static {
+    long cutOffValue;
+    try {
+      cutOffValue = 
Long.parseLong(System.getProperty(DEFAULT_ALLOCATION_CUTOFF_NAME));
+    } catch (Exception e) {
+      cutOffValue = Integer.MAX_VALUE;
+    }
+    DEFAULT_ALLOCATION_CUTOFF_VALUE = cutOffValue;
+  }
+

Review comment:
       none of this is necessary, please remove. you can create a custom 
`NettyAllocationManager` for tests and specify a different value there. See 
https://github.com/apache/arrow/blob/master/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java#L393

##########
File path: 
java/memory/src/test/java/org/apache/arrow/memory/TestLargeArrowBuf.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.memory;
+
+import static org.junit.Assert.assertEquals;
+
+import io.netty.buffer.ArrowBuf;
+
+/**
+ * Integration test for large (more than 2GB) {@link io.netty.buffer.ArrowBuf}.
+ * To run this test, please
+ *<li>Make sure there are 4GB memory available in the system.</li>
+ * <li>
+ *   Make sure the default allocation manager type is unsafe.
+ *   This can be achieved by the environmental variable or system property.
+ *   The details can be found in {@link DefaultAllocationManagerOption}.
+ * </li>
+ */
+public class TestLargeArrowBuf {
+
+  private static void testLargeArrowBuf() {
+    final long bufSize = 4 * 1024 * 1024 * 1024L;
+    try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
+         ArrowBuf largeBuf = allocator.buffer(bufSize)) {
+      assertEquals(bufSize, largeBuf.capacity());
+      System.out.println("Successfully allocated a buffer with capacity " + 
largeBuf.capacity());
+
+      for (long i = 0; i < bufSize / 8; i++) {
+        largeBuf.setLong(i * 8, i);
+
+        if ((i + 1) % 10000 == 0) {
+          System.out.println("Successfully written " + (i + 1) + " long 
words");
+        }
+      }
+      System.out.println("Successfully written " + (bufSize / 8) + " long 
words");
+
+      for (long i = 0; i < bufSize / 8; i++) {
+        long val = largeBuf.getLong(i * 8);
+        assertEquals(i, val);
+
+        if ((i + 1) % 10000 == 0) {
+          System.out.println("Successfully read " + (i + 1) + " long words");
+        }
+      }
+      System.out.println("Successfully read " + (bufSize / 8) + " long words");
+    }
+    System.out.println("Successfully released the large buffer.");
+  }
+
+  public static void main(String[] args) {

Review comment:
       So this is not run as part of the normal test suite correct? Does it 
need to run manually? if so could you put that in the javadoc




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to