From d79fd98ee7b3c67ccaf62ad71f60ebbe059286b8 Mon Sep 17 00:00:00 2001 From: Vanco Buca Date: Mon, 11 Jun 2018 11:06:07 -0700 Subject: [PATCH 1/6] Explicit listener added to newChildAllocator(), allowing each child to have a separate listener instance --- .../arrow/memory/AllocationListener.java | 4 +- .../apache/arrow/memory/BaseAllocator.java | 18 +++-- .../apache/arrow/memory/BufferAllocator.java | 11 ++++ .../apache/arrow/memory/ChildAllocator.java | 4 +- .../arrow/memory/TestBaseAllocator.java | 66 +++++++++++++++++++ 5 files changed, 96 insertions(+), 7 deletions(-) diff --git a/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java b/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java index d36cb37fc2e2..a355a896e90b 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java @@ -21,8 +21,8 @@ /** * An allocation listener being notified for allocation/deallocation *

- * It is expected to be called from multiple threads and as such, - * provider should take care of making the implementation thread-safe + * It might be called from multiple threads if the allocator hierarchy shares a listener, in which + * case, the provider should take care of making the implementation thread-safe. */ public interface AllocationListener { diff --git a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java index 4804b6b02a91..de6a4c0f9b52 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java @@ -72,7 +72,7 @@ protected BaseAllocator( this(parentAllocator.listener, parentAllocator, name, initReservation, maxAllocation); } - private BaseAllocator( + protected BaseAllocator( final AllocationListener listener, final BaseAllocator parentAllocator, final String name, @@ -333,22 +333,32 @@ public BufferAllocator newChildAllocator( final String name, final long initReservation, final long maxAllocation) { + return newChildAllocator(name, this.listener, initReservation, maxAllocation); + } + + @Override + public BufferAllocator newChildAllocator( + final String name, + final AllocationListener listener, + final long initReservation, + final long maxAllocation) { assertOpen(); - final ChildAllocator childAllocator = new ChildAllocator(this, name, initReservation, - maxAllocation); + final ChildAllocator childAllocator = new ChildAllocator(listener,this, name, initReservation, + maxAllocation); if (DEBUG) { synchronized (DEBUG_LOCK) { childAllocators.put(childAllocator, childAllocator); historicalLog.recordEvent("allocator[%s] created new child allocator[%s]", name, - childAllocator.name); + childAllocator.name); } } return childAllocator; } + @Override public AllocationReservation newReservation() { assertOpen(); diff --git a/java/memory/src/main/java/org/apache/arrow/memory/BufferAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/BufferAllocator.java index b23a6e4bd850..94ea62e6aad3 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/BufferAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/BufferAllocator.java @@ -68,6 +68,17 @@ public interface BufferAllocator extends AutoCloseable { */ public BufferAllocator newChildAllocator(String name, long initReservation, long maxAllocation); + /** + * Create a new child allocator. + * + * @param name the name of the allocator. + * @param listener allocation listener for the newly created child + * @param initReservation the initial space reservation (obtained from this allocator) + * @param maxAllocation maximum amount of space the new allocator can allocate + * @return the new allocator, or null if it can't be created + */ + public BufferAllocator newChildAllocator(String name, AllocationListener listener, long initReservation, long maxAllocation); + /** * Close and release all buffers generated from this buffer pool. * diff --git a/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java index f9a6dc72ece8..76d436cbd958 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java @@ -32,6 +32,7 @@ class ChildAllocator extends BaseAllocator { /** * Constructor. * + * @param listener Allocation listener to be used in this child * @param parentAllocator parent allocator -- the one creating this child * @param name the name of this child allocator * @param initReservation initial amount of space to reserve (obtained from the parent) @@ -41,11 +42,12 @@ class ChildAllocator extends BaseAllocator { * allocation policy in force, even less memory may be available */ ChildAllocator( + AllocationListener listener, BaseAllocator parentAllocator, String name, long initReservation, long maxAllocation) { - super(parentAllocator, name, initReservation, maxAllocation); + super(listener, parentAllocator, name, initReservation, maxAllocation); } diff --git a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index 76f2c501cf4c..a06086a6b79a 100644 --- a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -222,6 +222,72 @@ public void testRootAllocator_createChildDontClose() throws Exception { } } + // Allocation listener + // The only thing it does is it counts the number of times it has been invoked, and how much memory allocation it + // has seen + static class TestAllocationListener implements AllocationListener { + private int numCalls; + private long totalMem; + + TestAllocationListener() { + this.numCalls = 0; + this.totalMem = 0; + } + + @Override + public void onAllocation(long size) { + numCalls++; + totalMem += size; + } + + int getNumCalls() { + return numCalls; + } + + long getTotalMem() { + return totalMem; + } + } + + @Test + public void testRootAllocator_listeners() throws Exception { + TestAllocationListener l1 = new TestAllocationListener(); + assertEquals(0, l1.getNumCalls()); + assertEquals(0, l1.getTotalMem()); + TestAllocationListener l2 = new TestAllocationListener(); + assertEquals(0, l2.getNumCalls()); + assertEquals(0, l2.getTotalMem()); + // root and first-level child share the first listener + // second-level and third-level child share the second listener + try (final RootAllocator rootAllocator = new RootAllocator(l1, MAX_ALLOCATION)) { + try (final BufferAllocator c1 = rootAllocator.newChildAllocator("c1", 0, MAX_ALLOCATION)) { + final ArrowBuf buf1 = c1.buffer(16); + assertNotNull("allocation failed", buf1); + assertEquals(1, l1.getNumCalls()); + assertEquals(16, l1.getTotalMem()); + buf1.release(); + try (final BufferAllocator c2 = c1.newChildAllocator("c2", l2, 0, MAX_ALLOCATION)) { + final ArrowBuf buf2 = c2.buffer(32); + assertNotNull("allocation failed", buf2); + assertEquals(1, l1.getNumCalls()); + assertEquals(16, l1.getTotalMem()); + assertEquals(1, l2.getNumCalls()); + assertEquals(32, l2.getTotalMem()); + buf2.release(); + try (final BufferAllocator c3 = c2.newChildAllocator("c3", 0, MAX_ALLOCATION)) { + final ArrowBuf buf3 = c3.buffer(64); + assertNotNull("allocation failed", buf3); + assertEquals(1, l1.getNumCalls()); + assertEquals(16, l1.getTotalMem()); + assertEquals(2, l2.getNumCalls()); + assertEquals(32 + 64, l2.getTotalMem()); + buf3.release(); + } + } + } + } + } + private static void allocateAndFree(final BufferAllocator allocator) { final ArrowBuf arrowBuf = allocator.buffer(512); assertNotNull("allocation failed", arrowBuf); From e5188a89b347f7101a1c1cf0ed2ad0901d8978f6 Mon Sep 17 00:00:00 2001 From: Vanco Buca Date: Mon, 11 Jun 2018 11:22:15 -0700 Subject: [PATCH 2/6] AllocationListener.onFailedAlloc() method added, with default no-op implementation --- .../apache/arrow/memory/AllocationListener.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java b/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java index a355a896e90b..de181df247c8 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/AllocationListener.java @@ -30,6 +30,11 @@ public interface AllocationListener { @Override public void onAllocation(long size) { } + + @Override + public boolean onFailedAllocation(long size, Accountant.AllocationOutcome outcome) { + return false; + } }; /** @@ -39,4 +44,15 @@ public void onAllocation(long size) { */ void onAllocation(long size); + /** + * Called whenever an allocation failed, giving the caller a chance to create some space in the allocator + * (either by freeing some resource, or by changing the limit), and, if successful, allowing the allocator + * to retry the allocation. + * + * @param size the buffer size that was being allocated + * @param outcome the outcome of the failed allocation. Carries information of what failed + * @return true, if the allocation can be retried; false if the allocation should fail + */ + boolean onFailedAllocation(long size, Accountant.AllocationOutcome outcome); + } From 6a3fc8cff32ee01941d6e1a4fbe4f28f412184dc Mon Sep 17 00:00:00 2001 From: Vanco Buca Date: Mon, 11 Jun 2018 12:33:29 -0700 Subject: [PATCH 3/6] Call to onFailedAlloc() implemented --- .../apache/arrow/memory/BaseAllocator.java | 4 ++ .../arrow/memory/TestBaseAllocator.java | 55 ++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java index de6a4c0f9b52..a07929731d4f 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java @@ -275,6 +275,10 @@ public ArrowBuf buffer(final int initialRequestSize, BufferManager manager) { nextPowerOfTwo(initialRequestSize) : initialRequestSize; AllocationOutcome outcome = this.allocateBytes(actualRequestSize); + if (!outcome.isOk() && listener.onFailedAllocation(actualRequestSize, outcome)) { + // Second try, in case the listener can do something about it + outcome = this.allocateBytes(actualRequestSize); + } if (!outcome.isOk()) { throw new OutOfMemoryException(createErrorMsg(this, actualRequestSize, initialRequestSize)); } diff --git a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index a06086a6b79a..69378c7bd5bf 100644 --- a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -223,15 +223,21 @@ public void testRootAllocator_createChildDontClose() throws Exception { } // Allocation listener - // The only thing it does is it counts the number of times it has been invoked, and how much memory allocation it - // has seen + // It counts the number of times it has been invoked, and how much memory allocation it has seen + // When set to 'expand on fail', it attempts to expand the associated allocator's limit static class TestAllocationListener implements AllocationListener { private int numCalls; private long totalMem; + private boolean expandOnFail; + BufferAllocator expandAlloc; + long expandLimit; TestAllocationListener() { this.numCalls = 0; this.totalMem = 0; + this.expandOnFail = false; + this.expandAlloc = null; + this.expandLimit = 0; } @Override @@ -240,6 +246,21 @@ public void onAllocation(long size) { totalMem += size; } + @Override + public boolean onFailedAllocation(long size, Accountant.AllocationOutcome outcome) { + if (expandOnFail) { + expandAlloc.setLimit(expandLimit); + return true; + } + return false; + } + + void setExpandOnFail(BufferAllocator expandAlloc, long expandLimit) { + this.expandOnFail = true; + this.expandAlloc = expandAlloc; + this.expandLimit = expandLimit; + } + int getNumCalls() { return numCalls; } @@ -288,7 +309,35 @@ public void testRootAllocator_listeners() throws Exception { } } - private static void allocateAndFree(final BufferAllocator allocator) { + @Test + public void testRootAllocator_listenerAllocationFail() throws Exception { + TestAllocationListener l1 = new TestAllocationListener(); + assertEquals(0, l1.getNumCalls()); + assertEquals(0, l1.getTotalMem()); + // Test attempts to allocate too much from a child whose limit is set to half of the max allocation + // The listener's callback triggers, expanding the child allocator's limit, so then the allocation succeeds + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { + try (final BufferAllocator c1 = rootAllocator.newChildAllocator("c1", l1,0, MAX_ALLOCATION / 2)) { + try { + c1.buffer(MAX_ALLOCATION); + fail("allocated memory beyond max allowed"); + } catch (OutOfMemoryException e) { + // expected + } + assertEquals(0, l1.getNumCalls()); + assertEquals(0, l1.getTotalMem()); + + l1.setExpandOnFail(c1, MAX_ALLOCATION); + ArrowBuf arrowBuf = c1.buffer(MAX_ALLOCATION); + assertNotNull("allocation failed", arrowBuf); + assertEquals(1, l1.getNumCalls()); + assertEquals(MAX_ALLOCATION, l1.getTotalMem()); + arrowBuf.release(); + } + } + } + + private static void allocateAndFree(final BufferAllocator allocator) { final ArrowBuf arrowBuf = allocator.buffer(512); assertNotNull("allocation failed", arrowBuf); arrowBuf.release(); From fc97506958a4e7fd77e7acf96962f8a443ec1ba1 Mon Sep 17 00:00:00 2001 From: Vanco Buca Date: Tue, 12 Jun 2018 13:10:04 -0700 Subject: [PATCH 4/6] Review comments --- .../org/apache/arrow/memory/BaseAllocator.java | 17 +++++++++-------- .../apache/arrow/memory/TestBaseAllocator.java | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java index a07929731d4f..c94d95c444e8 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java @@ -275,12 +275,14 @@ public ArrowBuf buffer(final int initialRequestSize, BufferManager manager) { nextPowerOfTwo(initialRequestSize) : initialRequestSize; AllocationOutcome outcome = this.allocateBytes(actualRequestSize); - if (!outcome.isOk() && listener.onFailedAllocation(actualRequestSize, outcome)) { - // Second try, in case the listener can do something about it - outcome = this.allocateBytes(actualRequestSize); - } if (!outcome.isOk()) { - throw new OutOfMemoryException(createErrorMsg(this, actualRequestSize, initialRequestSize)); + if (listener.onFailedAllocation(actualRequestSize, outcome)) { + // Second try, in case the listener can do something about it + outcome = this.allocateBytes(actualRequestSize); + } + if (!outcome.isOk()) { + throw new OutOfMemoryException(createErrorMsg(this, actualRequestSize, initialRequestSize)); + } } boolean success = false; @@ -349,20 +351,19 @@ public BufferAllocator newChildAllocator( assertOpen(); final ChildAllocator childAllocator = new ChildAllocator(listener,this, name, initReservation, - maxAllocation); + maxAllocation); if (DEBUG) { synchronized (DEBUG_LOCK) { childAllocators.put(childAllocator, childAllocator); historicalLog.recordEvent("allocator[%s] created new child allocator[%s]", name, - childAllocator.name); + childAllocator.name); } } return childAllocator; } - @Override public AllocationReservation newReservation() { assertOpen(); diff --git a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index 69378c7bd5bf..62b0046b2ae9 100644 --- a/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/java/memory/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -225,7 +225,7 @@ public void testRootAllocator_createChildDontClose() throws Exception { // Allocation listener // It counts the number of times it has been invoked, and how much memory allocation it has seen // When set to 'expand on fail', it attempts to expand the associated allocator's limit - static class TestAllocationListener implements AllocationListener { + private static final class TestAllocationListener implements AllocationListener { private int numCalls; private long totalMem; private boolean expandOnFail; From ac0b81f4ae3172d8fcd231365a29d5ff999e6deb Mon Sep 17 00:00:00 2001 From: Vanco Buca Date: Wed, 13 Jun 2018 09:53:56 -0700 Subject: [PATCH 5/6] Review comment: missing space --- .../src/main/java/org/apache/arrow/memory/BaseAllocator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java index c94d95c444e8..525bc40a822a 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java @@ -350,7 +350,7 @@ public BufferAllocator newChildAllocator( final long maxAllocation) { assertOpen(); - final ChildAllocator childAllocator = new ChildAllocator(listener,this, name, initReservation, + final ChildAllocator childAllocator = new ChildAllocator(listener, this, name, initReservation, maxAllocation); if (DEBUG) { From 4f64f96bf7d64ab83aea6c46947d0c828304bae4 Mon Sep 17 00:00:00 2001 From: Vanco Buca Date: Wed, 13 Jun 2018 11:35:41 -0700 Subject: [PATCH 6/6] Consolidated BaseAllocator constructors --- .../apache/arrow/memory/BaseAllocator.java | 35 ++++++++----------- .../apache/arrow/memory/ChildAllocator.java | 2 +- .../apache/arrow/memory/RootAllocator.java | 2 +- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java index 525bc40a822a..6acbc1e12f9b 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/BaseAllocator.java @@ -56,28 +56,21 @@ public abstract class BaseAllocator extends Accountant implements BufferAllocato private final HistoricalLog historicalLog; private volatile boolean isClosed = false; // the allocator has been closed + /** + * Initialize an allocator + * @param parentAllocator parent allocator. null if defining a root allocator + * @param listener listener callback. Must be non-null -- use {@link AllocationListener#NOOP} if no listener + * desired + * @param name name of this allocator + * @param initReservation initial reservation. Cannot be modified after construction + * @param maxAllocation limit. Allocations past the limit fail. Can be modified after construction + */ protected BaseAllocator( - final AllocationListener listener, - final String name, - final long initReservation, - final long maxAllocation) throws OutOfMemoryException { - this(listener, null, name, initReservation, maxAllocation); - } - - protected BaseAllocator( - final BaseAllocator parentAllocator, - final String name, - final long initReservation, - final long maxAllocation) throws OutOfMemoryException { - this(parentAllocator.listener, parentAllocator, name, initReservation, maxAllocation); - } - - protected BaseAllocator( - final AllocationListener listener, - final BaseAllocator parentAllocator, - final String name, - final long initReservation, - final long maxAllocation) throws OutOfMemoryException { + final BaseAllocator parentAllocator, + final AllocationListener listener, + final String name, + final long initReservation, + final long maxAllocation) throws OutOfMemoryException { super(parentAllocator, initReservation, maxAllocation); this.listener = listener; diff --git a/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java index 76d436cbd958..03ec268d35cb 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/ChildAllocator.java @@ -47,7 +47,7 @@ class ChildAllocator extends BaseAllocator { String name, long initReservation, long maxAllocation) { - super(listener, parentAllocator, name, initReservation, maxAllocation); + super(parentAllocator, listener, name, initReservation, maxAllocation); } diff --git a/java/memory/src/main/java/org/apache/arrow/memory/RootAllocator.java b/java/memory/src/main/java/org/apache/arrow/memory/RootAllocator.java index 1dc6bf0c92fa..161b81a58b5b 100644 --- a/java/memory/src/main/java/org/apache/arrow/memory/RootAllocator.java +++ b/java/memory/src/main/java/org/apache/arrow/memory/RootAllocator.java @@ -31,7 +31,7 @@ public RootAllocator(final long limit) { } public RootAllocator(final AllocationListener listener, final long limit) { - super(listener, "ROOT", 0, limit); + super(null, listener, "ROOT", 0, limit); } /**