Skip to content

Commit

Permalink
Make TaskExecutor public (#12574)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
javanna and gf2121 committed Sep 20, 2023
1 parent 2420709 commit 1cb0d81
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ Other

API Changes
---------------------
(No changes)

New Features
---------------------
(No changes)

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
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
23 changes: 19 additions & 4 deletions lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> numberOfRunningTasksInCurrentThread =
Expand All @@ -61,7 +63,7 @@ class TaskExecutor {
* @return a list containing the results from the tasks execution
* @param <T> the return type of the task execution
*/
final <T> List<T> invokeAll(Collection<Task<T>> tasks) throws IOException {
public <T> List<T> invokeAll(Collection<Task<T>> tasks) throws IOException {
if (numberOfRunningTasksInCurrentThread.get() > 0) {
for (Task<T> task : tasks) {
task.run();
Expand All @@ -85,11 +87,24 @@ final <T> List<T> invokeAll(Collection<Task<T>> tasks) throws IOException {
return results;
}

final <C> Task<C> createTask(Callable<C> 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 <C> the return type of the task
*/
public <C> Task<C> createTask(Callable<C> callable) {
return new Task<>(callable);
}

static class Task<V> extends FutureTask<V> {
/**
* Extension of {@link FutureTask} that tracks the number of tasks that are running in each
* thread.
*
* @param <V> the return type of the task
*/
public static final class Task<V> extends FutureTask<V> {
private Task(Callable<V> callable) {
super(callable);
}
Expand Down

0 comments on commit 1cb0d81

Please sign in to comment.