mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
Java API: ActionFuture#actionGet to automatically unwrap failures, closes #1292.
This commit is contained in:
parent
c8a2f3e6f8
commit
b185078554
@ -20,6 +20,7 @@
|
||||
package org.elasticsearch.action;
|
||||
|
||||
import org.elasticsearch.ElasticSearchException;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
@ -36,6 +37,10 @@ public interface ActionFuture<T> extends Future<T> {
|
||||
* Similar to {@link #get()}, just wrapping the {@link InterruptedException} with
|
||||
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
|
||||
* cause of the {@link java.util.concurrent.ExecutionException}.
|
||||
*
|
||||
* <p>Note, the actual cause is unwrapped to the actual failure (for example, unwrapped
|
||||
* from {@link org.elasticsearch.transport.RemoteTransportException}. The root failure is
|
||||
* still accessible using {@link #getRootFailure()}.
|
||||
*/
|
||||
T actionGet() throws ElasticSearchException;
|
||||
|
||||
@ -43,6 +48,10 @@ public interface ActionFuture<T> extends Future<T> {
|
||||
* Similar to {@link #get(long, java.util.concurrent.TimeUnit)}, just wrapping the {@link InterruptedException} with
|
||||
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
|
||||
* cause of the {@link java.util.concurrent.ExecutionException}.
|
||||
*
|
||||
* <p>Note, the actual cause is unwrapped to the actual failure (for example, unwrapped
|
||||
* from {@link org.elasticsearch.transport.RemoteTransportException}. The root failure is
|
||||
* still accessible using {@link #getRootFailure()}.
|
||||
*/
|
||||
T actionGet(String timeout) throws ElasticSearchException;
|
||||
|
||||
@ -51,6 +60,10 @@ public interface ActionFuture<T> extends Future<T> {
|
||||
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
|
||||
* cause of the {@link java.util.concurrent.ExecutionException}.
|
||||
*
|
||||
* <p>Note, the actual cause is unwrapped to the actual failure (for example, unwrapped
|
||||
* from {@link org.elasticsearch.transport.RemoteTransportException}. The root failure is
|
||||
* still accessible using {@link #getRootFailure()}.
|
||||
*
|
||||
* @param timeoutMillis Timeout in millis
|
||||
*/
|
||||
T actionGet(long timeoutMillis) throws ElasticSearchException;
|
||||
@ -59,6 +72,10 @@ public interface ActionFuture<T> extends Future<T> {
|
||||
* Similar to {@link #get(long, java.util.concurrent.TimeUnit)}, just wrapping the {@link InterruptedException} with
|
||||
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
|
||||
* cause of the {@link java.util.concurrent.ExecutionException}.
|
||||
*
|
||||
* <p>Note, the actual cause is unwrapped to the actual failure (for example, unwrapped
|
||||
* from {@link org.elasticsearch.transport.RemoteTransportException}. The root failure is
|
||||
* still accessible using {@link #getRootFailure()}.
|
||||
*/
|
||||
T actionGet(long timeout, TimeUnit unit) throws ElasticSearchException;
|
||||
|
||||
@ -66,6 +83,15 @@ public interface ActionFuture<T> extends Future<T> {
|
||||
* Similar to {@link #get(long, java.util.concurrent.TimeUnit)}, just wrapping the {@link InterruptedException} with
|
||||
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
|
||||
* cause of the {@link java.util.concurrent.ExecutionException}.
|
||||
*
|
||||
* <p>Note, the actual cause is unwrapped to the actual failure (for example, unwrapped
|
||||
* from {@link org.elasticsearch.transport.RemoteTransportException}. The root failure is
|
||||
* still accessible using {@link #getRootFailure()}.
|
||||
*/
|
||||
T actionGet(TimeValue timeout) throws ElasticSearchException;
|
||||
|
||||
/**
|
||||
* The root (possibly) wrapped failure.
|
||||
*/
|
||||
@Nullable Throwable getRootFailure();
|
||||
}
|
||||
|
@ -37,17 +37,15 @@ import java.util.concurrent.TimeoutException;
|
||||
*/
|
||||
public abstract class AdapterActionFuture<T, L> extends AbstractFuture<T> implements ActionFuture<T>, ActionListener<L> {
|
||||
|
||||
private Throwable rootFailure;
|
||||
|
||||
@Override public T actionGet() throws ElasticSearchException {
|
||||
try {
|
||||
return get();
|
||||
} catch (InterruptedException e) {
|
||||
throw new ElasticSearchInterruptedException(e.getMessage());
|
||||
} catch (ExecutionException e) {
|
||||
if (e.getCause() instanceof ElasticSearchException) {
|
||||
throw (ElasticSearchException) e.getCause();
|
||||
} else {
|
||||
throw new UncategorizedExecutionException("Failed execution", e);
|
||||
}
|
||||
throw rethrowExecutionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,11 +69,20 @@ public abstract class AdapterActionFuture<T, L> extends AbstractFuture<T> implem
|
||||
} catch (InterruptedException e) {
|
||||
throw new ElasticSearchInterruptedException(e.getMessage());
|
||||
} catch (ExecutionException e) {
|
||||
if (e.getCause() instanceof ElasticSearchException) {
|
||||
throw (ElasticSearchException) e.getCause();
|
||||
} else {
|
||||
throw new UncategorizedExecutionException("Failed execution", e);
|
||||
throw rethrowExecutionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static ElasticSearchException rethrowExecutionException(ExecutionException e) {
|
||||
if (e.getCause() instanceof ElasticSearchException) {
|
||||
ElasticSearchException esEx = (ElasticSearchException) e.getCause();
|
||||
Throwable root = esEx.unwrapCause();
|
||||
if (root instanceof ElasticSearchException) {
|
||||
return (ElasticSearchException) root;
|
||||
}
|
||||
return new UncategorizedExecutionException("Failed execution", root);
|
||||
} else {
|
||||
return new UncategorizedExecutionException("Failed execution", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,4 +95,8 @@ public abstract class AdapterActionFuture<T, L> extends AbstractFuture<T> implem
|
||||
}
|
||||
|
||||
protected abstract T convert(L listenerResponse);
|
||||
|
||||
@Override public Throwable getRootFailure() {
|
||||
return rootFailure;
|
||||
}
|
||||
}
|
||||
|
@ -160,4 +160,8 @@ public class GActionFuture<T> implements ListenableActionFuture<T>, ActionListen
|
||||
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
return future.get(timeout, unit);
|
||||
}
|
||||
|
||||
@Override public Throwable getRootFailure() {
|
||||
return future.getRootFailure();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user