Apache Lucene open-source search software
TaskExecutor waits for all tasks to complete before returning (#12523)
Michael Peterson737149347879
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a45d61871be..6853ea9b073 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -150,6 +150,9 @@ New FeaturesImprovements---------------------+* GITHUB#12523: TaskExecutor waits for all tasks to complete before returning when Exceptions+ are thrown during concurrent operations (Michael Peterson)+* GITHUB#12574: Make TaskExecutor public so that it can be retrieved from the searcher and usedoutside of the o.a.l.search package (Luca Cavanna)diff --git a/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java b/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.javaindex 8e1f6c3fac8..0a383a8e815 100644--- a/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java+++ b/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java@@ -56,7 +56,8 @@ public final class TaskExecutor {/*** Execute all the callables provided as an argument, wait for them to complete and return the- * obtained results.+ * obtained results. If an exception is thrown by more than one callable, the subsequent ones will+ * be added as suppressed exceptions to the first one that was caught.** @param callables the callables to execute* @return a list containing the results from the tasks execution@@ -74,16 +75,30 @@ public final class TaskExecutor {executor.execute(task);}}++ Throwable exc = null;final List<T> results = new ArrayList<>();for (Future<T> future : tasks) {try {results.add(future.get());} catch (InterruptedException e) {- throw new ThreadInterruptedException(e);+ var newException = new ThreadInterruptedException(e);+ if (exc == null) {+ exc = newException;+ } else {+ exc.addSuppressed(newException);+ }} catch (ExecutionException e) {- throw IOUtils.rethrowAlways(e.getCause());+ if (exc == null) {+ exc = e.getCause();+ } else {+ exc.addSuppressed(e.getCause());+ }}}+ if (exc != null) {+ throw IOUtils.rethrowAlways(exc);+ }return results;}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java b/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.javaindex 89ccc03a3fe..c18be1ad4f4 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java@@ -17,11 +17,14 @@package org.apache.lucene.search;import java.io.IOException;+import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.List;+import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;+import java.util.concurrent.atomic.AtomicInteger;import org.apache.lucene.document.Document;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.LeafReaderContext;@@ -223,4 +226,63 @@ public class TestTaskExecutor extends LuceneTestCase {}}}++ public void testInvokeAllDoesNotLeaveTasksBehind() {+ TaskExecutor taskExecutor = new TaskExecutor(executorService);+ AtomicInteger tasksExecuted = new AtomicInteger(0);+ List<Callable<Void>> callables = new ArrayList<>();+ callables.add(+ () -> {+ throw new RuntimeException();+ });+ int tasksWithNormalExit = 99;+ for (int i = 0; i < tasksWithNormalExit; i++) {+ callables.add(+ () -> {+ tasksExecuted.incrementAndGet();+ return null;+ });+ }+ expectThrows(RuntimeException.class, () -> taskExecutor.invokeAll(callables));+ assertEquals(tasksWithNormalExit, tasksExecuted.get());+ }++ /**+ * Ensures that all invokeAll catches all exceptions thrown by Callables and adds subsequent ones+ * as suppressed exceptions to the first one caught.+ */+ public void testInvokeAllCatchesMultipleExceptions() {+ TaskExecutor taskExecutor = new TaskExecutor(executorService);+ AtomicInteger tasksExecuted = new AtomicInteger(0);+ List<Callable<Void>> callables = new ArrayList<>();+ callables.add(+ () -> {+ throw new RuntimeException("exception A");+ });+ int tasksWithNormalExit = 50;+ for (int i = 0; i < tasksWithNormalExit; i++) {+ callables.add(+ () -> {+ tasksExecuted.incrementAndGet();+ return null;+ });+ }+ callables.add(+ () -> {+ throw new IllegalStateException("exception B");+ });++ RuntimeException exc =+ expectThrows(RuntimeException.class, () -> taskExecutor.invokeAll(callables));+ Throwable[] suppressed = exc.getSuppressed();+ assertEquals(1, suppressed.length);+ if (exc.getMessage().equals("exception A")) {+ assertEquals("exception B", suppressed[0].getMessage());+ } else {+ assertEquals("exception A", suppressed[0].getMessage());+ assertEquals("exception B", exc.getMessage());+ }++ assertEquals(tasksWithNormalExit, tasksExecuted.get());+ }}