]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java
15120c252fca33996d97091be36d577a7a9da39d
[ceph.git] / ceph / src / arrow / java / memory / memory-core / src / main / java / org / apache / arrow / memory / DefaultAllocationManagerOption.java
1 /*
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18 package org.apache.arrow.memory;
19
20 import java.lang.reflect.Field;
21
22 /**
23 * A class for choosing the default allocation manager.
24 */
25 public class DefaultAllocationManagerOption {
26
27 /**
28 * The environmental variable to set the default allocation manager type.
29 */
30 public static final String ALLOCATION_MANAGER_TYPE_ENV_NAME = "ARROW_ALLOCATION_MANAGER_TYPE";
31
32 /**
33 * The system property to set the default allocation manager type.
34 */
35 public static final String ALLOCATION_MANAGER_TYPE_PROPERTY_NAME = "arrow.allocation.manager.type";
36
37 static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DefaultAllocationManagerOption.class);
38
39 /**
40 * The default allocation manager factory.
41 */
42 private static AllocationManager.Factory DEFAULT_ALLOCATION_MANAGER_FACTORY = null;
43
44 /**
45 * The allocation manager type.
46 */
47 public enum AllocationManagerType {
48 /**
49 * Netty based allocation manager.
50 */
51 Netty,
52
53 /**
54 * Unsafe based allocation manager.
55 */
56 Unsafe,
57
58 /**
59 * Unknown type.
60 */
61 Unknown,
62 }
63
64 static AllocationManagerType getDefaultAllocationManagerType() {
65 AllocationManagerType ret = AllocationManagerType.Unknown;
66
67 try {
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
72 }
73
74 // system property takes precedence
75 try {
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
80 }
81 return ret;
82 }
83
84 static AllocationManager.Factory getDefaultAllocationManagerFactory() {
85 if (DEFAULT_ALLOCATION_MANAGER_FACTORY != null) {
86 return DEFAULT_ALLOCATION_MANAGER_FACTORY;
87 }
88 AllocationManagerType type = getDefaultAllocationManagerType();
89 switch (type) {
90 case Netty:
91 DEFAULT_ALLOCATION_MANAGER_FACTORY = getNettyFactory();
92 break;
93 case Unsafe:
94 DEFAULT_ALLOCATION_MANAGER_FACTORY = getUnsafeFactory();
95 break;
96 case Unknown:
97 LOGGER.info("allocation manager type not specified, using netty as the default type");
98 DEFAULT_ALLOCATION_MANAGER_FACTORY = getFactory(CheckAllocator.check());
99 break;
100 default:
101 throw new IllegalStateException("Unknown allocation manager type: " + type);
102 }
103 return DEFAULT_ALLOCATION_MANAGER_FACTORY;
104 }
105
106 private static AllocationManager.Factory getFactory(String clazzName) {
107 try {
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);
113 }
114 }
115
116 private static AllocationManager.Factory getUnsafeFactory() {
117 try {
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);
122 }
123 }
124
125 private static AllocationManager.Factory getNettyFactory() {
126 try {
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);
131 }
132 }
133 }