-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
PluginManager.java
320 lines (280 loc) · 12.6 KB
/
PluginManager.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Licensed 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 com.facebook.presto.server;
import com.facebook.presto.block.BlockEncodingManager;
import com.facebook.presto.connector.ConnectorManager;
import com.facebook.presto.eventlistener.EventListenerManager;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.security.AccessControlManager;
import com.facebook.presto.spi.Plugin;
import com.facebook.presto.spi.block.BlockEncodingFactory;
import com.facebook.presto.spi.classloader.ThreadContextClassLoader;
import com.facebook.presto.spi.connector.ConnectorFactory;
import com.facebook.presto.spi.connector.ConnectorFactoryContext;
import com.facebook.presto.spi.eventlistener.EventListenerFactory;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.type.ParametricType;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import io.airlift.configuration.ConfigurationFactory;
import io.airlift.http.server.HttpServerInfo;
import io.airlift.log.Logger;
import io.airlift.node.NodeInfo;
import io.airlift.resolver.ArtifactResolver;
import io.airlift.resolver.DefaultArtifact;
import org.sonatype.aether.artifact.Artifact;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.facebook.presto.metadata.FunctionExtractor.extractFunctions;
import static com.facebook.presto.server.PluginDiscovery.discoverPlugins;
import static com.facebook.presto.server.PluginDiscovery.writePluginServices;
import static java.util.Objects.requireNonNull;
@ThreadSafe
public class PluginManager
{
private static final ImmutableList<String> SPI_PACKAGES = ImmutableList.<String>builder()
.add("com.facebook.presto.spi.")
.add("com.fasterxml.jackson.annotation.")
.add("io.airlift.slice.")
.add("io.airlift.units.")
.build();
private static final Logger log = Logger.get(PluginManager.class);
private final ConnectorManager connectorManager;
private final Metadata metadata;
private final AccessControlManager accessControlManager;
private final EventListenerManager eventListenerManager;
private final BlockEncodingManager blockEncodingManager;
private final TypeRegistry typeRegistry;
private final ConnectorFactoryContext connectorFactoryContext;
private final ArtifactResolver resolver;
private final File installedPluginsDir;
private final List<String> plugins;
private final Map<String, String> optionalConfig;
private final AtomicBoolean pluginsLoading = new AtomicBoolean();
private final AtomicBoolean pluginsLoaded = new AtomicBoolean();
@Inject
public PluginManager(
NodeInfo nodeInfo,
HttpServerInfo httpServerInfo,
PluginManagerConfig config,
ConnectorManager connectorManager,
ConfigurationFactory configurationFactory,
Metadata metadata,
AccessControlManager accessControlManager,
EventListenerManager eventListenerManager,
BlockEncodingManager blockEncodingManager,
TypeRegistry typeRegistry,
ConnectorFactoryContext connectorFactoryContext)
{
requireNonNull(nodeInfo, "nodeInfo is null");
requireNonNull(httpServerInfo, "httpServerInfo is null");
requireNonNull(config, "config is null");
requireNonNull(configurationFactory, "configurationFactory is null");
installedPluginsDir = config.getInstalledPluginsDir();
if (config.getPlugins() == null) {
this.plugins = ImmutableList.of();
}
else {
this.plugins = ImmutableList.copyOf(config.getPlugins());
}
this.resolver = new ArtifactResolver(config.getMavenLocalRepository(), config.getMavenRemoteRepository());
Map<String, String> optionalConfig = new TreeMap<>(configurationFactory.getProperties());
optionalConfig.put("node.id", nodeInfo.getNodeId());
// TODO: make this work with and without HTTP and HTTPS
optionalConfig.put("http-server.http.port", Integer.toString(httpServerInfo.getHttpUri().getPort()));
this.optionalConfig = ImmutableMap.copyOf(optionalConfig);
this.connectorManager = requireNonNull(connectorManager, "connectorManager is null");
this.metadata = requireNonNull(metadata, "metadata is null");
this.accessControlManager = requireNonNull(accessControlManager, "accessControlManager is null");
this.eventListenerManager = requireNonNull(eventListenerManager, "eventListenerManager is null");
this.blockEncodingManager = requireNonNull(blockEncodingManager, "blockEncodingManager is null");
this.typeRegistry = requireNonNull(typeRegistry, "typeRegistry is null");
this.connectorFactoryContext = requireNonNull(connectorFactoryContext, "connectorFactoryContext is null");
}
public boolean arePluginsLoaded()
{
return pluginsLoaded.get();
}
public void loadPlugins()
throws Exception
{
if (!pluginsLoading.compareAndSet(false, true)) {
return;
}
for (File file : listFiles(installedPluginsDir)) {
if (file.isDirectory()) {
loadPlugin(file.getAbsolutePath());
}
}
for (String plugin : plugins) {
loadPlugin(plugin);
}
metadata.verifyComparableOrderableContract();
pluginsLoaded.set(true);
}
private void loadPlugin(String plugin)
throws Exception
{
log.info("-- Loading plugin %s --", plugin);
URLClassLoader pluginClassLoader = buildClassLoader(plugin);
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(pluginClassLoader)) {
loadPlugin(pluginClassLoader);
}
log.info("-- Finished loading plugin %s --", plugin);
}
private void loadPlugin(URLClassLoader pluginClassLoader)
throws Exception
{
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, pluginClassLoader);
List<Plugin> plugins = ImmutableList.copyOf(serviceLoader);
if (plugins.isEmpty()) {
log.warn("No service providers of type %s", Plugin.class.getName());
}
for (Plugin plugin : plugins) {
log.info("Installing %s", plugin.getClass().getName());
installPlugin(plugin);
}
}
public void installPlugin(Plugin plugin)
{
plugin.setOptionalConfig(optionalConfig);
for (BlockEncodingFactory<?> blockEncodingFactory : plugin.getBlockEncodingFactories(blockEncodingManager)) {
log.info("Registering block encoding %s", blockEncodingFactory.getName());
blockEncodingManager.addBlockEncodingFactory(blockEncodingFactory);
}
for (Type type : plugin.getTypes()) {
log.info("Registering type %s", type.getTypeSignature());
typeRegistry.addType(type);
}
for (ParametricType parametricType : plugin.getParametricTypes()) {
log.info("Registering parametric type %s", parametricType.getName());
typeRegistry.addParametricType(parametricType);
}
for (com.facebook.presto.spi.ConnectorFactory connectorFactory : plugin.getLegacyConnectorFactories(connectorFactoryContext)) {
log.info("Registering legacy connector %s", connectorFactory.getName());
connectorManager.addConnectorFactory(connectorFactory);
}
for (ConnectorFactory connectorFactory : plugin.getConnectorFactories(connectorFactoryContext)) {
log.info("Registering connector %s", connectorFactory.getName());
connectorManager.addConnectorFactory(connectorFactory);
}
for (Class<?> functionClass : plugin.getFunctions()) {
log.info("Registering functions from %s", functionClass.getName());
metadata.addFunctions(extractFunctions(functionClass));
}
for (SystemAccessControlFactory accessControlFactory : plugin.getSystemAccessControlFactories()) {
log.info("Registering system access control %s", accessControlFactory.getName());
accessControlManager.addSystemAccessControlFactory(accessControlFactory);
}
for (EventListenerFactory eventListenerFactory : plugin.getEventListenerFactories()) {
log.info("Registering event listener %s", eventListenerFactory.getName());
eventListenerManager.addEventListenerFactory(eventListenerFactory);
}
}
private URLClassLoader buildClassLoader(String plugin)
throws Exception
{
File file = new File(plugin);
if (file.isFile() && (file.getName().equals("pom.xml") || file.getName().endsWith(".pom"))) {
return buildClassLoaderFromPom(file);
}
if (file.isDirectory()) {
return buildClassLoaderFromDirectory(file);
}
return buildClassLoaderFromCoordinates(plugin);
}
private URLClassLoader buildClassLoaderFromPom(File pomFile)
throws Exception
{
List<Artifact> artifacts = resolver.resolvePom(pomFile);
URLClassLoader classLoader = createClassLoader(artifacts, pomFile.getPath());
Artifact artifact = artifacts.get(0);
Set<String> plugins = discoverPlugins(artifact, classLoader);
if (!plugins.isEmpty()) {
writePluginServices(plugins, artifact.getFile());
}
return classLoader;
}
private URLClassLoader buildClassLoaderFromDirectory(File dir)
throws Exception
{
log.debug("Classpath for %s:", dir.getName());
List<URL> urls = new ArrayList<>();
for (File file : listFiles(dir)) {
log.debug(" %s", file);
urls.add(file.toURI().toURL());
}
return createClassLoader(urls);
}
private URLClassLoader buildClassLoaderFromCoordinates(String coordinates)
throws Exception
{
Artifact rootArtifact = new DefaultArtifact(coordinates);
List<Artifact> artifacts = resolver.resolveArtifacts(rootArtifact);
return createClassLoader(artifacts, rootArtifact.toString());
}
private URLClassLoader createClassLoader(List<Artifact> artifacts, String name)
throws IOException
{
log.debug("Classpath for %s:", name);
List<URL> urls = new ArrayList<>();
for (Artifact artifact : sortedArtifacts(artifacts)) {
if (artifact.getFile() == null) {
throw new RuntimeException("Could not resolve artifact: " + artifact);
}
File file = artifact.getFile().getCanonicalFile();
log.debug(" %s", file);
urls.add(file.toURI().toURL());
}
return createClassLoader(urls);
}
private URLClassLoader createClassLoader(List<URL> urls)
{
ClassLoader parent = getClass().getClassLoader();
return new PluginClassLoader(urls, parent, SPI_PACKAGES);
}
private static List<File> listFiles(File installedPluginsDir)
{
if (installedPluginsDir != null && installedPluginsDir.isDirectory()) {
File[] files = installedPluginsDir.listFiles();
if (files != null) {
Arrays.sort(files);
return ImmutableList.copyOf(files);
}
}
return ImmutableList.of();
}
private static List<Artifact> sortedArtifacts(List<Artifact> artifacts)
{
List<Artifact> list = new ArrayList<>(artifacts);
Collections.sort(list, Ordering.natural().nullsLast().onResultOf(Artifact::getFile));
return list;
}
}