Example #1
0
        public void testDeleteRecursive()
        {
            ZooKeeper zk = createClient();

            // making sure setdata works on /
            zk.setData("/", "some".getBytes(), -1);
            zk.create("/a", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            zk.create("/a/b", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            zk.create("/a/b/v", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            zk.create("/a/b/v/1", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            zk.create("/a/c", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            zk.create("/a/c/v", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            IList <string> children = zk.getChildren("/a", false);

            Assert.assertEquals("2 children - b & c should be present ", children.Count, 2);
            Assert.assertTrue(children.Contains("b"));
            Assert.assertTrue(children.Contains("c"));

            ZKUtil.deleteRecursiveAsync(zk, "/a").GetAwaiter().GetResult();
            Assert.assertNull(zk.exists("/a", null));
        }
Example #2
0
        public void testSequentialNodeNames()
        {
            string path     = "/SEQUENCE";
            string file     = "TEST";
            string filepath = path + "/" + file;

            ZooKeeper zk = null;

            try {
                zk = createClient();
                zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                IList <string> children = zk.getChildren(path, false);
                Assert.assertEquals(1, children.Count);
                Assert.assertEquals(file + "0000000000", children[0]);

                zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                children = zk.getChildren(path, false);
                Assert.assertEquals(2, children.Count);
                Assert.assertTrue("contains child 1", children.Contains(file + "0000000001"));

                zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                children = zk.getChildren(path, false);
                Assert.assertEquals(3, children.Count);
                Assert.assertTrue("contains child 2", children.Contains(file + "0000000002"));

                // The pattern is holding so far.  Let's run the counter a bit
                // to be sure it continues to spit out the correct answer
                for (int i = children.Count; i < 105; i++)
                {
                    zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                }

                children = zk.getChildren(path, false);
                Assert.assertTrue("contains child 104", children.Contains(file + "0000000104"));
            }
            finally {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
Example #3
0
        public void testChild()
        {
            string name = "/foo";

            zk.create(name, name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            string childname = name + "/bar";

            zk.create(childname, childname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

            Stat           stat = new Stat();
            IList <string> s    = zk.getChildren(name, false, stat);

            Assert.assertEquals(stat.getCzxid(), stat.getMzxid());
            Assert.assertEquals(stat.getCzxid() + 1, stat.getPzxid());
            Assert.assertEquals(stat.getCtime(), stat.getMtime());
            Assert.assertEquals(1, stat.getCversion());
            Assert.assertEquals(0, stat.getVersion());
            Assert.assertEquals(0, stat.getAversion());
            Assert.assertEquals(0, stat.getEphemeralOwner());
            Assert.assertEquals(name.Length, stat.getDataLength());
            Assert.assertEquals(1, stat.getNumChildren());
            Assert.assertEquals(s.Count, stat.getNumChildren());

            s = zk.getChildren(childname, false, stat);

            Assert.assertEquals(stat.getCzxid(), stat.getMzxid());
            Assert.assertEquals(stat.getCzxid(), stat.getPzxid());
            Assert.assertEquals(stat.getCtime(), stat.getMtime());
            Assert.assertEquals(0, stat.getCversion());
            Assert.assertEquals(0, stat.getVersion());
            Assert.assertEquals(0, stat.getAversion());
            Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner());
            Assert.assertEquals(childname.Length, stat.getDataLength());
            Assert.assertEquals(0, stat.getNumChildren());
            Assert.assertEquals(s.Count, stat.getNumChildren());
        }
Example #4
0
        public void testSequentialNodeData()
        {
            ZooKeeper    zk           = null;
            const string queue_handle = "/queue";

            try {
                zk = createClient();

                zk.create(queue_handle, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                zk.create(queue_handle + "/element", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                          CreateMode.PERSISTENT_SEQUENTIAL);
                zk.create(queue_handle + "/element", "1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                          CreateMode.PERSISTENT_SEQUENTIAL);
                IList <string> children = zk.getChildren(queue_handle, true);
                Assert.assertEquals(children.Count, 2);
                string child1        = children[0];
                string child2        = children[1];
                int    compareResult = child1.CompareTo(child2);
                Assert.assertNotEquals(compareResult, 0);
                if (compareResult < 0)
                {
                }
                else
                {
                    string temp = child1;
                    child1 = child2;
                    child2 = temp;
                }
                string child1data = Encoding.UTF8.GetString(zk.getData(queue_handle + "/" + child1, false, null));
                string child2data = Encoding.UTF8.GetString(zk.getData(queue_handle + "/" + child2, false, null));
                Assert.assertEquals(child1data, "0");
                Assert.assertEquals(child2data, "1");
            }
            finally {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
Example #5
0
			/// <summary>
			/// find if we have been created earler if not create our node
			/// </summary>
			/// <param name="prefix">    the prefix node </param>
			/// <param name="zookeeper"> teh zookeeper client </param>
			/// <param name="dir">       the dir paretn
			/// </param>
			/// <exception cref="KeeperException"> </exception>
			/// <exception cref="InterruptedException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void findPrefixInChildren(String prefix, org.apache.zookeeper.ZooKeeper zookeeper, String dir) throws org.apache.zookeeper.KeeperException, InterruptedException
			internal virtual void findPrefixInChildren(string prefix, ZooKeeper zookeeper, string dir)
			{
				IList<string> names = zookeeper.getChildren(dir, false);
				foreach (string name in names)
				{
					if (name.StartsWith(prefix, StringComparison.Ordinal))
					{
						outerInstance.id = name;
						if (LOG.DebugEnabled)
						{
							LOG.debug("Found id created last time: " + outerInstance.id);
						}
						break;
					}
				}
				if (string.ReferenceEquals(outerInstance.id, null))
				{
					outerInstance.id = zookeeper.create(dir + "/" + prefix, outerInstance.data, outerInstance.Acl, EPHEMERAL_SEQUENTIAL);

					if (LOG.DebugEnabled)
					{
						LOG.debug("Created id: " + outerInstance.id);
					}
				}

			}
Example #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void getConf(org.apache.zookeeper.ZooKeeper zk, String groupName, String displayName, java.util.List<String> retList) throws org.apache.zookeeper.KeeperException, InterruptedException
		private void getConf(ZooKeeper zk, string groupName, string displayName, IList<string> retList)
		{
			try
			{

				StringBuilder sb = new StringBuilder();

				int pathLength = StringUtils.countMatches(groupName, "/");
				for (int i = 0; i < pathLength - 2; ++i)
				{
					sb.Append("\t");
				}

				IList<string> children = zk.getChildren(groupName, false);

				if (!"/".Equals(groupName))
				{

					sb.Append("|----" + displayName);
					Stat stat = new Stat();
					sbyte[] data = zk.getData(groupName, null, stat);

					if (data != null && children.Count == 0)
					{
						sb.Append("\t" + StringHelperClass.NewString(data, CHARSET));
					}
				}
				else
				{
					sb.Append(groupName);
				}
				retList.Add(sb.ToString());

				//
				//
				//
				children.Sort(Collator.getInstance(java.util.Locale.CHINA));
				foreach (string child in children)
				{

					string nextName = "";

					if (!"/".Equals(groupName))
					{

						nextName = groupName + "/" + child;

					}
					else
					{
						nextName = groupName + "/" + child;
					}

					string node = StringUtils.substringAfterLast(nextName, "/");

					getConf(zk, groupName + "/" + child, node, retList);
				}

			}
			catch (KeeperException.NoNodeException)
			{
				LOG.error("Group " + groupName + " does not exist\n");
			}

		}
Example #7
0
		/// <summary>
		/// 获取指定 配置数据
		/// 
		/// @return
		/// </summary>
		/// <exception cref="InterruptedException"> </exception>
		/// <exception cref="KeeperException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private com.baidu.disconf.web.service.zookeeper.dto.ZkDisconfData getDisconfData(String path, String keyName, org.apache.zookeeper.ZooKeeper zooKeeper) throws org.apache.zookeeper.KeeperException, InterruptedException
		private ZkDisconfData getDisconfData(string path, string keyName, ZooKeeper zooKeeper)
		{

			string curPath = path + "/" + keyName;

			if (zooKeeper.exists(curPath, false) == null)
			{
				return null;
			}

			ZkDisconfData zkDisconfData = new ZkDisconfData();
			zkDisconfData.Key = keyName;

			IList<string> secChiList = zooKeeper.getChildren(curPath, false);
			IList<ZkDisconfData.ZkDisconfDataItem> zkDisconfDataItems = new List<ZkDisconfData.ZkDisconfDataItem>();

			// list
			foreach (string secKey in secChiList)
			{

				// machine
				ZkDisconfData.ZkDisconfDataItem zkDisconfDataItem = new ZkDisconfData.ZkDisconfDataItem();
				zkDisconfDataItem.Machine = secKey;

				string thirdPath = curPath + "/" + secKey;

				// value
				sbyte[] data = zooKeeper.getData(thirdPath, null, null);
				if (data != null)
				{
					zkDisconfDataItem.Value = StringHelperClass.NewString(data, CHARSET);
				}

				// add
				zkDisconfDataItems.Add(zkDisconfDataItem);
			}

			zkDisconfData.Data = zkDisconfDataItems;

			return zkDisconfData;
		}
Example #8
0
        private void verifyHammer(long start, HammerThread[] threads, int childCount)
        {
            // look for the clients to finish their create operations
            LOG.info("Starting check for completed hammers");
            int workingCount = threads.Length;

            for (int i = 0; i < 120; i++)
            {
                Thread.Sleep(10000);
                foreach (HammerThread h in threads)
                {
                    if (!h.isAlive() || h.current == h.count)
                    {
                        workingCount--;
                    }
                }
                if (workingCount == 0)
                {
                    break;
                }
                workingCount = threads.Length;
            }
            if (workingCount > 0)
            {
                foreach (HammerThread h in threads)
                {
                    LOG.warn(h.getName() + " never finished creation, current:" + h.current);
                }
            }
            else
            {
                LOG.info("Hammer threads completed creation operations");
            }

            foreach (HammerThread h in threads)
            {
                const int safetyFactor = 3;
                verifyThreadTerminated(h, threads.Length * childCount * HAMMERTHREAD_LATENCY * safetyFactor);
            }
            LOG.info(DateTime.Now + " Total time " + (TimeHelper.ElapsedMiliseconds - start));

            ZooKeeper zk = createClient();

            try
            {
                LOG.info("******************* Connected to ZooKeeper" + DateTime.Now);
                for (int i = 0; i < threads.Length; i++)
                {
                    LOG.info("Doing thread: " + i + " " + DateTime.Now);
                    IList <string> children = zk.getChildren("/test-" + i, false);
                    Assert.assertEquals(childCount, children.Count);
                    children = zk.getChildren("/test-" + i, false, null);
                    Assert.assertEquals(childCount, children.Count);
                }
                for (int i = 0; i < threads.Length; i++)
                {
                    IList <string> children = zk.getChildren("/test-" + i, false);
                    Assert.assertEquals(childCount, children.Count);
                    children = zk.getChildren("/test-" + i, false, null);
                    Assert.assertEquals(childCount, children.Count);
                }
            }
            finally
            {
                zk.close();
            }
        }
Example #9
0
        private void performClientTest(bool withWatcherObj)
        {
            ZooKeeper zk = null;

            try {
                MyWatcher watcher = new MyWatcher();
                zk = createClient(watcher);
                LOG.info("Before create /benwashere");
                zk.create("/benwashere", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                LOG.info("After create /benwashere");
                try {
                    zk.setData("/benwashere", "hi".getBytes(), 57);
                    Assert.fail("Should have gotten BadVersion exception");
                }
                catch (KeeperException.BadVersionException) {
                    // expected that
                }
                catch (KeeperException) {
                    Assert.fail("Should have gotten BadVersion exception");
                }
                LOG.info("Before delete /benwashere");
                zk.delete("/benwashere", 0);
                LOG.info("After delete /benwashere");
                zk.close();
                //LOG.info("Closed client: " + zk.describeCNXN());
                Thread.Sleep(2000);

                zk = createClient(watcher);
                //LOG.info("Created a new client: " + zk.describeCNXN());
                LOG.info("Before delete /");

                try {
                    zk.delete("/", -1);
                    Assert.fail("deleted root!");
                }
                catch (KeeperException.BadArgumentsException) {
                    // good, expected that
                }
                Stat stat = new Stat();
                // Test basic create, ls, and getData
                zk.create("/pat", "Pat was here".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                LOG.info("Before create /ben");
                zk.create("/pat/ben", "Ben was here".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                LOG.info("Before getChildren /pat");
                List <string> children = zk.getChildren("/pat", false);
                Assert.assertEquals(1, children.Count);
                Assert.assertEquals("ben", children[0]);
                IList <string> children2 = zk.getChildren("/pat", false, null);
                Assert.assertEquals(children, children2);
                string value = Encoding.UTF8.GetString(zk.getData("/pat/ben", false, stat));
                Assert.assertEquals("Ben was here", value);
                // Test stat and watch of non existent node

                try {
                    if (withWatcherObj)
                    {
                        Assert.assertEquals(null, zk.exists("/frog", watcher));
                    }
                    else
                    {
                        Assert.assertEquals(null, zk.exists("/frog", true));
                    }
                    LOG.info("Comment: asseting passed for frog setting /");
                }
                catch (KeeperException.NoNodeException) {
                    // OK, expected that
                }
                zk.create("/frog", "hi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                // the first poll is just a session delivery
                LOG.info("Comment: checking for events length " + watcher.events.size());
                WatchedEvent @event = watcher.events.poll(10 * 1000);
                Assert.assertEquals("/frog", @event.getPath());
                Assert.assertEquals(Watcher.Event.EventType.NodeCreated, @event.get_Type());
                Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());
                // Test child watch and create with sequence
                zk.getChildren("/pat/ben", true);
                for (int i = 0; i < 10; i++)
                {
                    zk.create("/pat/ben/" + i + "-", Convert.ToString(i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                              CreateMode.PERSISTENT_SEQUENTIAL);
                }
                children = zk.getChildren("/pat/ben", false);
                children.Sort();
                Assert.assertEquals(10, children.Count);
                for (int i = 0; i < 10; i++)
                {
                    string name = children[i];
                    Assert.assertTrue("starts with -", name.StartsWith(i + "-", StringComparison.Ordinal));
                    byte[] b;
                    if (withWatcherObj)
                    {
                        b = zk.getData("/pat/ben/" + name, watcher, stat);
                    }
                    else
                    {
                        b = zk.getData("/pat/ben/" + name, true, stat);
                    }
                    Assert.assertEquals(i, int.Parse(Encoding.UTF8.GetString(b)));
                    zk.setData("/pat/ben/" + name, "new".getBytes(), stat.getVersion());
                    if (withWatcherObj)
                    {
                        stat = zk.exists("/pat/ben/" + name, watcher);
                    }
                    else
                    {
                        stat = zk.exists("/pat/ben/" + name, true);
                    }
                    zk.delete("/pat/ben/" + name, stat.getVersion());
                }
                @event = watcher.events.poll(10 * 1000);
                Assert.assertEquals("/pat/ben", @event.getPath());
                Assert.assertEquals(Watcher.Event.EventType.NodeChildrenChanged, @event.get_Type());
                Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());
                for (int i = 0; i < 10; i++)
                {
                    @event = watcher.events.poll(10 * 1000);


                    string name = children[i];
                    Assert.assertEquals("/pat/ben/" + name, @event.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());
                    @event = watcher.events.poll(10 * 1000);
                    Assert.assertEquals("/pat/ben/" + name, @event.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, @event.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());
                }
                zk.create("/good\u0040path", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

                zk.create("/duplicate", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                try {
                    zk.create("/duplicate", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    Assert.fail("duplicate create allowed");
                }
                catch (KeeperException.NodeExistsException) {
                    // OK, expected that
                }
            }
            finally {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
Example #10
0
        public void testGetChildrenSync()
        {
            try
            {
                lsnr.getChildren("/foo", true);
                Assert.fail();
            }
            catch (KeeperException e)
            {
                Assert.assertEquals(KeeperException.Code.NONODE, e.getCode());
                Assert.assertEquals("/foo", e.getPath());
            }
            try
            {
                lsnr.getChildren("/foo/bar", true);
                Assert.fail();
            }
            catch (KeeperException e)
            {
                Assert.assertEquals(KeeperException.Code.NONODE, e.getCode());
                Assert.assertEquals("/foo/bar", e.getPath());
            }

            client.create("/foo", "parent".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            Assert.assertNotNull(lsnr.getChildren("/foo", true));

            client.create("/foo/bar", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            expected.Add(Watcher.Event.EventType.NodeChildrenChanged);             // /foo
            Assert.assertNotNull(lsnr.getChildren("/foo/bar", true));


            client.setData("/foo", "parent".getBytes(), -1);
            client.setData("/foo/bar", "child".getBytes(), -1);


            Assert.assertNotNull(lsnr.exists("/foo", true));

            Assert.assertNotNull(lsnr.getChildren("/foo", true));
            Assert.assertNotNull(lsnr.getChildren("/foo/bar", true));

            client.delete("/foo/bar", -1);
            expected.Add(Watcher.Event.EventType.NodeDeleted);             // /foo/bar childwatch
            expected.Add(Watcher.Event.EventType.NodeChildrenChanged);     // /foo
            client.delete("/foo", -1);
            expected.Add(Watcher.Event.EventType.NodeDeleted);

            verify();
        }
Example #11
0
        public void testChrootSynchronous()
        {
            ZooKeeper zk1 = createClient();

            try
            {
                zk1.create("/ch1", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            finally
            {
                if (zk1 != null)
                {
                    zk1.close();
                }
            }
            ZooKeeper zk2 = createClient("/ch1");

            try
            {
                Assert.assertEquals("/ch2", zk2.create("/ch2", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
            }
            finally
            {
                if (zk2 != null)
                {
                    zk2.close();
                }
            }

            zk1 = createClient();
            zk2 = createClient("/ch1");
            try
            {
                // check get
                MyWatcher w1 = new MyWatcher("/ch1");
                Assert.assertNotNull(zk1.exists("/ch1", w1));
                MyWatcher w2 = new MyWatcher("/ch1/ch2");
                Assert.assertNotNull(zk1.exists("/ch1/ch2", w2));

                MyWatcher w3 = new MyWatcher("/ch2");
                Assert.assertNotNull(zk2.exists("/ch2", w3));

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

                // check set
                zk1.setData("/ch1", "1".getBytes(), -1);
                zk2.setData("/ch2", "2".getBytes(), -1);

                // check watches
                Assert.assertTrue(w1.matches());
                Assert.assertTrue(w2.matches());
                Assert.assertTrue(w3.matches());

                // check exceptions
                try
                {
                    zk2.setData("/ch3", "3".getBytes(), -1);
                }
                catch (KeeperException.NoNodeException e)
                {
                    Assert.assertEquals("/ch3", e.getPath());
                }

                Assert.assertEquals("1".getBytes(), zk1.getData("/ch1", false, null));
                Assert.assertEquals("2".getBytes(), zk1.getData("/ch1/ch2", false, null));
                Assert.assertEquals("2".getBytes(), zk2.getData("/ch2", false, null));

                // check delete
                zk2.delete("/ch2", -1);
                Assert.assertTrue(w4.matches());
                Assert.assertTrue(w5.matches());

                zk1.delete("/ch1", -1);
                Assert.assertNull(zk1.exists("/ch1", false));
                Assert.assertNull(zk1.exists("/ch1/ch2", false));
                Assert.assertNull(zk2.exists("/ch2", false));
            }
            finally
            {
                if (zk1 != null)
                {
                    zk1.close();
                }
                if (zk2 != null)
                {
                    zk2.close();
                }
            }
        }