Class MultipleFuturesAvailabilityHelper


  • @Internal
    public class MultipleFuturesAvailabilityHelper
    extends Object
    This class is semi-thread safe. Only method notifyCompletion() is allowed to be executed from an outside of the task thread.

    It solves a problem of a potential memory leak as described in FLINK-25728. In short we have to ensure, that if there is one input (future) that rarely (or never) completes, that such future would not prevent previously returned combined futures (like CompletableFuture.anyOf(CompletableFuture[]) from being garbage collected. Additionally, we don't want to accumulate more and more completion stages on such rarely completed future, so we are registering CompletableFuture.thenRun(Runnable) only if it has not already been done.

    Note resetToUnAvailable() doesn't de register previously registered futures. If future was registered in the past, but for whatever reason now it is not, such future can still complete the newly created future.

    It might be no longer needed after upgrading to JDK9 (https://bugs.openjdk.java.net/browse/JDK-8160402).

    • Constructor Detail

      • MultipleFuturesAvailabilityHelper

        public MultipleFuturesAvailabilityHelper​(int size)
    • Method Detail

      • getAvailableFuture

        public CompletableFuture<?> getAvailableFuture()
        Returns:
        combined future using anyOf logic
      • resetToUnAvailable

        public void resetToUnAvailable()
      • anyOf

        public void anyOf​(int idx,
                          CompletableFuture<?> availabilityFuture)
        Combine availabilityFuture using anyOf logic with other previously registered futures.