Skip to content

Commit

Permalink
address comments, update maven profile
Browse files Browse the repository at this point in the history
  • Loading branch information
danepitkin committed Nov 14, 2023
1 parent 0cd7939 commit 8c05046
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/source/java/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ but some modules are JNI bindings to the C++ library.
* - arrow-memory-netty
- Memory management implementation based on Netty.
- Native
* - arrow-memory-ffm
* - arrow-memory-foreign
- (Experimental) Memory management implementation based on java.lang.foreign. Not released, can only be built from source.
- Native
* - arrow-vector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public class DefaultAllocationManagerOption {
*/
public static final String ALLOCATION_MANAGER_TYPE_PROPERTY_NAME = "arrow.allocation.manager.type";

// Java 1.8, 9, 11, 17, 21 becomes 1, 9, 11, 17, and 21.
private static final int majorVersion =
Integer.parseInt(System.getProperty("java.specification.version").split("\\D+")[0]);

static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DefaultAllocationManagerOption.class);

/**
Expand All @@ -60,9 +56,9 @@ public enum AllocationManagerType {
Unsafe,

/**
* (Experimental) FFM based allocation manager.
* (Experimental) java.lang.foreign based allocation manager.
*/
FFM,
Foreign,

/**
* Unknown type.
Expand Down Expand Up @@ -102,8 +98,8 @@ static AllocationManager.Factory getDefaultAllocationManagerFactory() {
case Unsafe:
DEFAULT_ALLOCATION_MANAGER_FACTORY = getUnsafeFactory();
break;
case FFM:
DEFAULT_ALLOCATION_MANAGER_FACTORY = getFfmFactory();
case Foreign:
DEFAULT_ALLOCATION_MANAGER_FACTORY = getForeignFactory();
break;
case Unknown:
LOGGER.info("allocation manager type not specified, using netty as the default type");
Expand Down Expand Up @@ -143,17 +139,12 @@ private static AllocationManager.Factory getNettyFactory() {
}
}

private static AllocationManager.Factory getFfmFactory() {
private static AllocationManager.Factory getForeignFactory() {
try {
return getFactory("org.apache.arrow.memory.FfmAllocationManager");
return getFactory("org.apache.arrow.memory.JavaForeignAllocationManager");
} catch (RuntimeException e) {
if (majorVersion < 21) {
throw new RuntimeException("arrow-memory-ffm requires JDK 21+, current JDK version: " +
majorVersion);
} else {
throw new RuntimeException("Please add arrow-memory-ffm to your classpath," +
" No DefaultAllocationManager found to instantiate an FfmAllocationManager", e);
}
throw new RuntimeException("Please add arrow-memory-foreign to your classpath," +
" No DefaultAllocationManager found to instantiate an JavaForeignAllocationManager", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,21 @@

package org.apache.arrow.memory;

import org.apache.arrow.memory.util.MemoryUtil;

/**
* The default Allocation Manager Factory for a module.
*
* This is only used by tests and contains only a simplistic allocator method.
*
*/
public class DefaultAllocationManagerFactory implements AllocationManager.Factory {

public static final AllocationManager.Factory FACTORY = new DefaultAllocationManagerFactory();
private static final ArrowBuf EMPTY = new ArrowBuf(ReferenceManager.NO_OP,
null,
0,
MemoryUtil.UNSAFE.allocateMemory(0));
public static final AllocationManager.Factory FACTORY = JavaForeignAllocationManager.FACTORY;

@Override
public AllocationManager create(BufferAllocator accountingAllocator, long size) {
return new AllocationManager(accountingAllocator) {
private final long allocatedSize = size;
private final long address = MemoryUtil.UNSAFE.allocateMemory(size);

@Override
public long getSize() {
return allocatedSize;
}

@Override
protected long memoryAddress() {
return address;
}

@Override
protected void release0() {
MemoryUtil.UNSAFE.freeMemory(address);
}
};
return FACTORY.create(accountingAllocator, size);
}

@Override
public ArrowBuf empty() {
return EMPTY;
return FACTORY.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Allocation manager based on java.lang.foreign API.
*/
public final class FfmAllocationManager extends AllocationManager {
public final class JavaForeignAllocationManager extends AllocationManager {

private static final ArrowBuf EMPTY = new ArrowBuf(ReferenceManager.NO_OP,
null,
Expand All @@ -34,7 +34,7 @@ public final class FfmAllocationManager extends AllocationManager {
public static final AllocationManager.Factory FACTORY = new Factory() {
@Override
public AllocationManager create(BufferAllocator accountingAllocator, long size) {
return new FfmAllocationManager(accountingAllocator, size);
return new JavaForeignAllocationManager(accountingAllocator, size);
}

@Override
Expand All @@ -51,7 +51,7 @@ public ArrowBuf empty() {

private final long allocatedAddress;

FfmAllocationManager(BufferAllocator accountingAllocator, long requestedSize) {
JavaForeignAllocationManager(BufferAllocator accountingAllocator, long requestedSize) {
super(accountingAllocator);
arena = Arena.ofShared();
allocatedMemorySegment = arena.allocate(requestedSize, /*byteAlignment*/ 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>arrow-memory-ffm</artifactId>
<name>Arrow Memory - FFM</name>
<artifactId>arrow-memory-foreign</artifactId>
<name>Arrow Memory - Foreign</name>
<description>Allocator and utils for allocating memory in Arrow based on java.lang.foreign</description>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class DefaultAllocationManagerFactory implements AllocationManager.Factory {

public static final AllocationManager.Factory FACTORY = FfmAllocationManager.FACTORY;
public static final AllocationManager.Factory FACTORY = JavaForeignAllocationManager.FACTORY;

@Override
public AllocationManager create(BufferAllocator accountingAllocator, long size) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

/**
* Allocation manager based on java.lang.foreign API.
*/
public final class JavaForeignAllocationManager extends AllocationManager {

private static final ArrowBuf EMPTY = new ArrowBuf(ReferenceManager.NO_OP,
null,
0,
MemorySegment.NULL.address()
);

public static final AllocationManager.Factory FACTORY = new Factory() {
@Override
public AllocationManager create(BufferAllocator accountingAllocator, long size) {
return new JavaForeignAllocationManager(accountingAllocator, size);
}

@Override
public ArrowBuf empty() {
return EMPTY;
}
};

private final Arena arena;

private final MemorySegment allocatedMemorySegment;

private final long allocatedSize;

private final long allocatedAddress;

JavaForeignAllocationManager(BufferAllocator accountingAllocator, long requestedSize) {
super(accountingAllocator);
arena = Arena.ofShared();
allocatedMemorySegment = arena.allocate(requestedSize, /*byteAlignment*/ 8);
allocatedAddress = allocatedMemorySegment.address();
allocatedSize = requestedSize;
}

@Override
public long getSize() {
return allocatedSize;
}

@Override
protected long memoryAddress() {
return allocatedAddress;
}

@Override
protected void release0() {
arena.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
/**
* Test cases for {@link AllocationManager}.
*/
public class TestAllocationManagerFfm {
public class TestAllocationManagerJavaForeign {

@Test
public void testAllocationManagerType() {

// test FFM allocation manager type
// test Java Foreign allocation manager type
System.setProperty(
DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME, "FFM");
DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME, "Foreign");
DefaultAllocationManagerOption.AllocationManagerType mgrType =
DefaultAllocationManagerOption.getDefaultAllocationManagerType();

assertEquals(DefaultAllocationManagerOption.AllocationManagerType.FFM, mgrType);
assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Foreign, mgrType);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import org.junit.Test;

/**
* Test cases for {@link FfmAllocationManager}.
* Test cases for {@link JavaForeignAllocationManager}.
*/
public class TestFfmAllocationManager {
public class TestJavaForeignAllocationManager {

private BaseAllocator createFfmAllocator() {
return new RootAllocator(BaseAllocator.configBuilder().allocationManagerFactory(FfmAllocationManager.FACTORY)
private BaseAllocator createJavaForeignAllocator() {
return new RootAllocator(BaseAllocator.configBuilder()
.allocationManagerFactory(JavaForeignAllocationManager.FACTORY)
.build());
}

Expand All @@ -46,22 +47,22 @@ private void readWriteArrowBuf(ArrowBuf buffer) {
}

/**
* Test the memory allocation for {@link FfmAllocationManager}.
* Test the memory allocation for {@link JavaForeignAllocationManager}.
*/
@Test
public void testBufferAllocation() {
final long bufSize = 4096L;
try (BaseAllocator allocator = createFfmAllocator();
try (BaseAllocator allocator = createJavaForeignAllocator();
ArrowBuf buffer = allocator.buffer(bufSize)) {
assertTrue(buffer.getReferenceManager() instanceof BufferLedger);
BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager();

// make sure we are using FFM allocation manager
// make sure we are using the Java Foreign allocation manager
AllocationManager allocMgr = bufferLedger.getAllocationManager();
assertTrue(allocMgr instanceof FfmAllocationManager);
FfmAllocationManager ffmMgr = (FfmAllocationManager) allocMgr;
assertTrue(allocMgr instanceof JavaForeignAllocationManager);
JavaForeignAllocationManager mgr = (JavaForeignAllocationManager) allocMgr;

assertEquals(bufSize, ffmMgr.getSize());
assertEquals(bufSize, mgr.getSize());
readWriteArrowBuf(buffer);
}
}
Expand Down
1 change: 1 addition & 0 deletions java/memory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<modules>
<module>memory-core</module>
<module>memory-foreign</module>
<module>memory-unsafe</module>
<module>memory-netty</module>
</modules>
Expand Down
Loading

0 comments on commit 8c05046

Please sign in to comment.