You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ki...@apache.org on 2019/12/07 19:31:17 UTC
[incubator-pinot] branch dynamic-plugin-loader created (now 55ca47f)
This is an automated email from the ASF dual-hosted git repository.
kishoreg pushed a change to branch dynamic-plugin-loader
in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git.
at 55ca47f Initial code dump for dynamically loading pinot plugins
This branch includes the following new commits:
new 55ca47f Initial code dump for dynamically loading pinot plugins
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
[incubator-pinot] 01/01: Initial code dump for dynamically loading
pinot plugins
Posted by ki...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
kishoreg pushed a commit to branch dynamic-plugin-loader
in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git
commit 55ca47fa3bbcdfc4b6017e8be62fd9bc784b0ffc
Author: kishoreg <g....@gmail.com>
AuthorDate: Sat Dec 7 10:39:33 2019 -0800
Initial code dump for dynamically loading pinot plugins
---
pinot-record-readers/pom.xml | 1 +
.../java/org/apache/pinot/spi/plugin/Plugin.java | 48 ++++++++
.../apache/pinot/spi/plugin/PluginClassLoader.java | 135 +++++++++++++++++++++
.../org/apache/pinot/spi/plugin/PluginManager.java | 105 ++++++++++++++++
.../apache/pinot/spi/plugin/PluginManagerTest.java | 86 +++++++++++++
5 files changed, 375 insertions(+)
diff --git a/pinot-record-readers/pom.xml b/pinot-record-readers/pom.xml
index 1dc63dc..96c9578 100644
--- a/pinot-record-readers/pom.xml
+++ b/pinot-record-readers/pom.xml
@@ -50,6 +50,7 @@
<dependency>
<groupId>org.apache.pinot</groupId>
<artifactId>pinot-spi</artifactId>
+ <scope>provided</scope>
</dependency>
<!-- test -->
<dependency>
diff --git a/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/Plugin.java b/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/Plugin.java
new file mode 100644
index 0000000..312e25e
--- /dev/null
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/Plugin.java
@@ -0,0 +1,48 @@
+/**
+ * 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.pinot.spi.plugin;
+
+import java.util.Objects;
+
+
+public class Plugin {
+
+ String _name;
+
+ public Plugin(String name) {
+ _name = name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Plugin plugin = (Plugin) o;
+ return Objects.equals(_name, plugin._name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(_name);
+ }
+}
diff --git a/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PluginClassLoader.java b/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PluginClassLoader.java
new file mode 100644
index 0000000..19057e2
--- /dev/null
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PluginClassLoader.java
@@ -0,0 +1,135 @@
+package org.apache.pinot.spi.plugin;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+
+
+public class PluginClassLoader extends URLClassLoader {
+
+ private final ClassLoader sysClzLoader;
+
+ public PluginClassLoader(URL[] urls, ClassLoader parent) {
+ super(urls, parent);
+ sysClzLoader = getSystemClassLoader();
+ URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
+ Method method = null;
+ try {
+ method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
+
+ } catch (NoSuchMethodException e) {
+ //this should never happen
+ ExceptionUtils.rethrow(e);
+ }
+ method.setAccessible(true);
+ for (URL url : urls) {
+ try {
+ method.invoke(classLoader, url);
+ } catch (Exception e) {
+ ExceptionUtils.rethrow(e);
+ }
+ }
+ }
+
+ @Override
+ protected Class<?> loadClass(String name, boolean resolve)
+ throws ClassNotFoundException {
+ if (name.endsWith("LocalFileSystem")) {
+ System.out.println("name = " + name);
+ }
+ System.out.println("PluginClassLoader.loadClass: " + name);
+ // has the class loaded already?
+ Class<?> loadedClass = findLoadedClass(name);
+ if (loadedClass == null) {
+ try {
+ if (sysClzLoader != null) {
+ loadedClass = sysClzLoader.loadClass(name);
+ }
+ } catch (ClassNotFoundException ex) {
+ // class not found in system class loader... silently skipping
+ }
+
+ try {
+ // find the class from given jar urls as in first constructor parameter.
+ if (loadedClass == null) {
+ loadedClass = findClass(name);
+ }
+ } catch (ClassNotFoundException e) {
+ // class is not found in the given urls.
+ // Let's try it in parent classloader.
+ // If class is still not found, then this method will throw class not found ex.
+ loadedClass = super.loadClass(name, resolve);
+ }
+ }
+
+ if (resolve) { // marked to resolve
+ resolveClass(loadedClass);
+ }
+ return loadedClass;
+ }
+
+ @Override
+ public Enumeration<URL> getResources(String name)
+ throws IOException {
+ List<URL> allRes = new LinkedList<>();
+
+ // load resources from sys class loader
+ Enumeration<URL> sysResources = sysClzLoader.getResources(name);
+ if (sysResources != null) {
+ while (sysResources.hasMoreElements()) {
+ allRes.add(sysResources.nextElement());
+ }
+ }
+
+ // load resource from this classloader
+ Enumeration<URL> thisRes = findResources(name);
+ if (thisRes != null) {
+ while (thisRes.hasMoreElements()) {
+ allRes.add(thisRes.nextElement());
+ }
+ }
+
+ // then try finding resources from parent classloaders
+ Enumeration<URL> parentRes = super.findResources(name);
+ if (parentRes != null) {
+ while (parentRes.hasMoreElements()) {
+ allRes.add(parentRes.nextElement());
+ }
+ }
+
+ return new Enumeration<URL>() {
+ Iterator<URL> it = allRes.iterator();
+
+ @Override
+ public boolean hasMoreElements() {
+ return it.hasNext();
+ }
+
+ @Override
+ public URL nextElement() {
+ return it.next();
+ }
+ };
+ }
+
+ @Override
+ public URL getResource(String name) {
+ URL res = null;
+ if (sysClzLoader != null) {
+ res = sysClzLoader.getResource(name);
+ }
+ if (res == null) {
+ res = findResource(name);
+ }
+ if (res == null) {
+ res = super.getResource(name);
+ }
+ return res;
+ }
+}
diff --git a/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PluginManager.java b/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PluginManager.java
new file mode 100644
index 0000000..ee2fc9e
--- /dev/null
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PluginManager.java
@@ -0,0 +1,105 @@
+/**
+ * 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.pinot.spi.plugin;
+
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.io.FileUtils;
+
+
+public class PluginManager {
+
+ static PluginManager PLUGIN_MANAGER = new PluginManager();
+
+ Map<Plugin, PluginClassLoader> _registry = new HashMap<>();
+
+ private PluginManager() {
+
+ }
+
+ /**
+ * Loads jars recursively
+ * @param pluginName
+ * @param directory
+ */
+ public void load(String pluginName, File directory) {
+ Collection<File> jarFiles = FileUtils.listFiles(directory, new String[]{"jar"}, true);
+ Collection<URL> urlList = new ArrayList<>();
+ for (File jarFile : jarFiles) {
+ try {
+ urlList.add(jarFile.toURI().toURL());
+ } catch (MalformedURLException e) {
+ //ignore
+ }
+ }
+ PluginClassLoader classLoader = createClassLoader(urlList);
+ _registry.put(new Plugin(pluginName), classLoader);
+ }
+
+ private PluginClassLoader createClassLoader(Collection<URL> urlList) {
+ URL[] urls = new URL[urlList.size()];
+ urlList.toArray(urls);
+ //always sort to make the behavior predictable
+ Arrays.sort(urls);
+ return new PluginClassLoader(urls, Thread.currentThread().getContextClassLoader());
+ }
+
+ public Class<?> loadClass(String pluginName, String className)
+ throws ClassNotFoundException {
+ return PLUGIN_MANAGER._registry.get(new Plugin(pluginName)).loadClass(className, true);
+ }
+
+ public <T> T createInstance(String pluginName, String className)
+ throws Exception {
+ return createInstance(pluginName, className, new Class[]{}, new Object[]{});
+ }
+
+ /**
+ *
+ * @param pluginName
+ * @param className
+ * @param argTypes
+ * @param argValues
+ * @param <T>
+ * @return
+ */
+ public <T> T createInstance(String pluginName, String className, Class[] argTypes, Object[] argValues)
+ throws Exception {
+ PluginClassLoader pluginClassLoader = PLUGIN_MANAGER._registry.get(new Plugin(pluginName));
+ Class<T> loadedClass = (Class<T>) pluginClassLoader.loadClass(className, true);
+ Constructor<?> constructor = loadedClass.getConstructor(argTypes);
+ if (constructor != null) {
+ Object instance = constructor.newInstance(argValues);
+ return (T) instance;
+ }
+ return null;
+ }
+
+ public static PluginManager get() {
+ return PLUGIN_MANAGER;
+ }
+}
+
diff --git a/pinot-spi/src/test/java/org/apache/pinot/spi/plugin/PluginManagerTest.java b/pinot-spi/src/test/java/org/apache/pinot/spi/plugin/PluginManagerTest.java
new file mode 100644
index 0000000..d177d12
--- /dev/null
+++ b/pinot-spi/src/test/java/org/apache/pinot/spi/plugin/PluginManagerTest.java
@@ -0,0 +1,86 @@
+package org.apache.pinot.spi.plugin;
+
+import java.io.File;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.data.readers.RecordReader;
+//import org.xeustechnologies.jcl.JarClassLoader;
+//import org.xeustechnologies.jcl.JclObjectFactory;
+
+
+public class PluginManagerTest {
+// public static void main(String[] args)
+// throws Exception {
+//// testAvro();
+//
+//// testParquetWithJcl();
+// testParquet();
+// }
+//
+// private static void testParquetWithJcl()
+// throws Exception {
+// File dir = new File("/tmp/pinot-plugins/pinot-parquet");
+// PluginManager pluginManager = PluginManager.get();
+// String pluginName = "pinot-parquet";
+// pluginManager.load(pluginName, dir);
+//
+// JarClassLoader jcl = new JarClassLoader();
+// jcl.add(dir.toURI().toURL());
+//
+// String parquetFilePath = "/tmp/sample.c000.snappy.parquet";
+// File pinotSchemaFile = new File("/tmp/ops_wb_table_schema.json");
+// Schema pinotSchema = Schema.fromFile(pinotSchemaFile);
+// String recordReaderClassName = "org.apache.pinot.parquet.data.readers.ParquetRecordReader";
+//
+// jcl.loadClass(recordReaderClassName, true);
+// JclObjectFactory objectFactory = JclObjectFactory.getInstance();
+// RecordReader recordReader = (RecordReader) objectFactory.create(jcl, recordReaderClassName);
+// recordReader.init(new File(parquetFilePath), pinotSchema, null);
+// while (recordReader.hasNext()) {
+// GenericRow row = recordReader.next();
+// System.out.println("row = " + row);
+// }
+// }
+//
+// private static void testParquet()
+// throws Exception {
+// File dir = new File("/tmp/pinot-plugins/pinot-parquet");
+// PluginManager pluginManager = PluginManager.get();
+// String pluginName = "pinot-parquet";
+// pluginManager.load(pluginName, dir);
+//
+// String recordReaderClassName = "org.apache.pinot.parquet.data.readers.ParquetRecordReader";
+// Class<?> recordReaderClass = pluginManager.loadClass(pluginName, recordReaderClassName);
+// Class<?> fsClass = pluginManager.loadClass(pluginName, "org.apache.hadoop.fs.LocalFileSystem");
+// System.out.println("recordReaderClass = " + recordReaderClass);
+//
+// String parquetFilePath = "/tmp/sample.c000.snappy.parquet";
+// File pinotSchemaFile = new File("/tmp/ops_wb_table_schema.json");
+// Schema pinotSchema = Schema.fromFile(pinotSchemaFile);
+//
+// RecordReader recordReader =
+// pluginManager.createInstance(pluginName, recordReaderClassName, new Class[]{}, new Object[]{});
+// Thread.currentThread().setContextClassLoader(recordReader.getClass().getClassLoader());
+// recordReader.init(new File(parquetFilePath), pinotSchema, null);
+//
+// while (recordReader.hasNext()) {
+// GenericRow row = recordReader.next();
+// System.out.println("row = " + row);
+// }
+// }
+//
+// private static void testAvro()
+// throws ClassNotFoundException {
+// File dir =
+// new File("/Users/kishoreg/projects/incubator-pinot/pinot-record-readers/pinot-avro/target/pinot-avro-pkg");
+// PluginManager pluginManager = PluginManager.get();
+// pluginManager.load("pinot-avro", dir);
+//
+// Class<?> recordReaderClass =
+// pluginManager.loadClass("pinot-avro", "org.apache.pinot.avro.data.readers.AvroRecordReader");
+// System.out.println("recordReaderClass = " + recordReaderClass);
+//
+// String avroFilePath =
+// "/Users/kishoreg/projects/incubator-pinot/pinot-tools/src/main/resources/sample_data/airlineStats_data.avro";
+// }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org