Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TaskExecutor public #12574

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading