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 defaultFuture
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
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)
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.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 completeAwaitable.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 completeAwaitable.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)
SupportFutures.transformAsync(ListenableFuture, AsyncFunction, Executor)
natively SeeaddListener(GenericFutureListener)
for ordering semantics.V
get()
V
get(long timeout, java.util.concurrent.TimeUnit unit)
V
getNow()
if isSuccess(), returns the value, otherwise returns nullprotected V
getWhenDone()
Shared implementation of get() after suitable await(); assumes isDone(), and returns either the success result or throws the suitable exception under failureboolean
isCancellable()
boolean
isCancelled()
boolean
isDone()
boolean
isSuccess()
protected boolean
isUncancellable()
<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.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
-
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:
notifyExecutor
in 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:
isSuccess
in interfaceio.netty.util.concurrent.Future<V>
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in interfacejava.util.concurrent.Future<V>
-
isDone
public boolean isDone()
- Specified by:
isDone
in interfacejava.util.concurrent.Future<V>
-
isCancellable
public boolean isCancellable()
- Specified by:
isCancellable
in interfaceio.netty.util.concurrent.Future<V>
-
cause
public java.lang.Throwable cause()
- Specified by:
cause
in interfaceio.netty.util.concurrent.Future<V>
-
getNow
public V getNow()
if isSuccess(), returns the value, otherwise returns null- Specified by:
getNow
in 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:
get
in interfacejava.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 interfacejava.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)
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:
addCallback
in 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:
addCallback
in 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:Future
SupportFutures.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 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:
addCallback
in 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:
addCallback
in 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:
addCallback
in 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:
addListener
in interfaceio.netty.util.concurrent.Future<V>
- Specified by:
addListener
in 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:
addListener
in interfaceFuture<V>
- Specified by:
addListener
in 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:
addListener
in interfaceFuture<V>
-
addListeners
public Future<V> addListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
- Specified by:
addListeners
in interfaceio.netty.util.concurrent.Future<V>
- Specified by:
addListeners
in interfaceFuture<V>
-
removeListener
public Future<V> removeListener(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
- Specified by:
removeListener
in interfaceio.netty.util.concurrent.Future<V>
- Specified by:
removeListener
in interfaceFuture<V>
-
removeListeners
public Future<V> removeListeners(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
- Specified by:
removeListeners
in interfaceio.netty.util.concurrent.Future<V>
- Specified by:
removeListeners
in interfaceFuture<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.
-
awaitThrowUncheckedOnInterrupt
public boolean awaitThrowUncheckedOnInterrupt(long time, java.util.concurrent.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 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:Awaitable
Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit). No spurious wakeups.- Specified by:
awaitUninterruptibly
in interfaceAwaitable
- Specified by:
awaitUninterruptibly
in 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:Awaitable
Await until the deadline (in nanoTime), throwing any interrupt as an unchecked exception. No spurious wakeups.- Specified by:
awaitUntilThrowUncheckedOnInterrupt
in 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:Awaitable
Await until the deadline (in nanoTime), ignoring interrupts (but maintaining the interrupt flag on exit). No spurious wakeups.- Specified by:
awaitUntilUninterruptibly
in 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:
awaitUninterruptibly
in interfaceAwaitable
- Specified by:
awaitUninterruptibly
in interfaceio.netty.util.concurrent.Future<V>
- Specified by:
awaitUninterruptibly
in interfaceFuture<V>
-
awaitThrowUncheckedOnInterrupt
public Future<V> awaitThrowUncheckedOnInterrupt() throws UncheckedInterruptedException
Wait for this future to completeAwaitable.awaitThrowUncheckedOnInterrupt()
- Specified by:
awaitThrowUncheckedOnInterrupt
in interfaceAwaitable
- Specified by:
awaitThrowUncheckedOnInterrupt
in interfaceFuture<V>
- Throws:
UncheckedInterruptedException
- if interrupted
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
description
protected java.lang.String description()
-
-