Esempio n. 1
1
        static void Main(string[] args)
        {
             //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 
            using (ZooKeeper zk = new ZooKeeper("121.199.25.195:2181", new TimeSpan(0, 0, 0, 50000), new Watcher()))
            {
                var stat = zk.Exists("/root", true);

                //创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) 
                zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

                //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 
                zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                //取得/root节点下的子节点名称,返回List<String> 
                zk.GetChildren("/root", true);
                //取得/root/childone节点下的数据,返回byte[] 
                zk.GetData("/root/childone", true, null);

                //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉
                zk.SetData("/root/childone", "childonemodify".GetBytes(), -1);
                //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 
                zk.Delete("/root/childone", -1);
            }


        }
Esempio n. 2
0
        public void waitForLock(String lower)
        {
            zk.Register(new LockerCommon());
            //监听比自己次小的节点
            Stat stat = zk.Exists(root + "/" + lower, true);

            if (stat != null)
            {
                Console.WriteLine($"Waiting for {lower}");
            }
            else
            {
                var t = DateTime.Now;
                Console.WriteLine($"{ourPath}访问资源,时间为{DateTime.Now}");
                //访问共享资源
                IsGetLock();
                //释放锁
                Console.WriteLine($"访问结束{(DateTime.Now - t).TotalMilliseconds}");
                try
                {
                    zk.Delete(ourPath, -1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.WriteLine($"{ourPath}已被删除,时间为{DateTime.Now}");
                Console.ReadLine();
            }
        }
 public void DeleteZookeeperNode(string path, int version)
 {
     if (_zk != null)
     {
         _zk.Delete(path, version);
     }
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            // 创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法
            using (var zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher()))
            {
                var stat = zk.Exists("/root", true);

                // 创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失)
                // zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

                // 在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的
                zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

                // 取得/root节点下的子节点名称,返回List<String>
                zk.GetChildren("/root", true);

                // 取得/root/childone节点下的数据,返回byte[]
                zk.GetData("/root/childone", true, null);

                // 修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉
                zk.SetData("/root/childone", "childonemodify".GetBytes(), -1);

                // 删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本
                zk.Delete("/root/childone", -1);
            }

            Console.ReadKey();
        }
        /// <summary>
        ///     Deletes znode for a given path
        /// </summary>
        /// <param name="path">
        ///     The given path.
        /// </param>
        public void Delete(string path)
        {
            Guard.NotNullNorEmpty(path, "path");

            EnsuresNotDisposedAndNotNull();
            _zkclient.Delete(path, -1);
        }
Esempio n. 6
0
 private void DeleteChild(ZooKeeper zk, string path)
 {
     if (!string.IsNullOrEmpty(path) && !path.Contains("praweda") && !path.Contains("zookeeper"))
     {
         var lstChild = zk.GetChildren(path, false);
         foreach (var child in lstChild)
         {
             if (path != "/")
             {
                 DeleteChild(zk, path + "/" + child);
             }
             else
             {
                 DeleteChild(zk, "/" + child);
             }
         }
         if (path != "/")
         {
             try
             {
                 zk.Delete(path, -1);
             }
             catch
             {
             }
         }
     }
 }
Esempio n. 7
0
 public void DeleteNode(string path)
 {
     if (!string.IsNullOrEmpty(path))
     {
         zk.Delete(path, zk.Exists(path, true).Version);
     }
 }
Esempio n. 8
0
        public bool DeleteNode(string nodePath, ref string message)
        {
            try
            {
                using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null))
                {
                    ZooKeeper.WaitUntilConnected(zk);

                    Stat stat = zk.Exists(nodePath, false);
                    if (stat != null)
                    {
                        zk.Delete(nodePath, stat.Version);
                    }
                }
            }
            catch (Exception ex)
            {
                message = string.Format("JinRi.Fx.Manage:{0}DeleteNode()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString());
                log.Error(message);
                message = string.Format("DeleteNode()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString());
                return(false);
            }

            return(true);
        }
Esempio n. 9
0
        public bool DataDelete(string Path)
        {
            int version = 1;

            zk.Delete(Path, version);

            return(true);
        }
Esempio n. 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="zkPath"></param>
 public void Remove(string zkPath)
 {
     _queue.Enqueue(() =>
     {
         //zookeeper在存在子节点时,不允许直接删除父节点,所以需要先删除子节点
         ZooKeeper.RemoveTmpChildNode(zkPath);
         ZooKeeper.Delete(zkPath, -1);
     });
 }
Esempio n. 11
0
 public void testDeleteWithChildren()
 {
     using (ZooKeeper zk = CreateClient())
     {
         zk.Create("/parent", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
         zk.Create("/parent/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
         try
         {
             zk.Delete("/parent", -1);
             Assert.True(false, "Should have received a not equals message");
         }
         catch (KeeperException e)
         {
             Assert.Equal(KeeperException.Code.NOTEMPTY, e.ErrorCode);
         }
         zk.Delete("/parent/child", -1);
         zk.Delete("/parent", -1);
     }
 }
Esempio n. 12
0
        public void testDeleteWithChildren()
        {
            ZooKeeper zk = CreateClient();

            zk.Create("/parent", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
            zk.Create("/parent/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
            try
            {
                zk.Delete("/parent", -1);
                Assert.Fail("Should have received a not equals message");
            }
            catch (KeeperException e)
            {
                Assert.AreEqual(KeeperException.Code.NOTEMPTY, e.GetCode());
            }
            zk.Delete("/parent/child", -1);
            zk.Delete("/parent", -1);
            zk.Dispose();
        }
Esempio n. 13
0
        /// <summary>
        /// 清空所有的服务路由。
        /// </summary>
        /// <returns>一个任务。</returns>
        public Task ClearAsync()
        {
            return(Task.Run(() =>
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.Information("准备清空所有路由配置。");
                }
                var path = _configInfo.RoutePath;
                var childrens = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                var index = 0;
                while (childrens.Any())
                {
                    var nodePath = "/" + string.Join("/", childrens);

                    if (_zooKeeper.Exists(nodePath, false) != null)
                    {
                        foreach (var child in _zooKeeper.GetChildren(nodePath, false))
                        {
                            var childPath = $"{nodePath}/{child}";
                            if (_logger.IsEnabled(LogLevel.Debug))
                            {
                                _logger.Debug($"准备删除:{childPath}。");
                            }
                            _zooKeeper.Delete(childPath, -1);
                        }
                        if (_logger.IsEnabled(LogLevel.Debug))
                        {
                            _logger.Debug($"准备删除:{nodePath}。");
                        }
                        _zooKeeper.Delete(nodePath, -1);
                    }
                    index++;
                    childrens = childrens.Take(childrens.Length - index).ToArray();
                }
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.Information("路由配置清空完成。");
                }
            }));
        }
Esempio n. 14
0
 /// <summary>
 /// 删除节点
 /// </summary>
 /// <param name="path"></param>
 public void DeleteNode(string path)
 {
     try
     {
         ZooKeeper.Delete(path, -1);
     }
     catch (Exception ex)
     {
         LogManager.GetLogger().Error(string.Format("DisconfClient.ZooKeeperClient.DeleteNode(path={0}),error:{1}", path, ex));
     }
 }
Esempio n. 15
0
 /// <summary>
 /// 解除锁
 /// </summary>
 public virtual void Unlock()
 {
     try
     {
         Console.WriteLine("unlock " + _myZnode);
         _zk.Delete(_myZnode, -1);
         _myZnode = null;
         _zk.Dispose();
     }
     catch (KeeperException e)
     {
         throw e;
     }
 }
Esempio n. 16
0
 public void CreateLeaderNode(string LeaderIp)
 {
     try
     {
         var ssn  = GetChildNodes($"/");
         var root = BOD.NodeDetails.ClusterName + "-Leader";
         var d    = GetChildNodes("/").Where(p => p == root).ToList().Count;
         if (d == 0)
         {
             zk.Create($"/{root}", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
         }
         var ss = GetChildNodes($"/{root}");
         foreach (var item in GetChildNodes($"/{root}"))
         {
             zk.Delete($"/{root}/{item}", 0);
         }
         zk.Create($"/{root}/{LeaderIp}", LeaderIp.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Esempio n. 17
0
        public virtual void TestNumberOfTransactionsWithGaps()
        {
            NamespaceInfo            nsi  = NewNSInfo();
            BookKeeperJournalManager bkjm = new BookKeeperJournalManager(conf, BKJMUtil.CreateJournalURI
                                                                             ("/hdfsjournal-gaps"), nsi);

            bkjm.Format(nsi);
            long txid = 1;

            for (long i = 0; i < 3; i++)
            {
                long start = txid;
                EditLogOutputStream @out = bkjm.StartLogSegment(start, NameNodeLayoutVersion.CurrentLayoutVersion
                                                                );
                for (long j = 1; j <= DefaultSegmentSize; j++)
                {
                    FSEditLogOp op = FSEditLogTestUtil.GetNoOpInstance();
                    op.SetTransactionId(txid++);
                    @out.Write(op);
                }
                @out.Close();
                bkjm.FinalizeLogSegment(start, txid - 1);
                NUnit.Framework.Assert.IsNotNull(zkc.Exists(bkjm.FinalizedLedgerZNode(start, txid
                                                                                      - 1), false));
            }
            zkc.Delete(bkjm.FinalizedLedgerZNode(DefaultSegmentSize + 1, DefaultSegmentSize *
                                                 2), -1);
            long numTrans = bkjm.GetNumberOfTransactions(1, true);

            NUnit.Framework.Assert.AreEqual(DefaultSegmentSize, numTrans);
            try
            {
                numTrans = bkjm.GetNumberOfTransactions(DefaultSegmentSize + 1, true);
                NUnit.Framework.Assert.Fail("Should have thrown corruption exception by this point"
                                            );
            }
            catch (JournalManager.CorruptionException)
            {
            }
            // if we get here, everything is going good
            numTrans = bkjm.GetNumberOfTransactions((DefaultSegmentSize * 2) + 1, true);
            NUnit.Framework.Assert.AreEqual(DefaultSegmentSize, numTrans);
        }
Esempio n. 18
0
        public void testChrootedWatchReestablishment()
        {
            string chroot = "/" + Guid.NewGuid() + "watchtest";

            using (ZooKeeper zk1 = CreateClient())
            {
                Assert.AreEqual(chroot, zk1.Create(chroot, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent));
            }

            ZooKeeper zk      = CreateClient(chroot);
            MyWatcher watcher = new MyWatcher();

            string name = "/watchtest";

            zk.GetChildren("/", watcher);
            zk.Create(name, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);

            WatchedEvent @event;

            Assert.IsTrue(watcher.events.TryTake(out @event, TimeSpan.FromSeconds(3d)));
            Assert.AreEqual("/", @event.Path);
            Assert.AreEqual(EventType.NodeChildrenChanged, @event.Type);
            Assert.AreEqual(KeeperState.SyncConnected, @event.State);

            zk.GetChildren("/", watcher);

            ClientConnection cnxn = (ClientConnection)zk.GetType().GetField("cnxn", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(zk);
            ClientConnectionRequestProducer producer = (ClientConnectionRequestProducer)cnxn.GetType().GetField("producer", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cnxn);
            TcpClient client = (TcpClient)producer.GetType().GetField("client", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(producer);

            client.Close();
            Thread.Sleep(100);

            zk.Delete(name, -1);

            Assert.IsTrue(watcher.events.TryTake(out @event, TimeSpan.FromSeconds(3d)));
            Assert.AreEqual("/", @event.Path);
            Assert.AreEqual(EventType.NodeChildrenChanged, @event.Type);
            Assert.AreEqual(KeeperState.SyncConnected, @event.State);

            Assert.AreEqual(0, watcher.events.Count);
        }
Esempio n. 19
0
        /**
         * Wait until all reach barrier
         *
         * @return
         * @throws KeeperException
         * @throws InterruptedException
         */
        public bool Leave()
        {
            _zk.Delete(_root + "/" + _nodeName, 0);

            while (true)
            {
                lock (_mutex)
                {
                    var children = _zk.GetChildren(_root, true);
                    if (children.Any())
                    {
                        Monitor.Wait(_mutex);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
        }
Esempio n. 20
0
        public void testPing()
        {
            ZooKeeper zkIdle         = null;
            ZooKeeper zkWatchCreator = null;

            try
            {
                CountdownWatcher watcher = new CountdownWatcher();
                zkIdle = CreateClient();

                zkWatchCreator = CreateClient(watcher);

                var node = Guid.NewGuid();
                for (int i = 0; i < 10; i++)
                {
                    zkWatchCreator.Create("/" + node + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                }
                for (int i = 0; i < 10; i++)
                {
                    zkIdle.Exists("/" + node + i, true);
                }
                for (int i = 0; i < 10; i++)
                {
                    Thread.Sleep(1000);
                    zkWatchCreator.Delete("/" + node + i, -1);
                }
                // The bug will manifest itself here because zkIdle will expire
                zkIdle.Exists("/0", false);
            }
            finally
            {
                if (zkIdle != null)
                {
                    zkIdle.Dispose();
                }
                if (zkWatchCreator != null)
                {
                    zkWatchCreator.Dispose();
                }
            }
        }
Esempio n. 21
0
        public void Delete(String path, int version)
        {
            int tries = retries;

            while ((tries--) > 0)
            {
                try
                {
                    zk.Delete(path, version);
                    break;
                }
                catch (Exception ex)
                {
                    if (tries == 0)
                    {
                        Console.WriteLine("Delete exception after #" + retries + " retries :\n" + ex.Message);
                        Console.WriteLine("Last retry, throwing exception");
                        throw ex;
                    }
                }
            }
        }
Esempio n. 22
0
        public bool Leave()
        {
            Console.WriteLine("[" + owner + "] Barrier.Leave Starting");
            //System.Threading.Thread.Sleep(1000);
            zk.GetChildren(synchPath, true);
            zk.Delete(name, 0);

            while (true)
            {
                lock (mutex)
                {
                    var list = zk.GetChildren(synchPath, true);
                    if (list.Count() > 0)
                    {
                        System.Threading.Monitor.Pulse(mutex);
                    }
                    else
                    {
                        Console.WriteLine("[" + owner + "] Barrier.Leave Done");
                        return(true);
                    }
                }
            }
        }
Esempio n. 23
0
        public void testChrootSynchronous()
        {
            string ch1 = "/" + Guid.NewGuid() + "ch1";

            using (ZooKeeper zk1 = CreateClient())
            {
                Assert.AreEqual(ch1, zk1.Create(ch1, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent));
            }

            string ch2 = "/" + Guid.NewGuid() + "ch2";

            using (ZooKeeper zk2 = CreateClient(ch1))
            {
                Assert.AreEqual(ch2, zk2.Create(ch2, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent));
            }

            using (ZooKeeper zk1 = CreateClient())
                using (ZooKeeper zk2 = CreateClient(ch1))
                {
                    // check get
                    MyWatcher w1 = new MyWatcher("w1", ch1);
                    Assert.NotNull(zk1.Exists(ch1, w1));
                    string    ch1Ch2 = string.Format("{0}{1}", ch1, ch2);
                    MyWatcher w2     = new MyWatcher("w2", ch1Ch2);
                    Assert.NotNull(zk1.Exists(ch1Ch2, w2));

                    MyWatcher w3 = new MyWatcher("w3", ch2);
                    Assert.NotNull(zk2.Exists(ch2, w3));

                    // set watches on child
                    MyWatcher w4 = new MyWatcher("w4", ch1);
                    zk1.GetChildren(ch1, w4);
                    MyWatcher w5 = new MyWatcher("w5", "/");
                    zk2.GetChildren("/", w5);

                    // check set
                    zk1.SetData(ch1, Encoding.UTF8.GetBytes("1"), -1);
                    zk2.SetData(ch2, "2".GetBytes(), -1);

                    // check watches
                    Assert.True(w1.Matches());
                    Assert.True(w2.Matches());
                    Assert.True(w3.Matches());

                    // check exceptions
                    string ch3 = "/" + Guid.NewGuid() + "ch3";
                    try
                    {
                        zk2.SetData(ch3, "3".GetBytes(), -1);
                    }
                    catch (KeeperException.NoNodeException e)
                    {
                        Assert.AreEqual(ch3, e.getPath());
                    }

                    Assert.AreEqual("1".GetBytes(), zk1.GetData(ch1, false, null));
                    Assert.AreEqual("2".GetBytes(), zk1.GetData(ch1Ch2, false, null));
                    Assert.AreEqual("2".GetBytes(), zk2.GetData(ch2, false, null));

                    // check delete
                    zk2.Delete(ch2, -1);
                    Assert.True(w4.Matches());
                    Assert.True(w5.Matches());

                    zk1.Delete(ch1, -1);
                    Assert.Null(zk1.Exists(ch1, false));
                    Assert.Null(zk1.Exists(ch1Ch2, false));
                    Assert.Null(zk2.Exists(ch2, false));
                }
        }
Esempio n. 24
0
        public void testRootAcl()
        {
            ZooKeeper zk = CreateClient();

            // set auth using digest
            zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("pat:test"));
            zk.SetACL("/", Ids.CREATOR_ALL_ACL, -1);
            zk.GetData("/", false, null);
            zk.Dispose();

            // verify no access
            zk = CreateClient();
            try
            {
                zk.GetData("/", false, null);
                Assert.Fail("validate auth");
            }
            catch (KeeperException.NoAuthException e)
            {
                // expected
            }
            string path = "/" + Guid.NewGuid() + "apps";

            try
            {
                zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent);
                Assert.Fail("validate auth");
            }
            catch (KeeperException.InvalidACLException e)
            {
                // expected
            }
            zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("world:anyone"));
            try
            {
                zk.Create(path, null, Ids.CREATOR_ALL_ACL,
                          CreateMode.Persistent);
                Assert.Fail("validate auth");
            }
            catch (KeeperException.NoAuthException e)
            {
                // expected
            }
            zk.Dispose();
            // verify access using original auth
            zk = CreateClient();
            zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("pat:test"));
            zk.GetData("/", false, null);
            zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent);
            zk.Delete(path, -1);
            // reset acl (back to open) and verify accessible again
            zk.SetACL("/", Ids.OPEN_ACL_UNSAFE, -1);
            zk.Dispose();
            zk = CreateClient();
            zk.GetData("/", false, null);
            zk.Create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
            try
            {
                zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent);
                Assert.Fail("validate auth");
            }
            catch (KeeperException.InvalidACLException e)
            {
                // expected
            }
            zk.Delete(path, -1);
            zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("world:anyone"));
            zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent);
            zk.Dispose();
            zk = CreateClient();
            zk.Delete(path, -1);
        }
Esempio n. 25
0
        private void Delete(ZooKeeper zk, List<string> children)
        {
            foreach (var child in children)
            {
                List<string> tmpChildren = zk.GetChildren(child, false).ToList();

                if (tmpChildren.Count == 0)
                {
                    zk.Delete(child, -1);
                }
                else
                {
                    List<string> realPath = new List<string>();
                    foreach (var tmpChild in tmpChildren)
                    {
                        realPath.Add(child + "/" + tmpChild);
                    }
                    Delete(zk, realPath);
                }
            }
        }
Esempio n. 26
0
 public static void Delete(string path, int version)
 {
     Zk.Delete(path, version);
 }
Esempio n. 27
0
 public void Dispose()
 {
     ZK.Delete(OurPath, -1);
 }