Class AbstractFuture<V>

java.lang.Object
org.apache.cassandra.utils.concurrent.AbstractFuture<V>
All Implemented Interfaces:
com.google.common.util.concurrent.ListenableFuture<V>, io.netty.util.concurrent.Future<V>, Future<V>, Awaitable, Future<V>
Direct Known Subclasses:
AsyncFuture, SyncFuture

public abstract class AbstractFuture<V> extends 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 Details

    • 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 Details

    • AbstractFuture

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

      public AbstractFuture()
    • AbstractFuture

      protected AbstractFuture(V immediateSuccess)
    • AbstractFuture

      protected AbstractFuture(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 Details

    • notifyExecutor

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

      protected boolean trySuccess(V v)
    • tryFailure

      protected boolean tryFailure(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 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 Future<V>
    • isDone

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

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

      public 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 ExecutionException
      Shared implementation of get() after suitable await(); assumes isDone(), and returns either the success result or throws the suitable exception under failure
      Throws:
      ExecutionException
    • get

      Specified by:
      get in interface Future<V>
      Throws:
      InterruptedException
      ExecutionException
    • get

      public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Specified by:
      get in interface Future<V>
      Throws:
      InterruptedException
      ExecutionException
      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(BiConsumer<? super V,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(BiConsumer<? super V,Throwable> callback, 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, 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(Consumer<? super V> onSuccess, Consumer<? super 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(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(Consumer<? super V> onSuccess, Consumer<? super Throwable> onFailure, 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, Function<? super V,? extends T> mapper, @Nullable 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, Function<? super V,? extends Future<T>> flatMapper, @Nullable 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(Runnable task, @Nullable 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(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, TimeUnit unit) throws 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:
      InterruptedException - if interrupted
    • awaitThrowUncheckedOnInterrupt

      public boolean awaitThrowUncheckedOnInterrupt(long time, TimeUnit units) throws UncheckedInterruptedException
      Description copied from interface: Awaitable
      Await for the specified period, throwing any interrupt as an unchecked exception. No spurious wakeups.
      Specified by:
      awaitThrowUncheckedOnInterrupt in interface Awaitable
      Returns:
      true if we were signalled, false if the timeout elapses
      Throws:
      UncheckedInterruptedException - if interrupted
    • awaitUninterruptibly

      public boolean awaitUninterruptibly(long timeout, 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
    • awaitUntilThrowUncheckedOnInterrupt

      public boolean awaitUntilThrowUncheckedOnInterrupt(long nanoTimeDeadline) throws UncheckedInterruptedException
      Description copied from interface: Awaitable
      Await until the deadline (in nanoTime), throwing any interrupt as an unchecked exception. No spurious wakeups.
      Specified by:
      awaitUntilThrowUncheckedOnInterrupt in interface Awaitable
      Returns:
      true if we were signalled, false if the deadline elapsed
      Throws:
      UncheckedInterruptedException - if interrupted
    • 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
    • awaitUninterruptibly

      public Future<V> awaitUninterruptibly()
      Wait for this future to complete Awaitable.awaitUninterruptibly()
      Specified by:
      awaitUninterruptibly in interface Awaitable
      Specified by:
      awaitUninterruptibly in interface io.netty.util.concurrent.Future<V>
      Specified by:
      awaitUninterruptibly in interface Future<V>
    • awaitThrowUncheckedOnInterrupt

      public Future<V> awaitThrowUncheckedOnInterrupt() throws UncheckedInterruptedException
      Wait for this future to complete Awaitable.awaitThrowUncheckedOnInterrupt()
      Specified by:
      awaitThrowUncheckedOnInterrupt in interface Awaitable
      Specified by:
      awaitThrowUncheckedOnInterrupt in interface Future<V>
      Throws:
      UncheckedInterruptedException - if interrupted
    • toString

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

      protected String description()