From 1cb0d81b40bd2bee2a254c5b0603b334879d28ca Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Wed, 20 Sep 2023 15:27:56 +0200 Subject: [PATCH] Make TaskExecutor public (#12574) TaskExecutor is currently package private. We have scenarios where we want to parallelize the execution and reuse it outside of its package, hence this commit makes it public (and experimental). Note that its constructor remains package private as it is supposed to be created by the index searcher, and later retrieved from it via the appropriate getter, which is also made public as part of this commit. Co-authored-by: gf2121 <52390227+gf2121@users.noreply.github.com> --- lucene/CHANGES.txt | 4 ++-- .../apache/lucene/search/IndexSearcher.java | 7 +++++- .../apache/lucene/search/TaskExecutor.java | 23 +++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 3fa2759e67a7..288536f297df 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -131,7 +131,6 @@ Other API Changes --------------------- -(No changes) New Features --------------------- @@ -139,7 +138,8 @@ New Features Improvements --------------------- -(No changes) +* GITHUB#12574: Make TaskExecutor public so that it can be retrieved from the searcher and used + outside of the o.a.l.search package (Luca Cavanna) Optimizations --------------------- diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java index 9aa865f351a8..6e66d5c2af19 100644 --- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java @@ -951,7 +951,12 @@ public Executor getExecutor() { return executor; } - TaskExecutor getTaskExecutor() { + /** + * Returns the {@link TaskExecutor} that this searcher relies on to execute concurrent operations + * + * @return the task executor + */ + public TaskExecutor getTaskExecutor() { return taskExecutor; } diff --git a/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java b/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java index d416d70c2f54..6e64df31400f 100644 --- a/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java +++ b/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java @@ -40,8 +40,10 @@ * calls, and not for potential {@link #invokeAll(Collection)} calls made from one of the tasks. * This is to prevent deadlock with certain types of pool based executors (e.g. {@link * java.util.concurrent.ThreadPoolExecutor}). + * + * @lucene.experimental */ -class TaskExecutor { +public final class TaskExecutor { // a static thread local is ok as long as we use a counter, which accounts for multiple // searchers holding a different TaskExecutor all backed by the same executor private static final ThreadLocal numberOfRunningTasksInCurrentThread = @@ -61,7 +63,7 @@ class TaskExecutor { * @return a list containing the results from the tasks execution * @param the return type of the task execution */ - final List invokeAll(Collection> tasks) throws IOException { + public List invokeAll(Collection> tasks) throws IOException { if (numberOfRunningTasksInCurrentThread.get() > 0) { for (Task task : tasks) { task.run(); @@ -85,11 +87,24 @@ final List invokeAll(Collection> tasks) throws IOException { return results; } - final Task createTask(Callable callable) { + /** + * Creates a task given the provided {@link Callable} + * + * @param callable the callable to be executed as part of the task + * @return the created task + * @param the return type of the task + */ + public Task createTask(Callable callable) { return new Task<>(callable); } - static class Task extends FutureTask { + /** + * Extension of {@link FutureTask} that tracks the number of tasks that are running in each + * thread. + * + * @param the return type of the task + */ + public static final class Task extends FutureTask { private Task(Callable callable) { super(callable); }