From a5475de57fed6b339cd5565bd1bd2650f265a537 Mon Sep 17 00:00:00 2001 From: Michael Froh Date: Mon, 2 Mar 2020 13:38:24 -0800 Subject: [PATCH] LUCENE-8962: Fix intermittent test failures 1. TestIndexWriterMergePolicy.testMergeOnCommit will fail if the last commit (the one that should trigger the full merge) doesn't have any pending changes (which could occur if the last indexing thread commits at the end). We can fix that by adding one more document before that commit. 2. The previous implementation was throwing IOException if the commit thread gets interrupted while waiting for merges to complete. This violates IndexWriter's documented behavior of throwing ThreadInterruptedException. --- .../src/java/org/apache/lucene/index/IndexWriter.java | 5 ++--- .../apache/lucene/index/TestIndexWriterMergePolicy.java | 9 ++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) 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 edebb8de5d0..1c31094d3df 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -3360,9 +3360,8 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable, } } } - } catch (InterruptedException e) { - Thread.interrupted(); - throw new IOException("Interrupted waiting for merges"); + } catch (InterruptedException ie) { + throw new ThreadInterruptedException(ie); } finally { if (infoStream.isEnabled("IW")) { infoStream.message("IW", String.format(Locale.ROOT, "Waited %.1f ms for commit merges", diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java index 16d7a51e75e..f58ecee67b3 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterMergePolicy.java @@ -364,13 +364,14 @@ public class TestIndexWriterMergePolicy extends LuceneTestCase { for (int i = 0; i < numIndexingThreads; i++) { Thread t = new Thread(() -> { try { + startingGun.await(); while (indexedDocs.getAndIncrement() < docCount) { writerWithMergePolicy.addDocument(lineFileDocs.nextDoc()); if (rarely()) { writerWithMergePolicy.commit(); } } - } catch (IOException e) { + } catch (IOException | InterruptedException e) { e.printStackTrace(); fail(); } @@ -392,6 +393,8 @@ public class TestIndexWriterMergePolicy extends LuceneTestCase { Thread.sleep(100); } abandonedMerges.set(0); + // Ensure there's at least one pending change so merge on commit happens + TestIndexWriter.addDoc(writerWithMergePolicy); writerWithMergePolicy.commit(); if (abandonedMerges.get() == 0) { assertEquals(1, writerWithMergePolicy.listOfSegmentCommitInfos().size()); @@ -401,8 +404,8 @@ public class TestIndexWriterMergePolicy extends LuceneTestCase { try (IndexReader reader = writerWithMergePolicy.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - assertEquals(docCount + 6, reader.numDocs()); - assertEquals(docCount + 6, searcher.count(new MatchAllDocsQuery())); + assertEquals(docCount + 7, reader.numDocs()); + assertEquals(docCount + 7, searcher.count(new MatchAllDocsQuery())); } writerWithMergePolicy.close();