From eb0da10bb4d07015f1b4a7efbe533f2fc249185a Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Thu, 12 Nov 2015 09:43:26 +0000 Subject: [PATCH] SOLR-8278: Use NIO2 APIs in ConfigSetService git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1713996 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + .../apache/solr/core/ConfigSetService.java | 64 ++++++++----------- .../org/apache/solr/core/CoreContainer.java | 7 +- .../java/org/apache/solr/core/NodeConfig.java | 27 ++++---- .../org/apache/solr/core/TestConfigSets.java | 19 +++--- .../apache/solr/core/TestCoreDiscovery.java | 4 +- .../org/apache/solr/core/TestLazyCores.java | 3 +- .../org/apache/solr/core/TestSolrXml.java | 12 ++-- .../org/apache/solr/util/TestHarness.java | 3 +- 9 files changed, 68 insertions(+), 73 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index c5640a408d5..22bb0c9d8cf 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -498,6 +498,8 @@ Other Changes * SOLR-8259: Deprecate JettySolrRunner.getDispatchFilter(), add .getSolrDispatchFilter() and .getCoreContainer() (Alan Woodward) +* SOLR-8278: Use NIO2 APIs in ConfigSetService (Alan Woodward) + ================== 5.3.1 ================== Bug Fixes diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java index 6e1cbac2edb..1120ea52eb0 100644 --- a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java +++ b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java @@ -17,6 +17,14 @@ package org.apache.solr.core; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Locale; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; + import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import org.apache.solr.cloud.CloudConfigSetService; @@ -30,11 +38,6 @@ import org.joda.time.format.DateTimeFormatter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.util.Locale; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; - /** * Service class used by the CoreContainer to load ConfigSets for use in SolrCore * creation. @@ -138,34 +141,22 @@ public abstract class ConfigSetService { */ public static class Default extends ConfigSetService { - private final File configSetBase; + private final Path configSetBase; /** * Create a new ConfigSetService.Default * @param loader the CoreContainer's resource loader * @param configSetBase the base directory under which to look for config set directories */ - public Default(SolrResourceLoader loader, String configSetBase) { + public Default(SolrResourceLoader loader, Path configSetBase) { super(loader); - this.configSetBase = resolveBaseDirectory(loader, configSetBase); - } - - private File resolveBaseDirectory(SolrResourceLoader loader, String configSetBase) { - File csBase = new File(configSetBase); - if (!csBase.isAbsolute()) - csBase = new File(loader.getInstanceDir(), configSetBase); - return csBase; - } - - // for testing - File getConfigSetBase() { - return this.configSetBase; + this.configSetBase = configSetBase; } @Override public SolrResourceLoader createCoreResourceLoader(CoreDescriptor cd) { - String instanceDir = locateInstanceDir(cd); - return new SolrResourceLoader(instanceDir, parentLoader.getClassLoader(), cd.getSubstitutableProperties()); + Path instanceDir = locateInstanceDir(cd); + return new SolrResourceLoader(instanceDir.toString(), parentLoader.getClassLoader(), cd.getSubstitutableProperties()); } @Override @@ -173,15 +164,15 @@ public abstract class ConfigSetService { return (cd.getConfigSet() == null ? "instancedir " : "configset ") + locateInstanceDir(cd); } - protected String locateInstanceDir(CoreDescriptor cd) { + protected Path locateInstanceDir(CoreDescriptor cd) { String configSet = cd.getConfigSet(); if (configSet == null) - return cd.getInstanceDir(); - File configSetDirectory = new File(configSetBase, configSet); - if (!configSetDirectory.exists() || !configSetDirectory.isDirectory()) + return Paths.get(cd.getInstanceDir()); + Path configSetDirectory = configSetBase.resolve(configSet); + if (!Files.isDirectory(configSetDirectory)) throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, - "Could not load configuration from directory " + configSetDirectory.getAbsolutePath()); - return configSetDirectory.getAbsolutePath(); + "Could not load configuration from directory " + configSetDirectory); + return configSetDirectory; } } @@ -195,25 +186,23 @@ public abstract class ConfigSetService { private final Cache schemaCache = CacheBuilder.newBuilder().build(); - public SchemaCaching(SolrResourceLoader loader, String configSetBase) { + public SchemaCaching(SolrResourceLoader loader, Path configSetBase) { super(loader, configSetBase); } public static final DateTimeFormatter cacheKeyFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmss"); - public static String cacheName(File schemaFile) { + public static String cacheName(Path schemaFile) throws IOException { + long lastModified = Files.getLastModifiedTime(schemaFile).toMillis(); return String.format(Locale.ROOT, "%s:%s", - schemaFile.getAbsolutePath(), cacheKeyFormatter.print(schemaFile.lastModified())); + schemaFile.toString(), cacheKeyFormatter.print(lastModified)); } @Override public IndexSchema createIndexSchema(final CoreDescriptor cd, final SolrConfig solrConfig) { final String resourceNameToBeUsed = IndexSchemaFactory.getResourceNameToBeUsed(cd.getSchemaName(), solrConfig); - File schemaFile = new File(resourceNameToBeUsed); - if (!schemaFile.isAbsolute()) { - schemaFile = new File(solrConfig.getResourceLoader().getConfigDir(), schemaFile.getPath()); - } - if (schemaFile.exists()) { + Path schemaFile = Paths.get(solrConfig.getResourceLoader().getConfigDir()).resolve(resourceNameToBeUsed); + if (Files.exists(schemaFile)) { try { return schemaCache.get(cacheName(schemaFile), new Callable() { @Override @@ -225,6 +214,9 @@ public abstract class ConfigSetService { } catch (ExecutionException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error creating index schema for core " + cd.getName(), e); + } catch (IOException e) { + logger.warn("Couldn't get last modified time for schema file {}: {}", schemaFile, e.getMessage()); + logger.warn("Will not use schema cache"); } } return IndexSchemaFactory.buildIndexSchema(cd.getSchemaName(), solrConfig); diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index 8e1bb56b38c..bae252e5345 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -19,7 +19,6 @@ package org.apache.solr.core; import java.io.File; import java.io.IOException; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -204,11 +203,11 @@ public class CoreContainer { } public CoreContainer(NodeConfig config, Properties properties) { - this(config, properties, new CorePropertiesLocator(Paths.get(config.getCoreRootDirectory()))); + this(config, properties, new CorePropertiesLocator(config.getCoreRootDirectory())); } public CoreContainer(NodeConfig config, Properties properties, boolean asyncSolrCoreLoad) { - this(config, properties, new CorePropertiesLocator(Paths.get(config.getCoreRootDirectory())), asyncSolrCoreLoad); + this(config, properties, new CorePropertiesLocator(config.getCoreRootDirectory()), asyncSolrCoreLoad); } public CoreContainer(NodeConfig config, Properties properties, CoresLocator locator) { @@ -955,7 +954,7 @@ public class CoreContainer { } public String getCoreRootDirectory() { - return cfg.getCoreRootDirectory(); + return cfg.getCoreRootDirectory().toString(); } /** diff --git a/solr/core/src/java/org/apache/solr/core/NodeConfig.java b/solr/core/src/java/org/apache/solr/core/NodeConfig.java index b5f114ea3ce..cca24758103 100644 --- a/solr/core/src/java/org/apache/solr/core/NodeConfig.java +++ b/solr/core/src/java/org/apache/solr/core/NodeConfig.java @@ -17,20 +17,22 @@ package org.apache.solr.core; * limitations under the License. */ +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; + import org.apache.solr.common.SolrException; import org.apache.solr.logging.LogWatcherConfig; import org.apache.solr.update.UpdateShardHandlerConfig; -import java.util.Properties; - public class NodeConfig { private final String nodeName; - private final String coreRootDirectory; + private final Path coreRootDirectory; - private final String configSetBaseDirectory; + private final Path configSetBaseDirectory; private final String sharedLibDirectory; @@ -58,7 +60,7 @@ public class NodeConfig { private final String managementPath; - private NodeConfig(String nodeName, String coreRootDirectory, String configSetBaseDirectory, String sharedLibDirectory, + private NodeConfig(String nodeName, Path coreRootDirectory, Path configSetBaseDirectory, String sharedLibDirectory, PluginInfo shardHandlerFactoryConfig, UpdateShardHandlerConfig updateShardHandlerConfig, String coreAdminHandlerClass, String collectionsAdminHandlerClass, String infoHandlerClass, String configSetsHandlerClass, @@ -94,7 +96,7 @@ public class NodeConfig { return nodeName; } - public String getCoreRootDirectory() { + public Path getCoreRootDirectory() { return coreRootDirectory; } @@ -158,7 +160,7 @@ public class NodeConfig { return managementPath; } - public String getConfigSetBaseDirectory() { + public Path getConfigSetBaseDirectory() { return configSetBaseDirectory; } @@ -187,8 +189,8 @@ public class NodeConfig { public static class NodeConfigBuilder { - private String coreRootDirectory = ""; - private String configSetBaseDirectory = "configsets"; + private Path coreRootDirectory; + private Path configSetBaseDirectory; private String sharedLibDirectory = "lib"; private PluginInfo shardHandlerFactoryConfig; private UpdateShardHandlerConfig updateShardHandlerConfig = UpdateShardHandlerConfig.DEFAULT; @@ -219,16 +221,17 @@ public class NodeConfig { public NodeConfigBuilder(String nodeName, SolrResourceLoader loader) { this.nodeName = nodeName; this.loader = loader; - this.coreRootDirectory = loader.getInstanceDir(); + this.coreRootDirectory = Paths.get(loader.getInstanceDir()); + this.configSetBaseDirectory = Paths.get(loader.getInstanceDir()).resolve("configsets"); } public NodeConfigBuilder setCoreRootDirectory(String coreRootDirectory) { - this.coreRootDirectory = loader.resolve(coreRootDirectory); + this.coreRootDirectory = Paths.get(loader.getInstanceDir()).resolve(coreRootDirectory); return this; } public NodeConfigBuilder setConfigSetBaseDirectory(String configSetBaseDirectory) { - this.configSetBaseDirectory = configSetBaseDirectory; + this.configSetBaseDirectory = Paths.get(loader.resolve(configSetBaseDirectory)); return this; } diff --git a/solr/core/src/test/org/apache/solr/core/TestConfigSets.java b/solr/core/src/test/org/apache/solr/core/TestConfigSets.java index 15257bf0550..4af7482bb68 100644 --- a/solr/core/src/test/org/apache/solr/core/TestConfigSets.java +++ b/solr/core/src/test/org/apache/solr/core/TestConfigSets.java @@ -17,6 +17,10 @@ package org.apache.solr.core; * limitations under the License. */ +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.commons.io.FileUtils; import org.apache.solr.SolrTestCaseJ4; @@ -25,9 +29,6 @@ import org.junit.Test; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; -import java.io.File; -import java.io.IOException; - import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.core.Is.is; @@ -56,13 +57,13 @@ public class TestConfigSets extends SolrTestCaseJ4 { public void testDefaultConfigSetBasePathResolution() throws IOException { try (SolrResourceLoader loader = new SolrResourceLoader(new File("/path/to/solr/home").getAbsolutePath())) { - ConfigSetService.Default relativeCSS = new ConfigSetService.Default(loader, "configsets"); - assertThat(relativeCSS.getConfigSetBase().getAbsoluteFile(), - is(new File("/path/to/solr/home/configsets").getAbsoluteFile())); + NodeConfig config + = SolrXmlConfig.fromString(loader, "configsets"); + assertThat(config.getConfigSetBaseDirectory(), is(Paths.get("/path/to/solr/home/configsets"))); - ConfigSetService.Default absoluteCSS = new ConfigSetService.Default(loader, new File("/path/to/configsets").getAbsolutePath()); - assertThat(absoluteCSS.getConfigSetBase().getAbsoluteFile(), - is(new File("/path/to/configsets").getAbsoluteFile())); + NodeConfig absConfig + = SolrXmlConfig.fromString(loader, "/path/to/configsets"); + assertThat(absConfig.getConfigSetBaseDirectory(), is(Paths.get("/path/to/configsets"))); } } diff --git a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java index 31f82c26287..e1d5e23fba7 100644 --- a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java +++ b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java @@ -460,9 +460,9 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 { SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath()); NodeConfig config = SolrXmlConfig.fromString(loader, "relative"); - assertThat(config.getCoreRootDirectory(), containsString(solrHomeDirectory.getAbsolutePath())); + assertThat(config.getCoreRootDirectory().toString(), containsString(solrHomeDirectory.getAbsolutePath())); NodeConfig absConfig = SolrXmlConfig.fromString(loader, "/absolute"); - assertThat(absConfig.getCoreRootDirectory(), not(containsString(solrHomeDirectory.getAbsolutePath()))); + assertThat(absConfig.getCoreRootDirectory().toString(), not(containsString(solrHomeDirectory.getAbsolutePath()))); } } diff --git a/solr/core/src/test/org/apache/solr/core/TestLazyCores.java b/solr/core/src/test/org/apache/solr/core/TestLazyCores.java index 745f4bd4d0f..1457c3823f0 100644 --- a/solr/core/src/test/org/apache/solr/core/TestLazyCores.java +++ b/solr/core/src/test/org/apache/solr/core/TestLazyCores.java @@ -19,7 +19,6 @@ package org.apache.solr.core; import java.io.File; import java.io.IOException; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -568,7 +567,7 @@ public class TestLazyCores extends SolrTestCaseJ4 { NodeConfig config = SolrXmlConfig.fromFile(loader, solrXml); // OK this should succeed, but at the end we should have recorded a series of errors. - return createCoreContainer(config, new CorePropertiesLocator(Paths.get(config.getCoreRootDirectory()))); + return createCoreContainer(config, new CorePropertiesLocator(config.getCoreRootDirectory())); } // We want to see that the core "heals itself" if an un-corrupted file is written to the directory. diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrXml.java b/solr/core/src/test/org/apache/solr/core/TestSolrXml.java index f23b13bea6c..377389cf16c 100644 --- a/solr/core/src/test/org/apache/solr/core/TestSolrXml.java +++ b/solr/core/src/test/org/apache/solr/core/TestSolrXml.java @@ -17,6 +17,10 @@ package org.apache.solr.core; * limitations under the License. */ +import java.io.File; +import java.io.IOException; +import java.util.Locale; + import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.commons.io.FileUtils; import org.apache.lucene.util.TestUtil; @@ -31,10 +35,6 @@ import org.junit.rules.ExpectedException; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; -import java.io.File; -import java.io.IOException; -import java.util.Locale; - import static org.junit.internal.matchers.StringContains.containsString; public class TestSolrXml extends SolrTestCaseJ4 { @@ -74,7 +74,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { assertEquals("info handler class", "testInfoHandler", cfg.getInfoHandlerClass()); assertEquals("config set handler class", "testConfigSetsHandler", cfg.getConfigSetsHandlerClass()); assertEquals("core load threads", 11, cfg.getCoreLoadThreadCount()); - assertThat("core root dir", cfg.getCoreRootDirectory(), containsString("testCoreRootDirectory")); + assertThat("core root dir", cfg.getCoreRootDirectory().toString(), containsString("testCoreRootDirectory")); assertEquals("distrib conn timeout", 22, cfg.getDistributedConnectionTimeout()); assertEquals("distrib conn timeout", 22, cfg.getUpdateShardHandlerConfig().getDistributedConnectionTimeout()); assertEquals("distrib socket timeout", 33, cfg.getDistributedSocketTimeout()); @@ -118,7 +118,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { FileUtils.copyFile(new File(testSrcRoot, "solr-50-all.xml"), new File(solrHome, "solr.xml")); NodeConfig cfg = SolrXmlConfig.fromSolrHome(loader, solrHome.getAbsolutePath()); - assertThat(cfg.getCoreRootDirectory(), containsString("myCoreRoot")); + assertThat(cfg.getCoreRootDirectory().toString(), containsString("myCoreRoot")); assertEquals("solr host port", 8888, cfg.getCloudConfig().getSolrHostPort()); assertEquals("schema cache", false, cfg.hasSchemaCache()); } diff --git a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java index 739039ee39e..5c889778554 100644 --- a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java +++ b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java @@ -20,7 +20,6 @@ package org.apache.solr.util; import java.io.File; import java.io.IOException; import java.io.StringWriter; -import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -154,7 +153,7 @@ public class TestHarness extends BaseTestHarness { } public TestHarness(NodeConfig nodeConfig) { - this(nodeConfig, new CorePropertiesLocator(Paths.get(nodeConfig.getCoreRootDirectory()))); + this(nodeConfig, new CorePropertiesLocator(nodeConfig.getCoreRootDirectory())); } /**