diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index ac4687a7446..f7de942c6db 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -320,6 +320,9 @@ Release 2.1.1-beta - UNRELEASED HADOOP-9787. ShutdownHelper util to shutdown threads and threadpools. (Karthik Kambatla via Sandy Ryza) + HADOOP-9803. Add a generic type parameter to RetryInvocationHandler. + (szetszwo) + OPTIMIZATIONS BUG FIXES 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 a57bc01af18..4bf000ee85c 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 @@ -44,9 +44,9 @@ * side. */ @InterfaceAudience.Private -public class RetryInvocationHandler implements RpcInvocationHandler { +public class RetryInvocationHandler implements RpcInvocationHandler { public static final Log LOG = LogFactory.getLog(RetryInvocationHandler.class); - private final FailoverProxyProvider proxyProvider; + private final FailoverProxyProvider proxyProvider; /** * The number of times the associated proxyProvider has ever been failed over. @@ -56,14 +56,14 @@ public class RetryInvocationHandler implements RpcInvocationHandler { private final RetryPolicy defaultPolicy; private final Map methodNameToPolicyMap; - private Object currentProxy; + private T currentProxy; - protected RetryInvocationHandler(FailoverProxyProvider proxyProvider, + protected RetryInvocationHandler(FailoverProxyProvider proxyProvider, RetryPolicy retryPolicy) { this(proxyProvider, retryPolicy, Collections.emptyMap()); } - RetryInvocationHandler(FailoverProxyProvider proxyProvider, + RetryInvocationHandler(FailoverProxyProvider proxyProvider, RetryPolicy defaultPolicy, Map methodNameToPolicyMap) { this.proxyProvider = proxyProvider; diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryProxy.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryProxy.java index 1baf478af48..9875bcd185d 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryProxy.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/retry/RetryProxy.java @@ -58,7 +58,7 @@ public static Object create(Class iface, return Proxy.newProxyInstance( proxyProvider.getInterface().getClassLoader(), new Class[] { iface }, - new RetryInvocationHandler(proxyProvider, retryPolicy) + new RetryInvocationHandler(proxyProvider, retryPolicy) ); } @@ -99,7 +99,7 @@ public static Object create(Class iface, return Proxy.newProxyInstance( proxyProvider.getInterface().getClassLoader(), new Class[] { iface }, - new RetryInvocationHandler(proxyProvider, defaultPolicy, + new RetryInvocationHandler(proxyProvider, defaultPolicy, methodNameToPolicyMap) ); } 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 cd9650824b4..3b4ebae4812 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 @@ -28,7 +28,6 @@ import org.apache.hadoop.util.ThreadUtil; import org.junit.Test; -@SuppressWarnings("unchecked") public class TestFailoverProxy { public static class FlipFlopProxyProvider implements FailoverProxyProvider { @@ -84,15 +83,29 @@ public RetryAction shouldRetry(Exception e, int retries, int failovers, } + private static FlipFlopProxyProvider + newFlipFlopProxyProvider() { + return new FlipFlopProxyProvider( + UnreliableInterface.class, + new UnreliableImplementation("impl1"), + new UnreliableImplementation("impl2")); + } + + private static FlipFlopProxyProvider + newFlipFlopProxyProvider(TypeOfExceptionToFailWith t1, + TypeOfExceptionToFailWith t2) { + return new FlipFlopProxyProvider( + UnreliableInterface.class, + new UnreliableImplementation("impl1", t1), + new UnreliableImplementation("impl2", t2)); + } + @Test public void testSuccedsOnceThenFailOver() throws UnreliableException, IOException, StandbyException { - UnreliableInterface unreliable = (UnreliableInterface)RetryProxy - .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1"), - new UnreliableImplementation("impl2")), - new FailOverOnceOnAnyExceptionPolicy()); + UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.create( + UnreliableInterface.class, newFlipFlopProxyProvider(), + new FailOverOnceOnAnyExceptionPolicy()); assertEquals("impl1", unreliable.succeedsOnceThenFailsReturningString()); assertEquals("impl2", unreliable.succeedsOnceThenFailsReturningString()); @@ -107,12 +120,10 @@ public void testSuccedsOnceThenFailOver() throws UnreliableException, @Test public void testSucceedsTenTimesThenFailOver() throws UnreliableException, IOException, StandbyException { - UnreliableInterface unreliable = (UnreliableInterface)RetryProxy - .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1"), - new UnreliableImplementation("impl2")), - new FailOverOnceOnAnyExceptionPolicy()); + UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.create( + UnreliableInterface.class, + newFlipFlopProxyProvider(), + new FailOverOnceOnAnyExceptionPolicy()); for (int i = 0; i < 10; i++) { assertEquals("impl1", unreliable.succeedsTenTimesThenFailsReturningString()); @@ -123,11 +134,9 @@ public void testSucceedsTenTimesThenFailOver() throws UnreliableException, @Test public void testNeverFailOver() throws UnreliableException, IOException, StandbyException { - UnreliableInterface unreliable = (UnreliableInterface)RetryProxy - .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1"), - new UnreliableImplementation("impl2")), + UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.create( + UnreliableInterface.class, + newFlipFlopProxyProvider(), RetryPolicies.TRY_ONCE_THEN_FAIL); unreliable.succeedsOnceThenFailsReturningString(); @@ -142,11 +151,9 @@ public void testNeverFailOver() throws UnreliableException, @Test public void testFailoverOnStandbyException() throws UnreliableException, IOException, StandbyException { - UnreliableInterface unreliable = (UnreliableInterface)RetryProxy - .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1"), - new UnreliableImplementation("impl2")), + UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.create( + UnreliableInterface.class, + newFlipFlopProxyProvider(), RetryPolicies.failoverOnNetworkException(1)); assertEquals("impl1", unreliable.succeedsOnceThenFailsReturningString()); @@ -160,9 +167,9 @@ public void testFailoverOnStandbyException() unreliable = (UnreliableInterface)RetryProxy .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1", TypeOfExceptionToFailWith.STANDBY_EXCEPTION), - new UnreliableImplementation("impl2", TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION)), + newFlipFlopProxyProvider( + TypeOfExceptionToFailWith.STANDBY_EXCEPTION, + TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION), RetryPolicies.failoverOnNetworkException(1)); assertEquals("impl1", unreliable.succeedsOnceThenFailsReturningString()); @@ -173,11 +180,11 @@ public void testFailoverOnStandbyException() @Test public void testFailoverOnNetworkExceptionIdempotentOperation() throws UnreliableException, IOException, StandbyException { - UnreliableInterface unreliable = (UnreliableInterface)RetryProxy - .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1", TypeOfExceptionToFailWith.IO_EXCEPTION), - new UnreliableImplementation("impl2", TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION)), + UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.create( + UnreliableInterface.class, + newFlipFlopProxyProvider( + TypeOfExceptionToFailWith.IO_EXCEPTION, + TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION), RetryPolicies.failoverOnNetworkException(1)); assertEquals("impl1", unreliable.succeedsOnceThenFailsReturningString()); @@ -204,9 +211,9 @@ public void testFailoverOnNetworkExceptionIdempotentOperation() public void testExceptionPropagatedForNonIdempotentVoid() throws Exception { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1", TypeOfExceptionToFailWith.IO_EXCEPTION), - new UnreliableImplementation("impl2", TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION)), + newFlipFlopProxyProvider( + TypeOfExceptionToFailWith.IO_EXCEPTION, + TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION), RetryPolicies.failoverOnNetworkException(1)); try { @@ -268,7 +275,8 @@ public void run() { */ @Test public void testConcurrentMethodFailures() throws InterruptedException { - FlipFlopProxyProvider proxyProvider = new FlipFlopProxyProvider( + FlipFlopProxyProvider proxyProvider + = new FlipFlopProxyProvider( UnreliableInterface.class, new SynchronizedUnreliableImplementation("impl1", TypeOfExceptionToFailWith.STANDBY_EXCEPTION, @@ -305,7 +313,8 @@ public void testFailoverBetweenMultipleStandbys() final UnreliableImplementation impl1 = new UnreliableImplementation("impl1", TypeOfExceptionToFailWith.STANDBY_EXCEPTION); - FlipFlopProxyProvider proxyProvider = new FlipFlopProxyProvider( + FlipFlopProxyProvider proxyProvider + = new FlipFlopProxyProvider( UnreliableInterface.class, impl1, new UnreliableImplementation("impl2", @@ -333,13 +342,13 @@ public void run() { */ @Test public void testExpectedIOException() { - UnreliableInterface unreliable = (UnreliableInterface)RetryProxy - .create(UnreliableInterface.class, - new FlipFlopProxyProvider(UnreliableInterface.class, - new UnreliableImplementation("impl1", TypeOfExceptionToFailWith.REMOTE_EXCEPTION), - new UnreliableImplementation("impl2", TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION)), - RetryPolicies.failoverOnNetworkException( - RetryPolicies.TRY_ONCE_THEN_FAIL, 10, 1000, 10000)); + UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.create( + UnreliableInterface.class, + newFlipFlopProxyProvider( + TypeOfExceptionToFailWith.REMOTE_EXCEPTION, + TypeOfExceptionToFailWith.UNRELIABLE_EXCEPTION), + RetryPolicies.failoverOnNetworkException( + RetryPolicies.TRY_ONCE_THEN_FAIL, 10, 1000, 10000)); try { unreliable.failsIfIdentifierDoesntMatch("no-such-identifier"); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/UnreliableInterface.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/UnreliableInterface.java index 66a8b853606..3fbe11a1c25 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/UnreliableInterface.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/retry/UnreliableInterface.java @@ -26,6 +26,8 @@ public interface UnreliableInterface { public static class UnreliableException extends Exception { + private static final long serialVersionUID = 1L; + private String identifier; public UnreliableException() { @@ -43,6 +45,7 @@ public String getMessage() { } public static class FatalException extends UnreliableException { + private static final long serialVersionUID = 1L; // no body } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestRetryCacheWithHA.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestRetryCacheWithHA.java index 7318a11e635..5e2a63203e9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestRetryCacheWithHA.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestRetryCacheWithHA.java @@ -74,7 +74,7 @@ public class TestRetryCacheWithHA { * a boolean flag to control whether the method invocation succeeds or not. */ private static class DummyRetryInvocationHandler extends - RetryInvocationHandler { + RetryInvocationHandler { static AtomicBoolean block = new AtomicBoolean(false); DummyRetryInvocationHandler(