Apache Solr open-source search software
LUCENE-3872: catch case where app calls close() after prepareCommit() without the matched commit()
Michael McCandless35dad942bbc6
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex cb2f64ac841..08fc4719a0a 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -933,6 +933,10 @@ Bug fixes* LUCENE-3841: Fix CloseableThreadLocal to also purge stale entries onget(); this fixes certain cases where we were holding onto objectsfor dead threads for too long (Matthew Bellew, Mike McCandless)++* LUCENE-3872: IndexWriter.close() now throws IllegalStateException if+ you call it after calling prepareCommit() without calling commit()+ first. (Tim Bogaert via Mike McCandless)Optimizationsdiff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 01079bd0887..364ad554f74 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -834,6 +834,11 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {private void closeInternal(boolean waitForMerges) throws CorruptIndexException, IOException {try {++ if (pendingCommit != null) {+ throw new IllegalStateException("cannot close: prepareCommit was already called with no corresponding call to commit");+ }+if (infoStream.isEnabled("IW")) {infoStream.message("IW", "now flush at close waitForMerges=" + waitForMerges);}@@ -2358,7 +2363,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {* #rollback()} to revert the commit and undo all changes* done since the writer was opened.</p>*- * You can also just call {@link #commit(Map)} directly+ * <p>You can also just call {@link #commit(Map)} directly* without prepareCommit first in which case that method* will internally call prepareCommit.*diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.javaindex b404e9783e5..7370a338b5b 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java@@ -924,6 +924,7 @@ public void testFilesOpenClose() throws IOException {writer.addDocument(new Document());writer.prepareCommit();assertFalse(DirectoryReader.indexExists(dir));+ writer.commit();writer.close();assertTrue(DirectoryReader.indexExists(dir));dir.close();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.javaindex b192d1a0840..55ad2cf8692 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -1801,4 +1801,54 @@ public class TestIndexWriter extends LuceneTestCase {w.close();dir.close();}++ // LUCENE-3872+ public void testPrepareCommitThenClose() throws Exception {+ Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir,+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));++ w.prepareCommit();+ try {+ w.close();+ fail("should have hit exception");+ } catch (IllegalStateException ise) {+ // expected+ }+ w.commit();+ w.close();+ IndexReader r = IndexReader.open(dir);+ assertEquals(0, r.maxDoc());+ r.close();+ dir.close();+ }++ // LUCENE-3872+ public void testPrepareCommitThenRollback() throws Exception {+ Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir,+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));++ w.prepareCommit();+ w.rollback();+ assertFalse(DirectoryReader.indexExists(dir));+ dir.close();+ }++ // LUCENE-3872+ public void testPrepareCommitThenRollback2() throws Exception {+ Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir,+ new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));++ w.commit();+ w.addDocument(new Document());+ w.prepareCommit();+ w.rollback();+ assertTrue(DirectoryReader.indexExists(dir));+ IndexReader r = IndexReader.open(dir);+ assertEquals(0, r.maxDoc());+ r.close();+ dir.close();+ }}