Esempio n. 1
0
        public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder)
        {
            IZookeeperFactory localZookeeperFactory = makeZookeeperFactory(builder.GetZookeeperFactory());

            this.client = new CuratorZookeeperClient(localZookeeperFactory,
                                                     builder.GetEnsembleProvider(),
                                                     builder.GetSessionTimeoutMs(),
                                                     builder.GetConnectionTimeoutMs(),
                                                     new WatchedWatcher(this),
                                                     builder.GetRetryPolicy(),
                                                     builder.CanBeReadOnly());

            listeners = new ListenerContainer <ICuratorListener>();
            unhandledErrorListeners = new ListenerContainer <IUnhandledErrorListener>();
            backgroundOperations    = new DelayQueue <OperationAndData <object> >();
            @namespace             = new NamespaceImpl(this, builder.GetNamespace());
            maxCloseWaitMs         = builder.GetMaxCloseWaitMs();
            connectionStateManager = new ConnectionStateManager(this, builder.GetEventQueueSize());
            compressionProvider    = builder.GetCompressionProvider();
            aclProvider            = builder.GetAclProvider();
            state = new AtomicReference <CuratorFrameworkState>(CuratorFrameworkState.LATENT);
            useContainerParentsIfAvailable = builder.UseContainerParentsIfAvailable();

            byte[] builderDefaultData = builder.GetDefaultData();
            byte[] destDefaults       = new byte[builderDefaultData.Length];
            Array.Copy(builderDefaultData, destDefaults, builderDefaultData.Length);
            defaultData = (builderDefaultData != null)
                            ?  destDefaults
                            : new byte[0];
            authInfos = buildAuths(builder);

            failedDeleteManager  = new FailedDeleteManager(this);
            namespaceWatcherMap  = new NamespaceWatcherMap(this);
            namespaceFacadeCache = new NamespaceFacadeCache(this);
        }
Esempio n. 2
0
        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));
        }
Esempio n. 3
0
        public void testBackgroundConnect()
        {
            int CONNECTION_TIMEOUT_MS = 4000;

            using (CuratorZookeeperClient client = new CuratorZookeeperClient(server.GetConnectionString(), 10000, CONNECTION_TIMEOUT_MS, null, new RetryOneTime(TimeSpan.FromSeconds(1))))
            {
                Assert.False(client.IsConnected());
                client.Start();

outer:
                do
                {
                    for (int i = 0; i < (CONNECTION_TIMEOUT_MS / 1000); ++i)
                    {
                        if (client.IsConnected())
                        {
                            goto outer;
                        }

                        Thread.Sleep(CONNECTION_TIMEOUT_MS);
                    }

                    Assert.Fail();
                } while (false);
            }
        }
Esempio n. 4
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();
            }
        }
Esempio n. 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);
             })
             );
         }
     }
 }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        public void testRetryLoopWithFailure()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       DefaultSessionTimeout,
                                                                       DefaultConnectionTimeout,
                                                                       null,
                                                                       new RetryOneTime(1));

            client.start();
            try
            {
                int       loopCount = 0;
                RetryLoop retryLoop = client.newRetryLoop();
                while (retryLoop.shouldContinue())
                {
                    ++loopCount;
                    switch (loopCount)
                    {
                    case 1:
                    {
                        //                            retryLoop.takeException();
                        break;
                    }

                    case 2:
                    {
                        retryLoop.markComplete();
                        break;
                    }

                    case 3:
                    case 4:
                    {
                        // ignore
                        break;
                    }

                    default:
                    {
                        Assert.Fail();
                        break;
                    }
                    }
                }

                Assert.True(loopCount >= 2);
            }
            finally
            {
                client.Dispose();
            }
        }
Esempio n. 8
0
        public void testBasic()
        {
            IRetryPolicy           retryPolicy = new RetryOneTime(1);
            EnsurePath             ensurePath  = new EnsurePath("/one/two/three");
            CuratorZookeeperClient curator
                = new CuratorZookeeperClient(new FixedEnsembleProvider(ZkDefaultHosts),
                                             DefaultSessionTimeout,
                                             DefaultConnectionTimeout,
                                             null,
                                             retryPolicy);

            curator.start();
            ensurePath.ensure(curator);
        }
Esempio n. 9
0
        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();
            }
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public void testSimple()
        {
            using (CuratorZookeeperClient client = new CuratorZookeeperClient(server.GetConnectionString(), 10000, 10000, null, new RetryOneTime(TimeSpan.FromSeconds(1))))
            {
                client.Start();

                client.BlockUntilConnectedOrTimedOut();
                var stat = client.GetZooKeeper().Exists("/test", false);

                if (stat != null)
                {
                    client.GetZooKeeper().Delete("/test", -1);
                }
                String path = client.GetZooKeeper().Create("/test", new byte[] { 1, 2, 3 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                Assert.AreEqual(path, "/test");
            }
        }
Esempio n. 12
0
 protected CuratorFrameworkImpl(CuratorFrameworkImpl parent)
 {
     client    = parent.client;
     listeners = parent.listeners;
     unhandledErrorListeners = parent.unhandledErrorListeners;
     maxCloseWaitMs          = parent.maxCloseWaitMs;
     backgroundOperations    = parent.backgroundOperations;
     connectionStateManager  = parent.connectionStateManager;
     defaultData             = parent.defaultData;
     failedDeleteManager     = parent.failedDeleteManager;
     compressionProvider     = parent.compressionProvider;
     aclProvider             = parent.aclProvider;
     namespaceFacadeCache    = parent.namespaceFacadeCache;
     @namespace = new NamespaceImpl(this, null);
     state      = parent.state;
     authInfos  = parent.authInfos;
     _useContainerParentsIfAvailable = parent._useContainerParentsIfAvailable;
 }
Esempio n. 13
0
        public void TestExpiredSession()
        {
            var timing  = new Timing();
            var latch   = new AutoResetEvent(false);
            var watcher = new CuratorWatcher((e) => {
                if (e.State == KeeperState.Expired)
                {
                    latch.Set();
                }
            });

            using (var client = new CuratorZookeeperClient(server.GetConnectionString(), timing.session(), timing.connection(), watcher, new RetryOneTime(TimeSpan.FromMilliseconds(2))))
            {
                client.Start();

                bool firstTime = true;
                RetryLoop.CallWithRetry <bool>(client, () =>
                {
                    if (firstTime)
                    {
                        try
                        {
                            var stat = client.GetZooKeeper().Exists("/foo", false);
                            if (stat == null)
                            {
                                client.GetZooKeeper().Create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                            }
                        }
                        catch (KeeperException.NodeExistsException ignore)
                        {
                        }

                        KillSession.kill(client.GetZooKeeper(), server.GetConnectionString());
                        Assert.IsFalse(timing.awaitLatch(latch));
                    }

                    IZooKeeper zooKeeper = client.GetZooKeeper();
                    client.BlockUntilConnectedOrTimedOut();
                    Assert.IsNotNull(zooKeeper.Exists("/foo", false));

                    return(true);
                });
            }
        }
Esempio n. 14
0
        public void testReconnect()
        {
            using (CuratorZookeeperClient client = new CuratorZookeeperClient(server.GetConnectionString(), 10000, 10000, null, new RetryOneTime(TimeSpan.FromSeconds(1))))
            {
                client.Start();

                client.BlockUntilConnectedOrTimedOut();

                byte[] writtenData = { 1, 2, 3 };

                var stat = client.GetZooKeeper().Exists("/test", false);
                if (stat == null)
                {
                    client.GetZooKeeper().Create("/test", writtenData, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                }

                Assert.IsTrue(client.BlockUntilConnectedOrTimedOut());
                byte[] readData = client.GetZooKeeper().GetData("/test", false, null);
                Assert.AreEqual(readData, writtenData);
            }
        }
Esempio n. 15
0
        public void testBackgroundConnect()
        {
            int CONNECTION_TIMEOUT_MS = 4000;

            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       10000,
                                                                       CONNECTION_TIMEOUT_MS,
                                                                       null,
                                                                       new RetryOneTime(1));

            try
            {
                Assert.False(client.isConnected());
                client.start();
                bool outerMustContinue = false;
                do
                {
                    for (int i = 0; i < (CONNECTION_TIMEOUT_MS / 1000); ++i)
                    {
                        if (client.isConnected())
                        {
                            outerMustContinue = true;
                            break;
                        }
                        Thread.Sleep(CONNECTION_TIMEOUT_MS);
                    }
                    if (outerMustContinue)
                    {
                        continue;
                    }
                    Assert.Fail();
                } while (false);
            }
            finally
            {
                client.Dispose();
            }
        }
Esempio n. 16
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();
            }
        }
Esempio n. 17
0
        /**
         * First time, synchronizes and makes sure all nodes in the path are created. Subsequent calls
         * with this instance are NOPs.
         *
         * @param client ZK client
         * @throws Exception ZK errors
         */
        public void ensure(CuratorZookeeperClient client)
        {
            Helper localHelper = helper.Get();

            localHelper.ensure(client, path, makeLastNode);
        }
Esempio n. 18
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);
            }
        }
Esempio n. 19
0
 public void ensure(CuratorZookeeperClient client, string path, bool makeLastNode)
 {
 }
Esempio n. 20
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;
                  })
             );
         }
     }
 }
Esempio n. 21
0
 /**
  * First time, synchronizes and makes sure all nodes in the path are created. Subsequent calls
  * with this instance are NOPs.
  *
  * @param client ZK client
  * @throws Exception ZK errors
  */
 public void ensure(CuratorZookeeperClient client)
 {
     Helper localHelper = helper.Get();
     localHelper.ensure(client, path, makeLastNode);
 }
Esempio n. 22
0
 public void ensure(CuratorZookeeperClient client, string path, bool makeLastNode)
 {
 }
Esempio n. 23
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();
            }
        }