public final class BatchingExecutor<W extends BatchingExecutor.WorkSet<? super P>,P extends BatchingExecutor.WorkProcessor> extends Object
Useful when works can be merged together for optimization purposes (bulking in Elasticsearch), or when they should never be executed in parallel (writes to a Lucene index).
| Modifier and Type | Class and Description |
|---|---|
static interface |
BatchingExecutor.WorkProcessor |
static interface |
BatchingExecutor.WorkSet<P extends BatchingExecutor.WorkProcessor> |
| Constructor and Description |
|---|
BatchingExecutor(String name,
P processor,
int maxTasksPerBatch,
boolean fair,
ErrorHandler errorHandler) |
| Modifier and Type | Method and Description |
|---|---|
void |
awaitCompletion()
Block the current thread until the executor has completed all pending worksets.
|
void |
start()
Start the executor, allowing works to be submitted
through
submit(WorkSet). |
void |
stop()
Stop the executor, no longer allowing works to be submitted
through
submit(WorkSet). |
void |
submit(W workset)
Submit a set of works for execution.
|
public BatchingExecutor(String name, P processor, int maxTasksPerBatch, boolean fair, ErrorHandler errorHandler)
name - The name of the executor thread (and of this executor when reporting errors)processor - A task processor. May not be thread-safe.maxTasksPerBatch - The maximum number of tasks to process in a single batch.
Higher values mean more opportunity for the processor to optimize execution, but higher heap consumption.fair - if true tasks are always submitted to the
processor in FIFO order, if false tasks submitted
when the internal queue is full may be submitted out of order.errorHandler - An error handler to report failures of the background thread.public void start()
submit(WorkSet).public void stop()
submit(WorkSet).
This will attempt to forcibly terminate currently executing works, and will remove pending works from the queue.
public void submit(W workset) throws InterruptedException
Must not be called when the executor is stopped.
workset - A set of works to execute.InterruptedException - If the current thread is interrupted while enqueuing the workset.public void awaitCompletion()
throws InterruptedException
Tasks submitted to the executor after entering this method may delay the wait. TODO HSEARCH-3576 we should use child executors, sharing the same execution service but each with its own child phaser, allowing each child executor to only wait until all of its *own* worksets are completed. That would however require to register/deregister the phaser for each single workset, instead of for each scheduling of the processing like we do now. Phaser only support up to 65535 parties, so we would may need to use multiple phasers per child executor...
InterruptedException - If the current thread is interrupted while waiting.Copyright © 2006-2019 Red Hat, Inc. and others. Licensed under the GNU Lesser General Public License (LGPL), version 2.1 or later.