You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by GitBox <gi...@apache.org> on 2022/01/23 02:45:56 UTC

[GitHub] [solr] sonatype-lift[bot] commented on a change in pull request #557: SOLR-15914 Make it super simple to add a contrib module to shared classpath

sonatype-lift[bot] commented on a change in pull request #557:
URL: https://github.com/apache/solr/pull/557#discussion_r790206940



##########
File path: solr/core/src/java/org/apache/solr/util/ModuleUtils.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.solr.util;
+
+import org.apache.solr.common.StringUtils;
+import org.apache.solr.common.util.StrUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * Parses the list of modules the user has requested in solr.xml, property solr.modules or environment SOLR_MODULES.
+ * Then resolves the lib folder for each, so they can be added to class path.
+ */
+public class ModuleUtils {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final String MODULES_FOLDER_NAME = "contrib"; // TODO Change to "modules"
+  private static final Pattern validModNamesPattern = Pattern.compile("[\\w\\d-_]+");
+
+  /**
+   * Returns a path to a module's lib folder
+   * @param moduleName name of module
+   * @return the path to the module's lib folder
+   */
+  public static Path getModuleLibPath(Path solrInstallDirPath, String moduleName) {
+    return getModulesPath(solrInstallDirPath).resolve(moduleName).resolve("lib");
+  }
+
+  /**
+   * Finds list of module names requested by system property or environment variable
+   * @return set of raw volume names from sysprop and/or env.var
+   */
+  static Set<String> resolveFromSyspropOrEnv() {
+    // Fall back to sysprop and env.var if nothing configured through solr.xml
+    Set<String> mods = new HashSet<>();
+    String modulesFromProps = System.getProperty("solr.modules");
+    if (!StringUtils.isEmpty(modulesFromProps)) {
+      mods.addAll(StrUtils.splitSmart(modulesFromProps, ',', true));
+    }
+    String modulesFromEnv = System.getenv("SOLR_MODULES");
+    if (!StringUtils.isEmpty(modulesFromEnv)) {
+      mods.addAll(StrUtils.splitSmart(modulesFromEnv, ',', true));
+    }
+    return mods.stream().map(String::trim).collect(Collectors.toSet());
+  }
+
+  /**
+   * Returns true if a module name is valid and exists in the system
+   */
+  public static boolean moduleExists(Path solrInstallDirPath, String moduleName) {
+    if (!isValidName(moduleName)) return false;
+    Path modPath = getModulesPath(solrInstallDirPath).resolve(moduleName);
+    return Files.isDirectory(modPath);
+  }
+
+  /**
+   * Returns nam of all existing modules
+   */
+  public static Set<String> listAvailableModules(Path solrInstallDirPath) {
+    try {
+      return Files.list(getModulesPath(solrInstallDirPath)).filter(Files::isDirectory)

Review comment:
       *StreamResourceLeak:*  Streams that encapsulate a closeable resource should be closed using try-with-resources [(details)](https://errorprone.info/bugpattern/StreamResourceLeak)
   (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`)

##########
File path: solr/core/src/java/org/apache/solr/core/NodeConfig.java
##########
@@ -360,6 +381,86 @@ public String getDefaultZkHost() {
     return allowUrls;
   }
 
+  // Configures SOLR_HOME/lib to the shared class loader
+  private void setupSharedLib() {
+    // Always add $SOLR_HOME/lib to the shared resource loader
+    Set<String> libDirs = new LinkedHashSet<>();
+    libDirs.add("lib");
+
+    // Always add $SOLR_TIP/lib to the shared resource loader, to allow loading of i.e. /opt/solr/lib/foo.jar
+    if (getSolrInstallDir() != null) {
+      libDirs.add(getSolrInstallDir().resolve("lib").toAbsolutePath().normalize().toString());
+    }
+
+    if (!StringUtils.isBlank(getSharedLibDirectory())) {
+      List<String> sharedLibs = Arrays.asList(getSharedLibDirectory().split("\\s*,\\s*"));
+      libDirs.addAll(sharedLibs);
+    }
+
+    addFoldersToSharedLib(libDirs);
+  }
+
+  /**
+   * Returns the modules as configured in solr.xml. Comma separated list. May be null if not defined
+   */
+  public String getModules() {
+    return modules;
+  }
+
+  // Finds every jar in each folder and adds it to shardLib, then reloads Lucene SPI
+  private void addFoldersToSharedLib(Set<String> libDirs) {
+    boolean modified = false;
+    // add the sharedLib to the shared resource loader before initializing cfg based plugins
+    for (String libDir : libDirs) {
+      Path libPath = getSolrHome().resolve(libDir);
+      if (Files.exists(libPath)) {
+        try {
+          loader.addToClassLoader(SolrResourceLoader.getURLs(libPath));
+          modified = true;
+        } catch (IOException e) {
+          throw new SolrException(ErrorCode.SERVER_ERROR, "Couldn't load libs: " + e, e);
+        }
+      }
+    }
+    if (modified) {
+      loader.reloadLuceneSPI();
+    }
+  }
+
+  // Adds modules to shared classpath
+  private void initModules() {
+    Set<String> moduleNames = ModuleUtils.resolveModulesFromStringOrSyspropOrEnv(getModules());
+    boolean modified = false;
+    for (String m : moduleNames) {
+      if (!ModuleUtils.moduleExists(getSolrInstallDir(), m)) {
+        log.error("No module with name {}, available modules are {}", m, ModuleUtils.listAvailableModules(getSolrInstallDir()));
+        // Fail-fast if user requests a non-existing module
+        throw new SolrException(ErrorCode.SERVER_ERROR, "No module with name " + m);
+      }
+      Path moduleLibPath = ModuleUtils.getModuleLibPath(getSolrInstallDir(), m);

Review comment:
       *NULL_DEREFERENCE:*  object returned by `getSolrInstallDir()` could be null and is dereferenced by call to `getModuleLibPath(...)` at line 440.
   (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`)

##########
File path: solr/core/src/java/org/apache/solr/core/NodeConfig.java
##########
@@ -360,6 +381,86 @@ public String getDefaultZkHost() {
     return allowUrls;
   }
 
+  // Configures SOLR_HOME/lib to the shared class loader
+  private void setupSharedLib() {
+    // Always add $SOLR_HOME/lib to the shared resource loader
+    Set<String> libDirs = new LinkedHashSet<>();
+    libDirs.add("lib");
+
+    // Always add $SOLR_TIP/lib to the shared resource loader, to allow loading of i.e. /opt/solr/lib/foo.jar
+    if (getSolrInstallDir() != null) {
+      libDirs.add(getSolrInstallDir().resolve("lib").toAbsolutePath().normalize().toString());
+    }
+
+    if (!StringUtils.isBlank(getSharedLibDirectory())) {
+      List<String> sharedLibs = Arrays.asList(getSharedLibDirectory().split("\\s*,\\s*"));
+      libDirs.addAll(sharedLibs);
+    }
+
+    addFoldersToSharedLib(libDirs);
+  }
+
+  /**
+   * Returns the modules as configured in solr.xml. Comma separated list. May be null if not defined
+   */
+  public String getModules() {
+    return modules;
+  }
+
+  // Finds every jar in each folder and adds it to shardLib, then reloads Lucene SPI
+  private void addFoldersToSharedLib(Set<String> libDirs) {
+    boolean modified = false;
+    // add the sharedLib to the shared resource loader before initializing cfg based plugins
+    for (String libDir : libDirs) {
+      Path libPath = getSolrHome().resolve(libDir);
+      if (Files.exists(libPath)) {
+        try {
+          loader.addToClassLoader(SolrResourceLoader.getURLs(libPath));
+          modified = true;
+        } catch (IOException e) {
+          throw new SolrException(ErrorCode.SERVER_ERROR, "Couldn't load libs: " + e, e);
+        }
+      }
+    }
+    if (modified) {
+      loader.reloadLuceneSPI();
+    }
+  }
+
+  // Adds modules to shared classpath
+  private void initModules() {
+    Set<String> moduleNames = ModuleUtils.resolveModulesFromStringOrSyspropOrEnv(getModules());
+    boolean modified = false;
+    for (String m : moduleNames) {
+      if (!ModuleUtils.moduleExists(getSolrInstallDir(), m)) {
+        log.error("No module with name {}, available modules are {}", m, ModuleUtils.listAvailableModules(getSolrInstallDir()));

Review comment:
       *NULL_DEREFERENCE:*  object returned by `getSolrInstallDir()` could be null and is dereferenced by call to `listAvailableModules(...)` at line 436.
   (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`)

##########
File path: solr/core/src/java/org/apache/solr/core/NodeConfig.java
##########
@@ -360,6 +381,86 @@ public String getDefaultZkHost() {
     return allowUrls;
   }
 
+  // Configures SOLR_HOME/lib to the shared class loader
+  private void setupSharedLib() {
+    // Always add $SOLR_HOME/lib to the shared resource loader
+    Set<String> libDirs = new LinkedHashSet<>();
+    libDirs.add("lib");
+
+    // Always add $SOLR_TIP/lib to the shared resource loader, to allow loading of i.e. /opt/solr/lib/foo.jar
+    if (getSolrInstallDir() != null) {
+      libDirs.add(getSolrInstallDir().resolve("lib").toAbsolutePath().normalize().toString());
+    }
+
+    if (!StringUtils.isBlank(getSharedLibDirectory())) {
+      List<String> sharedLibs = Arrays.asList(getSharedLibDirectory().split("\\s*,\\s*"));
+      libDirs.addAll(sharedLibs);
+    }
+
+    addFoldersToSharedLib(libDirs);
+  }
+
+  /**
+   * Returns the modules as configured in solr.xml. Comma separated list. May be null if not defined
+   */
+  public String getModules() {
+    return modules;
+  }
+
+  // Finds every jar in each folder and adds it to shardLib, then reloads Lucene SPI
+  private void addFoldersToSharedLib(Set<String> libDirs) {
+    boolean modified = false;
+    // add the sharedLib to the shared resource loader before initializing cfg based plugins
+    for (String libDir : libDirs) {
+      Path libPath = getSolrHome().resolve(libDir);
+      if (Files.exists(libPath)) {
+        try {
+          loader.addToClassLoader(SolrResourceLoader.getURLs(libPath));
+          modified = true;
+        } catch (IOException e) {
+          throw new SolrException(ErrorCode.SERVER_ERROR, "Couldn't load libs: " + e, e);
+        }
+      }
+    }
+    if (modified) {
+      loader.reloadLuceneSPI();
+    }
+  }
+
+  // Adds modules to shared classpath
+  private void initModules() {
+    Set<String> moduleNames = ModuleUtils.resolveModulesFromStringOrSyspropOrEnv(getModules());
+    boolean modified = false;
+    for (String m : moduleNames) {
+      if (!ModuleUtils.moduleExists(getSolrInstallDir(), m)) {

Review comment:
       *NULL_DEREFERENCE:*  object returned by `getSolrInstallDir()` could be null and is dereferenced by call to `moduleExists(...)` at line 435.
   (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org