From 50b5e3ec7ce59a0e9b11041a6feaa3550148896f Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 6 Nov 2013 13:17:51 +0000 Subject: [PATCH] LUCENE-5330: Fix IndexWriter to process all internal events on getReader / close / rollback git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1539332 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 5 +++++ .../java/org/apache/lucene/index/IndexWriter.java | 4 ++++ .../org/apache/lucene/index/TestIndexWriter.java | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index b9a13c48007..ae37ffdd1e5 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -191,6 +191,11 @@ Bug Fixes inside ConstantScoreQuery, which now rewrites to a query removing the obsolete QueryWrapperFilter. (Adrien Grand, Uwe Schindler) +* LUCENE-5330: IndexWriter didn't process all internal events on + #getReader(), #close() and #rollback() which causes files to be + deleted at a later point in time. This could cause short-term disk + pollution or OOM if in-memory directories are used. (Simon Willnauer) + API Changes: * LUCENE-5222: Add SortField.needsScores(). Previously it was not possible 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 a684c42b4eb..453134f7585 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -394,6 +394,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{ } // Done: finish the full flush! docWriter.finishFullFlush(success); + processEvents(false, true); doAfterFlush(); } } @@ -929,6 +930,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{ closeInternal(waitForMerges, true); } } + assert eventQueue.isEmpty(); } } @@ -1061,6 +1063,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{ } // finally, restore interrupt status: if (interrupted) Thread.currentThread().interrupt(); + processEvents(false, true); } } @@ -2020,6 +2023,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{ rollbackInternal(); } } + assert eventQueue.isEmpty() : eventQueue; } private void rollbackInternal() throws IOException { diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java index 4f0bc600797..0046ea651b9 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -1321,6 +1321,7 @@ public class TestIndexWriter extends LuceneTestCase { dir.close(); } + public void testDeleteUnusedFiles() throws Exception { for(int iter=0;iter<2;iter++) { Directory dir = newMockDirectory(); // relies on windows semantics @@ -1350,7 +1351,19 @@ public class TestIndexWriter extends LuceneTestCase { } List files = Arrays.asList(dir.listAll()); + assertTrue(files.contains("_0.cfs")); + assertTrue(files.contains("_0.cfe")); + assertTrue(files.contains("_0.si")); + if (iter == 1) { + // we run a full commit so there should be a segments file etc. + assertTrue(files.contains("segments_1")); + assertTrue(files.contains("segments.gen")); + assertEquals(files.toString(), files.size(), 5); + } else { + // this is an NRT reopen - no segments files yet + assertEquals(files.toString(), files.size(), 3); + } w.addDocument(doc); w.forceMerge(1); if (iter == 1) {