-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pinot plugin loader #13907
Closed
Closed
pinot plugin loader #13907
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d94887e
STP-3362: Introduce Plexus ClassWorld for more control over the class…
rfscholte 51a9c24
Fix checkstyle reported issues
rfscholte 81ad98b
Add plexus-classworlds dependency to pom
rfscholte b8b430d
Cannot use PluginManager.get() as this returns a singleton, whereas t…
rfscholte 36b2245
Specify version of classworlds org.apache.pinot:pinot
rfscholte e629621
Let Maven download the jars
rfscholte 339a0db
Start handling pinot plugin zipfiles
rfscholte e3b1223
Create plugin handlers
rfscholte 4107f87
Merge branch 'master' into STP-3364_pinot-plugin-loader
rfscholte dd8d56d
Merge branch 'apache:master' into STP-3364_pinot-plugin-loader
rfscholte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
pinot-spi/src/main/java/org/apache/pinot/spi/plugin/PinotPluginHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* 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.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.stream.Stream; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static java.util.function.Predicate.not; | ||
|
||
|
||
/** | ||
* This handler assumes the following: | ||
* Packed: | ||
* [plugins-directory]/[plugin-name]/[plugin].zip | ||
* | ||
* Unpacked | ||
* [plugins-directory]/[plugin-name]/classes/[**]/*.class | ||
* [plugins-directory]/[plugin-name]/lib/*.jar | ||
*/ | ||
class PinotPluginHandler { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(PinotPluginHandler.class); | ||
|
||
Collection<URL> toURLs(String pluginName, File directory) { | ||
LOGGER.info("Trying to load plugin [{}] from location [{}]", pluginName, directory); | ||
|
||
Collection<URL> urlList = new ArrayList<>(); | ||
|
||
// classes directory always has to go first | ||
Path classes = directory.toPath().resolve("classes"); | ||
if (Files.isDirectory(classes)) { | ||
try { | ||
urlList.add(classes.toUri().toURL()); | ||
} catch (MalformedURLException e) { | ||
LOGGER.error("Unable to load plugin [{}] classes directory", pluginName, e); | ||
} | ||
} | ||
|
||
try (var stream = Files.newDirectoryStream(directory.toPath().resolve("lib"), e -> e.getFileName().toString().endsWith(".jar"))) { | ||
stream.forEach(f -> { | ||
try { | ||
urlList.add(f.toUri().toURL()); | ||
} catch (MalformedURLException e) { | ||
LOGGER.error("Unable to load plugin [{}] jar file [{}]", pluginName, f.getFileName(), e); | ||
} | ||
}); | ||
} catch (IOException e) { | ||
LOGGER.error("Unable to load plugin [{}]", pluginName, e); | ||
} | ||
return urlList; | ||
} | ||
|
||
boolean isPluginDirectory(Path p) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some javadocs here? Also I'm not sure about the method naming. From the caller's perspective a method called |
||
// if there's a zip, first unpack it | ||
try (Stream<Path> stream = Files.find(p, 1, (f,a) -> (Files.isRegularFile(f) && p.getFileName().toString().endsWith(".zip")))) { | ||
stream | ||
.findFirst() | ||
.ifPresent(f -> unpack(f.getParent(), f)); | ||
} catch (IOException e) { | ||
LOGGER.warn("Failed to decide if plugin directory exists", e); | ||
return false; | ||
} | ||
|
||
return Files.isDirectory(p.resolve("classes")) || Files.isDirectory(p.resolve("lib")); | ||
} | ||
|
||
// THIS UNPACKS BASED ON pinot-plugins/assembly-descriptor/src/main/resources/assemblies/pinot-plugin.xml | ||
void unpack(Path pluginDirectory, Path pluginZip) { | ||
try (ZipFile zipFile = new ZipFile(pluginZip.toFile())) { | ||
zipFile.stream().filter(not(ZipEntry::isDirectory)).filter(e -> e.getName().startsWith("PINOT-INF/")) | ||
.forEach(e -> { | ||
String newPath = e.getName().substring("PINOT-INF/".length()); | ||
|
||
Path targetPath = pluginDirectory.resolve(newPath).normalize(); | ||
|
||
// prevent zipslip!! | ||
if (targetPath.startsWith(pluginDirectory)) { | ||
if (!Files.exists(targetPath.getParent())) { | ||
try { | ||
Files.createDirectories(targetPath.getParent()); | ||
} catch (IOException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
if (!Files.exists(targetPath)) { | ||
try { | ||
Files.createFile(targetPath); | ||
} catch (IOException ex) { | ||
LOGGER.warn("Failed to create file [{}]", targetPath, ex); | ||
} | ||
} | ||
|
||
try (InputStream in = zipFile.getInputStream(e); OutputStream out = Files.newOutputStream(targetPath)) { | ||
in.transferTo(out); | ||
} catch (IOException ex) { | ||
LOGGER.warn("Unable to unpack plugin [{}] [{}]", pluginDirectory, newPath, ex); | ||
} | ||
} | ||
}); | ||
} catch (IOException e) { | ||
LOGGER.warn("Failed to unpack plugin [{}]", pluginDirectory, e); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad news! We cannot use
var
, given we need to be compatible with Java 11