You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by si...@apache.org on 2018/09/07 21:16:57 UTC

[arrow] branch master updated: ARROW-3061: [JAVA] Fix BufferAllocator#getHeadroom (#2434)

This is an automated email from the ASF dual-hosted git repository.

siddteotia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new fbba3be  ARROW-3061: [JAVA] Fix BufferAllocator#getHeadroom (#2434)
fbba3be is described below

commit fbba3be707222a2d77f0aa54f40ac076066ec27f
Author: Laurent Goujon <la...@users.noreply.github.com>
AuthorDate: Fri Sep 7 14:16:54 2018 -0700

    ARROW-3061: [JAVA] Fix BufferAllocator#getHeadroom (#2434)
    
    Buffer allocator headroom is the minimum between the parent headroom
    and the memory available for the current allocator. But if the allocator
    also has some reserved memory (which has been accounted for in the parent
    allocator), the headroom is actually less than what is currently available.
    
    For example if parent allocator limit is 10, and child allocator has 2 of
    reserved memory, assuming that no other memory has been allocated, headroom
    for the child allocator is 10, not 8.
    
    Fix Accountant#getHeadroom to take into account reserved memory into its
    calculation.
---
 java/memory/src/main/java/org/apache/arrow/memory/Accountant.java   | 4 +++-
 .../src/test/java/org/apache/arrow/memory/TestAccountant.java       | 6 +++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/java/memory/src/main/java/org/apache/arrow/memory/Accountant.java b/java/memory/src/main/java/org/apache/arrow/memory/Accountant.java
index 7a2531e..e041e7e 100644
--- a/java/memory/src/main/java/org/apache/arrow/memory/Accountant.java
+++ b/java/memory/src/main/java/org/apache/arrow/memory/Accountant.java
@@ -255,7 +255,9 @@ class Accountant implements AutoCloseable {
       return localHeadroom;
     }
 
-    return Math.min(localHeadroom, parent.getHeadroom());
+    // Amount of reserved memory left on top of what parent has
+    long reservedHeadroom = Math.max(0, reservation - locallyHeldMemory.get());
+    return Math.min(localHeadroom, parent.getHeadroom() + reservedHeadroom);
   }
 
 }
diff --git a/java/memory/src/test/java/org/apache/arrow/memory/TestAccountant.java b/java/memory/src/test/java/org/apache/arrow/memory/TestAccountant.java
index a45de48..db5d858 100644
--- a/java/memory/src/test/java/org/apache/arrow/memory/TestAccountant.java
+++ b/java/memory/src/test/java/org/apache/arrow/memory/TestAccountant.java
@@ -80,7 +80,7 @@ public class TestAccountant {
 
     final Accountant child = new Accountant(parent, 2, Long.MAX_VALUE);
     assertEquals(2, parent.getAllocatedMemory());
-
+    assertEquals(10, child.getHeadroom());
     {
       AllocationOutcome first = child.allocateBytes(1);
       assertEquals(AllocationOutcome.SUCCESS, first);
@@ -139,7 +139,7 @@ public class TestAccountant {
     child.releaseBytes(9);
 
     assertEquals(1, child.getAllocatedMemory());
-    assertEquals(8, child.getHeadroom());
+    assertEquals(9, child.getHeadroom());
 
     // back to reservation size
     assertEquals(2, parent.getAllocatedMemory());
@@ -164,7 +164,7 @@ public class TestAccountant {
     child.releaseBytes(11);
     assertEquals(child.getAllocatedMemory(), 0);
     assertEquals(parent.getAllocatedMemory(), 2);
-    assertEquals(8, child.getHeadroom());
+    assertEquals(10, child.getHeadroom());
     assertEquals(8, parent.getHeadroom());
 
     child.close();