-
Notifications
You must be signed in to change notification settings - Fork 335
/
CatalogInjector.java
291 lines (248 loc) · 12 KB
/
CatalogInjector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright 2023 Datastrato.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.trino.connector.catalog;
import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_CREATE_INNER_CONNECTOR_FAILED;
import static com.datastrato.gravitino.trino.connector.GravitinoErrorCode.GRAVITINO_UNSUPPORTED_TRINO_VERSION;
import com.datastrato.gravitino.trino.connector.GravitinoErrorCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.Connector;
import io.trino.spi.connector.ConnectorContext;
import io.trino.spi.connector.MetadataProvider;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class dynamically injects the Catalog managed by Gravitino into Trino using reflection
* techniques. It allows it to be used in Trino like a regular Trino catalog. In Gravitino, the
* catalog name consists of the "metalake" and catalog name, for example, "user_0.hive_us." We can
* use it directly in Trino.
*/
public class CatalogInjector {
private static final Logger LOG = LoggerFactory.getLogger(CatalogInjector.class);
private static final int MIN_TRINO_SPI_VERSION = 360;
// It is used to inject catalogs to trino
private InjectCatalogHandle injectHandle;
// It is used to create internal catalogs.
private CreateCatalogHandle createHandle;
private String trinoVersion;
private void checkTrinoSpiVersion(ConnectorContext context) {
this.trinoVersion = context.getSpiVersion();
int version = Integer.parseInt(context.getSpiVersion());
if (version < MIN_TRINO_SPI_VERSION) {
String errmsg =
String.format(
"Unsupported trino-%s version. min support version is trino-%d",
trinoVersion, MIN_TRINO_SPI_VERSION);
throw new TrinoException(GravitinoErrorCode.GRAVITINO_UNSUPPORTED_TRINO_VERSION, errmsg);
}
}
private static Field getField(Object targetObject, String fieldName) throws NoSuchFieldException {
Field field = targetObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field;
}
private static Object getFiledObject(Object targetObject, String fieldName)
throws NoSuchFieldException, IllegalAccessException {
return getField(targetObject, fieldName).get(targetObject);
}
private static boolean isClassObject(Object targetObject, String className) {
return targetObject.getClass().getName().endsWith(className);
}
private static Class getClass(ClassLoader classLoader, String className)
throws ClassNotFoundException {
return classLoader.loadClass(className);
}
/**
* @param context
* <pre>
* This function does the following tasks by ConnectorContext:
* 1. Retrieve the DiscoveryNodeManager object.
* 2. To enable Trino to handle tables on every node,
* set 'allCatalogsOnAllNodes' to 'true' and 'activeNodesByCatalogHandle' to empty.
* 3. Retrieve the catalogManager object.
* 4. Get createCatalog function in catalogFactory
* 5. Create a CreateCatalogHandle for the Gravitino connector's internal connector.
* 6. Create InjectCatalogHandle for injection catalogs to trino.
*
* A runtime ConnectorContext hierarchy:
* context (ConnectorContext)
* --nodeManager (ConnectorAwareNodeManager)
* ----nodeManager (DiscoveryNodeManager)
* ------nodeManager (DiscoveryNodeManager)
* ------allCatalogsOnAllNodes (boolean)
* ------activeNodesByCatalogHandle (SetMultimap)
* --metadataProvider(InternalMetadataProvider)
* ----metadata (TracingMetadata)
* ------delegate (MetadataManager)
* --------transactionManager (InMemoryTransactionManager)
* ----------catalogManager (StaticCatalogManager)
* ------------catalogFactory (LazyCatalogFactory)
* --------------createCatalog() (Function)
* ------------catalogs (ConcurrentHashMap)
* </pre>
*/
public void init(ConnectorContext context) {
// Injector trino catalog need NodeManager support allCatalogsOnAllNodes;
checkTrinoSpiVersion(context);
try {
// 1. Retrieve the DiscoveryNodeManager object.
Object nodeManager = context.getNodeManager();
nodeManager = getFiledObject(nodeManager, "nodeManager");
if (isClassObject(nodeManager, "DiscoveryNodeManager")) {
// 2. To enable Trino to handle tables on every node
Field allCatalogsOnAllNodes = getField(nodeManager, "allCatalogsOnAllNodes");
allCatalogsOnAllNodes.setBoolean(nodeManager, true);
Field activeNodesByCatalogHandle = getField(nodeManager, "activeNodesByCatalogHandle");
activeNodesByCatalogHandle.set(nodeManager, Optional.empty());
}
// 3. Retrieve the catalogManager object.
MetadataProvider metadataProvider = context.getMetadataProvider();
Object metadata = getFiledObject(metadataProvider, "metadata");
Object metadataManager = metadata;
if (isClassObject(metadata, "TracingMetadata")) {
metadataManager = getFiledObject(metadata, "delegate");
}
Preconditions.checkNotNull(metadataManager, "metadataManager should not be null");
Object transactionManager = getFiledObject(metadataManager, "transactionManager");
Object catalogManager = getFiledObject(transactionManager, "catalogManager");
Preconditions.checkNotNull(catalogManager, "catalogManager should not be null");
// 4. Get createCatalog function in catalogFactory
Object catalogFactory = getFiledObject(catalogManager, "catalogFactory");
Preconditions.checkNotNull(catalogFactory, "catalogFactory should not be null");
Class catalogPropertiesClass =
getClass(
catalogManager.getClass().getClassLoader(), "io.trino.connector.CatalogProperties");
Method createCatalogMethod =
catalogFactory.getClass().getDeclaredMethod("createCatalog", catalogPropertiesClass);
Preconditions.checkNotNull(createCatalogMethod, "createCatalogMethod should not be null");
// 5. Create a CreateCatalogHandle
createHandle =
(catalogName, catalogProperties) -> {
ObjectMapper objectMapper = new ObjectMapper();
Object catalogPropertiesObject =
objectMapper.readValue(catalogProperties, catalogPropertiesClass);
// Call catalogFactory.createCatalog() return CatalogConnector
Object catalogConnector =
createCatalogMethod.invoke(catalogFactory, catalogPropertiesObject);
// The catalogConnector hierarchy:
// --catalogConnector (CatalogConnector)
// ----catalogConnector (ConnectorServices)
// ------connector (Connector)
// Get a connector object from trino CatalogConnector.
Object catalogConnectorObject = getFiledObject(catalogConnector, "catalogConnector");
return getFiledObject(catalogConnectorObject, "connector");
};
// 6. Create InjectCatalogHandle
createInjectHandler(
catalogManager, catalogFactory, createCatalogMethod, catalogPropertiesClass);
LOG.info("Bind Trino catalog manager successfully.");
} catch (Exception e) {
String message =
String.format(
"Bind Trino catalog manager failed, unsupported trino-%s version", trinoVersion);
LOG.error(message, e);
throw new TrinoException(GRAVITINO_UNSUPPORTED_TRINO_VERSION, message, e);
}
}
private void createInjectHandler(
Object catalogManager,
Object catalogFactory,
Method createCatalogMethod,
Class catalogPropertiesClass)
throws NoSuchFieldException, IllegalAccessException {
// The catalogManager is an instance of CoordinatorDynamicCatalogManager
if (isClassObject(catalogManager, "CoordinatorDynamicCatalogManager")) {
ConcurrentHashMap activeCatalogs =
(ConcurrentHashMap) getFiledObject(catalogManager, "activeCatalogs");
Preconditions.checkNotNull(activeCatalogs, "activeCatalogs should not be null");
ConcurrentHashMap allCatalogs =
(ConcurrentHashMap) getFiledObject(catalogManager, "allCatalogs");
Preconditions.checkNotNull(allCatalogs, "allCatalogs should not be null");
injectHandle =
(catalogName, catalogProperties) -> {
// Call CatalogFactory:createCatalog and add the catalog to
// CoordinatorDynamicCatalogManager
ObjectMapper objectMapper = new ObjectMapper();
Object catalogPropertiesObject =
objectMapper.readValue(catalogProperties, catalogPropertiesClass);
Object catalogConnector =
createCatalogMethod.invoke(catalogFactory, catalogPropertiesObject);
Field catelogField = catalogConnector.getClass().getDeclaredField("catalog");
catelogField.setAccessible(true);
Object catalog = catelogField.get(catalogConnector);
activeCatalogs.put(catalogName, catalog);
Field catelogHandleField =
catalogConnector.getClass().getDeclaredField("catalogHandle");
catelogHandleField.setAccessible(true);
Object catalogHandle = catelogHandleField.get(catalogConnector);
allCatalogs.put(catalogHandle, catalogConnector);
};
} else {
// The catalogManager is an instance of StaticCatalogManager
ConcurrentHashMap catalogs = (ConcurrentHashMap) getFiledObject(catalogManager, "catalogs");
Preconditions.checkNotNull(catalogs, "catalogs should not be null");
injectHandle =
(catalogName, catalogProperties) -> {
// call CatalogFactory:createCatalog and add the catalog to StaticCatalogManager
ObjectMapper objectMapper = new ObjectMapper();
Object catalogPropertiesObject =
objectMapper.readValue(catalogProperties, catalogPropertiesClass);
Object catalogConnector =
createCatalogMethod.invoke(catalogFactory, catalogPropertiesObject);
catalogs.put(catalogName, catalogConnector);
};
}
}
void injectCatalogConnector(String catalogName) {
try {
String catalogProperties = createCatalogProperties(catalogName);
injectHandle.invoke(catalogName, catalogProperties);
LOG.info("Inject trino catalog {} successfully.", catalogName);
} catch (Exception e) {
LOG.error("Inject trino catalog {} failed.", catalogName, e);
throw new TrinoException(GRAVITINO_CREATE_INNER_CONNECTOR_FAILED, e);
}
}
String createCatalogProperties(String catalogName) {
String catalogPropertiesTemplate =
"{\"catalogHandle\": \"%s:normal:default\",\"connectorName\":\"gravitino\", \"properties\": "
+ "{\"gravitino.internal\": \"true\"}"
+ "}";
return String.format(catalogPropertiesTemplate, catalogName);
}
Connector createConnector(String connectorName, Map<String, Object> properties) {
String connectorProperties;
try {
ObjectMapper objectMapper = new ObjectMapper();
connectorProperties = objectMapper.writeValueAsString(properties);
LOG.debug(
"Create internal catalog connector {}. The config:{} .",
connectorName,
connectorProperties);
Object catalogConnector = createHandle.invoke(connectorName, connectorProperties);
LOG.info("Create internal catalog connector {} successfully.", connectorName);
return (Connector) catalogConnector;
} catch (Exception e) {
LOG.error(
"Create internal catalog connector {} failed. Connector properties: {} ",
connectorName,
properties.toString(),
e);
throw new TrinoException(GRAVITINO_CREATE_INNER_CONNECTOR_FAILED, e);
}
}
interface InjectCatalogHandle {
void invoke(String name, String properties) throws Exception;
}
interface CreateCatalogHandle {
Object invoke(String name, String properties) throws Exception;
}
}