Skip to content

Commit

Permalink
[serving] Avoid unecessary copy plugin jars. (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Apr 22, 2024
1 parent 8bf6106 commit 605df37
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
3 changes: 1 addition & 2 deletions serving/docker/scripts/install_djl_serving.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ if [ -z "$PYTORCH_JNI" ]; then
dpkg -i djl-serving_all.deb
rm djl-serving_all.deb

cp /usr/local/djl-serving-*/conf/log4j2.xml /opt/djl/conf/
cp -r /usr/local/djl-serving-*/plugins /opt/djl/plugins
mkdir -p /opt/djl/plugins
else
if [[ ! "$DJL_VERSION" == *SNAPSHOT ]]; then
djl-serving -i ai.djl.pytorch:pytorch-jni:${PYTORCH_JNI}-${DJL_VERSION}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -200,29 +203,30 @@ public <T> Set<T> findImplementations(Class<T> pluginInterface) {
}

private URL[] listPluginJars() throws IOException {
Path pluginsFolder = configManager.getPluginFolder();
if (pluginsFolder == null || !Files.isDirectory(pluginsFolder)) {
logger.info("plug-in folder not exists:{}", pluginsFolder);
return new URL[0];
}
logger.info("scanning in plug-in folder :{}", pluginsFolder);

try (Stream<Path> stream = Files.walk(pluginsFolder, Integer.MAX_VALUE)) {
return stream.filter(file -> !Files.isDirectory(file))
.filter(file -> file.getFileName() != null)
.filter(file -> file.getFileName().toString().toLowerCase().endsWith(".jar"))
.map(Path::toUri)
.map(
t -> {
try {
return t.toURL();
} catch (MalformedURLException e) {
logger.error(e.getMessage(), e);
List<Path> pluginsFolders = configManager.getPluginFolder();
List<URL> ret = new ArrayList<>();
for (Path dir : pluginsFolders) {
if (dir == null || !Files.isDirectory(dir)) {
logger.info("plug-in folder not exists:{}", dir);
continue;
}
logger.info("scanning in plug-in folder :{}", dir);

try (Stream<Path> stream = Files.walk(dir)) {
stream.forEach(
f -> {
String name = f.toFile().getName().toLowerCase(Locale.ROOT);
try {
if (Files.isRegularFile(f) && name.endsWith(".jar")) {
ret.add(f.toUri().toURL());
}
return null;
})
.toArray(URL[]::new);
} catch (MalformedURLException e) {
logger.error("scan plugins folder failed", e);
}
});
}
}
return ret.toArray(new URL[0]);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions serving/src/main/java/ai/djl/serving/util/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
Expand Down Expand Up @@ -302,10 +303,21 @@ public String getCorsAllowedHeaders() {
/**
* return the folder where the model search for plugins.
*
* @return the configured plugin folder or the default folder.
* @return the configured plugin folder or the default folder
* @throws IOException if failed to resolve plugin folder
*/
public Path getPluginFolder() {
return getPathProperty(PLUGIN_FOLDER, "plugins");
public List<Path> getPluginFolder() throws IOException {
List<Path> list = new ArrayList<>();
Path plugin = getPathProperty(PLUGIN_FOLDER, "plugins");
list.add(plugin);
String appHome = Utils.getenv("APP_HOME");
if (appHome != null) {
Path path = Paths.get(appHome, "plugins");
if (!Files.isSameFile(path, plugin)) {
list.add(path);
}
}
return list;
}

/**
Expand Down

0 comments on commit 605df37

Please sign in to comment.