You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2024/07/31 17:34:40 UTC
(lucene) branch main updated: Wrap Executor in TaskExecutor to never reject (#13622)
This is an automated email from the ASF dual-hosted git repository.
abr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new a1816e3f65c Wrap Executor in TaskExecutor to never reject (#13622)
a1816e3f65c is described below
commit a1816e3f65cda4ba4ba6c518efcf75e5b96fbeff
Author: Armin Braun <me...@obrown.io>
AuthorDate: Wed Jul 31 19:34:35 2024 +0200
Wrap Executor in TaskExecutor to never reject (#13622)
Make it so rejected tasks are execute right away on the caller thread.
Users of the API shouldn't have to worry about rejections when we don't
expose any upper limit to the task count that we put on the executor that
would help in sizing a queue for the executor.
---
.../java/org/apache/lucene/search/TaskExecutor.java | 16 +++++++++++++++-
.../org/apache/lucene/search/TestTaskExecutor.java | 20 ++++++++++++++++++++
2 files changed, 35 insertions(+), 1 deletion(-)
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 8832cf478e9..9d2176fd3af 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java
@@ -28,6 +28,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
+import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@@ -53,7 +54,20 @@ public final class TaskExecutor {
* @param executor the executor to be used for running tasks concurrently
*/
public TaskExecutor(Executor executor) {
- this.executor = Objects.requireNonNull(executor, "Executor is null");
+ Objects.requireNonNull(executor, "Executor is null");
+ this.executor =
+ r -> {
+ try {
+ executor.execute(r);
+ } catch (
+ @SuppressWarnings("unused")
+ RejectedExecutionException rejectedExecutionException) {
+ // execute directly on the current thread in case of rejection to ensure a rejecting
+ // executor only reduces parallelism and does not
+ // result in failure
+ r.run();
+ }
+ };
}
/**
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java b/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java
index 1949afbd51c..26847f60a2a 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestTaskExecutor.java
@@ -363,4 +363,24 @@ public class TestTaskExecutor extends LuceneTestCase {
assertEquals(0, throwable.getSuppressed().length);
assertEquals(throwingTask, executedTasks.get());
}
+
+ public void testTaskRejectionDoesNotFailExecution() throws Exception {
+ try (ThreadPoolExecutor threadPoolExecutor =
+ new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1))) {
+ final int taskCount = 1000; // enough tasks to cause queuing and rejections on the executor
+ final ArrayList<Callable<Void>> callables = new ArrayList<>(taskCount);
+ final AtomicInteger executedTasks = new AtomicInteger(0);
+ for (int i = 0; i < taskCount; i++) {
+ callables.add(
+ () -> {
+ executedTasks.incrementAndGet();
+ return null;
+ });
+ }
+ final TaskExecutor taskExecutor = new TaskExecutor(threadPoolExecutor);
+ var res = taskExecutor.invokeAll(callables);
+ assertEquals(taskCount, res.size());
+ assertEquals(taskCount, executedTasks.get());
+ }
+ }
}