Exemple #1
0
        public async Task testSimple()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       10000,
                                                                       10000,
                                                                       null,
                                                                       new RetryOneTime(1));

            client.start();
            try
            {
                client.blockUntilConnectedOrTimedOut();
                var path = "/test";
                if (await client.getZooKeeper().existsAsync(path, false) != null)
                {
                    client.getZooKeeper().deleteAsync(path).Wait();
                }

                var pathTaskResult = await client.getZooKeeper().createAsync(path,
                                                                             new byte[] { 1, 2, 3 },
                                                                             ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                                             CreateMode.PERSISTENT);

                Assert.AreEqual(pathTaskResult, "/test");
            }
            finally
            {
                client.Dispose();
            }
        }
Exemple #2
0
        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 async Task testRetryLoop()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       DefaultSessionTimeout,
                                                                       DefaultConnectionTimeout,
                                                                       null,
                                                                       new RetryOneTime(1));

            client.start();
            try
            {
                int       loopCount = 0;
                RetryLoop retryLoop = client.newRetryLoop();
                while (retryLoop.shouldContinue())
                {
                    if (++loopCount > 2)
                    {
                        Assert.Fail();
                        break;
                    }

                    try
                    {
                        var path = "/test";
                        if (await client.getZooKeeper().existsAsync(path, false) != null)
                        {
                            client.getZooKeeper().deleteAsync(path).Wait();
                        }

                        client.getZooKeeper().createAsync(path,
                                                          new byte[] { 1, 2, 3 },
                                                          ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                          CreateMode.EPHEMERAL)
                        .Wait();
                        retryLoop.markComplete();
                    }
                    catch (Exception e)
                    {
                        retryLoop.takeException(e);
                    }
                }

                Assert.True(loopCount > 0);
            }
            finally
            {
                client.Dispose();
            }
        }
        internal String fixForNamespace(String path, bool isSequential)
        {
            if (ensurePathNeeded.get())
            {
                try
                {
                    CuratorZookeeperClient zookeeperClient = client.getZookeeperClient();
                    RetryLoop.callWithRetry
                    (
                        zookeeperClient,
                        CallableUtils.FromFunc <object>(() =>
                    {
                        ZKPaths.mkdirs(zookeeperClient.getZooKeeper(),
                                       ZKPaths.makePath("/", @namespace),
                                       true,
                                       client.getAclProvider(),
                                       true);
                        return(null);
                    })
                    );
                    ensurePathNeeded.set(false);
                }
                catch (Exception e)
                {
                    ThreadUtils.checkInterrupted(e);
                    client.logError("Ensure path threw exception", e);
                }
            }

            return(ZKPaths.fixForNamespace(@namespace, path, isSequential));
        }
Exemple #5
0
 public void ensure(CuratorZookeeperClient client,
                    string path,
                    bool makeLastNode)
 {
     lock (this)
     {
         if (!isSet)
         {
             RetryLoop.callWithRetry
             (
                 client,
                 CallableUtils.FromFunc <object>(() =>
             {
                 ZKPaths.mkdirs(client.getZooKeeper(),
                                path,
                                makeLastNode,
                                _ensurePath.aclProvider,
                                _ensurePath.asContainers());
                 _ensurePath.helper.Set(doNothingHelper);
                 isSet = true;
                 return(null);
             })
             );
         }
     }
 }
Exemple #6
0
        public void testFactory()
        {
            IZookeeperFactory      zookeeperFactory = new SingleInstanceZkFactory(Zookeeper);
            CuratorZookeeperClient client
                = new CuratorZookeeperClient(zookeeperFactory,
                                             new FixedEnsembleProvider(ZkDefaultHosts),
                                             DefaultSessionTimeout,
                                             DefaultConnectionTimeout,
                                             null,
                                             new RetryOneTime(1),
                                             false);

            client.start();
            Assert.AreEqual(client.getZooKeeper(), Zookeeper);
        }
Exemple #7
0
        public void testSimple()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       10000,
                                                                       10000,
                                                                       null,
                                                                       new RetryOneTime(1));

            client.start();
            try
            {
                client.blockUntilConnectedOrTimedOut();
                Task <string> pathTask = client.getZooKeeper().createAsync("/test",
                                                                           new byte[] { 1, 2, 3 },
                                                                           ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                                           CreateMode.PERSISTENT);
                pathTask.Wait();
                Assert.AreEqual(pathTask.Result, "/test");
            }
            finally
            {
                client.Dispose();
            }
        }
Exemple #8
0
        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);
            }
        }
Exemple #9
0
 public void ensure(CuratorZookeeperClient client, 
                                     string path, 
                                     bool makeLastNode)
 {
     lock (this)
     {
         if ( !isSet )
         {
             RetryLoop.callWithRetry
             (
                  client,
                  CallableUtils.FromFunc<object>(() =>
                  {
                      ZKPaths.mkdirs(client.getZooKeeper(),
                                         path,
                                         makeLastNode,
                                         _ensurePath.aclProvider,
                                         _ensurePath.asContainers());
                      _ensurePath.helper.Set(doNothingHelper);
                      isSet = true;
                      return null;
                  })
             );
         }
     }
 }
Exemple #10
0
        public void testExpiredSession()
        {
            // WARN: test requires that this must be address of one ZK host,
            // not a connection string to many nodes
            Barrier expiresBarrier        = new Barrier(2);
            Watcher watcher               = new ExpiredWatcher(expiresBarrier);
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       DefaultSessionTimeout,
                                                                       DefaultConnectionTimeout,
                                                                       watcher,
                                                                       new RetryOneTime(2));

            client.start();
            try
            {
                AtomicBoolean firstTime = new AtomicBoolean(true);
                RetryLoop.callWithRetry(
                    client,
                    CallableUtils.FromFunc <object>(() =>
                {
                    if (firstTime.compareAndSet(true, false))
                    {
                        try
                        {
                            Task <string> createTask = client.getZooKeeper()
                                                       .createAsync("/foo",
                                                                    new byte[0],
                                                                    ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                                    CreateMode.PERSISTENT);
                            createTask.Wait();
                        }
                        catch (AggregateException e)
                        {
                            if (e.InnerException is KeeperException.NodeExistsException)
                            {
                                // ignore
                            }
                            else
                            {
                                throw e;
                            }
                        }

                        KillSession.kill(client.getZooKeeper(), ZkDefaultHosts, DefaultSessionTimeout * 3);
                        Assert.True(expiresBarrier.SignalAndWait(DefaultSessionTimeout));
                    }
                    ZooKeeper zooKeeper = client.getZooKeeper();
                    client.blockUntilConnectedOrTimedOut();
                    Task <Stat> task = zooKeeper.existsAsync("/foo", false);
                    task.Wait();
                    Stat stat = task.Result;
                    Assert.NotNull(stat);
                    Assert.Greater(stat.getCzxid(), 0);
                    return(null);
                })
                    );
            }
            finally
            {
                client.Dispose();
            }
        }
Exemple #11
0
 internal ZooKeeper getZooKeeper()
 {
     return(client.getZooKeeper());
 }