From 98abb0ed1b5d9146e1bf49c9e8fe2138583abcea Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Wed, 4 Feb 2015 21:05:57 +0000 Subject: [PATCH] LUCENE-6217: add IW.isOpen and IW.getTragicException git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1657398 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 +++ .../java/org/apache/lucene/index/IndexWriter.java | 12 ++++++++++++ .../TestCompressingStoredFieldsFormat.java | 11 ++++++----- .../lucene/index/TestIndexWriterExceptions.java | 7 +++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 79d0c98920e..4a943f778c1 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -65,6 +65,9 @@ API Changes * LUCENE-6204, LUCENE-6208: Simplify CompoundFormat: remove files() and remove files parameter to write(). (Robert Muir) +* LUCENE-6217: Add IndexWriter.isOpen and getTragicException. (Simon + Willnauer, Mike McCandless) + Other * LUCENE-6193: Collapse identical catch branches in try-catch statements. diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java index 206eda73288..a7dbd5ffacc 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -4390,6 +4390,18 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable { IOUtils.reThrow(tragedy); } + /** If this {@code IndexWriter} was closed as a side-effect of a tragic exception, + * e.g. disk full while flushing a new segment, this returns the root cause exception. + * Otherwise (no tragic exception has occurred) it returns null. */ + public Throwable getTragicException() { + return tragedy; + } + + /** Returns {@code true} if this {@code IndexWriter} is still open. */ + public boolean isOpen() { + return closing == false && closed == false; + } + // Used for testing. Current points: // startDoFlush // startCommitMerge diff --git a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java index 75f7367eeae..2afda049b86 100644 --- a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java +++ b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java @@ -54,7 +54,6 @@ public class TestCompressingStoredFieldsFormat extends BaseStoredFieldsFormatTes return CompressingCodec.randomInstance(random()); } - @Test(expected=IllegalArgumentException.class) public void testDeletePartiallyWrittenFilesIfAbort() throws IOException { Directory dir = newDirectory(); // test explicitly needs files to always be actually deleted @@ -94,11 +93,13 @@ public class TestCompressingStoredFieldsFormat extends BaseStoredFieldsFormatTes try { iw.addDocument(invalidDoc); iw.commit(); + } catch(IllegalArgumentException iae) { + // expected + assertEquals(iae, iw.getTragicException()); } - finally { - // Abort should have closed the deleter: - dir.close(); - } + // Writer should be closed by tragedy + assertFalse(iw.isOpen()); + dir.close(); } public void testZFloat() throws Exception { diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java index 77ee7e52a4d..b5a76c07f52 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java @@ -1520,6 +1520,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (IllegalArgumentException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc DirectoryReader r = DirectoryReader.open(dir); @@ -1544,6 +1545,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (IllegalArgumentException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc DirectoryReader r = DirectoryReader.open(dir); @@ -1569,6 +1571,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (NullPointerException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc DirectoryReader r = DirectoryReader.open(dir); @@ -1594,6 +1597,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (NullPointerException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc DirectoryReader r = DirectoryReader.open(dir); @@ -1619,6 +1623,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (IllegalArgumentException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc DirectoryReader r = DirectoryReader.open(dir); @@ -1644,6 +1649,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (IllegalArgumentException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc DirectoryReader r = DirectoryReader.open(dir); @@ -1675,6 +1681,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { iw.addDocument(doc); fail("didn't get expected exception"); } catch (IllegalArgumentException expected) {} + assertNull(iw.getTragicException()); iw.close(); // make sure we see our good doc