Apache Lucene open-source search software
LUCENE-6217: add IW.isOpen and IW.getTragicException
Michael McCandless98abb0ed1b5d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 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.javaindex 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// startCommitMergediff --git a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.javaindex 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 BaseStoredFieldsFormatTesreturn 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 BaseStoredFieldsFormatTestry {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.javaindex 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 docDirectoryReader 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 docDirectoryReader 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 docDirectoryReader 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 docDirectoryReader 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 docDirectoryReader 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 docDirectoryReader 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