From 97f0da17117ad2ea5f8cd9e2d4d891b6678f4c34 Mon Sep 17 00:00:00 2001 From: James Duong Date: Fri, 24 Nov 2023 06:30:46 -0800 Subject: [PATCH] Refactor memory modules for JPMS support Based on #13072 - Avoid having multiple memory modules contribute to the same package - Introduce memory-netty-buffer-patch module for patching classes into Netty modules - Patch classes into netty's io.netty.buffer package (specifically use the JDK9 compiler to do this instead of inheriting JDK8). - Avoid using BaseAllocator in tests and use RootAllocator instead - Move TestBaseAllocator#testMemoryUsage() to a new test in memory-netty TestNettyAllocator because TestBaseAllocator is now in memory-core, but that specific test has Netty dependencies. --- .../apache/arrow/memory/CheckAllocator.java | 39 ++++- .../DefaultAllocationManagerOption.java | 13 +- .../memory/CountingAllocationListener.java | 4 + .../arrow/memory/TestBaseAllocator.java | 134 ++++++------------ .../arrow/memory/TestForeignAllocation.java | 7 + java/memory/memory-netty-buffer-patch/pom.xml | 44 ++++++ .../io/netty/buffer/ExpandableByteBuf.java | 0 .../java/io/netty/buffer/LargeBuffer.java | 0 .../netty/buffer/MutableWrappedByteBuf.java | 0 .../java/io/netty/buffer/NettyArrowBuf.java | 16 ++- .../netty/buffer/PooledByteBufAllocatorL.java | 7 +- .../buffer/UnsafeDirectLittleEndian.java | 0 .../memory/patch}/ArrowByteBufAllocator.java | 4 +- .../buffer/TestUnsafeDirectLittleEndian.java | 5 + java/memory/memory-netty/pom.xml | 23 +++ .../DefaultAllocationManagerFactory.java | 6 +- .../{ => netty}/NettyAllocationManager.java | 7 +- .../netty/buffer/TestExpandableByteBuf.java | 4 + .../io/netty/buffer/TestNettyArrowBuf.java | 5 +- .../{ => netty}/ITTestLargeArrowBuf.java | 5 +- .../TestAllocationManagerNetty.java | 4 +- .../memory/{ => netty}/TestEmptyArrowBuf.java | 9 +- .../memory/{ => netty}/TestEndianness.java | 4 +- .../TestNettyAllocationManager.java | 15 +- .../memory/netty/TestNettyAllocator.java | 75 ++++++++++ .../DefaultAllocationManagerFactory.java | 6 +- .../{ => unsafe}/UnsafeAllocationManager.java | 6 +- .../TestAllocationManagerUnsafe.java | 4 +- .../TestUnsafeAllocationManager.java | 13 +- java/memory/pom.xml | 1 + 30 files changed, 330 insertions(+), 130 deletions(-) rename java/memory/{memory-netty => memory-core}/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java (95%) rename java/memory/{memory-netty => memory-core}/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java (91%) rename java/memory/{memory-netty => memory-core}/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java (93%) create mode 100644 java/memory/memory-netty-buffer-patch/pom.xml rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/main/java/io/netty/buffer/ExpandableByteBuf.java (100%) rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/main/java/io/netty/buffer/LargeBuffer.java (100%) rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java (100%) rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/main/java/io/netty/buffer/NettyArrowBuf.java (96%) rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java (97%) rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java (100%) rename java/memory/{memory-netty/src/main/java/org/apache/arrow/memory => memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch}/ArrowByteBufAllocator.java (97%) rename java/memory/{memory-netty => memory-netty-buffer-patch}/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java (95%) rename java/memory/memory-netty/src/main/java/org/apache/arrow/memory/{ => netty}/DefaultAllocationManagerFactory.java (87%) rename java/memory/memory-netty/src/main/java/org/apache/arrow/memory/{ => netty}/NettyAllocationManager.java (94%) rename java/memory/memory-netty/src/test/java/org/apache/arrow/memory/{ => netty}/ITTestLargeArrowBuf.java (93%) rename java/memory/memory-netty/src/test/java/org/apache/arrow/memory/{ => netty}/TestAllocationManagerNetty.java (90%) rename java/memory/memory-netty/src/test/java/org/apache/arrow/memory/{ => netty}/TestEmptyArrowBuf.java (90%) rename java/memory/memory-netty/src/test/java/org/apache/arrow/memory/{ => netty}/TestEndianness.java (92%) rename java/memory/memory-netty/src/test/java/org/apache/arrow/memory/{ => netty}/TestNettyAllocationManager.java (87%) create mode 100644 java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java rename java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/{ => unsafe}/DefaultAllocationManagerFactory.java (87%) rename java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/{ => unsafe}/UnsafeAllocationManager.java (89%) rename java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/{ => unsafe}/TestAllocationManagerUnsafe.java (90%) rename java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/{ => unsafe}/TestUnsafeAllocationManager.java (82%) diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java index 79b825aa2e898..dac4a3fcff59a 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java @@ -31,20 +31,35 @@ */ final class CheckAllocator { private static final Logger logger = LoggerFactory.getLogger(CheckAllocator.class); - private static final String ALLOCATOR_PATH = "org/apache/arrow/memory/DefaultAllocationManagerFactory.class"; + // unique package names needed by JPMS module naming + private static final String ALLOCATOR_PATH_CORE = + "org/apache/arrow/memory/DefaultAllocationManagerFactory.class"; + private static final String ALLOCATOR_PATH_UNSAFE = + "org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.class"; + private static final String ALLOCATOR_PATH_NETTY = + "org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.class"; private CheckAllocator() { - } static String check() { Set urls = scanClasspath(); URL rootAllocator = assertOnlyOne(urls); reportResult(rootAllocator); - return "org.apache.arrow.memory.DefaultAllocationManagerFactory"; + if (rootAllocator.getPath().contains("memory-core") || + rootAllocator.getPath().contains("/org/apache/arrow/memory/core/")) { + return "org.apache.arrow.memory.DefaultAllocationManagerFactory"; + } else if (rootAllocator.getPath().contains("memory-unsafe") || + rootAllocator.getPath().contains("/org/apache/arrow/memory/unsafe/")) { + return "org.apache.arrow.memory.unsafe.DefaultAllocationManagerFactory"; + } else if (rootAllocator.getPath().contains("memory-netty") || + rootAllocator.getPath().contains("/org/apache/arrow/memory/netty/")) { + return "org.apache.arrow.memory.netty.DefaultAllocationManagerFactory"; + } else { + throw new IllegalStateException("Unknown allocation manager type to infer. Current: " + rootAllocator.getPath()); + } } - private static Set scanClasspath() { // LinkedHashSet appropriate here because it preserves insertion order // during iteration @@ -53,9 +68,21 @@ private static Set scanClasspath() { ClassLoader allocatorClassLoader = CheckAllocator.class.getClassLoader(); Enumeration paths; if (allocatorClassLoader == null) { - paths = ClassLoader.getSystemResources(ALLOCATOR_PATH); + paths = ClassLoader.getSystemResources(ALLOCATOR_PATH_CORE); + if (!paths.hasMoreElements()) { + paths = ClassLoader.getSystemResources(ALLOCATOR_PATH_UNSAFE); + } + if (!paths.hasMoreElements()) { + paths = ClassLoader.getSystemResources(ALLOCATOR_PATH_NETTY); + } } else { - paths = allocatorClassLoader.getResources(ALLOCATOR_PATH); + paths = allocatorClassLoader.getResources(ALLOCATOR_PATH_CORE); + if (!paths.hasMoreElements()) { + paths = allocatorClassLoader.getResources(ALLOCATOR_PATH_UNSAFE); + } + if (!paths.hasMoreElements()) { + paths = allocatorClassLoader.getResources(ALLOCATOR_PATH_NETTY); + } } while (paths.hasMoreElements()) { URL path = paths.nextElement(); diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java index 15120c252fca3..564d59994b7cd 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java @@ -19,6 +19,8 @@ import java.lang.reflect.Field; +import org.apache.arrow.util.VisibleForTesting; + /** * A class for choosing the default allocation manager. */ @@ -61,7 +63,12 @@ public enum AllocationManagerType { Unknown, } - static AllocationManagerType getDefaultAllocationManagerType() { + /** + * Returns the default allocation manager type. + * @return the default allocation manager type. + */ + @VisibleForTesting + public static AllocationManagerType getDefaultAllocationManagerType() { AllocationManagerType ret = AllocationManagerType.Unknown; try { @@ -115,7 +122,7 @@ private static AllocationManager.Factory getFactory(String clazzName) { private static AllocationManager.Factory getUnsafeFactory() { try { - return getFactory("org.apache.arrow.memory.UnsafeAllocationManager"); + return getFactory("org.apache.arrow.memory.unsafe.UnsafeAllocationManager"); } catch (RuntimeException e) { throw new RuntimeException("Please add arrow-memory-unsafe to your classpath," + " No DefaultAllocationManager found to instantiate an UnsafeAllocationManager", e); @@ -124,7 +131,7 @@ private static AllocationManager.Factory getUnsafeFactory() { private static AllocationManager.Factory getNettyFactory() { try { - return getFactory("org.apache.arrow.memory.NettyAllocationManager"); + return getFactory("org.apache.arrow.memory.netty.NettyAllocationManager"); } catch (RuntimeException e) { throw new RuntimeException("Please add arrow-memory-netty to your classpath," + " No DefaultAllocationManager found to instantiate an NettyAllocationManager", e); diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java similarity index 95% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java rename to java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java index 78c78c8ad8cba..f1dd7e92c5cb3 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java @@ -17,6 +17,10 @@ package org.apache.arrow.memory; +import org.apache.arrow.memory.AllocationListener; +import org.apache.arrow.memory.AllocationOutcome; +import org.apache.arrow.memory.BufferAllocator; + /** * Counting allocation listener. * It counts the number of times it has been invoked, and how much memory allocation it has seen diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java similarity index 91% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java rename to java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index 7613d073f8c0d..365c84f5a2b70 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -31,22 +31,16 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.stream.Collectors; import org.apache.arrow.memory.AllocationOutcomeDetails.Entry; import org.apache.arrow.memory.rounding.RoundingPolicy; import org.apache.arrow.memory.rounding.SegmentRoundingPolicy; import org.apache.arrow.memory.util.AssertionUtil; +import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.slf4j.LoggerFactory; -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.read.ListAppender; -import io.netty.buffer.PooledByteBufAllocatorL; import sun.misc.Unsafe; public class TestBaseAllocator { @@ -448,73 +442,73 @@ public ArrowBuf empty() { @Test public void testRootAllocator_listeners() throws Exception { CountingAllocationListener l1 = new CountingAllocationListener(); - assertEquals(0, l1.getNumPreCalls()); - assertEquals(0, l1.getNumCalls()); - assertEquals(0, l1.getNumReleaseCalls()); - assertEquals(0, l1.getNumChildren()); - assertEquals(0, l1.getTotalMem()); + Assert.assertEquals(0, l1.getNumPreCalls()); + Assert.assertEquals(0, l1.getNumCalls()); + Assert.assertEquals(0, l1.getNumReleaseCalls()); + Assert.assertEquals(0, l1.getNumChildren()); + Assert.assertEquals(0, l1.getTotalMem()); CountingAllocationListener l2 = new CountingAllocationListener(); - assertEquals(0, l2.getNumPreCalls()); - assertEquals(0, l2.getNumCalls()); - assertEquals(0, l2.getNumReleaseCalls()); - assertEquals(0, l2.getNumChildren()); - assertEquals(0, l2.getTotalMem()); + Assert.assertEquals(0, l2.getNumPreCalls()); + Assert.assertEquals(0, l2.getNumCalls()); + Assert.assertEquals(0, l2.getNumReleaseCalls()); + Assert.assertEquals(0, l2.getNumChildren()); + Assert.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)) { - assertEquals(1, l1.getNumChildren()); + Assert.assertEquals(1, l1.getNumChildren()); final ArrowBuf buf1 = c1.buffer(16); assertNotNull("allocation failed", buf1); - assertEquals(1, l1.getNumPreCalls()); - assertEquals(1, l1.getNumCalls()); - assertEquals(0, l1.getNumReleaseCalls()); - assertEquals(16, l1.getTotalMem()); + Assert.assertEquals(1, l1.getNumPreCalls()); + Assert.assertEquals(1, l1.getNumCalls()); + Assert.assertEquals(0, l1.getNumReleaseCalls()); + Assert.assertEquals(16, l1.getTotalMem()); buf1.getReferenceManager().release(); try (final BufferAllocator c2 = c1.newChildAllocator("c2", l2, 0, MAX_ALLOCATION)) { - assertEquals(2, l1.getNumChildren()); // c1 got a new child, so c1's listener (l1) is notified - assertEquals(0, l2.getNumChildren()); + Assert.assertEquals(2, l1.getNumChildren()); // c1 got a new child, so c1's listener (l1) is notified + Assert.assertEquals(0, l2.getNumChildren()); final ArrowBuf buf2 = c2.buffer(32); assertNotNull("allocation failed", buf2); - assertEquals(1, l1.getNumCalls()); - assertEquals(16, l1.getTotalMem()); - assertEquals(1, l2.getNumPreCalls()); - assertEquals(1, l2.getNumCalls()); - assertEquals(0, l2.getNumReleaseCalls()); - assertEquals(32, l2.getTotalMem()); + Assert.assertEquals(1, l1.getNumCalls()); + Assert.assertEquals(16, l1.getTotalMem()); + Assert.assertEquals(1, l2.getNumPreCalls()); + Assert.assertEquals(1, l2.getNumCalls()); + Assert.assertEquals(0, l2.getNumReleaseCalls()); + Assert.assertEquals(32, l2.getTotalMem()); buf2.getReferenceManager().release(); try (final BufferAllocator c3 = c2.newChildAllocator("c3", 0, MAX_ALLOCATION)) { - assertEquals(2, l1.getNumChildren()); - assertEquals(1, l2.getNumChildren()); + Assert.assertEquals(2, l1.getNumChildren()); + Assert.assertEquals(1, l2.getNumChildren()); final ArrowBuf buf3 = c3.buffer(64); assertNotNull("allocation failed", buf3); - assertEquals(1, l1.getNumPreCalls()); - assertEquals(1, l1.getNumCalls()); - assertEquals(1, l1.getNumReleaseCalls()); - assertEquals(16, l1.getTotalMem()); - assertEquals(2, l2.getNumPreCalls()); - assertEquals(2, l2.getNumCalls()); - assertEquals(1, l2.getNumReleaseCalls()); - assertEquals(32 + 64, l2.getTotalMem()); + Assert.assertEquals(1, l1.getNumPreCalls()); + Assert.assertEquals(1, l1.getNumCalls()); + Assert.assertEquals(1, l1.getNumReleaseCalls()); + Assert.assertEquals(16, l1.getTotalMem()); + Assert.assertEquals(2, l2.getNumPreCalls()); + Assert.assertEquals(2, l2.getNumCalls()); + Assert.assertEquals(1, l2.getNumReleaseCalls()); + Assert.assertEquals(32 + 64, l2.getTotalMem()); buf3.getReferenceManager().release(); } - assertEquals(2, l1.getNumChildren()); - assertEquals(0, l2.getNumChildren()); // third-level child removed + Assert.assertEquals(2, l1.getNumChildren()); + Assert.assertEquals(0, l2.getNumChildren()); // third-level child removed } - assertEquals(1, l1.getNumChildren()); // second-level child removed - assertEquals(0, l2.getNumChildren()); + Assert.assertEquals(1, l1.getNumChildren()); // second-level child removed + Assert.assertEquals(0, l2.getNumChildren()); } - assertEquals(0, l1.getNumChildren()); // first-level child removed + Assert.assertEquals(0, l1.getNumChildren()); // first-level child removed - assertEquals(2, l2.getNumReleaseCalls()); + Assert.assertEquals(2, l2.getNumReleaseCalls()); } } @Test public void testRootAllocator_listenerAllocationFail() throws Exception { CountingAllocationListener l1 = new CountingAllocationListener(); - assertEquals(0, l1.getNumCalls()); - assertEquals(0, l1.getTotalMem()); + Assert.assertEquals(0, l1.getNumCalls()); + Assert.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. @@ -527,14 +521,14 @@ public void testRootAllocator_listenerAllocationFail() throws Exception { } catch (OutOfMemoryException e) { // expected } - assertEquals(0, l1.getNumCalls()); - assertEquals(0, l1.getTotalMem()); + Assert.assertEquals(0, l1.getNumCalls()); + Assert.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()); + Assert.assertEquals(1, l1.getNumCalls()); + Assert.assertEquals(MAX_ALLOCATION, l1.getTotalMem()); arrowBuf.getReferenceManager().release(); } } @@ -1098,42 +1092,6 @@ public void testMemoryLeakWithReservation() throws Exception { } } - @Test - public void testMemoryUsage() { - ListAppender memoryLogsAppender = new ListAppender<>(); - Logger logger = (Logger) LoggerFactory.getLogger("arrow.allocator"); - try { - logger.setLevel(Level.TRACE); - logger.addAppender(memoryLogsAppender); - memoryLogsAppender.start(); - try (ArrowBuf buf = new ArrowBuf(ReferenceManager.NO_OP, null, - 1024, new PooledByteBufAllocatorL().empty.memoryAddress())) { - buf.memoryAddress(); - } - boolean result = false; - long startTime = System.currentTimeMillis(); - while ((System.currentTimeMillis() - startTime) < 10000) { // 10 seconds maximum for time to read logs - result = memoryLogsAppender.list.stream() - .anyMatch( - log -> log.toString().contains("Memory Usage: \n") && - log.toString().contains("Large buffers outstanding: ") && - log.toString().contains("Normal buffers outstanding: ") && - log.getLevel().equals(Level.TRACE) - ); - if (result) { - break; - } - } - assertTrue("Log messages are:\n" + - memoryLogsAppender.list.stream().map(ILoggingEvent::toString).collect(Collectors.joining("\n")), - result); - } finally { - memoryLogsAppender.stop(); - logger.detachAppender(memoryLogsAppender); - logger.setLevel(null); - } - } - @Test public void testOverlimit() { try (BufferAllocator allocator = new RootAllocator(1024)) { diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java similarity index 93% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java rename to java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java index ec049ca6923bc..46e94fad37b05 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java @@ -23,6 +23,13 @@ import java.util.ArrayList; import java.util.List; +import org.apache.arrow.memory.AllocationListener; +import org.apache.arrow.memory.AllocationOutcome; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.ForeignAllocation; +import org.apache.arrow.memory.OutOfMemoryException; +import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.util.MemoryUtil; import org.junit.After; import org.junit.Before; diff --git a/java/memory/memory-netty-buffer-patch/pom.xml b/java/memory/memory-netty-buffer-patch/pom.xml new file mode 100644 index 0000000000000..1d4407c638d8a --- /dev/null +++ b/java/memory/memory-netty-buffer-patch/pom.xml @@ -0,0 +1,44 @@ + + + + + arrow-memory + org.apache.arrow + 15.0.0-SNAPSHOT + + 4.0.0 + + arrow-memory-netty-buffer-patch + Arrow Memory - Netty Buffer + Netty Buffer needed to patch that is consumed by Arrow Memory Netty + + + + org.apache.arrow + arrow-memory-core + + + io.netty + netty-buffer + + + io.netty + netty-common + + + org.slf4j + slf4j-api + + + diff --git a/java/memory/memory-netty/src/main/java/io/netty/buffer/ExpandableByteBuf.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/ExpandableByteBuf.java similarity index 100% rename from java/memory/memory-netty/src/main/java/io/netty/buffer/ExpandableByteBuf.java rename to java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/ExpandableByteBuf.java diff --git a/java/memory/memory-netty/src/main/java/io/netty/buffer/LargeBuffer.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/LargeBuffer.java similarity index 100% rename from java/memory/memory-netty/src/main/java/io/netty/buffer/LargeBuffer.java rename to java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/LargeBuffer.java diff --git a/java/memory/memory-netty/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java similarity index 100% rename from java/memory/memory-netty/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java rename to java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java diff --git a/java/memory/memory-netty/src/main/java/io/netty/buffer/NettyArrowBuf.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java similarity index 96% rename from java/memory/memory-netty/src/main/java/io/netty/buffer/NettyArrowBuf.java rename to java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java index 71e4b7cb6d522..466444c7d53e8 100644 --- a/java/memory/memory-netty/src/main/java/io/netty/buffer/NettyArrowBuf.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java @@ -17,8 +17,6 @@ package io.netty.buffer; -import static org.apache.arrow.memory.util.LargeMemoryUtil.checkedCastToInt; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -29,10 +27,12 @@ import java.nio.channels.ScatteringByteChannel; import org.apache.arrow.memory.ArrowBuf; -import org.apache.arrow.memory.ArrowByteBufAllocator; import org.apache.arrow.memory.BoundsChecking; import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.patch.ArrowByteBufAllocator; +import org.apache.arrow.memory.util.LargeMemoryUtil; import org.apache.arrow.util.Preconditions; +import org.apache.arrow.util.VisibleForTesting; import io.netty.util.internal.PlatformDependent; @@ -264,7 +264,7 @@ public ByteBuffer nioBuffer(long index, int length) { * @return ByteBuffer */ private ByteBuffer getDirectBuffer(long index) { - return PlatformDependent.directBuffer(addr(index), checkedCastToInt(length - index)); + return PlatformDependent.directBuffer(addr(index), LargeMemoryUtil.checkedCastToInt(length - index)); } @Override @@ -580,11 +580,13 @@ public NettyArrowBuf setMedium(int index, int value) { } @Override + @VisibleForTesting protected void _setInt(int index, int value) { setInt(index, value); } @Override + @VisibleForTesting protected void _setIntLE(int index, int value) { this.chk(index, 4); PlatformDependent.putInt(this.addr(index), Integer.reverseBytes(value)); @@ -620,9 +622,9 @@ public static NettyArrowBuf unwrapBuffer(ArrowBuf buf) { final NettyArrowBuf nettyArrowBuf = new NettyArrowBuf( buf, buf.getReferenceManager().getAllocator(), - checkedCastToInt(buf.capacity())); - nettyArrowBuf.readerIndex(checkedCastToInt(buf.readerIndex())); - nettyArrowBuf.writerIndex(checkedCastToInt(buf.writerIndex())); + LargeMemoryUtil.checkedCastToInt(buf.capacity())); + nettyArrowBuf.readerIndex(LargeMemoryUtil.checkedCastToInt(buf.readerIndex())); + nettyArrowBuf.writerIndex(LargeMemoryUtil.checkedCastToInt(buf.writerIndex())); return nettyArrowBuf; } diff --git a/java/memory/memory-netty/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java similarity index 97% rename from java/memory/memory-netty/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java rename to java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java index 06c6669cfd162..bc95b9df545a3 100644 --- a/java/memory/memory-netty/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java @@ -17,13 +17,12 @@ package io.netty.buffer; -import static org.apache.arrow.memory.util.AssertionUtil.ASSERT_ENABLED; - import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicLong; import org.apache.arrow.memory.OutOfMemoryException; +import org.apache.arrow.memory.util.AssertionUtil; import org.apache.arrow.memory.util.LargeMemoryUtil; import io.netty.util.internal.OutOfDirectMemoryError; @@ -51,7 +50,7 @@ public PooledByteBufAllocatorL() { } /** - * Returns a {@linkplain io.netty.buffer.UnsafeDirectLittleEndian} of the given size. + * Returns a {@linkplain UnsafeDirectLittleEndian} of the given size. */ public UnsafeDirectLittleEndian allocate(long size) { try { @@ -183,7 +182,7 @@ private UnsafeDirectLittleEndian newDirectBufferL(int initialCapacity, int maxCa fail(); } - if (!ASSERT_ENABLED) { + if (!AssertionUtil.ASSERT_ENABLED) { return new UnsafeDirectLittleEndian((PooledUnsafeDirectByteBuf) buf); } diff --git a/java/memory/memory-netty/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java similarity index 100% rename from java/memory/memory-netty/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java rename to java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java diff --git a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/ArrowByteBufAllocator.java b/java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java similarity index 97% rename from java/memory/memory-netty/src/main/java/org/apache/arrow/memory/ArrowByteBufAllocator.java rename to java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java index ff40b49ff6f5c..6ce08b5a5902b 100644 --- a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/ArrowByteBufAllocator.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java @@ -15,7 +15,9 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.patch; + +import org.apache.arrow.memory.BufferAllocator; import io.netty.buffer.AbstractByteBufAllocator; import io.netty.buffer.ByteBuf; diff --git a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java b/java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java similarity index 95% rename from java/memory/memory-netty/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java rename to java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java index c2bd95bb3d939..043c2c1605a63 100644 --- a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java +++ b/java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java @@ -28,6 +28,11 @@ import org.junit.Test; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.LargeBuffer; +import io.netty.buffer.Unpooled; +import io.netty.buffer.UnsafeDirectLittleEndian; + public class TestUnsafeDirectLittleEndian { private static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; diff --git a/java/memory/memory-netty/pom.xml b/java/memory/memory-netty/pom.xml index 307f6ad81a0f1..06b5e97fc2e42 100644 --- a/java/memory/memory-netty/pom.xml +++ b/java/memory/memory-netty/pom.xml @@ -26,9 +26,15 @@ org.apache.arrow arrow-memory-core + + org.apache.arrow + arrow-memory-netty-buffer-patch + ${project.version} + io.netty netty-buffer + provided io.netty @@ -37,6 +43,7 @@ org.slf4j slf4j-api + test ch.qos.logback @@ -74,4 +81,20 @@ + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + 9 + + --patch-module=io.netty.buffer=${project.basedir}/../memory-netty-buffer-patch/target/arrow-memory-netty-buffer-patch-${project.version}.jar + + + + + diff --git a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java similarity index 87% rename from java/memory/memory-netty/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java rename to java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java index 10cfb5c164855..8ece77178f09f 100644 --- a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java +++ b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java @@ -15,7 +15,11 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; + +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; /** * The default Allocation Manager Factory for a module. diff --git a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java similarity index 94% rename from java/memory/memory-netty/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java rename to java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java index 2000477830735..58354d0c2eebd 100644 --- a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/NettyAllocationManager.java +++ b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java @@ -15,7 +15,12 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; + +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.ReferenceManager; import io.netty.buffer.PooledByteBufAllocatorL; import io.netty.buffer.UnsafeDirectLittleEndian; diff --git a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java index b39cca8e8e7ee..67557b65a6267 100644 --- a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java +++ b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java @@ -23,6 +23,10 @@ import org.junit.Assert; import org.junit.Test; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ExpandableByteBuf; +import io.netty.buffer.NettyArrowBuf; + public class TestExpandableByteBuf { @Test diff --git a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java index 45d3b41e8a652..f18bccb4c9d8f 100644 --- a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java +++ b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java @@ -20,12 +20,15 @@ import java.nio.ByteBuffer; import org.apache.arrow.memory.ArrowBuf; -import org.apache.arrow.memory.ArrowByteBufAllocator; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.memory.patch.ArrowByteBufAllocator; import org.junit.Assert; import org.junit.Test; +import io.netty.buffer.CompositeByteBuf; +import io.netty.buffer.NettyArrowBuf; + public class TestNettyArrowBuf { @Test diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/ITTestLargeArrowBuf.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java similarity index 93% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/ITTestLargeArrowBuf.java rename to java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java index fa8d510e3616b..71dba73d2896e 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/ITTestLargeArrowBuf.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java @@ -15,10 +15,13 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; import static org.junit.Assert.assertEquals; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestAllocationManagerNetty.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java similarity index 90% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestAllocationManagerNetty.java rename to java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java index 2dbd56480b8ef..7f1e34ddc5fc2 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestAllocationManagerNetty.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java @@ -15,10 +15,12 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; import static org.junit.Assert.assertEquals; +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.DefaultAllocationManagerOption; import org.junit.Test; /** diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestEmptyArrowBuf.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java similarity index 90% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestEmptyArrowBuf.java rename to java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java index 3fd7ce74aab9d..b9948083e6f82 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestEmptyArrowBuf.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java @@ -15,10 +15,13 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; import static org.junit.Assert.assertEquals; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.ReferenceManager; +import org.apache.arrow.memory.RootAllocator; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -47,8 +50,8 @@ public static void afterClass() { public void testZeroBuf() { // Exercise the historical log inside the empty ArrowBuf. This is initialized statically, and there is a circular // dependency between ArrowBuf and BaseAllocator, so if the initialization happens in the wrong order, the - // historical log will be null even though BaseAllocator.DEBUG is true. - allocator.getEmpty().print(new StringBuilder(), 0, BaseAllocator.Verbosity.LOG_WITH_STACKTRACE); + // historical log will be null even though RootAllocator.DEBUG is true. + allocator.getEmpty().print(new StringBuilder(), 0, RootAllocator.Verbosity.LOG_WITH_STACKTRACE); } @Test diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestEndianness.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java similarity index 92% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestEndianness.java rename to java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java index dcaeb24889e0a..a782523cbc6d6 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestEndianness.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java @@ -15,12 +15,14 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; import static org.junit.Assert.assertEquals; import java.nio.ByteOrder; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; import org.junit.Test; import io.netty.buffer.ByteBuf; diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestNettyAllocationManager.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java similarity index 87% rename from java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestNettyAllocationManager.java rename to java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java index 1b64cd73363cf..39692c96ceb3d 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/TestNettyAllocationManager.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java @@ -15,13 +15,18 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.netty; 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.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.BufferLedger; +import org.apache.arrow.memory.RootAllocator; import org.junit.Test; /** @@ -31,8 +36,8 @@ public class TestNettyAllocationManager { static int CUSTOMIZED_ALLOCATION_CUTOFF_VALUE = 1024; - private BaseAllocator createCustomizedAllocator() { - return new RootAllocator(BaseAllocator.configBuilder() + private RootAllocator createCustomizedAllocator() { + return new RootAllocator(RootAllocator.configBuilder() .allocationManagerFactory(new AllocationManager.Factory() { @Override public AllocationManager create(BufferAllocator accountingAllocator, long size) { @@ -65,7 +70,7 @@ private void readWriteArrowBuf(ArrowBuf buffer) { @Test public void testSmallBufferAllocation() { final long bufSize = CUSTOMIZED_ALLOCATION_CUTOFF_VALUE - 512L; - try (BaseAllocator allocator = createCustomizedAllocator(); + try (RootAllocator allocator = createCustomizedAllocator(); ArrowBuf buffer = allocator.buffer(bufSize)) { assertTrue(buffer.getReferenceManager() instanceof BufferLedger); @@ -89,7 +94,7 @@ public void testSmallBufferAllocation() { @Test public void testLargeBufferAllocation() { final long bufSize = CUSTOMIZED_ALLOCATION_CUTOFF_VALUE + 1024L; - try (BaseAllocator allocator = createCustomizedAllocator(); + try (RootAllocator allocator = createCustomizedAllocator(); ArrowBuf buffer = allocator.buffer(bufSize)) { assertTrue(buffer.getReferenceManager() instanceof BufferLedger); BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java new file mode 100644 index 0000000000000..b9525d0fe9d31 --- /dev/null +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java @@ -0,0 +1,75 @@ +/* + * 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.netty; + +import static org.junit.Assert.assertTrue; + +import java.util.stream.Collectors; + +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.ReferenceManager; +import org.junit.Test; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import io.netty.buffer.PooledByteBufAllocatorL; + +/** + * Test netty allocators. + */ +public class TestNettyAllocator { + + @Test + public void testMemoryUsage() { + ListAppender memoryLogsAppender = new ListAppender<>(); + Logger logger = (Logger) LoggerFactory.getLogger("arrow.allocator"); + try { + logger.setLevel(Level.TRACE); + logger.addAppender(memoryLogsAppender); + memoryLogsAppender.start(); + try (ArrowBuf buf = new ArrowBuf(ReferenceManager.NO_OP, null, + 1024, new PooledByteBufAllocatorL().empty.memoryAddress())) { + buf.memoryAddress(); + } + boolean result = false; + long startTime = System.currentTimeMillis(); + while ((System.currentTimeMillis() - startTime) < 10000) { // 10 seconds maximum for time to read logs + result = memoryLogsAppender.list.stream() + .anyMatch( + log -> log.toString().contains("Memory Usage: \n") && + log.toString().contains("Large buffers outstanding: ") && + log.toString().contains("Normal buffers outstanding: ") && + log.getLevel().equals(Level.TRACE) + ); + if (result) { + break; + } + } + assertTrue("Log messages are:\n" + + memoryLogsAppender.list.stream().map(ILoggingEvent::toString).collect(Collectors.joining("\n")), + result); + } finally { + memoryLogsAppender.stop(); + logger.detachAppender(memoryLogsAppender); + logger.setLevel(null); + } + } +} diff --git a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java similarity index 87% rename from java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java rename to java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java index 720c3d02d23e4..dfb6c706856b6 100644 --- a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java +++ b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java @@ -15,7 +15,11 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.unsafe; + +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; /** * The default Allocation Manager Factory for a module. diff --git a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java similarity index 89% rename from java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java rename to java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java index b10aba3598def..3468a6ec65c2f 100644 --- a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java +++ b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java @@ -15,8 +15,12 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.unsafe; +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.ReferenceManager; import org.apache.arrow.memory.util.MemoryUtil; /** diff --git a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.java b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java similarity index 90% rename from java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.java rename to java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java index 33abe92e50f12..f1ca96eea0f20 100644 --- a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.java +++ b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java @@ -15,10 +15,12 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.unsafe; import static org.junit.Assert.assertEquals; +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.DefaultAllocationManagerOption; import org.junit.Test; /** diff --git a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java similarity index 82% rename from java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java rename to java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java index c15882a37a6d1..5d74c398e2665 100644 --- a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java +++ b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java @@ -15,11 +15,16 @@ * limitations under the License. */ -package org.apache.arrow.memory; +package org.apache.arrow.memory.unsafe; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import org.apache.arrow.memory.AllocationManager; +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.memory.BufferLedger; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.memory.unsafe.UnsafeAllocationManager; import org.junit.Test; /** @@ -27,8 +32,8 @@ */ public class TestUnsafeAllocationManager { - private BaseAllocator createUnsafeAllocator() { - return new RootAllocator(BaseAllocator.configBuilder().allocationManagerFactory(UnsafeAllocationManager.FACTORY) + private RootAllocator createUnsafeAllocator() { + return new RootAllocator(RootAllocator.configBuilder().allocationManagerFactory(UnsafeAllocationManager.FACTORY) .build()); } @@ -51,7 +56,7 @@ private void readWriteArrowBuf(ArrowBuf buffer) { @Test public void testBufferAllocation() { final long bufSize = 4096L; - try (BaseAllocator allocator = createUnsafeAllocator(); + try (RootAllocator allocator = createUnsafeAllocator(); ArrowBuf buffer = allocator.buffer(bufSize)) { assertTrue(buffer.getReferenceManager() instanceof BufferLedger); BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); diff --git a/java/memory/pom.xml b/java/memory/pom.xml index c10263b97f1bf..55fbb90353f34 100644 --- a/java/memory/pom.xml +++ b/java/memory/pom.xml @@ -23,6 +23,7 @@ memory-core memory-unsafe + memory-netty-buffer-patch memory-netty