java.lang.Object
org.elasticsearch.action.bulk.BulkProcessor2
- All Implemented Interfaces:
Closeable
,AutoCloseable
A bulk processor is a thread safe bulk processing class, allowing to easily set when to "flush" a new bulk request
(either based on number of actions, based on the size, or time), and to easily control the number of concurrent bulk
requests allowed to be executed in parallel.
In order to create a new bulk processor, use the BulkProcessor2.Builder
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A builder used to create a build an instance of a bulk processor.static interface
A listener for the execution. -
Method Summary
Modifier and TypeMethodDescriptionadd
(DeleteRequest request) Adds anDeleteRequest
to the list of actions to execute.add
(IndexRequest request) Adds anIndexRequest
to the list of actions to execute.addWithBackpressure
(IndexRequest request, Supplier<Boolean> shouldAbort) This method is similar toadd()
, except that if adding the approximate size in bytes of the request to totalBytesInFlight would exceed maxBytesInFlight then this method will block until the request can be added without exceeding maxBytesInFlight (or until shouldAbort returns false).boolean
awaitClose
(long timeout, TimeUnit unit) Closes the processor.static BulkProcessor2.Builder
builder
(BiConsumer<BulkRequest, ActionListener<BulkResponse>> consumer, BulkProcessor2.Listener listener, ThreadPool threadPool) void
close()
This method flushes any requests, prevents any future retries, closes this processor, and returns once any pending requests complete.protected void
-
Method Details
-
builder
public static BulkProcessor2.Builder builder(BiConsumer<BulkRequest, ActionListener<BulkResponse>> consumer, BulkProcessor2.Listener listener, ThreadPool threadPool) - Parameters:
consumer
- The consumer that is called to fulfil bulk operations. This consumer _must_ operate either very fast or asynchronously.listener
- The BulkProcessor2 listener that gets called on bulk eventsthreadPool
- The threadpool used to schedule the flush task for this bulk processor, if flushInterval is not null.- Returns:
- the builder for BulkProcessor2
-
awaitClose
Closes the processor. Any remaining bulk actions are flushed if they can be flushed in the given time.Waits for up to the specified timeout for all bulk requests to complete then returns
- Parameters:
timeout
- The maximum time to wait for the bulk requests to completeunit
- The time unit of thetimeout
argument- Returns:
- True if the bulk processor was able to be closed in the given time, false otherwise
- Throws:
InterruptedException
- If the current thread is interrupted
-
close
public void close()This method flushes any requests, prevents any future retries, closes this processor, and returns once any pending requests complete.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
add
Adds anIndexRequest
to the list of actions to execute. Follows the same behavior ofIndexRequest
(for example, if no id is provided, one will be generated, or usage of the create flag).- Throws:
EsRejectedExecutionException
- if adding the approximate size in bytes of the request to totalBytesInFlight would exceed maxBytesInFlight
-
addWithBackpressure
public BulkProcessor2 addWithBackpressure(IndexRequest request, Supplier<Boolean> shouldAbort) throws EsRejectedExecutionException This method is similar toadd()
, except that if adding the approximate size in bytes of the request to totalBytesInFlight would exceed maxBytesInFlight then this method will block until the request can be added without exceeding maxBytesInFlight (or until shouldAbort returns false). This method should be used carefully, ideally from a single thread. This is because calling it with multiple threads would either hang up all the threads in the pool (in the case of a bounded thread pool) or effectively create an unbounded queue (in the case of an unbounded thread pool).- Parameters:
request
- The request to add to a batch to be consumedshouldAbort
- If this returns true then this method bails out with an EsRejectedExecutionException- Returns:
- this BulkProcessor2
- Throws:
EsRejectedExecutionException
- if shouldAbort returns true before the request has been added to a batch
-
add
Adds anDeleteRequest
to the list of actions to execute.- Throws:
EsRejectedExecutionException
- if adding the approximate size in bytes of the request to totalBytesInFlight would exceed maxBytesInFlight
-
ensureOpen
protected void ensureOpen()
-