Module org.elasticsearch.server
Class ThrottledIterator<T>
java.lang.Object
org.elasticsearch.common.util.concurrent.ThrottledIterator<T>
- All Implemented Interfaces:
Closeable,AutoCloseable,Releasable
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static <T> voidrun(Iterator<T> iterator, BiConsumer<Releasable, T> itemConsumer, int maxConcurrency, Runnable onCompletion) Iterate through the given collection, performing an operation on each item which may fork background tasks, but with a limit on the number of such background tasks running concurrently to avoid overwhelming the rest of the system (e.g.
-
Method Details
-
run
public static <T> void run(Iterator<T> iterator, BiConsumer<Releasable, T> itemConsumer, int maxConcurrency, Runnable onCompletion) Iterate through the given collection, performing an operation on each item which may fork background tasks, but with a limit on the number of such background tasks running concurrently to avoid overwhelming the rest of the system (e.g. starving other work of access to an executor).- Parameters:
iterator- The items to iterate. May be accessed by multiple threads, but accesses are always strictly sequential: for any two method callsM1andM2on this iterator, either the return ofM1_happens-before_ the invocation ofM2or vice versa.itemConsumer- The operation to perform on each item. Each operation receives aReleasableresource which can be used to track the execution of any background tasks spawned for this item and which must be closed when all processing of the item has finished. If theiteratorhas not been fully consumed then closing an operation's resource triggers the start of an operation on the next item.This operation may run on the thread which originally called
run(java.util.Iterator<T>, java.util.function.BiConsumer<org.elasticsearch.core.Releasable, T>, int, java.lang.Runnable), if this method has not yet returned. Otherwise, it will run on a thread on which a background task previously closed its tracking resource.This operation must not throw any exceptions.
maxConcurrency- The maximum number of ongoing operations at any time.onCompletion- Executed when all items are completed.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceReleasable
-