LUCENE-1191: if we hit OOM then don't commit any changes to the index

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@632124 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-02-28 21:35:42 +00:00
parent 8f9781bbdb
commit 7af43e46d6
1 changed files with 376 additions and 311 deletions

View File

@ -289,6 +289,7 @@ public class IndexWriter {
private static Object MESSAGE_ID_LOCK = new Object();
private static int MESSAGE_ID = 0;
private int messageID = -1;
volatile private boolean hitOOM;
private Directory directory; // where this index resides
private Analyzer analyzer; // how to analyze text
@ -1610,6 +1611,13 @@ public class IndexWriter {
*/
public void close(boolean waitForMerges) throws CorruptIndexException, IOException {
boolean doClose;
// If any methods have hit OutOfMemoryError, then abort
// on close, in case the internal state of IndexWriter
// or DocumentsWriter is corrupt
if (hitOOM)
abort();
synchronized(this) {
// Ensure that only one thread actually gets to do the closing:
if (!closing) {
@ -1676,7 +1684,9 @@ public class IndexWriter {
synchronized(this) {
closed = true;
}
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
} finally {
synchronized(this) {
if (!closed)
@ -1861,6 +1871,7 @@ public class IndexWriter {
ensureOpen();
boolean doFlush = false;
boolean success = false;
try {
try {
doFlush = docWriter.addDocument(doc, analyzer);
success = true;
@ -1883,6 +1894,10 @@ public class IndexWriter {
}
if (doFlush)
flush(true, false);
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
/**
@ -1893,9 +1908,14 @@ public class IndexWriter {
*/
public void deleteDocuments(Term term) throws CorruptIndexException, IOException {
ensureOpen();
try {
boolean doFlush = docWriter.bufferDeleteTerm(term);
if (doFlush)
flush(true, false);
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
/**
@ -1908,9 +1928,14 @@ public class IndexWriter {
*/
public void deleteDocuments(Term[] terms) throws CorruptIndexException, IOException {
ensureOpen();
try {
boolean doFlush = docWriter.bufferDeleteTerms(terms);
if (doFlush)
flush(true, false);
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
/**
@ -1946,6 +1971,7 @@ public class IndexWriter {
public void updateDocument(Term term, Document doc, Analyzer analyzer)
throws CorruptIndexException, IOException {
ensureOpen();
try {
boolean doFlush = false;
boolean success = false;
try {
@ -1968,6 +1994,10 @@ public class IndexWriter {
}
if (doFlush)
flush(true, false);
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
// for test purpose
@ -2644,6 +2674,7 @@ public class IndexWriter {
throws CorruptIndexException, IOException {
ensureOpen();
try {
if (infoStream != null)
message("flush at addIndexes");
flush(true, false);
@ -2671,6 +2702,10 @@ public class IndexWriter {
rollbackTransaction();
}
}
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
private synchronized void resetMergeExceptions() {
@ -2706,6 +2741,8 @@ public class IndexWriter {
throws CorruptIndexException, IOException {
ensureOpen();
try {
if (infoStream != null)
message("flush at addIndexesNoOptimize");
flush(true, false);
@ -2748,6 +2785,10 @@ public class IndexWriter {
rollbackTransaction();
}
}
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
/* If any of our segments are using a directory != ours
@ -2793,6 +2834,7 @@ public class IndexWriter {
throws CorruptIndexException, IOException {
ensureOpen();
try {
optimize(); // start with zero or 1 seg
final String mergedName = newSegmentName();
@ -2865,6 +2907,10 @@ public class IndexWriter {
}
}
}
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
// This is called after pending added and deleted
@ -3121,6 +3167,9 @@ public class IndexWriter {
return flushDocs || flushDeletes;
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
} finally {
docWriter.clearFlushPending();
docWriter.resumeAllThreads();
@ -3259,6 +3308,9 @@ public class IndexWriter {
/* FIXME if we want to support non-contiguous segment merges */
synchronized private boolean commitMerge(MergePolicy.OneMerge merge) throws IOException {
if (hitOOM)
return false;
if (infoStream != null)
message("commitMerge: " + merge.segString(directory));
@ -3344,7 +3396,7 @@ public class IndexWriter {
boolean success = false;
try {
try {
try {
mergeInit(merge);
@ -3392,6 +3444,10 @@ public class IndexWriter {
}
}
}
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
/** Checks whether this merge involves any segments
@ -3988,6 +4044,11 @@ public class IndexWriter {
* that. */
private void sync(boolean includeFlushes, long sizeInBytes) throws IOException {
if (hitOOM)
return;
try {
message("start sync() includeFlushes=" + includeFlushes);
if (!includeFlushes)
@ -4126,6 +4187,10 @@ public class IndexWriter {
commitPending = true;
}
}
} catch (OutOfMemoryError oom) {
hitOOM = true;
throw oom;
}
}
/**