コード例 #1
0
            /// <exception cref="System.Exception"/>
            public override RetryPolicy.RetryAction ShouldRetry(Exception e, int retries, int
                                                                failovers, bool isMethodIdempotent)
            {
                if (e is ServiceException)
                {
                    //unwrap ServiceException
                    Exception cause = e.InnerException;
                    if (cause != null && cause is Exception)
                    {
                        e = (Exception)cause;
                    }
                }
                //see (1) and (2) in the javadoc of this method.
                RetryPolicy p;

                if (e is RetriableException || RetryPolicies.GetWrappedRetriableException(e) != null)
                {
                    // RetriableException or RetriableException wrapped
                    p = multipleLinearRandomRetry;
                }
                else
                {
                    if (e is RemoteException)
                    {
                        RemoteException re = (RemoteException)e;
                        p = remoteExceptionToRetry.FullName.Equals(re.GetClassName()) ? multipleLinearRandomRetry
                                                         : RetryPolicies.TryOnceThenFail;
                    }
                    else
                    {
                        if (e is IOException || e is ServiceException)
                        {
                            p = multipleLinearRandomRetry;
                        }
                        else
                        {
                            //non-IOException
                            p = RetryPolicies.TryOnceThenFail;
                        }
                    }
                }
                if (RetryUtils.Log.IsDebugEnabled())
                {
                    RetryUtils.Log.Debug("RETRY " + retries + ") policy=" + p.GetType().Name + ", exception="
                                         + e);
                }
                return(p.ShouldRetry(e, retries, failovers, isMethodIdempotent));
            }
コード例 #2
0
        public virtual void TestExpectedIOException()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(NewFlipFlopProxyProvider(UnreliableImplementation.TypeOfExceptionToFailWith.RemoteException
                                                                                                                , UnreliableImplementation.TypeOfExceptionToFailWith.UnreliableException), RetryPolicies
                                                                                       .FailoverOnNetworkException(RetryPolicies.TryOnceThenFail, 10, 1000, 10000));

            try
            {
                unreliable.FailsIfIdentifierDoesntMatch("no-such-identifier");
                NUnit.Framework.Assert.Fail("Should have thrown *some* exception");
            }
            catch (Exception e)
            {
                Assert.True("Expected IOE but got " + e.GetType(), e is IOException
                            );
            }
        }
コード例 #3
0
        public virtual void TestConcurrentMethodFailures()
        {
            TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface> proxyProvider = new
                                                                                          TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface>(typeof(UnreliableInterface
                                                                                                                                                               ), new TestFailoverProxy.SynchronizedUnreliableImplementation("impl1", UnreliableImplementation.TypeOfExceptionToFailWith
                                                                                                                                                                                                                             .StandbyException, 2), new UnreliableImplementation("impl2", UnreliableImplementation.TypeOfExceptionToFailWith
                                                                                                                                                                                                                                                                                 .StandbyException));
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(proxyProvider, RetryPolicies.FailoverOnNetworkException(10));

            TestFailoverProxy.ConcurrentMethodThread t1 = new TestFailoverProxy.ConcurrentMethodThread
                                                              (unreliable);
            TestFailoverProxy.ConcurrentMethodThread t2 = new TestFailoverProxy.ConcurrentMethodThread
                                                              (unreliable);
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            Assert.Equal("impl2", t1.result);
            Assert.Equal("impl2", t2.result);
            Assert.Equal(1, proxyProvider.GetFailoversOccurred());
        }
コード例 #4
0
        public virtual void TestFailoverBetweenMultipleStandbys()
        {
            long millisToSleep             = 10000;
            UnreliableImplementation impl1 = new UnreliableImplementation("impl1", UnreliableImplementation.TypeOfExceptionToFailWith
                                                                          .StandbyException);

            TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface> proxyProvider = new
                                                                                          TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface>(typeof(UnreliableInterface
                                                                                                                                                               ), impl1, new UnreliableImplementation("impl2", UnreliableImplementation.TypeOfExceptionToFailWith
                                                                                                                                                                                                      .StandbyException));
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(proxyProvider, RetryPolicies.FailoverOnNetworkException(RetryPolicies.TryOnceThenFail
                                                                                                                                               , 10, 1000, 10000));

            new _Thread_328(millisToSleep, impl1).Start();
            string result = unreliable.FailsIfIdentifierDoesntMatch("renamed-impl1");

            Assert.Equal("renamed-impl1", result);
        }
コード例 #5
0
        public virtual void TestFailoverOnNetworkExceptionIdempotentOperation()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(NewFlipFlopProxyProvider(UnreliableImplementation.TypeOfExceptionToFailWith.IoException
                                                                                                                , UnreliableImplementation.TypeOfExceptionToFailWith.UnreliableException), RetryPolicies
                                                                                       .FailoverOnNetworkException(1));

            Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString
                             ());
            try
            {
                unreliable.SucceedsOnceThenFailsReturningString();
                NUnit.Framework.Assert.Fail("should not have succeeded twice");
            }
            catch (IOException e)
            {
                // Make sure we *don't* fail over since the first implementation threw an
                // IOException and this method is not idempotent
                Assert.Equal("impl1", e.Message);
            }
            Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningStringIdempotent
                             ());
            // Make sure we fail over since the first implementation threw an
            // IOException and this method is idempotent.
            Assert.Equal("impl2", unreliable.SucceedsOnceThenFailsReturningStringIdempotent
                             ());
        }
コード例 #6
0
        public virtual void TestExceptionPropagatedForNonIdempotentVoid()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(NewFlipFlopProxyProvider(UnreliableImplementation.TypeOfExceptionToFailWith.IoException
                                                                                                                , UnreliableImplementation.TypeOfExceptionToFailWith.UnreliableException), RetryPolicies
                                                                                       .FailoverOnNetworkException(1));

            try
            {
                unreliable.NonIdempotentVoidFailsIfIdentifierDoesntMatch("impl2");
                NUnit.Framework.Assert.Fail("did not throw an exception");
            }
            catch (Exception)
            {
            }
        }
コード例 #7
0
        public virtual void TestFailoverOnStandbyException()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(NewFlipFlopProxyProvider(), RetryPolicies.FailoverOnNetworkException(1));

            Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString
                             ());
            try
            {
                unreliable.SucceedsOnceThenFailsReturningString();
                NUnit.Framework.Assert.Fail("should not have succeeded twice");
            }
            catch (UnreliableInterface.UnreliableException e)
            {
                // Make sure there was no failover on normal exception.
                Assert.Equal("impl1", e.Message);
            }
            unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface>(NewFlipFlopProxyProvider
                                                                                          (UnreliableImplementation.TypeOfExceptionToFailWith.StandbyException, UnreliableImplementation.TypeOfExceptionToFailWith
                                                                                          .UnreliableException), RetryPolicies.FailoverOnNetworkException(1));
            Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString
                             ());
            // Make sure we fail over since the first implementation threw a StandbyException
            Assert.Equal("impl2", unreliable.SucceedsOnceThenFailsReturningString
                             ());
        }
コード例 #8
0
        public virtual void TestRetryInterruptible()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(unreliableImpl, RetryPolicies.RetryUpToMaximumTimeWithFixedSleep(10, 10, TimeUnit
                                                                                                                                                        .Seconds));
            CountDownLatch           latch        = new CountDownLatch(1);
            AtomicReference <Thread> futureThread = new AtomicReference <Thread
                                                                         >();
            ExecutorService    exec   = Executors.NewSingleThreadExecutor();
            Future <Exception> future = exec.Submit(new _Callable_216(futureThread, latch, unreliable
                                                                      ));

            latch.Await();
            Thread.Sleep(1000);
            // time to fail and sleep
            Assert.True(futureThread.Get().IsAlive());
            futureThread.Get().Interrupt();
            Exception e = future.Get(1, TimeUnit.Seconds);

            // should return immediately
            NUnit.Framework.Assert.IsNotNull(e);
            Assert.Equal(typeof(Exception), e.GetType());
            Assert.Equal("sleep interrupted", e.Message);
        }
コード例 #9
0
        public virtual void TestRetryByRemoteException()
        {
            IDictionary <Type, RetryPolicy> exceptionToPolicyMap = Collections.SingletonMap
                                                                   <Type, RetryPolicy>(typeof(UnreliableInterface.FatalException), RetryPolicies.TryOnceThenFail
                                                                                       );
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(unreliableImpl, RetryPolicies.RetryByRemoteException(RetryPolicies.RetryForever
                                                                                                                                            , exceptionToPolicyMap));

            try
            {
                unreliable.AlwaysFailsWithRemoteFatalException();
                NUnit.Framework.Assert.Fail("Should fail");
            }
            catch (RemoteException)
            {
            }
        }
コード例 #10
0
        public virtual void TestExponentialRetry()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(unreliableImpl, RetryPolicies.ExponentialBackoffRetry(5, 1L, TimeUnit.Nanoseconds
                                                                                                                                             ));

            unreliable.AlwaysSucceeds();
            unreliable.FailsOnceThenSucceeds();
            try
            {
                unreliable.FailsTenTimesThenSucceeds();
                NUnit.Framework.Assert.Fail("Should fail");
            }
            catch (UnreliableInterface.UnreliableException)
            {
            }
        }
コード例 #11
0
        public virtual void TestRetryUpToMaximumCountWithProportionalSleep()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(unreliableImpl, RetryPolicies.RetryUpToMaximumCountWithProportionalSleep(8, 1,
                                                                                                                                                                TimeUnit.Nanoseconds));

            unreliable.AlwaysSucceeds();
            unreliable.FailsOnceThenSucceeds();
            try
            {
                unreliable.FailsTenTimesThenSucceeds();
                NUnit.Framework.Assert.Fail("Should fail");
            }
            catch (UnreliableInterface.UnreliableException)
            {
            }
        }