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
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.-
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
FieldsModifier and TypeFieldDescriptionprotected static final org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolderprotected static final org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolderprotected static final org.apache.cassandra.utils.concurrent.AbstractFuture.FailureHolder -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractFuture(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener) protectedAbstractFuture(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
Modifier and TypeMethodDescriptionaddCallback(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.addCallback(com.google.common.util.concurrent.FutureCallback<? super V> callback, 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.addCallback(BiConsumer<? super V, 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.addCallback(BiConsumer<? super V, Throwable> callback, Executor executor) SupportFutures.addCallback(com.google.common.util.concurrent.ListenableFuture<V>, com.google.common.util.concurrent.FutureCallback<? super V>, java.util.concurrent.Executor)nativelyaddCallback(Consumer<? super V> onSuccess, Consumer<? super 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.addCallback(Consumer<? super V> onSuccess, Consumer<? super Throwable> onFailure, 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.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(Runnable task) Add a listener to be invoked once this future completes.voidaddListener(Runnable task, Executor executor) Add a listener to be invoked once this future completes.addListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners) booleanAwait for the specified period, throwing any interrupt.Wait for this future to completeAwaitable.awaitThrowUncheckedOnInterrupt()booleanawaitThrowUncheckedOnInterrupt(long time, TimeUnit units) Await for the specified period, throwing any interrupt as an unchecked exception.Wait for this future to completeAwaitable.awaitUninterruptibly()booleanawaitUninterruptibly(long timeout, 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) cause()protected Stringprotected <T> Future<T>flatMap(AbstractFuture<T> result, Function<? super V, ? extends Future<T>> flatMapper, Executor executor) SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.get()getNow()if isSuccess(), returns the value, otherwise returns nullprotected VShared implementation of get() after suitable await(); assumes isDone(), and returns either the success result or throws the suitable exception under failurebooleanbooleanbooleanisDone()booleanprotected boolean<T> Future<T>SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.protected <T> Future<T>map(AbstractFuture<T> result, Function<? super V, ? extends T> mapper, Executor executor) SupportFutures.transform(ListenableFuture, com.google.common.base.Function, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics.removeListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener) removeListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners) protected booleanprotected booleantoString()protected booleantryFailure(Throwable throwable) protected booleantrySuccess(V v) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.cassandra.utils.concurrent.Awaitable
awaitUntilMethods inherited from interface org.apache.cassandra.utils.concurrent.Future
await, await, awaitUninterruptibly, flatMap, flatMap, map, rethrowIfFailed, sync, syncThrowUncheckedOnInterrupt, syncUninterruptibly
-
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
-
AbstractFuture
-
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
- Specified by:
notifyExecutorin interfaceFuture<V>
-
trySuccess
-
tryFailure
-
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 interfaceFuture<V>
-
isDone
public boolean isDone() -
isCancellable
public boolean isCancellable()- Specified by:
isCancellablein interfaceio.netty.util.concurrent.Future<V>
-
cause
- Specified by:
causein interfaceio.netty.util.concurrent.Future<V>
-
getNow
if isSuccess(), returns the value, otherwise returns null- Specified by:
getNowin interfaceio.netty.util.concurrent.Future<V>
-
getWhenDone
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:
getin interfaceFuture<V>- Throws:
InterruptedExceptionExecutionException
-
get
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
getin interfaceFuture<V>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
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
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
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, 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(Consumer<? super V> onSuccess, Consumer<? super 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
SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)natively SeeaddListener(GenericFutureListener)for ordering semantics. -
addCallback
public AbstractFuture<V> addCallback(Consumer<? super V> onSuccess, Consumer<? super Throwable> onFailure, 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, Function<? super V, ? extends T> mapper, @Nullable 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, Function<? super V, ? extends Future<T>> flatMapper, @Nullable 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
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
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
Description copied from interface:AwaitableAwait for the specified period, throwing any interrupt. No spurious wakeups.- Specified by:
awaitin interfaceAwaitable- Specified by:
awaitin interfaceio.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: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
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 UncheckedInterruptedException Description 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
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
Wait for this future to completeAwaitable.awaitThrowUncheckedOnInterrupt()- Specified by:
awaitThrowUncheckedOnInterruptin interfaceAwaitable- Specified by:
awaitThrowUncheckedOnInterruptin interfaceFuture<V>- Throws:
UncheckedInterruptedException- if interrupted
-
toString
-
description
-