Java API: ActionFuture#actionGet to automatically unwrap failures, closes #1292.

This commit is contained in:
Shay Banon 2011-09-01 00:10:15 +03:00
parent c8a2f3e6f8
commit b185078554
3 changed files with 50 additions and 9 deletions

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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();
}
}