Esempio n. 1
0
        /**
         * Convenience utility: creates a "session fail" retry loop calling the given proc
         *
         * @param client Zookeeper
         * @param mode how to handle session failures
         * @param proc procedure to call with retry
         * @param <T> return type
         * @return procedure result
         * @throws Exception any non-retriable errors
         */
        public static T callWithRetry <T>(CuratorZookeeperClient client, Mode mode, ICallable <T> proc)
        {
            T result = default(T);
            SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(mode);

            retryLoop.start();
            try
            {
                while (retryLoop.shouldContinue())
                {
                    try
                    {
                        result = proc.call();
                    }
                    catch (Exception e)
                    {
                        ThreadUtils.checkInterrupted(e);
                        retryLoop.takeException(e);
                    }
                }
            }
            finally
            {
                retryLoop.Dispose();
            }
            return(result);
        }
        public void testBasic()
        {
            CuratorZookeeperClient    client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                        DefaultSessionTimeout,
                                                                        DefaultConnectionTimeout,
                                                                        null,
                                                                        new RetryOneTime(1));
            SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
            retryLoop.start();
            try
            {
            client.start();
            try
            {
                while ( retryLoop.shouldContinue() )
                {
                    try
                    {
                        RetryLoop.callWithRetry
                        (
                            client,
                            CallableUtils.FromFunc<object>(() =>
                            {
                                Task<Stat> existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                existsTask.Wait();
                                Assert.Null(existsTask.Result);
                                KillSession.kill(client.getZooKeeper(), ZkDefaultHosts,DefaultSessionTimeout);

                                client.getZooKeeper();
                                client.blockUntilConnectedOrTimedOut();
                                existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                existsTask.Wait();
                                Assert.Null(existsTask.Result);
                                return null;
                            }
                        ));
                    }
                    catch ( Exception e )
                    {
                        retryLoop.takeException(e);
                    }
                }
                Assert.Fail();
            }
            catch ( SessionFailRetryLoop.SessionFailedException dummy )
            {
                // correct
            }
            }
            finally
            {
            retryLoop.Dispose();
            CloseableUtils.closeQuietly(client);
            }
        }
        public void testRetryStatic()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                        DefaultSessionTimeout,
                                                                        DefaultConnectionTimeout,
                                                                        null,
                                                                        new RetryOneTime(1));
            SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
            retryLoop.start();
                try
                {
                    client.start();
                    AtomicBoolean     secondWasDone = new AtomicBoolean(false);
                    AtomicBoolean     firstTime = new AtomicBoolean(true);
                    SessionFailRetryLoop.callWithRetry
                    (
                        client,
                        SessionFailRetryLoop.Mode.RETRY,
                        CallableUtils.FromFunc<object>(() =>
                        {
                            RetryLoop.callWithRetry(
                                    client,
                                    CallableUtils.FromFunc<object>(() =>
                                    {
                                        Task<Stat> existsTask;
                                        if ( firstTime.compareAndSet(true, false) )
                                        {
                                            existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                            existsTask.Wait();
                                            Assert.Null(existsTask.Result);
                                            KillSession.kill(client.getZooKeeper(), ZkDefaultHosts, DefaultSessionTimeout);
                                            client.getZooKeeper();
                                            client.blockUntilConnectedOrTimedOut();
                                        }
                                        existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                        existsTask.Wait();
                                        Assert.Null(existsTask.Result);
                                        return null;
                                    }
                            ));

                            RetryLoop.callWithRetry
                            (
                                client,
                                CallableUtils.FromFunc<object>(() =>
                                {
                                    Assert.False(firstTime.get());
                                    Task<Stat> existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                    existsTask.Wait();
                                    Assert.Null(existsTask.Result);
                                    secondWasDone.set(true);
                                    return null;
                                }
                            ));
                            return null;
                        }
                    ));

            Assert.True(secondWasDone.get());
            }
            finally
            {
            retryLoop.Dispose();
            CloseableUtils.closeQuietly(client);
            }
        }