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
This commit is contained in:
Alan Woodward 2015-11-12 09:43:26 +00:00
parent 1161f2d018
commit eb0da10bb4
9 changed files with 68 additions and 73 deletions

View File

@ -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

View File

@ -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<String, IndexSchema> 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<IndexSchema>() {
@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);

View File

@ -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();
}
/**

View File

@ -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;
}

View File

@ -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, "<solr><str name=\"configSetBaseDir\">configsets</str></solr>");
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, "<solr><str name=\"configSetBaseDir\">/path/to/configsets</str></solr>");
assertThat(absConfig.getConfigSetBaseDirectory(), is(Paths.get("/path/to/configsets")));
}
}

View File

@ -460,9 +460,9 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 {
SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath());
NodeConfig config = SolrXmlConfig.fromString(loader, "<solr><str name=\"coreRootDirectory\">relative</str></solr>");
assertThat(config.getCoreRootDirectory(), containsString(solrHomeDirectory.getAbsolutePath()));
assertThat(config.getCoreRootDirectory().toString(), containsString(solrHomeDirectory.getAbsolutePath()));
NodeConfig absConfig = SolrXmlConfig.fromString(loader, "<solr><str name=\"coreRootDirectory\">/absolute</str></solr>");
assertThat(absConfig.getCoreRootDirectory(), not(containsString(solrHomeDirectory.getAbsolutePath())));
assertThat(absConfig.getCoreRootDirectory().toString(), not(containsString(solrHomeDirectory.getAbsolutePath())));
}
}

View File

@ -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.

View File

@ -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());
}

View File

@ -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()));
}
/**