2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org
.apache
.arrow
.memory
;
20 import java
.lang
.reflect
.Field
;
23 * A class for choosing the default allocation manager.
25 public class DefaultAllocationManagerOption
{
28 * The environmental variable to set the default allocation manager type.
30 public static final String ALLOCATION_MANAGER_TYPE_ENV_NAME
= "ARROW_ALLOCATION_MANAGER_TYPE";
33 * The system property to set the default allocation manager type.
35 public static final String ALLOCATION_MANAGER_TYPE_PROPERTY_NAME
= "arrow.allocation.manager.type";
37 static final org
.slf4j
.Logger LOGGER
= org
.slf4j
.LoggerFactory
.getLogger(DefaultAllocationManagerOption
.class);
40 * The default allocation manager factory.
42 private static AllocationManager
.Factory DEFAULT_ALLOCATION_MANAGER_FACTORY
= null;
45 * The allocation manager type.
47 public enum AllocationManagerType
{
49 * Netty based allocation manager.
54 * Unsafe based allocation manager.
64 static AllocationManagerType
getDefaultAllocationManagerType() {
65 AllocationManagerType ret
= AllocationManagerType
.Unknown
;
68 String envValue
= System
.getenv(ALLOCATION_MANAGER_TYPE_ENV_NAME
);
69 ret
= AllocationManagerType
.valueOf(envValue
);
70 } catch (IllegalArgumentException
| NullPointerException e
) {
71 // ignore the exception, and make the allocation manager type remain unchanged
74 // system property takes precedence
76 String propValue
= System
.getProperty(ALLOCATION_MANAGER_TYPE_PROPERTY_NAME
);
77 ret
= AllocationManagerType
.valueOf(propValue
);
78 } catch (IllegalArgumentException
| NullPointerException e
) {
79 // ignore the exception, and make the allocation manager type remain unchanged
84 static AllocationManager
.Factory
getDefaultAllocationManagerFactory() {
85 if (DEFAULT_ALLOCATION_MANAGER_FACTORY
!= null) {
86 return DEFAULT_ALLOCATION_MANAGER_FACTORY
;
88 AllocationManagerType type
= getDefaultAllocationManagerType();
91 DEFAULT_ALLOCATION_MANAGER_FACTORY
= getNettyFactory();
94 DEFAULT_ALLOCATION_MANAGER_FACTORY
= getUnsafeFactory();
97 LOGGER
.info("allocation manager type not specified, using netty as the default type");
98 DEFAULT_ALLOCATION_MANAGER_FACTORY
= getFactory(CheckAllocator
.check());
101 throw new IllegalStateException("Unknown allocation manager type: " + type
);
103 return DEFAULT_ALLOCATION_MANAGER_FACTORY
;
106 private static AllocationManager
.Factory
getFactory(String clazzName
) {
108 Field field
= Class
.forName(clazzName
).getDeclaredField("FACTORY");
109 field
.setAccessible(true);
110 return (AllocationManager
.Factory
) field
.get(null);
111 } catch (Exception e
) {
112 throw new RuntimeException("Unable to instantiate Allocation Manager for " + clazzName
, e
);
116 private static AllocationManager
.Factory
getUnsafeFactory() {
118 return getFactory("org.apache.arrow.memory.UnsafeAllocationManager");
119 } catch (RuntimeException e
) {
120 throw new RuntimeException("Please add arrow-memory-unsafe to your classpath," +
121 " No DefaultAllocationManager found to instantiate an UnsafeAllocationManager", e
);
125 private static AllocationManager
.Factory
getNettyFactory() {
127 return getFactory("org.apache.arrow.memory.NettyAllocationManager");
128 } catch (RuntimeException e
) {
129 throw new RuntimeException("Please add arrow-memory-netty to your classpath," +
130 " No DefaultAllocationManager found to instantiate an NettyAllocationManager", e
);