From 58966ab728d291bf136ccfe68429ded0956d8639 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 28 Oct 2008 12:08:19 +0000 Subject: [PATCH] LUCENE-1429: don't throw IllegalStateEx during close() after hitting OOME when autoCommit=true git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@708549 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/index/IndexWriter.java | 22 ++++++++------- .../apache/lucene/index/TestIndexWriter.java | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.java index 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.java index 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 LuceneTestCase dir.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(); + } }