SOLR-728 -- Add synchronization to avoid race condition of multiple imports working concurrently

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@707295 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-10-23 06:54:56 +00:00
parent ca6e3f100f
commit 1e5562a910
3 changed files with 32 additions and 16 deletions

View File

@ -37,6 +37,9 @@ Bug Fixes
2. SOLR-823: Request parameter variables ${dataimporter.request.xxx} are not resolved 2. SOLR-823: Request parameter variables ${dataimporter.request.xxx} are not resolved
(Mck SembWever, Noble Paul, shalin) (Mck SembWever, Noble Paul, shalin)
3. SOLR-728: Add synchronization to avoid race condition of multiple imports working concurrently
(Walter Ferrara, shalin)
Documentation Documentation
---------------------- ----------------------

View File

@ -141,7 +141,7 @@ public class DataImportHandler extends RequestHandlerBase implements
if (command != null) if (command != null)
rsp.add("command", command); rsp.add("command", command);
if (requestParams.debug) { if (requestParams.debug && (importer == null || !importer.isBusy())) {
// Reload the data-config.xml // Reload the data-config.xml
importer = null; importer = null;
if (requestParams.dataConfig != null) { if (requestParams.dataConfig != null) {
@ -168,7 +168,7 @@ public class DataImportHandler extends RequestHandlerBase implements
if (command != null && DataImporter.ABORT_CMD.equals(command)) { if (command != null && DataImporter.ABORT_CMD.equals(command)) {
importer.runCmd(requestParams, null, null); importer.runCmd(requestParams, null, null);
} else if (importer.getStatus() != DataImporter.Status.IDLE) { } else if (importer.isBusy()) {
message = DataImporter.MSG.CMD_RUNNING; message = DataImporter.MSG.CMD_RUNNING;
} else if (command != null) { } else if (command != null) {
if (DataImporter.FULL_IMPORT_CMD.equals(command) if (DataImporter.FULL_IMPORT_CMD.equals(command)
@ -202,8 +202,7 @@ public class DataImportHandler extends RequestHandlerBase implements
message = DataImporter.MSG.CONFIG_RELOADED; message = DataImporter.MSG.CONFIG_RELOADED;
} }
} }
rsp.add("status", importer.getStatus() == DataImporter.Status.IDLE ? "idle" rsp.add("status", importer.isBusy() ? "busy" : "idle");
: "busy");
rsp.add("importResponse", message); rsp.add("importResponse", message);
rsp.add("statusMessages", importer.getStatusMessages()); rsp.add("statusMessages", importer.getStatusMessages());

View File

@ -32,6 +32,8 @@ import java.io.StringReader;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -75,6 +77,8 @@ public class DataImporter {
private SolrCore core; private SolrCore core;
private ReentrantLock importLock = new ReentrantLock();
/** /**
* Only for testing purposes * Only for testing purposes
*/ */
@ -318,6 +322,10 @@ public class DataImporter {
this.status = status; this.status = status;
} }
public boolean isBusy() {
return importLock.isLocked();
}
public void doFullImport(SolrWriter writer, RequestParams requestParams, public void doFullImport(SolrWriter writer, RequestParams requestParams,
Map<String, String> variables) { Map<String, String> variables) {
LOG.info("Starting Full Import"); LOG.info("Starting Full Import");
@ -376,18 +384,24 @@ public class DataImporter {
}.start(); }.start();
} }
void runCmd(RequestParams reqParams, SolrWriter sw, void runCmd(RequestParams reqParams, SolrWriter sw, Map<String, String> variables) {
Map<String, String> variables) { if (importLock.isLocked())
String command = reqParams.command; return;
Date lastModified = sw.loadIndexStartTime(); importLock.lock();
setLastIndexTime(lastModified); try {
if (command.equals("full-import")) { String command = reqParams.command;
doFullImport(sw, reqParams, variables); Date lastModified = sw.loadIndexStartTime();
} else if (command.equals(DELTA_IMPORT_CMD)) { setLastIndexTime(lastModified);
doDeltaImport(sw, reqParams, variables); if (command.equals("full-import")) {
} else if (command.equals(ABORT_CMD)) { doFullImport(sw, reqParams, variables);
if (docBuilder != null) } else if (command.equals(DELTA_IMPORT_CMD)) {
docBuilder.abort(); doDeltaImport(sw, reqParams, variables);
} else if (command.equals(ABORT_CMD)) {
if (docBuilder != null)
docBuilder.abort();
}
} finally {
importLock.unlock();
} }
} }