Class FutureIO
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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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> TawaitFuture(Future<T> future) Given a future, evaluate it.static <T> TawaitFuture(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 voidpropagateOptions(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.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> TExtract the cause of a completion failure and rethrow it if an IOE or RTE.static <T> TFrom the inner cause of an execution exception, extract the inner cause if it is an IOE or RTE.static IOExceptionFrom the inner cause of an execution exception, extract the inner cause to an IOException, raising RuntimeExceptions and Errors immediately.
-
Method Details
-
awaitFuture
public static <T> T awaitFuture(Future<T> future) throws InterruptedIOException, IOException, CancellationException, RuntimeException Given a future, evaluate it.Any exception generated in the future is extracted and rethrown.
If this thread is interrupted while waiting for the future to complete, anInterruptedIOExceptionis raised. However, if the future is cancelled, aCancellationExceptionis 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.- Type Parameters:
T- type of the result.- Parameters:
future- future to evaluate- Returns:
- the result, if all went well.
- Throws:
InterruptedIOException- waiting for future completion was interruptedCancellationException- if the future itself was cancelledIOException- if something went wrongRuntimeException- any nested RTE thrown
-
awaitFuture
public static <T> T awaitFuture(Future<T> future, long timeout, TimeUnit unit) throws InterruptedIOException, IOException, CancellationException, RuntimeException, TimeoutException Given a future, evaluate it.Any exception generated in the future is extracted and rethrown.
- Type Parameters:
T- type of the result.- Parameters:
future- future to evaluatetimeout- timeout to wait.unit- time unit.- Returns:
- the result, if all went well.
- Throws:
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.
-
awaitAllFutures
public static <T> List<T> awaitAllFutures(Collection<Future<T>> collection) throws InterruptedIOException, IOException, CancellationException, RuntimeException Evaluates a collection of futures and returns their results as a list.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.
- Type Parameters:
T- type of the result.- Parameters:
collection- collection of futures to be evaluated- Returns:
- the list of future's result, if all went well.
- Throws:
InterruptedIOException- waiting for future completion was interruptedCancellationException- if the future itself was cancelledIOException- if something went wrongRuntimeException- any nested RTE thrown
-
awaitAllFutures
public static <T> List<T> awaitAllFutures(Collection<Future<T>> collection, Duration duration) throws InterruptedIOException, IOException, CancellationException, RuntimeException, TimeoutException 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.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.
- Type Parameters:
T- type of the result.- Parameters:
collection- collection of futures to be evaluatedduration- timeout duration- Returns:
- the list of future's result, if all went well.
- Throws:
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.
-
cancelAllFuturesAndAwaitCompletion
public 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.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.
- Type Parameters:
T- type of the result.- Parameters:
collection- collection of futures to be evaluatedinterruptIfRunning- should the cancel interrupt any active futures?duration- total timeout duration- Returns:
- all futures which completed successfully.
-
raiseInnerCause
From the inner cause of an execution exception, extract the inner cause if it is an IOE or RTE. This will always raise an exception, either the inner IOException, an inner RuntimeException, or a new IOException wrapping the raised exception.- Type Parameters:
T- type of return value.- Parameters:
e- exception.- Returns:
- nothing, ever.
- Throws:
IOException- either the inner IOException, or a wrapper around any non-Runtime-ExceptionRuntimeException- if that is the inner cause.
-
raiseInnerCause
Extract the cause of a completion failure and rethrow it if an IOE or RTE.- Type Parameters:
T- type of return value.- Parameters:
e- exception.- Returns:
- nothing, ever.
- Throws:
IOException- either the inner IOException, or a wrapper around any non-Runtime-ExceptionRuntimeException- if that is the inner cause.
-
unwrapInnerException
From the inner cause of an execution exception, extract the inner cause to an IOException, raising RuntimeExceptions and Errors immediately.- If it is an IOE: Return.
- If it is a
UncheckedIOException: return the cause - Completion/Execution Exceptions: extract and repeat
- If it is an RTE or Error: throw.
- Any other type: wrap in an IOE
- Parameters:
e- exception.- Returns:
- an IOException extracted or built from the cause.
- Throws:
RuntimeException- if that is the inner cause.Error- if that is the inner cause.
-
propagateOptions
public static <T,U extends FSBuilder<T, FSBuilder<T,U>> 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. SeepropagateOptions(FSBuilder, Configuration, String, boolean).- Type Parameters:
T- type of resultU- type of builder- Parameters:
builder- builder to modifyconf- configuration to readoptionalPrefix- prefix for optional settingsmandatoryPrefix- prefix for mandatory settings- Returns:
- the builder passed in.
-
propagateOptions
public 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.fs.example.s3a.option becomes "s3a.option" fs.example.fs.io.policy becomes "fs.io.policy" fs.example.something becomes "something"
- Parameters:
builder- builder to modifyconf- configuration to readprefix- prefix to scan/stripmandatory- are the options to be mandatory or optional?
-
eval
public 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.- Type Parameters:
T- Return type.- Parameters:
callable- callable to invoke- Returns:
- the evaluated result.
- Throws:
UnsupportedOperationException- fail fast if unsupportedIllegalArgumentException- invalid argument
-