Skip to content

Commit

Permalink
SOLR-14197: SolrResourceLoader refactorings to reduce API
Browse files Browse the repository at this point in the history
* Deprecate SRL.listConfigDir (unused)
* Deprecate SRL.getDataDir
* Deprecate SRL.getCoreProperties
 XmlConfigFile needs to be passed in the substitutableProperties
 IndexSchema needs to be passed in the substitutableProperties
 Remove redundant Properties from CoreContainer constructors
* Remove SRL.newAdminHandlerInstance (unused)
* Deprecate SRL.openSchema and openConfig
* Avoid SRL.getConfigDir
 Also harmonized similar initialization logic between DIH Tika processor & ExtractingRequestHandler.
* Ensure SRL.addToClassLoader and reloadLuceneSPI are called at most once
 Don't auto-load "lib" in constructor; wrong place for this logic.
* Avoid SRL.getInstancePath
 Added SolrCore.getInstancePath instead
 Use CoreContainer.getSolrHome instead
 NodeConfig should track solrHome separate from SolrResourceLoader
* Simplify some SolrCore constructors
* Move locateSolrHome to new SolrPaths (Deprecate existing)
* Move "User Files" stuff to SolrPaths (Deprecate existing)

(cherry picked from commit 732348e)
  • Loading branch information
dsmiley committed Mar 11, 2020
1 parent d45ad81 commit 17ae79b
Show file tree
Hide file tree
Showing 59 changed files with 625 additions and 746 deletions.
3 changes: 2 additions & 1 deletion solr/CHANGES.txt
Expand Up @@ -26,7 +26,8 @@ Bug Fixes

Other Changes
---------------------
(No changes)
* SOLR-14197: SolrResourceLoader: marked many methods as deprecated, and in some cases rerouted exiting logic to avoid
them. (David Smiley)

================== 8.5.0 ==================

Expand Down
Expand Up @@ -82,22 +82,26 @@ public void init(Context context) {
@Override
protected void firstInit(Context context) {
super.firstInit(context);
// See similar code in ExtractingRequestHandler.inform
try {
String tikaConfigFile = context.getResolvedEntityAttribute("tikaConfig");
if (tikaConfigFile == null) {
String tikaConfigLoc = context.getResolvedEntityAttribute("tikaConfig");
if (tikaConfigLoc == null) {
ClassLoader classLoader = context.getSolrCore().getResourceLoader().getClassLoader();
try (InputStream is = classLoader.getResourceAsStream("solr-default-tika-config.xml")) {
tikaConfig = new TikaConfig(is);
}
} else {
File configFile = new File(tikaConfigFile);
if (!configFile.isAbsolute()) {
configFile = new File(context.getSolrCore().getResourceLoader().getConfigDir(), tikaConfigFile);
File configFile = new File(tikaConfigLoc);
if (configFile.isAbsolute()) {
tikaConfig = new TikaConfig(configFile);
} else { // in conf/
try (InputStream is = context.getSolrCore().getResourceLoader().openResource(tikaConfigLoc)) {
tikaConfig = new TikaConfig(is);
}
}
tikaConfig = new TikaConfig(configFile);
}
} catch (Exception e) {
wrapAndThrow (SEVERE, e,"Unable to load Tika Config");
wrapAndThrow(SEVERE, e,"Unable to load Tika Config");
}

String extractEmbeddedString = context.getResolvedEntityAttribute("extractEmbedded");
Expand Down
Expand Up @@ -113,8 +113,8 @@ private VariableResolver getVariableResolver() {
VariableResolver resolver = null;
String epoch = propWriter.convertDateToString(EPOCH);
if(dataImporter != null && dataImporter.getCore() != null
&& dataImporter.getCore().getResourceLoader().getCoreProperties() != null){
resolver = new VariableResolver(dataImporter.getCore().getResourceLoader().getCoreProperties());
&& dataImporter.getCore().getCoreDescriptor().getSubstitutableProperties() != null){
resolver = new VariableResolver(dataImporter.getCore().getCoreDescriptor().getSubstitutableProperties());
} else {
resolver = new VariableResolver();
}
Expand Down
Expand Up @@ -38,7 +38,7 @@
import org.apache.lucene.util.IOUtils;
import org.apache.solr.common.util.SuppressForbidden;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.core.SolrPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -126,7 +126,7 @@ protected void findDirectory(DataImporter dataImporter, Map<String, String> para
} else {
SolrCore core = dataImporter.getCore();
if (core == null) {
configDir = SolrResourceLoader.locateSolrHome().toString();
configDir = SolrPaths.locateSolrHome().toString();
} else {
configDir = core.getResourceLoader().getConfigDir();
}
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.apache.solr.handler.extraction;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.solr.common.SolrException;
Expand All @@ -32,8 +31,6 @@
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.util.plugin.SolrCoreAware;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.exception.TikaException;
import org.xml.sax.SAXException;

/**
* Handler for rich documents like PDF or Word or any other file format that Tika handles that need the text to be extracted
Expand Down Expand Up @@ -61,40 +58,34 @@ public void init(NamedList args) {

@Override
public void inform(SolrCore core) {
if (initArgs != null) {
//if relative,then relative to config dir, otherwise, absolute path
try {
String tikaConfigLoc = (String) initArgs.get(CONFIG_LOCATION);
if (tikaConfigLoc != null) {
File configFile = new File(tikaConfigLoc);
if (configFile.isAbsolute() == false) {
configFile = new File(core.getResourceLoader().getConfigDir(), configFile.getPath());
if (tikaConfigLoc == null) { // default
ClassLoader classLoader = core.getResourceLoader().getClassLoader();
try (InputStream is = classLoader.getResourceAsStream("solr-default-tika-config.xml")) {
config = new TikaConfig(is);
}
try {
} else {
File configFile = new File(tikaConfigLoc);
if (configFile.isAbsolute()) {
config = new TikaConfig(configFile);
} catch (Exception e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
} else { // in conf/
try (InputStream is = core.getResourceLoader().openResource(tikaConfigLoc)) {
config = new TikaConfig(is);
}
}
}

String parseContextConfigLoc = (String) initArgs.get(PARSE_CONTEXT_CONFIG);
if (parseContextConfigLoc != null) {
try {
parseContextConfig = new ParseContextConfig(core.getResourceLoader(), parseContextConfigLoc);
} catch (Exception e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
}
if (parseContextConfigLoc == null) { // default:
parseContextConfig = new ParseContextConfig();
} else {
parseContextConfig = new ParseContextConfig(core.getResourceLoader(), parseContextConfigLoc);
}
} catch (Exception e) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Unable to load Tika Config", e);
}
if (config == null) {
try (InputStream is = core.getResourceLoader().getClassLoader().getResourceAsStream("solr-default-tika-config.xml")){
config = new TikaConfig(is);
} catch (IOException | SAXException | TikaException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
}
}
if (parseContextConfig == null) {
parseContextConfig = new ParseContextConfig();
}

factory = createFactory();
}

Expand Down
Expand Up @@ -216,7 +216,7 @@ public static void main(String[] args) {

private static MetricsConfiguration loadMetricsConfiguration(Path configPath) {
try (SolrResourceLoader loader = new SolrResourceLoader(configPath.getParent())) {
XmlConfigFile config = new XmlConfigFile(loader, configPath.getFileName().toString());
XmlConfigFile config = new XmlConfigFile(loader, configPath.getFileName().toString(), null, null);
return MetricsConfiguration.from(config);
} catch (Exception e) {
log.error("Could not load scrape configuration from {}", configPath.toAbsolutePath());
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;

Expand All @@ -50,7 +51,6 @@
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.NodeConfig;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrXmlConfig;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.request.SolrRequestInfo;
Expand Down Expand Up @@ -92,7 +92,7 @@ public RequestWriter newRequestWriter() {
* @param defaultCoreName the core to route requests to by default (optional)
*/
public EmbeddedSolrServer(Path solrHome, String defaultCoreName) {
this(load(new CoreContainer(SolrXmlConfig.fromSolrHome(solrHome))), defaultCoreName);
this(load(new CoreContainer(solrHome, new Properties())), defaultCoreName);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions solr/core/src/java/org/apache/solr/cloud/ZkCLI.java
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -205,14 +206,14 @@ public static void main(String[] args) throws InterruptedException,
System.exit(1);
}

CoreContainer cc = new CoreContainer(solrHome);
CoreContainer cc = new CoreContainer(Paths.get(solrHome), new Properties());

if(!ZkController.checkChrootPath(zkServerAddress, true)) {
stdout.println("A chroot was specified in zkHost but the znode doesn't exist. ");
System.exit(1);
}

ZkController.bootstrapConf(zkClient, cc, solrHome);
ZkController.bootstrapConf(zkClient, cc);

// No need to close the CoreContainer, as it wasn't started
// up in the first place...
Expand Down
4 changes: 2 additions & 2 deletions solr/core/src/java/org/apache/solr/cloud/ZkController.java
Expand Up @@ -2022,14 +2022,14 @@ public static void linkConfSet(SolrZkClient zkClient, String collection, String
/**
* If in SolrCloud mode, upload config sets for each SolrCore in solr.xml.
*/
public static void bootstrapConf(SolrZkClient zkClient, CoreContainer cc, String solrHome) throws IOException {
public static void bootstrapConf(SolrZkClient zkClient, CoreContainer cc) throws IOException {

ZkConfigManager configManager = new ZkConfigManager(zkClient);

//List<String> allCoreNames = cfg.getAllCoreNames();
List<CoreDescriptor> cds = cc.getCoresLocator().discover(cc);

log.info("bootstrapping config for " + cds.size() + " cores into ZooKeeper using solr.xml from " + solrHome);
log.info("bootstrapping config for " + cds.size() + " cores into ZooKeeper using solr.xml from " + cc.getSolrHome());

for (CoreDescriptor cd : cds) {
String coreName = cd.getName();
Expand Down
Expand Up @@ -22,10 +22,8 @@
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.cloud.ZkConfigManager;
import org.apache.solr.common.cloud.ZooKeeperException;
Expand All @@ -49,12 +47,6 @@ public class ZkSolrResourceLoader extends SolrResourceLoader {

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

public ZkSolrResourceLoader(Path instanceDir, String configSet, ZkController zooKeeperController) {
super(instanceDir);
this.zkController = zooKeeperController;
configSetZkPath = ZkConfigManager.CONFIGS_ZKNODE + "/" + configSet;
}

/**
* <p>
* This loader will first attempt to load resources from ZooKeeper, but if not found
Expand All @@ -63,7 +55,7 @@ public ZkSolrResourceLoader(Path instanceDir, String configSet, ZkController zoo
* the "lib/" directory in the specified instance directory.
*/
public ZkSolrResourceLoader(Path instanceDir, String configSet, ClassLoader parent,
Properties coreProperties, ZkController zooKeeperController) {
Properties coreProperties, ZkController zooKeeperController) {
super(instanceDir, parent, coreProperties);
this.zkController = zooKeeperController;
configSetZkPath = ZkConfigManager.CONFIGS_ZKNODE + "/" + configSet;
Expand Down Expand Up @@ -152,25 +144,6 @@ public String getConfigDir() {
ErrorCode.SERVER_ERROR,
"ZkSolrResourceLoader does not support getConfigDir() - likely, what you are trying to do is not supported in ZooKeeper mode");
}

@Override
public String[] listConfigDir() {
List<String> list;
try {
list = zkController.getZkClient().getChildren(configSetZkPath, null, true);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
log.error("", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"", e);
} catch (KeeperException e) {
log.error("", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"", e);
}
return list.toArray(new String[0]);
}

public String getConfigSetZkPath() {
return configSetZkPath;
Expand Down
4 changes: 2 additions & 2 deletions solr/core/src/java/org/apache/solr/core/ConfigSetService.java
Expand Up @@ -108,7 +108,7 @@ public ConfigSetService(SolrResourceLoader loader, boolean shareSchema) {
* @return a SolrConfig object
*/
protected SolrConfig createSolrConfig(CoreDescriptor cd, SolrResourceLoader loader, boolean isTrusted) {
return SolrConfig.readFromResourceLoader(loader, cd.getConfigName(), isTrusted);
return SolrConfig.readFromResourceLoader(loader, cd.getConfigName(), isTrusted, cd.getSubstitutableProperties());
}

/**
Expand All @@ -126,10 +126,10 @@ protected IndexSchema createIndexSchema(CoreDescriptor cd, SolrConfig solrConfig
// want to pay the overhead of that at this juncture. If we guess wrong, no schema sharing.
// The fix is usually to name your schema managed-schema instead of schema.xml.
IndexSchemaFactory indexSchemaFactory = IndexSchemaFactory.newIndexSchemaFactory(solrConfig);
String guessSchemaName = indexSchemaFactory.getSchemaResourceName(cdSchemaName);

String configSet = cd.getConfigSet();
if (configSet != null && schemaCache != null) {
String guessSchemaName = indexSchemaFactory.getSchemaResourceName(cdSchemaName);
Long modVersion = getCurrentSchemaModificationVersion(configSet, solrConfig, guessSchemaName);
if (modVersion != null) {
// note: luceneMatchVersion influences the schema
Expand Down

0 comments on commit 17ae79b

Please sign in to comment.