@InterfaceAudience.Public @InterfaceStability.Unstable public final class FutureIO extends Object
Contains methods promoted from
FutureIOSupport
because they
are a key part of integrating async IO in application code.
One key feature is that the awaitFuture(Future)
and
awaitFuture(Future, long, TimeUnit)
calls will
extract and rethrow exceptions raised in the future's execution,
including extracting the inner IOException of any
UncheckedIOException
raised in the future.
This makes it somewhat easier to execute IOException-raising
code inside futures.
Important: any CancellationException
raised by the future
is rethrown unchanged. This has been the implicit behavior since
this code was first written, and is now explicitly documented.
Modifier and Type | Method and Description |
---|---|
static <T> List<T> |
awaitAllFutures(Collection<Future<T>> collection)
Evaluates a collection of futures and returns their results as a list.
|
static <T> List<T> |
awaitAllFutures(Collection<Future<T>> collection,
Duration duration)
Evaluates a collection of futures and returns their results as a list,
but only waits up to the specified timeout for each future to complete.
|
static <T> T |
awaitFuture(Future<T> future)
Given a future, evaluate it.
|
static <T> T |
awaitFuture(Future<T> future,
long timeout,
TimeUnit unit)
Given a future, evaluate it.
|
static <T> List<T> |
cancelAllFuturesAndAwaitCompletion(Collection<Future<T>> collection,
boolean interruptIfRunning,
Duration duration)
Cancels a collection of futures and awaits the specified duration for their completion.
|
static <T> CompletableFuture<T> |
eval(org.apache.hadoop.util.functional.CallableRaisingIOE<T> callable)
Evaluate a CallableRaisingIOE in the current thread,
converting IOEs to RTEs and propagating.
|
static void |
propagateOptions(FSBuilder<?,?> builder,
Configuration conf,
String prefix,
boolean mandatory)
Propagate options to any builder, converting everything with the
prefix to an option where, if there were 2+ dot-separated elements,
it is converted to a schema.
|
static <T,U extends FSBuilder<T,U>> |
propagateOptions(FSBuilder<T,U> builder,
Configuration conf,
String optionalPrefix,
String mandatoryPrefix)
Propagate options to any builder, converting everything with the
prefix to an option where, if there were 2+ dot-separated elements,
it is converted to a schema.
|
static <T> T |
raiseInnerCause(CompletionException e)
Extract the cause of a completion failure and rethrow it if an IOE
or RTE.
|
static <T> T |
raiseInnerCause(ExecutionException e)
From the inner cause of an execution exception, extract the inner cause
if it is an IOE or RTE.
|
static IOException |
unwrapInnerException(Throwable e)
From the inner cause of an execution exception, extract the inner cause
to an IOException, raising RuntimeExceptions and Errors immediately.
|
public static <T> T awaitFuture(Future<T> future) throws InterruptedIOException, IOException, CancellationException, RuntimeException
Any exception generated in the future is extracted and rethrown.
If this thread is interrupted while waiting for the future to complete, anInterruptedIOException
is raised.
However, if the future is cancelled, a CancellationException
is raised in the {code Future.get()} call. This is
passed up as is -so allowing the caller to distinguish between
thread interruption (such as when speculative task execution is aborted)
and future cancellation.T
- type of the result.future
- future to evaluateInterruptedIOException
- waiting for future completion was interruptedCancellationException
- if the future itself was cancelledIOException
- if something went wrongRuntimeException
- any nested RTE thrownpublic static <T> T awaitFuture(Future<T> future, long timeout, TimeUnit unit) throws InterruptedIOException, IOException, CancellationException, RuntimeException, TimeoutException
Any exception generated in the future is extracted and rethrown.
T
- type of the result.future
- future to evaluatetimeout
- timeout to wait.unit
- time unit.InterruptedIOException
- waiting for future completion was interruptedCancellationException
- if the future itself was cancelledIOException
- if something went wrongRuntimeException
- any nested RTE thrownTimeoutException
- the future timed out.public static <T> List<T> awaitAllFutures(Collection<Future<T>> collection) throws InterruptedIOException, IOException, CancellationException, RuntimeException
This method blocks until all futures in the collection have completed. If any future throws an exception during its execution, this method extracts and rethrows that exception.
T
- type of the result.collection
- collection of futures to be evaluatedInterruptedIOException
- waiting for future completion was interruptedCancellationException
- if the future itself was cancelledIOException
- if something went wrongRuntimeException
- any nested RTE thrownpublic static <T> List<T> awaitAllFutures(Collection<Future<T>> collection, Duration duration) throws InterruptedIOException, IOException, CancellationException, RuntimeException, TimeoutException
This method blocks until all futures in the collection have completed or the timeout expires, whichever happens first. If any future throws an exception during its execution, this method extracts and rethrows that exception.
T
- type of the result.collection
- collection of futures to be evaluatedduration
- timeout durationInterruptedIOException
- waiting for future completion was interruptedCancellationException
- if the future itself was cancelledIOException
- if something went wrongRuntimeException
- any nested RTE thrownTimeoutException
- the future timed out.public static <T> List<T> cancelAllFuturesAndAwaitCompletion(Collection<Future<T>> collection, boolean interruptIfRunning, Duration duration)
This method blocks until all futures in the collection have completed or the timeout expires, whichever happens first. All exceptions thrown by the futures are ignored. as is any TimeoutException.
T
- type of the result.collection
- collection of futures to be evaluatedinterruptIfRunning
- should the cancel interrupt any active futures?duration
- total timeout durationpublic static <T> T raiseInnerCause(ExecutionException e) throws IOException
T
- type of return value.e
- exception.IOException
- either the inner IOException, or a wrapper around
any non-Runtime-ExceptionRuntimeException
- if that is the inner cause.public static <T> T raiseInnerCause(CompletionException e) throws IOException
T
- type of return value.e
- exception.IOException
- either the inner IOException, or a wrapper around
any non-Runtime-ExceptionRuntimeException
- if that is the inner cause.public static IOException unwrapInnerException(Throwable e)
UncheckedIOException
: return the causee
- exception.RuntimeException
- if that is the inner cause.Error
- if that is the inner cause.public static <T,U extends FSBuilder<T,U>> FSBuilder<T,U> propagateOptions(FSBuilder<T,U> builder, Configuration conf, String optionalPrefix, String mandatoryPrefix)
propagateOptions(FSBuilder, Configuration, String, boolean)
.T
- type of resultU
- type of builderbuilder
- builder to modifyconf
- configuration to readoptionalPrefix
- prefix for optional settingsmandatoryPrefix
- prefix for mandatory settingspublic static void propagateOptions(FSBuilder<?,?> builder, Configuration conf, String prefix, boolean mandatory)
fs.example.s3a.option becomes "s3a.option" fs.example.fs.io.policy becomes "fs.io.policy" fs.example.something becomes "something"
builder
- builder to modifyconf
- configuration to readprefix
- prefix to scan/stripmandatory
- are the options to be mandatory or optional?public static <T> CompletableFuture<T> eval(org.apache.hadoop.util.functional.CallableRaisingIOE<T> callable)
T
- Return type.callable
- callable to invokeUnsupportedOperationException
- fail fast if unsupportedIllegalArgumentException
- invalid argumentCopyright © 2024 Apache Software Foundation. All rights reserved.