From 1b8522d03d97522eacb884a01c6aa17716cb5ceb Mon Sep 17 00:00:00 2001 From: Suresh Srinivas Date: Tue, 30 Jul 2013 06:24:02 +0000 Subject: [PATCH] HADOOP-9792. Merge 1508312 from trunk. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1508313 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 +++ .../io/retry/FailoverProxyProvider.java | 4 +-- .../io/retry/RetryInvocationHandler.java | 13 +++++++--- .../apache/hadoop/io/retry/RetryPolicies.java | 22 ++++++++-------- .../apache/hadoop/io/retry/RetryPolicy.java | 25 ++++++++++--------- .../hadoop/io/retry/TestFailoverProxy.java | 2 +- 6 files changed, 39 insertions(+), 30 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 47d1eea57fb..61ad518fa64 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -107,6 +107,9 @@ Release 2.1.0-beta - 2013-07-02 HADOOP-9762. RetryCache utility for implementing RPC retries. (Suresh Srinivas via jing9) + HADOOP-9792. Retry the methods that are tagged @AtMostOnce along + with @Idempotent. (suresh) + IMPROVEMENTS HADOOP-9164. Print paths of loaded native libraries in diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/FailoverProxyProvider.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/FailoverProxyProvider.java index ba7d29f0d52..c267d028798 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/FailoverProxyProvider.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/FailoverProxyProvider.java @@ -51,8 +51,8 @@ public interface FailoverProxyProvider extends Closeable { /** * Return a reference to the interface this provider's proxy objects actually * implement. If any of the methods on this interface are annotated as being - * {@link Idempotent}, then this fact will be passed to the - * {@link RetryPolicy#shouldRetry(Exception, int, int, boolean)} method on + * {@link Idempotent} or {@link AtMostOnce}, then this fact will be passed to + * the {@link RetryPolicy#shouldRetry(Exception, int, int, boolean)} method on * error, for use in determining whether or not failover should be attempted. * * @return the interface implemented by the proxy objects returned by diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryInvocationHandler.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryInvocationHandler.java index cdc7493e83d..044caa3602a 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryInvocationHandler.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryInvocationHandler.java @@ -53,7 +53,7 @@ class RetryInvocationHandler implements RpcInvocationHandler { private final Map methodNameToPolicyMap; private Object currentProxy; - RetryInvocationHandler(FailoverProxyProvider proxyProvider, + protected RetryInvocationHandler(FailoverProxyProvider proxyProvider, RetryPolicy retryPolicy) { this(proxyProvider, retryPolicy, Collections.emptyMap()); } @@ -97,11 +97,16 @@ class RetryInvocationHandler implements RpcInvocationHandler { hasMadeASuccessfulCall = true; return ret; } catch (Exception e) { - boolean isMethodIdempotent = proxyProvider.getInterface() + boolean isIdempotentOrAtMostOnce = proxyProvider.getInterface() .getMethod(method.getName(), method.getParameterTypes()) .isAnnotationPresent(Idempotent.class); + if (!isIdempotentOrAtMostOnce) { + isIdempotentOrAtMostOnce = proxyProvider.getInterface() + .getMethod(method.getName(), method.getParameterTypes()) + .isAnnotationPresent(AtMostOnce.class); + } RetryAction action = policy.shouldRetry(e, retries++, - invocationFailoverCount, isMethodIdempotent); + invocationFailoverCount, isIdempotentOrAtMostOnce); if (action.action == RetryAction.RetryDecision.FAIL) { if (action.reason != null) { LOG.warn("Exception while invoking " + @@ -169,7 +174,7 @@ class RetryInvocationHandler implements RpcInvocationHandler { } } - private Object invokeMethod(Method method, Object[] args) throws Throwable { + protected Object invokeMethod(Method method, Object[] args) throws Throwable { try { if (!method.isAccessible()) { method.setAccessible(true); diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicies.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicies.java index 9d99c47b32f..11cbc34f012 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicies.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicies.java @@ -153,7 +153,7 @@ public class RetryPolicies { static class TryOnceThenFail implements RetryPolicy { @Override public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) throws Exception { + boolean isIdempotentOrAtMostOnce) throws Exception { return RetryAction.FAIL; } } @@ -161,7 +161,7 @@ public class RetryPolicies { static class RetryForever implements RetryPolicy { @Override public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) throws Exception { + boolean isIdempotentOrAtMostOnce) throws Exception { return RetryAction.RETRY; } } @@ -196,7 +196,7 @@ public class RetryPolicies { @Override public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) throws Exception { + boolean isIdempotentOrAtMostOnce) throws Exception { if (retries >= maxRetries) { return RetryAction.FAIL; } @@ -305,7 +305,7 @@ public class RetryPolicies { @Override public RetryAction shouldRetry(Exception e, int curRetry, int failovers, - boolean isMethodIdempotent) throws Exception { + boolean isIdempotentOrAtMostOnce) throws Exception { final Pair p = searchPair(curRetry); if (p == null) { //no more retries. @@ -435,12 +435,12 @@ public class RetryPolicies { @Override public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) throws Exception { + boolean isIdempotentOrAtMostOnce) throws Exception { RetryPolicy policy = exceptionToPolicyMap.get(e.getClass()); if (policy == null) { policy = defaultPolicy; } - return policy.shouldRetry(e, retries, failovers, isMethodIdempotent); + return policy.shouldRetry(e, retries, failovers, isIdempotentOrAtMostOnce); } } @@ -463,7 +463,7 @@ public class RetryPolicies { @Override public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) throws Exception { + boolean isIdempotentOrAtMostOnce) throws Exception { RetryPolicy policy = null; if (e instanceof RemoteException) { policy = exceptionNameToPolicyMap.get( @@ -472,7 +472,7 @@ public class RetryPolicies { if (policy == null) { policy = defaultPolicy; } - return policy.shouldRetry(e, retries, failovers, isMethodIdempotent); + return policy.shouldRetry(e, retries, failovers, isIdempotentOrAtMostOnce); } } @@ -533,7 +533,7 @@ public class RetryPolicies { @Override public RetryAction shouldRetry(Exception e, int retries, - int failovers, boolean isMethodIdempotent) throws Exception { + int failovers, boolean isIdempotentOrAtMostOnce) throws Exception { if (failovers >= maxFailovers) { return new RetryAction(RetryAction.RetryDecision.FAIL, 0, "failovers (" + failovers + ") exceeded maximum allowed (" @@ -553,7 +553,7 @@ public class RetryPolicies { calculateExponentialTime(delayMillis, failovers, maxDelayBase)); } else if (e instanceof SocketException || (e instanceof IOException && !(e instanceof RemoteException))) { - if (isMethodIdempotent) { + if (isIdempotentOrAtMostOnce) { return RetryAction.FAILOVER_AND_RETRY; } else { return new RetryAction(RetryAction.RetryDecision.FAIL, 0, @@ -562,7 +562,7 @@ public class RetryPolicies { } } else { return fallbackPolicy.shouldRetry(e, retries, failovers, - isMethodIdempotent); + isIdempotentOrAtMostOnce); } } diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicy.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicy.java index e1f38994579..f3e2bd1713f 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicy.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryPolicy.java @@ -75,24 +75,25 @@ public interface RetryPolicy { /** *

- * Determines whether the framework should retry a - * method for the given exception, and the number - * of retries that have been made for that operation + * Determines whether the framework should retry a method for the given + * exception, and the number of retries that have been made for that operation * so far. *

+ * * @param e The exception that caused the method to fail * @param retries The number of times the method has been retried * @param failovers The number of times the method has failed over to a - * different backend implementation - * @param isMethodIdempotent true if the method is idempotent - * and so can reasonably be retried on failover when we don't know if the - * previous attempt reached the server or not + * different backend implementation + * @param isIdempotentOrAtMostOnce true if the method is + * {@link Idempotent} or {@link AtMostOnce} and so can reasonably be + * retried on failover when we don't know if the previous attempt + * reached the server or not * @return true if the method should be retried, - * false if the method should not be retried - * but shouldn't fail with an exception (only for void methods) - * @throws Exception The re-thrown exception e indicating - * that the method failed and should not be retried further + * false if the method should not be retried but + * shouldn't fail with an exception (only for void methods) + * @throws Exception The re-thrown exception e indicating that + * the method failed and should not be retried further */ public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) throws Exception; + boolean isIdempotentOrAtMostOnce) throws Exception; } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/TestFailoverProxy.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/TestFailoverProxy.java index 77c9e30eed4..cd9650824b4 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/TestFailoverProxy.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/TestFailoverProxy.java @@ -78,7 +78,7 @@ public class TestFailoverProxy { @Override public RetryAction shouldRetry(Exception e, int retries, int failovers, - boolean isMethodIdempotent) { + boolean isIdempotentOrAtMostOnce) { return failovers < 1 ? RetryAction.FAILOVER_AND_RETRY : RetryAction.FAIL; }