javanna commented on code in PR #12689:
URL: https://github.com/apache/lucene/pull/12689#discussion_r1363626228


##########
lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java:
##########
@@ -64,64 +67,124 @@ public final class TaskExecutor {
    * @param <T> the return type of the task execution
    */
   public <T> List<T> invokeAll(Collection<Callable<T>> callables) throws 
IOException {
-    List<Task<T>> tasks = new ArrayList<>(callables.size());
-    boolean runOnCallerThread = numberOfRunningTasksInCurrentThread.get() > 0;
-    for (Callable<T> callable : callables) {
-      Task<T> task = new Task<>(callable);
-      tasks.add(task);
-      if (runOnCallerThread) {
-        task.run();
-      } else {
-        executor.execute(task);
+    TaskGroup<T> taskGroup = new TaskGroup<>(callables);
+    return taskGroup.invokeAll(executor);
+  }
+
+  /**
+   * Holds all the sub-tasks that a certain operation gets split into as it 
gets parallelized and
+   * exposes the ability to invoke such tasks and wait for them all to 
complete their execution and
+   * provide their results. Ensures that each task does not get parallelized 
further: this is
+   * important to avoid a deadlock in situations where one executor thread 
waits on other executor
+   * threads to complete before it can progress. This happens in situations 
where for instance
+   * {@link Query#createWeight(IndexSearcher, ScoreMode, float)} is called as 
part of searching each
+   * slice, like {@link TopFieldCollector#populateScores(ScoreDoc[], 
IndexSearcher, Query)} does.
+   * Additionally, if one task throws an exception, all other tasks from the 
same group are
+   * cancelled, to avoid needless computation as their results would not be 
exposed anyways. Creates
+   * one {@link FutureTask} for each {@link Callable} provided
+   *
+   * @param <T> the return type of all the callables
+   */
+  private static final class TaskGroup<T> {
+    private final Collection<RunnableFuture<T>> futures;
+
+    TaskGroup(Collection<Callable<T>> callables) {
+      List<RunnableFuture<T>> tasks = new ArrayList<>(callables.size());
+      for (Callable<T> callable : callables) {
+        tasks.add(createTask(callable));
       }
+      this.futures = Collections.unmodifiableCollection(tasks);
     }
 
-    Throwable exc = null;
-    final List<T> results = new ArrayList<>();
-    for (Future<T> future : tasks) {
-      try {
-        results.add(future.get());
-      } catch (InterruptedException e) {
-        var newException = new ThreadInterruptedException(e);
-        if (exc == null) {
-          exc = newException;
-        } else {
-          exc.addSuppressed(newException);
+    private FutureTask<T> createTask(Callable<T> callable) {
+      AtomicBoolean started = new AtomicBoolean(false);
+      return new FutureTask<>(callable) {

Review Comment:
   I am not sure. I guess that overriding run, setException and cancel makes it 
a lot. yet RunnableFuture has a lot of methods that I am not keen on 
implementing myself :)
   
   I could go for wrapping the callable instead , that would remove the need to 
override run and setException. It would be the same logic but integrated into 
the callable itself that is provided to FutureTask. Would you be more 
comfortable with that approach?
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to