Class AbstractFuture<V>

  • All Implemented Interfaces:
    com.google.common.util.concurrent.ListenableFuture<V>, io.netty.util.concurrent.Future<V>, java.util.concurrent.Future<V>, Awaitable, Future<V>
    Direct Known Subclasses:
    AsyncFuture, SyncFuture

    public abstract class AbstractFuture<V>
    extends java.lang.Object
    implements Future<V>
    Our default Future implementation, with all state being managed without locks (except those used by the JVM). Some implementation comments versus Netty's default promise: - We permit efficient initial state declaration, avoiding unnecessary CAS or lock acquisitions when mutating a Promise we are ourselves constructing (and can easily add more; only those we use have been added) - We guarantee the order of invocation of listeners (and callbacks etc, and with respect to each other) - We save some space when registering listeners, especially if there is only one listener, as we perform no extra allocations in this case. - We implement our invocation list as a concurrent stack, that is cleared on notification - We handle special values slightly differently. - We do not use a special value for null, instead using a special value to indicate the result has not been set. This means that once isSuccess() holds, the result must be a correctly typed object (modulo generics pitfalls). - All special values are also instances of FailureHolder, which simplifies a number of the logical conditions.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder CANCELLED  
      protected static org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder UNCANCELLABLE  
      protected static org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder UNSET  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        AbstractFuture()  
      protected AbstractFuture​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
      protected AbstractFuture​(java.lang.Throwable immediateFailure)  
      protected AbstractFuture​(org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder initialState)  
      protected AbstractFuture​(org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder initialState, io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
      protected AbstractFuture​(V immediateSuccess)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      AbstractFuture<V> addCallback​(com.google.common.util.concurrent.FutureCallback<? super V> callback)
      Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      AbstractFuture<V> addCallback​(com.google.common.util.concurrent.FutureCallback<? super V> callback, java.util.concurrent.Executor executor)
      Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      AbstractFuture<V> addCallback​(java.util.function.BiConsumer<? super V,​java.lang.Throwable> callback)
      Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      Future<V> addCallback​(java.util.function.BiConsumer<? super V,​java.lang.Throwable> callback, java.util.concurrent.Executor executor)
      Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively
      AbstractFuture<V> addCallback​(java.util.function.Consumer<? super V> onSuccess, java.util.function.Consumer<? super java.lang.Throwable> onFailure)
      Support more fluid version of Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) See addListener(GenericFutureListener) for ordering semantics.
      AbstractFuture<V> addCallback​(java.util.function.Consumer<? super V> onSuccess, java.util.function.Consumer<? super java.lang.Throwable> onFailure, java.util.concurrent.Executor executor)
      Support more fluid version of Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) See addListener(GenericFutureListener) for ordering semantics.
      Future<V> addListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
      Add a listener to be invoked once this future completes.
      void addListener​(java.lang.Runnable task)
      Add a listener to be invoked once this future completes.
      void addListener​(java.lang.Runnable task, java.util.concurrent.Executor executor)
      Add a listener to be invoked once this future completes.
      Future<V> addListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)  
      boolean await​(long timeout, java.util.concurrent.TimeUnit unit)
      Await for the specified period, throwing any interrupt.
      Future<V> awaitThrowUncheckedOnInterrupt()
      Wait for this future to complete Awaitable.awaitThrowUncheckedOnInterrupt()
      boolean awaitThrowUncheckedOnInterrupt​(long time, java.util.concurrent.TimeUnit units)
      Await for the specified period, throwing any interrupt as an unchecked exception.
      Future<V> awaitUninterruptibly()
      Wait for this future to complete Awaitable.awaitUninterruptibly()
      boolean awaitUninterruptibly​(long timeout, java.util.concurrent.TimeUnit unit)
      Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit).
      boolean awaitUntilThrowUncheckedOnInterrupt​(long nanoTimeDeadline)
      Await until the deadline (in nanoTime), throwing any interrupt as an unchecked exception.
      boolean awaitUntilUninterruptibly​(long nanoTimeDeadline)
      Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit).
      boolean cancel​(boolean b)  
      java.lang.Throwable cause()  
      protected java.lang.String description()  
      protected <T> Future<T> flatMap​(AbstractFuture<T> result, java.util.function.Function<? super V,​? extends Future<T>> flatMapper, java.util.concurrent.Executor executor)
      Support Futures.transformAsync(ListenableFuture, AsyncFunction, Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      V get()  
      V get​(long timeout, java.util.concurrent.TimeUnit unit)  
      V getNow()
      if isSuccess(), returns the value, otherwise returns null
      protected V getWhenDone()
      Shared implementation of get() after suitable await(); assumes isDone(), and returns either the success result or throws the suitable exception under failure
      boolean isCancellable()  
      boolean isCancelled()  
      boolean isDone()  
      boolean isSuccess()  
      protected boolean isUncancellable()  
      <T> Future<T> map​(java.util.function.Function<? super V,​? extends T> mapper)
      Support Futures.transformAsync(ListenableFuture, AsyncFunction, Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      protected <T> Future<T> map​(AbstractFuture<T> result, java.util.function.Function<? super V,​? extends T> mapper, java.util.concurrent.Executor executor)
      Support Futures.transform(ListenableFuture, com.google.common.base.Function, Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      java.util.concurrent.Executor notifyExecutor()  
      Future<V> removeListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
      Future<V> removeListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)  
      protected boolean setUncancellable()  
      protected boolean setUncancellableExclusive()  
      java.lang.String toString()  
      protected boolean tryFailure​(java.lang.Throwable throwable)  
      protected boolean trySuccess​(V v)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • UNSET

        protected static final org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder UNSET
      • UNCANCELLABLE

        protected static final org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder UNCANCELLABLE
      • CANCELLED

        protected static final org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder CANCELLED
    • Constructor Detail

      • AbstractFuture

        protected AbstractFuture​(org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder initialState)
      • AbstractFuture

        public AbstractFuture()
      • AbstractFuture

        protected AbstractFuture​(V immediateSuccess)
      • AbstractFuture

        protected AbstractFuture​(java.lang.Throwable immediateFailure)
      • AbstractFuture

        protected AbstractFuture​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
      • AbstractFuture

        protected AbstractFuture​(org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder initialState,
                                 io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
    • Method Detail

      • notifyExecutor

        public java.util.concurrent.Executor notifyExecutor()
        Specified by:
        notifyExecutor in interface Future<V>
      • trySuccess

        protected boolean trySuccess​(V v)
      • tryFailure

        protected boolean tryFailure​(java.lang.Throwable throwable)
      • setUncancellable

        protected boolean setUncancellable()
      • setUncancellableExclusive

        protected boolean setUncancellableExclusive()
      • isUncancellable

        protected boolean isUncancellable()
      • cancel

        public boolean cancel​(boolean b)
        Specified by:
        cancel in interface io.netty.util.concurrent.Future<V>
        Specified by:
        cancel in interface java.util.concurrent.Future<V>
      • isSuccess

        public boolean isSuccess()
        Specified by:
        isSuccess in interface io.netty.util.concurrent.Future<V>
      • isCancelled

        public boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<V>
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface java.util.concurrent.Future<V>
      • isCancellable

        public boolean isCancellable()
        Specified by:
        isCancellable in interface io.netty.util.concurrent.Future<V>
      • cause

        public java.lang.Throwable cause()
        Specified by:
        cause in interface io.netty.util.concurrent.Future<V>
      • getNow

        public V getNow()
        if isSuccess(), returns the value, otherwise returns null
        Specified by:
        getNow in interface io.netty.util.concurrent.Future<V>
      • getWhenDone

        protected V getWhenDone()
                         throws java.util.concurrent.ExecutionException
        Shared implementation of get() after suitable await(); assumes isDone(), and returns either the success result or throws the suitable exception under failure
        Throws:
        java.util.concurrent.ExecutionException
      • get

        public V get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.util.concurrent.Future<V>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • get

        public V get​(long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException,
                     java.util.concurrent.TimeoutException
        Specified by:
        get in interface java.util.concurrent.Future<V>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • addCallback

        public AbstractFuture<V> addCallback​(com.google.common.util.concurrent.FutureCallback<? super V> callback)
        Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively See addListener(GenericFutureListener) for ordering semantics.
        Specified by:
        addCallback in interface Future<V>
      • addCallback

        public AbstractFuture<V> addCallback​(java.util.function.BiConsumer<? super V,​java.lang.Throwable> callback)
        Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively See addListener(GenericFutureListener) for ordering semantics.
        Specified by:
        addCallback in interface Future<V>
      • addCallback

        public Future<V> addCallback​(java.util.function.BiConsumer<? super V,​java.lang.Throwable> callback,
                                     java.util.concurrent.Executor executor)
        Description copied from interface: Future
        Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively
        Specified by:
        addCallback in interface Future<V>
      • addCallback

        public AbstractFuture<V> addCallback​(com.google.common.util.concurrent.FutureCallback<? super V> callback,
                                             java.util.concurrent.Executor executor)
        Support Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) natively See addListener(GenericFutureListener) for ordering semantics.
        Specified by:
        addCallback in interface Future<V>
      • addCallback

        public AbstractFuture<V> addCallback​(java.util.function.Consumer<? super V> onSuccess,
                                             java.util.function.Consumer<? super java.lang.Throwable> onFailure)
        Support more fluid version of Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) See addListener(GenericFutureListener) for ordering semantics.
        Specified by:
        addCallback in interface Future<V>
      • map

        public <T> Future<T> map​(java.util.function.Function<? super V,​? extends T> mapper)
        Support Futures.transformAsync(ListenableFuture, AsyncFunction, Executor) natively See addListener(GenericFutureListener) for ordering semantics.
        Specified by:
        map in interface Future<V>
      • addCallback

        public AbstractFuture<V> addCallback​(java.util.function.Consumer<? super V> onSuccess,
                                             java.util.function.Consumer<? super java.lang.Throwable> onFailure,
                                             java.util.concurrent.Executor executor)
        Support more fluid version of Futures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor) See addListener(GenericFutureListener) for ordering semantics.
        Specified by:
        addCallback in interface Future<V>
      • map

        protected <T> Future<T> map​(AbstractFuture<T> result,
                                    java.util.function.Function<? super V,​? extends T> mapper,
                                    @Nullable
                                    java.util.concurrent.Executor executor)
        Support Futures.transform(ListenableFuture, com.google.common.base.Function, Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      • flatMap

        protected <T> Future<T> flatMap​(AbstractFuture<T> result,
                                        java.util.function.Function<? super V,​? extends Future<T>> flatMapper,
                                        @Nullable
                                        java.util.concurrent.Executor executor)
        Support Futures.transformAsync(ListenableFuture, AsyncFunction, Executor) natively See addListener(GenericFutureListener) for ordering semantics.
      • addListener

        public Future<V> addListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
        Add a listener to be invoked once this future completes. Listeners are submitted to notifyExecutor() in the order they are added (or the specified executor in the case of addListener(Runnable, Executor). if notifyExecutor() is unset, they are invoked in the order they are added. The ordering holds across all variants of this method.
        Specified by:
        addListener in interface io.netty.util.concurrent.Future<V>
        Specified by:
        addListener in interface Future<V>
      • addListener

        public void addListener​(java.lang.Runnable task,
                                @Nullable
                                java.util.concurrent.Executor executor)
        Add a listener to be invoked once this future completes. Listeners are submitted to their #executor (or notifyExecutor()) in the order they are added; if notifyExecutor() is unset, they are invoked in the order they are added. The ordering holds across all variants of this method.
        Specified by:
        addListener in interface Future<V>
        Specified by:
        addListener in interface com.google.common.util.concurrent.ListenableFuture<V>
      • addListener

        public void addListener​(java.lang.Runnable task)
        Add a listener to be invoked once this future completes. Listeners are submitted to notifyExecutor() in the order they are added (or the specified executor in the case of addListener(Runnable, Executor). if notifyExecutor() is unset, they are invoked in the order they are added. The ordering holds across all variants of this method.
        Specified by:
        addListener in interface Future<V>
      • addListeners

        public Future<V> addListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
        Specified by:
        addListeners in interface io.netty.util.concurrent.Future<V>
        Specified by:
        addListeners in interface Future<V>
      • removeListener

        public Future<V> removeListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
        Specified by:
        removeListener in interface io.netty.util.concurrent.Future<V>
        Specified by:
        removeListener in interface Future<V>
      • removeListeners

        public Future<V> removeListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
        Specified by:
        removeListeners in interface io.netty.util.concurrent.Future<V>
        Specified by:
        removeListeners in interface Future<V>
      • await

        public boolean await​(long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Description copied from interface: Awaitable
        Await for the specified period, throwing any interrupt. No spurious wakeups.
        Specified by:
        await in interface Awaitable
        Specified by:
        await in interface io.netty.util.concurrent.Future<V>
        Returns:
        true if we were signalled, false if the timeout elapses
        Throws:
        java.lang.InterruptedException - if interrupted
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeout,
                                            java.util.concurrent.TimeUnit unit)
        Description copied from interface: Awaitable
        Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit). No spurious wakeups.
        Specified by:
        awaitUninterruptibly in interface Awaitable
        Specified by:
        awaitUninterruptibly in interface io.netty.util.concurrent.Future<V>
        Returns:
        true if we were signalled, false if the timeout elapses
      • awaitUntilUninterruptibly

        public boolean awaitUntilUninterruptibly​(long nanoTimeDeadline)
        Description copied from interface: Awaitable
        Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit). No spurious wakeups.
        Specified by:
        awaitUntilUninterruptibly in interface Awaitable
        Returns:
        true if we were signalled, false if the deadline elapsed
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • description

        protected java.lang.String description()