java.lang.Object
org.apache.hadoop.hdfs.server.federation.router.async.utils.AsyncForEachRun<I,R>
Type Parameters:
I - the type of the elements being iterated over
R - the type of the final result after applying the thenApply function
All Implemented Interfaces:
Async<R>, AsyncRun<R>

public class AsyncForEachRun<I,R> extends Object implements AsyncRun<R>
The AsyncForEachRun class is part of the asynchronous operation utilities within the Hadoop Distributed File System (HDFS) Federation router. It provides the functionality to perform asynchronous operations on each element of an Iterator, applying a given async function.

This class is designed to work with other asynchronous interfaces and utility classes to enable complex asynchronous workflows. It allows for non-blocking execution of tasks, which can improve the performance and responsiveness of HDFS operations.

The class implements the AsyncRun interface, which means it can be used in asynchronous task chains. It maintains an Iterator of elements to process, an asyncDoOnce to apply to each element.

The run method initiates the asynchronous operation, and the doOnce method recursively applies the asyncDoOnce to each element and handles the results. If the shouldBreak flag is set, the operation is completed with the current result.

AsyncForEachRun is used to implement the following semantics:

 
 for (I element : elements) {
     R result = asyncDoOnce(element);
 }
 return result;
 
 
See Also:
  • Constructor Details

    • AsyncForEachRun

      public AsyncForEachRun()
  • Method Details

    • run

      public void run()
      Initiates the asynchronous foreach operation by starting the iteration process over the elements provided by the iterator. This method sets up the initial call to doOnce(R) with a null result, which begins the recursive application of the async function to each element of the iterator.

      This method is an implementation of the AsyncRun interface's run method, allowing it to be used in a chain of asynchronous operations. It is responsible for starting the asynchronous processing and handling the completion of the operation through the internal CompletableFuture.

      If an exception occurs during the first call to doOnce, the exception is caught and the internal CompletableFuture is completed exceptionally with a CompletionException wrapping the original IOException.

      After initiating the operation, the method sets the current thread's Async CompletableFuture by calling Async.setCurCompletableFuture(CompletableFuture) with the internal result CompletableFuture. This allows other parts of the asynchronous workflow to chain further operations or handle the final result once the foreach loop completes.

      Specified by:
      run in interface AsyncRun<I>
      See Also:
    • breakNow

      public void breakNow()
      Triggers the termination of the current asynchronous iteration.

      This method is used to break out of the asynchronous for-each loop prematurely. It sets a flag that indicates the iteration should be terminated at the earliest opportunity. This is particularly useful when the processing logic determines that further iteration is unnecessary or when a specific condition has been met.

      Once this method is called, the next time the loop is about to process a new element, it will check the flag and cease operation, allowing the application to move on to the next step or complete the task.

    • forEach

      public AsyncForEachRun<I,R> forEach(Iterator<I> forEach)
      Sets the Iterator for the elements to be processed in the asynchronous operation.
      Parameters:
      forEach - The Iterator over the elements.
      Returns:
      The current AsyncForEachRun instance for chaining.
    • asyncDo

      public AsyncForEachRun<I,R> asyncDo(AsyncBiFunction<AsyncForEachRun<I,R>,I,R> asyncDo)
      Sets the async function to apply to each element from the iterator.
      Parameters:
      asyncDo - The async function.
      Returns:
      The current AsyncForEachRun instance for chaining.