Skip to content
Snippets Groups Projects
Commit 0561567f authored by Thomas Mortagne's avatar Thomas Mortagne
Browse files

XWIKI-20161: Warnings in console related to SLF4J and SolrConfig when upgrading XWiki to 14.8 RC1

(cherry picked from commit 95860d01)
parent 6756ea5b
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
...@@ -78,6 +80,8 @@ public class EmbeddedSolr extends AbstractSolr implements Disposable, Initializa ...@@ -78,6 +80,8 @@ public class EmbeddedSolr extends AbstractSolr implements Disposable, Initializa
*/ */
public static final String TYPE = "embedded"; public static final String TYPE = "embedded";
private final String SOLRCONFIG_PATH = "conf/solrconfig.xml";
/** /**
* Solr configuration. * Solr configuration.
*/ */
...@@ -106,6 +110,8 @@ public void initialize() throws InitializationException ...@@ -106,6 +110,8 @@ public void initialize() throws InitializationException
// Create the Solr home if it does not already exist // Create the Solr home if it does not already exist
if (!Files.exists(this.solrHomePath)) { if (!Files.exists(this.solrHomePath)) {
createHomeDirectory(); createHomeDirectory();
} else {
updateHomeDirectory();
} }
// Validate and create the search core // Validate and create the search core
...@@ -176,7 +182,8 @@ protected SolrClient createCore(SolrCoreInitializer initializer) throws SolrExce ...@@ -176,7 +182,8 @@ protected SolrClient createCore(SolrCoreInitializer initializer) throws SolrExce
// Indicate the path of the data // Indicate the path of the data
if (initializer.isCache()) { if (initializer.isCache()) {
parameters.put(CoreDescriptor.CORE_DATADIR, getCacheCoreDataDir(corePath, initializer.getCoreName()).toString()); parameters.put(CoreDescriptor.CORE_DATADIR,
getCacheCoreDataDir(corePath, initializer.getCoreName()).toString());
} }
// Don't load the core on startup to workaround a possible dead lock during Solr init // Don't load the core on startup to workaround a possible dead lock during Solr init
...@@ -198,7 +205,7 @@ private Path prepareCore(SolrCoreInitializer initializer) throws IOException ...@@ -198,7 +205,7 @@ private Path prepareCore(SolrCoreInitializer initializer) throws IOException
// Copy configuration // Copy configuration
try (InputStream stream = this.solrConfiguration.getMinimalCoreDefaultContent()) { try (InputStream stream = this.solrConfiguration.getMinimalCoreDefaultContent()) {
copyCoreConfiguration(stream, corePath, true); copyCoreConfiguration(stream, corePath, true, null);
} }
return corePath; return corePath;
...@@ -258,7 +265,7 @@ private boolean isSearchCoreValid() ...@@ -258,7 +265,7 @@ private boolean isSearchCoreValid()
} }
// Check solrconfig.xml // Check solrconfig.xml
File solrconfigFile = this.solrSearchCorePath.resolve("conf/solrconfig.xml").toFile(); File solrconfigFile = this.solrSearchCorePath.resolve(SOLRCONFIG_PATH).toFile();
return solrconfigFile.exists() && isExpectedSolrVersion(solrconfigFile); return solrconfigFile.exists() && isExpectedSolrVersion(solrconfigFile);
} }
...@@ -280,6 +287,17 @@ private void recreateSearchCore() throws IOException ...@@ -280,6 +287,17 @@ private void recreateSearchCore() throws IOException
createSearchCore(); createSearchCore();
} }
private void writeHomeConfiguration() throws IOException
{
StringBuilder builder = new StringBuilder();
builder.append("<solr>");
// Disable the log watcher until Solr support SLF4J 2
// FIXME: remove when Solr upgrade SLF4J (or stop logging a stack trace at least)
builder.append("<logging><str name=\"enabled\">false</str></logging>");
builder.append("</solr>");
FileUtils.write(this.solrHomePath.resolve("solr.xml").toFile(), builder.toString(), StandardCharsets.UTF_8);
}
private void createHomeDirectory() throws IOException private void createHomeDirectory() throws IOException
{ {
// Initialize the Solr Home with the default configuration files if the folder does not already exist. // Initialize the Solr Home with the default configuration files if the folder does not already exist.
...@@ -290,14 +308,8 @@ private void createHomeDirectory() throws IOException ...@@ -290,14 +308,8 @@ private void createHomeDirectory() throws IOException
// Create the home directory // Create the home directory
Files.createDirectories(this.solrHomePath); Files.createDirectories(this.solrHomePath);
// Copy the default solr.xml configuration file // Write the default solr.xml configuration file
StringBuilder builder = new StringBuilder(); writeHomeConfiguration();
builder.append("<solr>");
// Disable the log watcher until Solr support SLF4J 2
// FIXME: remove when Solr upgrade SLF4J (or stop logging a stack trace at least)
builder.append("<logging><str name=\"enabled\">false</str></logging>");
builder.append("</solr>");
FileUtils.write(this.solrHomePath.resolve("solr.xml").toFile(), builder.toString(), StandardCharsets.UTF_8);
// [RETRO COMPATIBILITY for < 12.3] // [RETRO COMPATIBILITY for < 12.3]
// Check if the solr home is not already at the old location (/solr) and move things // Check if the solr home is not already at the old location (/solr) and move things
...@@ -314,15 +326,51 @@ private void createHomeDirectory() throws IOException ...@@ -314,15 +326,51 @@ private void createHomeDirectory() throws IOException
} }
} }
private void copyCoreConfiguration(InputStream stream, Path corePath, boolean skipCoreProperties) throws IOException private void updateHomeDirectory() throws IOException
{
// Make sure the Solr Home contains expected configuration
this.logger.info("Updating Solr home directory at [{}]", this.solrHomePath);
// Reset the solr.xml configuration file
writeHomeConfiguration();
// Make sure cores have the expected configuration
try (Stream<Path> stream = Files.list(this.solrHomePath)) {
stream.filter(Files::isDirectory).forEach(this::updateCore);
}
}
private void updateCore(Path corePath)
{
try {
if (this.componentManager.hasComponent(SolrCoreInitializer.class, corePath.getFileName().toString())) {
Path solrconfig = corePath.resolve(SOLRCONFIG_PATH);
// If Solr was upgraded, reset the solrconfig.xml
if (Files.exists(solrconfig) && !isExpectedSolrVersion(solrconfig.toFile())) {
// Reset solr configuration
try (InputStream stream = this.solrConfiguration.getMinimalCoreDefaultContent()) {
copyCoreConfiguration(stream, corePath, true, Set.of(SOLRCONFIG_PATH));
}
}
}
} catch (Exception e) {
this.logger.error("Failed to update Solr core located at [{}]", corePath, e);
}
}
private void copyCoreConfiguration(InputStream stream, Path corePath, boolean skipCoreProperties, Set<String> force)
throws IOException
{ {
try (ZipInputStream zstream = new ZipInputStream(stream)) { try (ZipInputStream zstream = new ZipInputStream(stream)) {
for (ZipEntry entry = zstream.getNextEntry(); entry != null; entry = zstream.getNextEntry()) { for (ZipEntry entry = zstream.getNextEntry(); entry != null; entry = zstream.getNextEntry()) {
Path targetPath = corePath.resolve(entry.getName()); Path targetPath = corePath.resolve(entry.getName());
if (entry.isDirectory()) { if (entry.isDirectory()) {
Files.createDirectories(targetPath); Files.createDirectories(targetPath);
} else if (!skipCoreProperties || !entry.getName().equals("core.properties")) { } else if ((force != null && force.contains(entry.getName())) || (!Files.exists(targetPath)
FileUtils.copyInputStreamToFile(new CloseShieldInputStream(zstream), targetPath.toFile()); && (!skipCoreProperties || !entry.getName().equals("core.properties")))) {
FileUtils.copyInputStreamToFile(CloseShieldInputStream.wrap(zstream), targetPath.toFile());
} }
} }
} }
...@@ -331,7 +379,8 @@ private void copyCoreConfiguration(InputStream stream, Path corePath, boolean sk ...@@ -331,7 +379,8 @@ private void copyCoreConfiguration(InputStream stream, Path corePath, boolean sk
private void createSearchCore() throws IOException private void createSearchCore() throws IOException
{ {
// Copy configuration // Copy configuration
copyCoreConfiguration(this.solrConfiguration.getSearchCoreDefaultContent(), this.solrSearchCorePath, false); copyCoreConfiguration(this.solrConfiguration.getSearchCoreDefaultContent(), this.solrSearchCorePath, false,
null);
// Indicate the path of the data // Indicate the path of the data
createCacheCore(this.solrSearchCorePath, SolrClientInstance.CORE_NAME); createCacheCore(this.solrSearchCorePath, SolrClientInstance.CORE_NAME);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment