Ensure an embedded Solr is available for Solr dump/restore operations

Otherwise, these operations triggered NullPointerException when only an
external Solr index is attached.
pull/137/head
luccioman 7 years ago
parent d92b191942
commit 0d34034f17

@ -68,17 +68,18 @@
#(dumprestore)#:: #(dumprestore)#::
<form action="IndexExport_p.html" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> <form action="IndexExport_p.html" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
<fieldset><legend>Dump and Restore of Solr Index</legend> <fieldset><legend>Dump and Restore of Solr Index</legend>
#(dumpRestoreEnabled)#<div class="alert alert-info">This feature is available only when a local embedded Solr is active.</div>::#(/dumpRestoreEnabled)#
<dl> <dl>
<dt>&nbsp;</dt> <dt>&nbsp;</dt>
<dd><input type="submit" name="indexdump" value="Create Dump" class="btn btn-primary" style="width:240px;"/> <dd><input type="submit" name="indexdump" value="Create Dump" class="btn btn-primary" style="width:240px;" #(dumpRestoreEnabled)#disabled="disabled"::#(/dumpRestoreEnabled)#/>
</dd> </dd>
</dl> </dl>
<dl> <dl>
<dt class="TableCellDark">Dump File</dt> <dt class="TableCellDark">Dump File</dt>
<dd><input type="text" name="dumpfile" value="#[dumpfile]#" size="80" maxlength="250" /> <dd><input type="text" name="dumpfile" value="#[dumpfile]#" size="80" maxlength="250" #(dumpRestoreEnabled)#disabled="disabled"::#(/dumpRestoreEnabled)#/>
</dd> </dd>
<dt>&nbsp;</dt> <dt>&nbsp;</dt>
<dd><input type="submit" name="indexrestore" value="Restore Dump" class="btn btn-primary" style="width:240px;"/> <dd><input type="submit" name="indexrestore" value="Restore Dump" class="btn btn-primary" style="width:240px;" #(dumpRestoreEnabled)#disabled="disabled"::#(/dumpRestoreEnabled)#/>
</dd> </dd>
</dl> </dl>
</fieldset> </fieldset>
@ -86,9 +87,17 @@
#(/dumprestore)# #(/dumprestore)#
#(indexdump)#:: #(indexdump)#::
<div class="alert alert-success">Stored a solr dump to file #[dumpfile]#</div>:: <div class="alert alert-success" role="alert">Stored a solr dump to file #[dumpfile]#</div>::
<div class="alert alert-danger" role="alert">Could not create the Solr dump : no embedded Solr is available.</div>::
<div class="alert alert-danger" role="alert">An error occurred while trying to create the Solr dump.</div>
#(/indexdump)# #(/indexdump)#
#(indexRestore)#::
<div class="alert alert-success" role="alert">Successfully restored Solr index from dump file!</div>::
<div class="alert alert-danger" role="alert">Could not restore the Solr dump : no embedded Solr is available.</div>::
<div class="alert alert-danger" role="alert">An error occurred while trying to restore the Solr dump.</div>
#(/indexRestore)#
#%env/templates/footer.template%# #%env/templates/footer.template%#
</body> </body>
</html> </html>

@ -25,9 +25,13 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import net.yacy.cora.protocol.RequestHeader; import net.yacy.cora.protocol.RequestHeader;
import net.yacy.data.WorkTables; import net.yacy.data.WorkTables;
import net.yacy.search.Switchboard; import net.yacy.search.Switchboard;
import net.yacy.search.SwitchboardConstants;
import net.yacy.search.index.Fulltext; import net.yacy.search.index.Fulltext;
import net.yacy.search.index.Segment; import net.yacy.search.index.Segment;
import net.yacy.server.serverObjects; import net.yacy.server.serverObjects;
@ -48,9 +52,12 @@ public class IndexExport_p {
prop.put("otherHosts", ""); prop.put("otherHosts", "");
prop.put("reload", 0); prop.put("reload", 0);
prop.put("indexdump", 0); prop.put("indexdump", 0);
prop.put("indexRestore", 0);
prop.put("lurlexport", 0); prop.put("lurlexport", 0);
prop.put("reload", 0); prop.put("reload", 0);
prop.put("dumprestore", 1); prop.put("dumprestore", 1);
prop.put("dumprestore_dumpRestoreEnabled", sb.getConfigBool(SwitchboardConstants.CORE_SERVICE_FULLTEXT,
SwitchboardConstants.CORE_SERVICE_FULLTEXT_DEFAULT));
List<File> dumpFiles = segment.fulltext().dumpFiles(); List<File> dumpFiles = segment.fulltext().dumpFiles();
prop.put("dumprestore_dumpfile", dumpFiles.size() == 0 ? "" : dumpFiles.get(dumpFiles.size() - 1).getAbsolutePath()); prop.put("dumprestore_dumpfile", dumpFiles.size() == 0 ? "" : dumpFiles.get(dumpFiles.size() - 1).getAbsolutePath());
prop.put("dumprestore_optimizemax", 10); prop.put("dumprestore_optimizemax", 10);
@ -132,17 +139,34 @@ public class IndexExport_p {
} }
if (post.containsKey("indexdump")) { if (post.containsKey("indexdump")) {
final File dump = segment.fulltext().dumpSolr(); try {
prop.put("indexdump", 1); final File dump = segment.fulltext().dumpEmbeddedSolr();
prop.put("indexdump_dumpfile", dump.getAbsolutePath()); prop.put("indexdump", 1);
dumpFiles = segment.fulltext().dumpFiles(); prop.put("indexdump_dumpfile", dump.getAbsolutePath());
prop.put("dumprestore_dumpfile", dumpFiles.size() == 0 ? "" : dumpFiles.get(dumpFiles.size() - 1).getAbsolutePath()); dumpFiles = segment.fulltext().dumpFiles();
//sb.tables.recordAPICall(post, "IndexExport_p.html", WorkTables.TABLE_API_TYPE_STEERING, "solr dump generation"); prop.put("dumprestore_dumpfile", dumpFiles.size() == 0 ? "" : dumpFiles.get(dumpFiles.size() - 1).getAbsolutePath());
// sb.tables.recordAPICall(post, "IndexExport_p.html", WorkTables.TABLE_API_TYPE_STEERING, "solr dump generation");
} catch(final SolrException e) {
if(ErrorCode.SERVICE_UNAVAILABLE.code == e.code()) {
prop.put("indexdump", 2);
} else {
prop.put("indexdump", 3);
}
}
} }
if (post.containsKey("indexrestore")) { if (post.containsKey("indexrestore")) {
final File dump = new File(post.get("dumpfile", "")); try {
segment.fulltext().restoreSolr(dump); final File dump = new File(post.get("dumpfile", ""));
segment.fulltext().restoreEmbeddedSolr(dump);
prop.put("indexRestore", 1);
} catch(final SolrException e) {
if(ErrorCode.SERVICE_UNAVAILABLE.code == e.code()) {
prop.put("indexRestore", 2);
} else {
prop.put("indexRestore", 3);
}
}
} }
// insert constants // insert constants

@ -82,6 +82,7 @@ import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.core.SolrInfoMBean; import org.apache.solr.core.SolrInfoMBean;
import org.apache.lucene.util.Version; import org.apache.lucene.util.Version;
@ -581,13 +582,17 @@ public final class Fulltext {
} }
/** /**
* create a dump file from the current solr directory * Create a dump file from the current embedded solr directory
* @return file reference to the dump * @return file reference to the dump
* @throws SolrException when no embedded Solr is available
*/ */
public File dumpSolr() { public File dumpEmbeddedSolr() throws SolrException {
EmbeddedInstance esc = this.solrInstances.getEmbedded(); final EmbeddedInstance esc = this.solrInstances.getEmbedded();
File storagePath = esc.getContainerPath(); if(esc == null) {
File zipOut = new File(this.archivePath, storagePath.getName() + "_" + GenericFormatter.SHORT_DAY_FORMATTER.format() + ".zip"); throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "No embedded Solr available.");
}
final File storagePath = esc.getContainerPath();
final File zipOut = new File(this.archivePath, storagePath.getName() + "_" + GenericFormatter.SHORT_DAY_FORMATTER.format() + ".zip");
synchronized (this.solrInstances) { synchronized (this.solrInstances) {
this.disconnectLocalSolr(); this.disconnectLocalSolr();
try { try {
@ -606,12 +611,16 @@ public final class Fulltext {
} }
/** /**
* restore a solr dump to the current solr directory * Restore a solr dump to the current embedded solr directory
* @param solrDumpZipFile * @param solrDumpZipFile the dump file to use
* @throws SolrException when no embedded Solr is available
*/ */
public void restoreSolr(File solrDumpZipFile) { public void restoreEmbeddedSolr(final File solrDumpZipFile) {
EmbeddedInstance esc = this.solrInstances.getEmbedded(); final EmbeddedInstance esc = this.solrInstances.getEmbedded();
File storagePath = esc.getContainerPath(); if(esc == null) {
throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "No embedded Solr available.");
}
final File storagePath = esc.getContainerPath();
synchronized (this.solrInstances) { synchronized (this.solrInstances) {
// this.disconnectLocalSolr(); // moved to (InstanceMirror) sorlInstances.close() // this.disconnectLocalSolr(); // moved to (InstanceMirror) sorlInstances.close()
this.solrInstances.close(); this.solrInstances.close();

Loading…
Cancel
Save