SOLR-5317: Fix race when persisting cores in solr.xml

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1531163 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2013-10-11 01:37:24 +00:00
parent d6bff5951c
commit 4a894a8882
2 changed files with 31 additions and 6 deletions

View File

@ -20,6 +20,7 @@ package org.apache.solr.core;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,9 +32,11 @@ import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -142,23 +145,33 @@ public class SolrXMLCoresLocator implements CoresLocator {
}
@Override
public final void persist(CoreContainer cc, CoreDescriptor... coreDescriptors) {
public synchronized final void persist(CoreContainer cc, CoreDescriptor... coreDescriptors) {
List<CoreDescriptor> cds = new ArrayList<CoreDescriptor>(cc.getCoreDescriptors().size() + coreDescriptors.length);
System.err.println("Persist existing cores:" + cc.getCoreDescriptors());
System.err.println("Persist new cores:" + cc.getCoreDescriptors());
cds.addAll(cc.getCoreDescriptors());
cds.addAll(Arrays.asList(coreDescriptors));
doPersist(buildSolrXML(cds));
}
protected synchronized void doPersist(String xml) {
protected void doPersist(String xml) {
File file = new File(cfg.config.getResourceLoader().getInstanceDir(), ConfigSolr.SOLR_XML_FILE);
Writer writer = null;
FileOutputStream fos = null;
try {
Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
fos = new FileOutputStream(file);
writer = new OutputStreamWriter(fos, Charsets.UTF_8);
writer.write(xml);
writer.close();
logger.info("Persisted core descriptions to {}", file.getAbsolutePath());
}
catch (IOException e) {
logger.error("Couldn't persist core descriptions to {} : {}", file.getAbsolutePath(), e);
} catch (IOException e) {
logger.error("Couldn't persist core descriptions to {} : {}",
file.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(fos);
}
}

View File

@ -19,6 +19,7 @@ package org.apache.solr.handler.admin;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.DirectoryReader;
@ -48,6 +49,7 @@ import org.apache.solr.core.CoreDescriptor;
import org.apache.solr.core.DirectoryFactory;
import org.apache.solr.core.DirectoryFactory.DirContext;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrXMLCoresLocator;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
@ -486,9 +488,19 @@ public class CoreAdminHandler extends RequestHandlerBase {
if (coreContainer.getZkController() != null) {
coreContainer.preRegisterInZk(dcore);
}
// make sure we can write out the descriptor first
coreContainer.getCoresLocator().create(coreContainer, dcore);
SolrCore core = coreContainer.create(dcore);
coreContainer.register(dcore.getName(), core, false);
if (coreContainer.getCoresLocator() instanceof SolrXMLCoresLocator) {
// hack - in this case we persist once more because a core create race might
// have dropped entries.
coreContainer.getCoresLocator().create(coreContainer);
}
rsp.add("core", core.getName());
}
catch (Exception ex) {