shubhamvishu commented on code in PR #12183: URL: https://github.com/apache/lucene/pull/12183#discussion_r1329306960
########## lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java: ########## @@ -64,4 +68,46 @@ final <T> List<T> invokeAll(Collection<RunnableFuture<T>> tasks) throws IOExcept } return results; } + + /** + * Execute all the tasks provided as an argument concurrently only if it is a {@link + * ThreadPoolExecutor} and the current thread invoking this is not a {@link ThreadPoolExecutor} + * thread, else run all the tasks sequentially, wait for them to complete and return the obtained + * results. + * + * @param tasks the tasks to execute + * @return a list containing the results from the tasks execution + * @param <T> the return type of the task execution + */ + public final <T> List<T> invokeAllWithThreadPoolExecutor(Collection<RunnableFuture<T>> tasks) + throws IOException { + boolean executeOnCallerThread = + StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES) + .walk( + (stream) -> + stream.anyMatch( + frame -> + frame.getClassName().contains(THREAD_POOL_EXECUTOR_WORKER_CLASS) + && frame.getMethodName().contains(RUN_METHOD))); + if (executor instanceof ThreadPoolExecutor && executeOnCallerThread == false) { + for (Runnable task : tasks) { + executor.execute(task); + } + } else { + for (Runnable task : tasks) { + task.run(); + } + } + final List<T> results = new ArrayList<>(); + for (Future<T> future : tasks) { + try { + results.add(future.get()); + } catch (InterruptedException e) { + throw new ThreadInterruptedException(e); + } catch (ExecutionException e) { + throw IOUtils.rethrowAlways(e.getCause()); + } + } + return results; Review Comment: The idea here was to not restrict any other type of executor that might not deadlock for this situation and only apply this to `ThreadPoolExecutor` till we have something concrete that addresses #12438. I really liked your idea to generalize it to every executor implementations but are we okay to restrict every executor implementation to 1 level of parallelism always? If yes, that indeed seems like a more cleaner and better approach to me. I think it pretty straightforward with your PR to also maybe even increase this level of parallelism which in this case would mean the more calls in stack the slower it would be. -- 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