-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-8481: [Java] Provide an allocation manager based on Unsafe API #6956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
java/memory/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * A class for choosing the default allocation manager. | ||
| */ | ||
| public class DefaultAllocationManagerOption { | ||
|
|
||
| /** | ||
| * The environmental variable to set the default allocation manager type. | ||
| */ | ||
| public static final String ALLOCATION_MANAGER_TYPE_ENV_NAME = "ARROW_ALLOCATION_MANAGER_TYPE"; | ||
|
|
||
| /** | ||
| * The system property to set the default allocation manager type. | ||
| */ | ||
| public static final String ALLOCATION_MANAGER_TYPE_PROPERTY_NAME = "arrow.allocation.manager.type"; | ||
|
|
||
| static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DefaultAllocationManagerOption.class); | ||
|
|
||
| /** | ||
| * The default allocation manager factory. | ||
| */ | ||
| public static final AllocationManager.Factory DEFAULT_ALLOCATION_MANAGER_FACTORY = | ||
| getDefaultAllocationManagerFactory(); | ||
|
|
||
| /** | ||
| * The allocation manager type. | ||
| */ | ||
| public enum AllocationManagerType { | ||
| /** | ||
| * Netty based allocation manager. | ||
| */ | ||
| Netty, | ||
|
|
||
| /** | ||
| * Unsafe based allocation manager. | ||
| */ | ||
| Unsafe, | ||
|
|
||
| /** | ||
| * Unknown type. | ||
| */ | ||
| Unknown, | ||
| } | ||
|
|
||
| static AllocationManagerType getDefaultAllocationManagerType() { | ||
| AllocationManagerType ret = AllocationManagerType.Unknown; | ||
|
|
||
| try { | ||
| String envValue = System.getenv(ALLOCATION_MANAGER_TYPE_ENV_NAME); | ||
| ret = AllocationManagerType.valueOf(envValue); | ||
| } catch (IllegalArgumentException | NullPointerException e) { | ||
| // ignore the exception, and make the allocation manager type remain unchanged | ||
| } | ||
|
|
||
| // system property takes precedence | ||
| try { | ||
| String propValue = System.getProperty(ALLOCATION_MANAGER_TYPE_PROPERTY_NAME); | ||
| ret = AllocationManagerType.valueOf(propValue); | ||
| } catch (IllegalArgumentException | NullPointerException e) { | ||
| // ignore the exception, and make the allocation manager type remain unchanged | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| static AllocationManager.Factory getDefaultAllocationManagerFactory() { | ||
| AllocationManagerType type = getDefaultAllocationManagerType(); | ||
|
|
||
| switch (type) { | ||
| case Netty: | ||
| return NettyAllocationManager.FACTORY; | ||
| case Unsafe: | ||
| return UnsafeAllocationManager.FACTORY; | ||
| case Unknown: | ||
| LOGGER.info("allocation manager type not specified, using netty as the default type"); | ||
| return NettyAllocationManager.FACTORY; | ||
| default: | ||
| throw new IllegalStateException("Unknown allocation manager type: " + type); | ||
| } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
java/memory/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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 org.apache.arrow.memory.util.MemoryUtil; | ||
|
|
||
| /** | ||
| * Allocation manager based on unsafe API. | ||
| */ | ||
| public final class UnsafeAllocationManager extends AllocationManager { | ||
|
|
||
| public static final Factory FACTORY = new Factory(); | ||
|
|
||
| private final long allocatedSize; | ||
|
|
||
| private final long allocatedAddress; | ||
|
|
||
| UnsafeAllocationManager(BaseAllocator accountingAllocator, long requestedSize) { | ||
| super(accountingAllocator); | ||
| allocatedAddress = MemoryUtil.UNSAFE.allocateMemory(requestedSize); | ||
| allocatedSize = requestedSize; | ||
| } | ||
|
|
||
| @Override | ||
| public long getSize() { | ||
| return allocatedSize; | ||
| } | ||
|
|
||
| @Override | ||
| protected long memoryAddress() { | ||
| return allocatedAddress; | ||
| } | ||
|
|
||
| @Override | ||
| protected void release0() { | ||
| MemoryUtil.UNSAFE.freeMemory(allocatedAddress); | ||
| } | ||
|
|
||
| /** | ||
| * Factory for creating {@link UnsafeAllocationManager}. | ||
| */ | ||
| public static class Factory implements AllocationManager.Factory { | ||
| private Factory() {} | ||
|
|
||
| @Override | ||
| public AllocationManager create(BaseAllocator accountingAllocator, long size) { | ||
| return new UnsafeAllocationManager(accountingAllocator, size); | ||
| } | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
java/memory/src/test/java/org/apache/arrow/memory/TestAllocationManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * 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 TestAllocationManager { | ||
|
|
||
| @Test | ||
| public void testAllocationManagerType() { | ||
| // test netty allocation manager type | ||
| System.setProperty( | ||
| DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME, "Netty"); | ||
| DefaultAllocationManagerOption.AllocationManagerType mgrType = | ||
| DefaultAllocationManagerOption.getDefaultAllocationManagerType(); | ||
|
|
||
| assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Netty, mgrType); | ||
|
|
||
| // test unsafe allocation manager type | ||
| System.setProperty( | ||
| DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME, "Unsafe"); | ||
| mgrType = DefaultAllocationManagerOption.getDefaultAllocationManagerType(); | ||
|
|
||
| assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Unsafe, mgrType); | ||
|
|
||
| // test unknown allocation manager type | ||
| System.clearProperty(DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME); | ||
| mgrType = DefaultAllocationManagerOption.getDefaultAllocationManagerType(); | ||
|
|
||
| assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Unknown, mgrType); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
java/memory/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * 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 static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Test cases for {@link UnsafeAllocationManager}. | ||
| */ | ||
| public class TestUnsafeAllocationManager { | ||
|
|
||
| private BaseAllocator createUnsafeAllocator() { | ||
| return new RootAllocator(BaseAllocator.configBuilder() | ||
| .allocationManagerFactory((accountingAllocator, requestedSize) -> | ||
| new UnsafeAllocationManager( | ||
| accountingAllocator, requestedSize)).build()); | ||
| } | ||
|
|
||
| private void readWriteArrowBuf(ArrowBuf buffer) { | ||
| // write buffer | ||
| for (long i = 0; i < buffer.capacity() / 8; i++) { | ||
| buffer.setLong(i * 8, i); | ||
| } | ||
|
|
||
| // read buffer | ||
| for (long i = 0; i < buffer.capacity() / 8; i++) { | ||
| long val = buffer.getLong(i * 8); | ||
| assertEquals(i, val); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test the memory allocation for {@link UnsafeAllocationManager}. | ||
| */ | ||
| @Test | ||
| public void testBufferAllocation() { | ||
| final long bufSize = 4096L; | ||
| try (BaseAllocator allocator = createUnsafeAllocator(); | ||
| ArrowBuf buffer = allocator.buffer(bufSize)) { | ||
| assertTrue(buffer.getReferenceManager() instanceof BufferLedger); | ||
| BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); | ||
|
|
||
| // make sure we are using unsafe allocation manager | ||
| AllocationManager allocMgr = bufferLedger.getAllocationManager(); | ||
| assertTrue(allocMgr instanceof UnsafeAllocationManager); | ||
| UnsafeAllocationManager unsafeMgr = (UnsafeAllocationManager) allocMgr; | ||
|
|
||
| assertEquals(bufSize, unsafeMgr.getSize()); | ||
| readWriteArrowBuf(buffer); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the default allocator is changing
RootAllocatorshould probably have its defaults[1] changed so that it doesn't generate a default allocator when it wants a netty one.[1]
arrow/java/memory/src/main/java/org/apache/arrow/memory/RootAllocator.java
Line 51 in dff612e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignore me...default is still netty