Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dev/tasks/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ groups:
c-glib:
- test-*c-glib*

java:
- "*java*"

python:
- test-*python*

Expand Down
4 changes: 3 additions & 1 deletion docs/source/developers/java/building.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ Arrow Java uses the `Maven <https://maven.apache.org/>`_ build system.

Building requires:

* JDK 8, 9, 10, 11, 17, or 18, but only JDK 8, 11 and 17 are tested in CI.
* JDK 8+
* Maven 3+

Note: CI will test all supported JDK LTS versions, plus the latest non-LTS version.

Building
========

Expand Down
3 changes: 3 additions & 0 deletions docs/source/java/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ but some modules are JNI bindings to the C++ library.
* - arrow-memory-netty
- Memory management implementation based on Netty.
- Native
* - arrow-memory-foreign
- (Experimental) Memory management implementation based on java.lang.foreign. Not released, can only be built from source.
- Native
* - arrow-vector
- An off-heap reference implementation for Arrow columnar data format.
- Native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public enum AllocationManagerType {
*/
Unsafe,

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

/**
* Unknown type.
*/
Expand Down Expand Up @@ -93,6 +98,9 @@ static AllocationManager.Factory getDefaultAllocationManagerFactory() {
case Unsafe:
DEFAULT_ALLOCATION_MANAGER_FACTORY = getUnsafeFactory();
break;
case Foreign:
DEFAULT_ALLOCATION_MANAGER_FACTORY = getForeignFactory();
break;
case Unknown:
LOGGER.info("allocation manager type not specified, using netty as the default type");
DEFAULT_ALLOCATION_MANAGER_FACTORY = getFactory(CheckAllocator.check());
Expand Down Expand Up @@ -130,4 +138,13 @@ private static AllocationManager.Factory getNettyFactory() {
" No DefaultAllocationManager found to instantiate an NettyAllocationManager", e);
}
}

private static AllocationManager.Factory getForeignFactory() {
try {
return getFactory("org.apache.arrow.memory.JavaForeignAllocationManager");
} catch (RuntimeException 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
@@ -0,0 +1,77 @@
/*
Copy link
Member Author

@danepitkin danepitkin Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was added as a hack to test the memory-core module with the FFM APIs. It will be useful for testing later, but can be ignored otherwise.

* 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();
}

}
30 changes: 30 additions & 0 deletions java/memory/memory-foreign/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 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. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>arrow-memory</artifactId>
<groupId>org.apache.arrow</groupId>
<version>15.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<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>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>arrow-memory-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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;

/**
* The default Allocation Manager Factory for a module.
*
*/
public class DefaultAllocationManagerFactory implements AllocationManager.Factory {

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

@Override
public AllocationManager create(BufferAllocator accountingAllocator, long size) {
return FACTORY.create(accountingAllocator, size);
}

@Override
public ArrowBuf empty() {
return FACTORY.empty();
}
}
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
@@ -0,0 +1,41 @@
/*
* 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 org.junit.Test;

/**
* Test cases for {@link AllocationManager}.
*/
public class TestAllocationManagerJavaForeign {

@Test
public void testAllocationManagerType() {

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

assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Foreign, mgrType);

}
}
Loading