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>,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 defaultFutureimplementation, 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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.cassandra.utils.concurrent.Awaitable
Awaitable.AbstractAwaitable, Awaitable.AsyncAwaitable, Awaitable.Defaults, Awaitable.SyncAwaitable
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolderCANCELLEDprotected static org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolderUNCANCELLABLEprotected static org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolderUNSET
-
Constructor Summary
Constructors Modifier Constructor Description AbstractFuture()protectedAbstractFuture(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)protectedAbstractFuture(java.lang.Throwable immediateFailure)protectedAbstractFuture(org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder initialState)protectedAbstractFuture(org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder initialState, io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)protectedAbstractFuture(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)SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.AbstractFuture<V>addCallback(com.google.common.util.concurrent.FutureCallback<? super V> callback, java.util.concurrent.Executor executor)SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.AbstractFuture<V>addCallback(java.util.function.BiConsumer<? super V,java.lang.Throwable> callback)SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.Future<V>addCallback(java.util.function.BiConsumer<? super V,java.lang.Throwable> callback, java.util.concurrent.Executor executor)SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)nativelyAbstractFuture<V>addCallback(java.util.function.Consumer<? super V> onSuccess, java.util.function.Consumer<? super java.lang.Throwable> onFailure)Support more fluid version ofFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)SeeaddListener(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 ofFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)SeeaddListener(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.voidaddListener(java.lang.Runnable task)Add a listener to be invoked once this future completes.voidaddListener(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)booleanawait(long timeout, java.util.concurrent.TimeUnit unit)Await for the specified period, throwing any interrupt.Future<V>awaitThrowUncheckedOnInterrupt()Wait for this future to completeAwaitable.awaitThrowUncheckedOnInterrupt()booleanawaitThrowUncheckedOnInterrupt(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 completeAwaitable.awaitUninterruptibly()booleanawaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit).booleanawaitUntilThrowUncheckedOnInterrupt(long nanoTimeDeadline)Await until the deadline (in nanoTime), throwing any interrupt as an unchecked exception.booleanawaitUntilUninterruptibly(long nanoTimeDeadline)Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit).booleancancel(boolean b)java.lang.Throwablecause()protected java.lang.Stringdescription()protected <T> Future<T>flatMap(AbstractFuture<T> result, java.util.function.Function<? super V,? extends Future<T>> flatMapper, java.util.concurrent.Executor executor)SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.Vget()Vget(long timeout, java.util.concurrent.TimeUnit unit)VgetNow()if isSuccess(), returns the value, otherwise returns nullprotected VgetWhenDone()Shared implementation of get() after suitable await(); assumes isDone(), and returns either the success result or throws the suitable exception under failurebooleanisCancellable()booleanisCancelled()booleanisDone()booleanisSuccess()protected booleanisUncancellable()<T> Future<T>map(java.util.function.Function<? super V,? extends T> mapper)SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(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)SupportFutures.transform(ListenableFuture, com.google.common.base.Function, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.java.util.concurrent.ExecutornotifyExecutor()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 booleansetUncancellable()protected booleansetUncancellableExclusive()java.lang.StringtoString()protected booleantryFailure(java.lang.Throwable throwable)protected booleantrySuccess(V v)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.cassandra.utils.concurrent.Awaitable
awaitUntil
-
Methods inherited from interface org.apache.cassandra.utils.concurrent.Future
await, await, awaitUninterruptibly, flatMap, flatMap, map, rethrowIfFailed, sync, syncThrowUncheckedOnInterrupt, syncUninterruptibly
-
-
-
-
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:
notifyExecutorin interfaceFuture<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)
-
isSuccess
public boolean isSuccess()
- Specified by:
isSuccessin interfaceio.netty.util.concurrent.Future<V>
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<V>
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<V>
-
isCancellable
public boolean isCancellable()
- Specified by:
isCancellablein interfaceio.netty.util.concurrent.Future<V>
-
cause
public java.lang.Throwable cause()
- Specified by:
causein interfaceio.netty.util.concurrent.Future<V>
-
getNow
public V getNow()
if isSuccess(), returns the value, otherwise returns null- Specified by:
getNowin interfaceio.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:
getin interfacejava.util.concurrent.Future<V>- Throws:
java.lang.InterruptedExceptionjava.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:
getin interfacejava.util.concurrent.Future<V>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
addCallback
public AbstractFuture<V> addCallback(com.google.common.util.concurrent.FutureCallback<? super V> callback)
SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.- Specified by:
addCallbackin interfaceFuture<V>
-
addCallback
public AbstractFuture<V> addCallback(java.util.function.BiConsumer<? super V,java.lang.Throwable> callback)
SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.- Specified by:
addCallbackin interfaceFuture<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:FutureSupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively- Specified by:
addCallbackin interfaceFuture<V>
-
addCallback
public AbstractFuture<V> addCallback(com.google.common.util.concurrent.FutureCallback<? super V> callback, java.util.concurrent.Executor executor)
SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.- Specified by:
addCallbackin interfaceFuture<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 ofFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)SeeaddListener(GenericFutureListener)for ordering semantics.- Specified by:
addCallbackin interfaceFuture<V>
-
map
public <T> Future<T> map(java.util.function.Function<? super V,? extends T> mapper)
SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.
-
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 ofFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)SeeaddListener(GenericFutureListener)for ordering semantics.- Specified by:
addCallbackin interfaceFuture<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)
SupportFutures.transform(ListenableFuture, com.google.common.base.Function, Executor)natively SeeaddListener(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)
SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(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 tonotifyExecutor()in the order they are added (or the specified executor in the case ofaddListener(Runnable, Executor). ifnotifyExecutor()is unset, they are invoked in the order they are added. The ordering holds across all variants of this method.- Specified by:
addListenerin interfaceio.netty.util.concurrent.Future<V>- Specified by:
addListenerin interfaceFuture<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(ornotifyExecutor()) in the order they are added; ifnotifyExecutor()is unset, they are invoked in the order they are added. The ordering holds across all variants of this method.- Specified by:
addListenerin interfaceFuture<V>- Specified by:
addListenerin interfacecom.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 tonotifyExecutor()in the order they are added (or the specified executor in the case ofaddListener(Runnable, Executor). ifnotifyExecutor()is unset, they are invoked in the order they are added. The ordering holds across all variants of this method.- Specified by:
addListenerin interfaceFuture<V>
-
addListeners
public Future<V> addListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
- Specified by:
addListenersin interfaceio.netty.util.concurrent.Future<V>- Specified by:
addListenersin interfaceFuture<V>
-
removeListener
public Future<V> removeListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
- Specified by:
removeListenerin interfaceio.netty.util.concurrent.Future<V>- Specified by:
removeListenerin interfaceFuture<V>
-
removeListeners
public Future<V> removeListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
- Specified by:
removeListenersin interfaceio.netty.util.concurrent.Future<V>- Specified by:
removeListenersin interfaceFuture<V>
-
await
public boolean await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionDescription copied from interface:AwaitableAwait for the specified period, throwing any interrupt. No spurious wakeups.
-
awaitThrowUncheckedOnInterrupt
public boolean awaitThrowUncheckedOnInterrupt(long time, java.util.concurrent.TimeUnit units) throws UncheckedInterruptedExceptionDescription copied from interface:AwaitableAwait for the specified period, throwing any interrupt as an unchecked exception. No spurious wakeups.- Specified by:
awaitThrowUncheckedOnInterruptin interfaceAwaitable- Returns:
- true if we were signalled, false if the timeout elapses
- Throws:
UncheckedInterruptedException- if interrupted
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)Description copied from interface:AwaitableAwait until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit). No spurious wakeups.- Specified by:
awaitUninterruptiblyin interfaceAwaitable- Specified by:
awaitUninterruptiblyin interfaceio.netty.util.concurrent.Future<V>- Returns:
- true if we were signalled, false if the timeout elapses
-
awaitUntilThrowUncheckedOnInterrupt
public boolean awaitUntilThrowUncheckedOnInterrupt(long nanoTimeDeadline) throws UncheckedInterruptedExceptionDescription copied from interface:AwaitableAwait until the deadline (in nanoTime), throwing any interrupt as an unchecked exception. No spurious wakeups.- Specified by:
awaitUntilThrowUncheckedOnInterruptin interfaceAwaitable- 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:AwaitableAwait until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit). No spurious wakeups.- Specified by:
awaitUntilUninterruptiblyin interfaceAwaitable- Returns:
- true if we were signalled, false if the deadline elapsed
-
awaitUninterruptibly
public Future<V> awaitUninterruptibly()
Wait for this future to completeAwaitable.awaitUninterruptibly()- Specified by:
awaitUninterruptiblyin interfaceAwaitable- Specified by:
awaitUninterruptiblyin interfaceio.netty.util.concurrent.Future<V>- Specified by:
awaitUninterruptiblyin interfaceFuture<V>
-
awaitThrowUncheckedOnInterrupt
public Future<V> awaitThrowUncheckedOnInterrupt() throws UncheckedInterruptedException
Wait for this future to completeAwaitable.awaitThrowUncheckedOnInterrupt()- Specified by:
awaitThrowUncheckedOnInterruptin interfaceAwaitable- Specified by:
awaitThrowUncheckedOnInterruptin interfaceFuture<V>- Throws:
UncheckedInterruptedException- if interrupted
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
description
protected java.lang.String description()
-
-