SOLR-3180: do an explicit commit and not just close when shutting down

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1296712 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2012-03-03 22:01:36 +00:00
parent fc3972fa1f
commit 7cf946898d
2 changed files with 55 additions and 20 deletions

View File

@ -42,12 +42,19 @@ import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrConfig.UpdateHandlerInfo;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestInfo;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.FunctionRangeQuery;
import org.apache.solr.search.QParser;
@ -524,49 +531,73 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState
// IndexWriterCloser interface method - called from solrCoreState.decref(this)
@Override
public void closeWriter(IndexWriter writer) throws IOException {
boolean clearRequestInfo = false;
commitLock.lock();
try {
SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
SolrQueryResponse rsp = new SolrQueryResponse();
if (SolrRequestInfo.getRequestInfo() == null) {
clearRequestInfo = true;
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp)); // important for debugging
}
if (!commitOnClose) {
if (writer != null) {
writer.rollback();
}
// we shouldn't close the transaction logs either, but leaving them open
// means we can't delete them on windows.
// means we can't delete them on windows (needed for tests)
if (ulog != null) ulog.close(false);
return;
}
// if we are later going to mark everything in the tlog as committed, then we
// need to block all updates from coming in so we can be sure that the close
// will contain all of the updates.
// do a commit before we quit?
boolean tryToCommit = writer != null && ulog != null && ulog.hasUncommittedChanges() && ulog.getState() == UpdateLog.State.ACTIVE;
VersionInfo vinfo = ulog == null ? null : ulog.getVersionInfo();
if (vinfo != null) {
// TODO: move the RW update lock somewhere else?
vinfo.blockUpdates();
}
try {
if (tryToCommit) {
boolean succeeded = false;
try {
if (writer != null) {
writer.close();
CommitUpdateCommand cmd = new CommitUpdateCommand(req, false);
cmd.openSearcher = false;
cmd.waitSearcher = false;
cmd.softCommit = false;
// TODO: keep other commit callbacks from being called?
// this.commit(cmd); // too many test failures using this method... is it because of callbacks?
synchronized (this) {
ulog.preCommit(cmd);
}
succeeded = true;
} finally {
if (ulog != null) ulog.close(succeeded);
}
} finally {
if (vinfo != null) {
vinfo.unblockUpdates();
// todo: refactor this shared code (or figure out why a real CommitUpdateCommand can't be used)
final Map<String,String> commitData = new HashMap<String,String>();
commitData.put(SolrIndexWriter.COMMIT_TIME_MSEC_KEY, String.valueOf(System.currentTimeMillis()));
writer.commit(commitData);
synchronized (this) {
ulog.postCommit(cmd);
}
}
} catch (Throwable th) {
log.error("Error in final commit", th);
}
// we went through the normal process to commit, so we don't have to artificially
// cap any ulog files.
try {
if (ulog != null) ulog.close(false);
} catch (Throwable th) {
log.error("Error closing log files", th);
}
if (writer != null) writer.close();
} finally {
commitLock.unlock();
if (clearRequestInfo) SolrRequestInfo.clearRequestInfo();
}
}

View File

@ -375,6 +375,10 @@ public class UpdateLog implements PluginInfoInitialized {
prevMap2 = null;
}
public boolean hasUncommittedChanges() {
return tlog != null;
}
public void preCommit(CommitUpdateCommand cmd) {
synchronized (this) {
if (debug) {