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.
This commit is contained in:
Michael Froh 2020-03-02 13:38:24 -08:00 committed by Michael Sokolov
parent a1791e7714
commit a5475de57f
2 changed files with 8 additions and 6 deletions

View File

@ -3360,9 +3360,8 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,
} }
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException ie) {
Thread.interrupted(); throw new ThreadInterruptedException(ie);
throw new IOException("Interrupted waiting for merges");
} finally { } finally {
if (infoStream.isEnabled("IW")) { if (infoStream.isEnabled("IW")) {
infoStream.message("IW", String.format(Locale.ROOT, "Waited %.1f ms for commit merges", infoStream.message("IW", String.format(Locale.ROOT, "Waited %.1f ms for commit merges",

View File

@ -364,13 +364,14 @@ public class TestIndexWriterMergePolicy extends LuceneTestCase {
for (int i = 0; i < numIndexingThreads; i++) { for (int i = 0; i < numIndexingThreads; i++) {
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
try { try {
startingGun.await();
while (indexedDocs.getAndIncrement() < docCount) { while (indexedDocs.getAndIncrement() < docCount) {
writerWithMergePolicy.addDocument(lineFileDocs.nextDoc()); writerWithMergePolicy.addDocument(lineFileDocs.nextDoc());
if (rarely()) { if (rarely()) {
writerWithMergePolicy.commit(); writerWithMergePolicy.commit();
} }
} }
} catch (IOException e) { } catch (IOException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
fail(); fail();
} }
@ -392,6 +393,8 @@ public class TestIndexWriterMergePolicy extends LuceneTestCase {
Thread.sleep(100); Thread.sleep(100);
} }
abandonedMerges.set(0); abandonedMerges.set(0);
// Ensure there's at least one pending change so merge on commit happens
TestIndexWriter.addDoc(writerWithMergePolicy);
writerWithMergePolicy.commit(); writerWithMergePolicy.commit();
if (abandonedMerges.get() == 0) { if (abandonedMerges.get() == 0) {
assertEquals(1, writerWithMergePolicy.listOfSegmentCommitInfos().size()); assertEquals(1, writerWithMergePolicy.listOfSegmentCommitInfos().size());
@ -401,8 +404,8 @@ public class TestIndexWriterMergePolicy extends LuceneTestCase {
try (IndexReader reader = writerWithMergePolicy.getReader()) { try (IndexReader reader = writerWithMergePolicy.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader); IndexSearcher searcher = new IndexSearcher(reader);
assertEquals(docCount + 6, reader.numDocs()); assertEquals(docCount + 7, reader.numDocs());
assertEquals(docCount + 6, searcher.count(new MatchAllDocsQuery())); assertEquals(docCount + 7, searcher.count(new MatchAllDocsQuery()));
} }
writerWithMergePolicy.close(); writerWithMergePolicy.close();