Apache Lucene open-source search software
LUCENE-1429: don't throw IllegalStateEx during close() after hitting OOME when autoCommit=true
Michael McCandless58966ab728d2
diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.javaindex a88a9d70c0e..2761626a74a 100644--- a/src/java/org/apache/lucene/index/IndexWriter.java+++ b/src/java/org/apache/lucene/index/IndexWriter.java@@ -1675,17 +1675,16 @@ public class IndexWriter {*/public void close(boolean waitForMerges) throws CorruptIndexException, IOException {- // If any methods have hit OutOfMemoryError, then abort- // on close, in case the internal state of IndexWriter- // or DocumentsWriter is corrupt- if (hitOOM) {- rollback();- return;- }-// Ensure that only one thread actually gets to do the closing:- if (shouldClose())- closeInternal(waitForMerges);+ if (shouldClose()) {+ // If any methods have hit OutOfMemoryError, then abort+ // on close, in case the internal state of IndexWriter+ // or DocumentsWriter is corrupt+ if (hitOOM)+ rollbackInternal();+ else+ closeInternal(waitForMerges);+ }}// Returns true if this thread should attempt to close, or@@ -3397,6 +3396,9 @@ public class IndexWriter {* @deprecated please call {@link #commit()}) instead*/public final void flush() throws CorruptIndexException, IOException {+ if (hitOOM)+ throw new IllegalStateException("this writer hit an OutOfMemoryError; cannot flush");+flush(true, false, true);}diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 7d08ba673f4..ee51c54d94e 100644--- a/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -20,6 +20,8 @@ package org.apache.lucene.index;import java.io.IOException;import java.io.Reader;import java.io.File;+import java.io.ByteArrayOutputStream;+import java.io.PrintStream;import java.util.Arrays;import java.util.ArrayList;import java.util.List;@@ -4178,4 +4180,30 @@ public class TestIndexWriter extends LuceneTestCasedir.close();}}++ // LUCENE-1429+ public void testOutOfMemoryErrorCausesCloseToFail() throws Exception {++ final List thrown = new ArrayList();++ final IndexWriter writer = new IndexWriter(new MockRAMDirectory(), new StandardAnalyzer()) {+ public void message(final String message) {+ if (message.startsWith("now flush at close") && 0 == thrown.size()) {+ thrown.add(null);+ throw new OutOfMemoryError("fake OOME at " + message);+ }+ }+ };++ // need to set an info stream so message is called+ writer.setInfoStream(new PrintStream(new ByteArrayOutputStream()));+ try {+ writer.close();+ fail("OutOfMemoryError expected");+ }+ catch (final OutOfMemoryError expected) {}++ // throws IllegalStateEx w/o bug fix+ writer.close();+ }}