-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Extract ThrottledTaskRunner #93436
Merged
DaveCTurner
merged 3 commits into
elastic:main
from
DaveCTurner:2023-02-02-ThrottledTaskRunner
Feb 6, 2023
Merged
Extract ThrottledTaskRunner #93436
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
...r/src/main/java/org/elasticsearch/common/util/concurrent/AbstractThrottledTaskRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.common.util.concurrent; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.core.Releasable; | ||
import org.elasticsearch.core.Releasables; | ||
import org.elasticsearch.core.Strings; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* {@link AbstractThrottledTaskRunner} runs the enqueued tasks using the given executor, limiting the number of tasks that are submitted to | ||
* the executor at once. | ||
*/ | ||
public class AbstractThrottledTaskRunner<T extends ActionListener<Releasable>> { | ||
private static final Logger logger = LogManager.getLogger(AbstractThrottledTaskRunner.class); | ||
|
||
private final String taskRunnerName; | ||
// The max number of tasks that this runner will schedule to concurrently run on the executor. | ||
private final int maxRunningTasks; | ||
// As we fork off dequeued tasks to the given executor, technically the following counter represents | ||
// the number of the concurrent pollAndSpawn calls currently checking the queue for a task to run. This | ||
// doesn't necessarily correspond to currently running tasks, since a pollAndSpawn could return without | ||
// actually running a task when the queue is empty. | ||
private final AtomicInteger runningTasks = new AtomicInteger(); | ||
private final Queue<T> tasks; | ||
private final Executor executor; | ||
|
||
public AbstractThrottledTaskRunner(final String name, final int maxRunningTasks, final Executor executor, final Queue<T> taskQueue) { | ||
assert maxRunningTasks > 0; | ||
this.taskRunnerName = name; | ||
this.maxRunningTasks = maxRunningTasks; | ||
this.executor = executor; | ||
this.tasks = taskQueue; | ||
} | ||
|
||
/** | ||
* Submits a task for execution. If there are fewer than {@code maxRunningTasks} tasks currently running then this task is immediately | ||
* submitted to the executor. Otherwise this task is enqueued and will be submitted to the executor in turn on completion of some other | ||
* task. | ||
* | ||
* Tasks are executed via their {@link ActionListener#onResponse} method, receiving a {@link Releasable} which must be closed on | ||
* completion of the task. Task which are rejected from their executor are notified via their {@link ActionListener#onFailure} method. | ||
* Neither of these methods may themselves throw exceptions. | ||
*/ | ||
public void enqueueTask(final T task) { | ||
logger.trace("[{}] enqueuing task {}", taskRunnerName, task); | ||
tasks.add(task); | ||
// Try to run a task since now there is at least one in the queue. If the maxRunningTasks is | ||
// reached, the task is just enqueued. | ||
pollAndSpawn(); | ||
} | ||
|
||
/** | ||
* Allows certain tasks to force their execution, bypassing the queue-length limit on the executor. See also {@link | ||
* AbstractRunnable#isForceExecution()}. | ||
*/ | ||
protected boolean isForceExecution(@SuppressWarnings("unused") /* TODO test this */ T task) { | ||
return false; | ||
} | ||
|
||
private void pollAndSpawn() { | ||
// A pollAndSpawn attempts to run a new task. There could be many concurrent pollAndSpawn calls competing | ||
// to get a "free slot", since we attempt to run a new task on every enqueueTask call and every time an | ||
// existing task is finished. | ||
while (incrementRunningTasks()) { | ||
T task = tasks.poll(); | ||
if (task == null) { | ||
logger.trace("[{}] task queue is empty", taskRunnerName); | ||
// We have taken up a "free slot", but there are no tasks in the queue! This could happen each time a worker | ||
// sees an empty queue after running a task. Decrement to give competing pollAndSpawn calls a chance! | ||
int decremented = runningTasks.decrementAndGet(); | ||
assert decremented >= 0; | ||
// We might have blocked all competing pollAndSpawn calls. This could happen for example when | ||
// maxRunningTasks=1 and a task got enqueued just after checking the queue but before decrementing. | ||
// To be sure, return only if the queue is still empty. If the queue is not empty, this might be the | ||
// only pollAndSpawn call in progress, and returning without peeking would risk ending up with a | ||
// non-empty queue and no workers! | ||
if (tasks.peek() == null) break; | ||
} else { | ||
final boolean isForceExecution = isForceExecution(task); | ||
executor.execute(new AbstractRunnable() { | ||
private boolean rejected; // need not be volatile - if we're rejected then that happens-before calling onAfter | ||
|
||
private final Releasable releasable = Releasables.releaseOnce(() -> { | ||
// To avoid missing to run tasks that are enqueued and waiting, we check the queue again once running | ||
// a task is finished. | ||
int decremented = runningTasks.decrementAndGet(); | ||
assert decremented >= 0; | ||
|
||
if (rejected == false) { | ||
pollAndSpawn(); | ||
} | ||
}); | ||
|
||
@Override | ||
public boolean isForceExecution() { | ||
return isForceExecution; | ||
} | ||
|
||
@Override | ||
public void onRejection(Exception e) { | ||
logger.trace("[{}] task {} rejected", taskRunnerName, task); | ||
rejected = true; | ||
try { | ||
task.onFailure(e); | ||
} finally { | ||
releasable.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// should not happen | ||
logger.error(() -> Strings.format("[%s] task %s failed", taskRunnerName, task), e); | ||
assert false : e; | ||
task.onFailure(e); | ||
} | ||
|
||
@Override | ||
protected void doRun() { | ||
logger.trace("[{}] running task {}", taskRunnerName, task); | ||
task.onResponse(releasable); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return task.toString(); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// Each worker thread that runs a task, first needs to get a "free slot" in order to respect maxRunningTasks. | ||
private boolean incrementRunningTasks() { | ||
int preUpdateValue = runningTasks.getAndAccumulate(maxRunningTasks, (v, maxRunning) -> v < maxRunning ? v + 1 : v); | ||
assert preUpdateValue <= maxRunningTasks; | ||
return preUpdateValue < maxRunningTasks; | ||
} | ||
|
||
// exposed for testing | ||
int runningTasks() { | ||
return runningTasks.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
server/src/main/java/org/elasticsearch/common/util/concurrent/ThrottledTaskRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.common.util.concurrent; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.core.Releasable; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
public class ThrottledTaskRunner extends AbstractThrottledTaskRunner<ActionListener<Releasable>> { | ||
// a simple AbstractThrottledTaskRunner which fixes the task type and uses a regular FIFO blocking queue. | ||
public ThrottledTaskRunner(String name, int maxRunningTasks, Executor executor) { | ||
super(name, maxRunningTasks, executor, ConcurrentCollections.newBlockingQueue()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used anywhere? If not, could we introduce it, when it is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will be using it in #92373 which is already quite a big change so I'd like to have prerequisites like this reviewed separately. It also came up in a discussion about search execution so I wanted to have the code somewhere visible. I'm happy to delay merging it until #92373 is closer to ready tho.