示例#1
0
        public void testACLs()
        {
            ZooKeeper zk = null;

            try {
                zk = createClient();
                try {
                    zk.create("/acltest", new byte[0], ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                    Assert.fail("Should have received an invalid acl error");
                }
                catch (KeeperException.InvalidACLException e) {
                    LOG.info("Test successful, invalid acl received : " + e.Message);
                }
                try {
                    List <ACL> testACL = new List <ACL>();
                    testACL.Add(new ACL((int)(ZooDefs.Perms.ALL | ZooDefs.Perms.ADMIN), ZooDefs.Ids.AUTH_IDS));
                    testACL.Add(new ACL((int)(ZooDefs.Perms.ALL | ZooDefs.Perms.ADMIN), new Id("ip", "127.0.0.1/8")));
                    zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT);
                    Assert.fail("Should have received an invalid acl error");
                }
                catch (KeeperException.InvalidACLException e) {
                    LOG.info("Test successful, invalid acl received : " + e.Message);
                }
                zk.addAuthInfo("digest", "ben:passwd".getBytes());
                zk.create("/acltest", new byte[0], ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                zk.close();
                zk = createClient();
                zk.addAuthInfo("digest", "ben:passwd2".getBytes());
                try {
                    zk.getData("/acltest", false, new Stat());
                    Assert.fail("Should have received a permission error");
                }
                catch (KeeperException e) {
                    Assert.assertEquals(KeeperException.Code.NOAUTH, e.getCode());
                }
                zk.addAuthInfo("digest", "ben:passwd".getBytes());
                zk.getData("/acltest", false, new Stat());
                zk.setACL("/acltest", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
                zk.close();
                zk = createClient();
                zk.getData("/acltest", false, new Stat());
                IList <ACL> acls = zk.getACL("/acltest", new Stat());
                Assert.assertEquals(1, acls.Count);
                Assert.assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, acls);
                zk.close();
            }
            finally {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
示例#2
0
        private void runHammer(int threadCount, int childCount)
        {
            try
            {
                HammerThread[] threads = new HammerThread[threadCount];
                long           start   = TimeHelper.ElapsedMiliseconds;
                for (int i = 0; i < threads.Length; i++)
                {
                    ZooKeeper zk     = createClient();
                    string    prefix = "/test-" + i;
                    zk.create(prefix, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    prefix += "/";
                    HammerThread thread = new BasicHammerThread("BasicHammerThread-" + i, zk, prefix, childCount);
                    thread.start();

                    threads[i] = thread;
                }

                verifyHammer(start, threads, childCount);
            }
            catch (Exception t)
            {
                LOG.error("test Assert.failed", t);
                throw;
            }
        }
示例#3
0
 protected override void run()
 {
     byte[] b = new byte[256];
     try
     {
         for (; current < count; current++)
         {
             // Simulate a bit of network latency...
             Thread.Sleep(HAMMERTHREAD_LATENCY);
             zk.create(prefix + current, b, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
         }
     }
     catch (Exception t)
     {
         LOG.error("Client create operation Assert.failed", t);
     }
     finally
     {
         try
         {
             zk.close();
         }
         catch (ThreadInterruptedException e)
         {
             LOG.warn("Unexpected", e);
         }
     }
 }
示例#4
0
 protected override void run()
 {
     byte[] b = new byte[256];
     try
     {
         for (; current < count; current++)
         {
             ZooKeeper zk = parent.createClient();
             try
             {
                 zk.create(prefix + current, b, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
             }
             finally
             {
                 try
                 {
                     zk.close();
                 }
                 catch (ThreadInterruptedException e)
                 {
                     LOG.warn("Unexpected", e);
                 }
             }
         }
     }
     catch (Exception t)
     {
         LOG.error("Client create operation Assert.failed", t);
     }
 }
示例#5
0
        public void testDeleteWithChildren()
        {
            ZooKeeper zk = createClient();

            zk.create("/parent", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.create("/parent/child", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            try {
                zk.delete("/parent", -1);
                Assert.fail("Should have received a not equals message");
            }
            catch (KeeperException e) {
                Assert.assertEquals(KeeperException.Code.NOTEMPTY, e.getCode());
            }
            zk.delete("/parent/child", -1);
            zk.delete("/parent", -1);
            zk.close();
        }
示例#6
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();
                }
            }
        }
示例#7
0
 private void verifyCreateFails(string path, ZooKeeper zk)
 {
     try {
         zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
     }
     catch (ArgumentException) {
         // this is good
         return;
     }
     Assert.fail("bad path \"" + path + "\" not caught");
 }
示例#8
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();
                }
            }
        }
示例#9
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));
        }
示例#10
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());
        }
示例#11
0
        public void testLargeNodeData()
        {
            ZooKeeper    zk           = null;
            const string queue_handle = "/large";

            try {
                zk = createClient();

                zk.create(queue_handle, new byte[500000], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            finally {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
示例#12
0
        public void testWatcherCorrectness()
        {
            ZooKeeper zk = null;

            try
            {
                MyWatcher watcher = new MyWatcher();
                zk = createClient(watcher);

                string[] names = new string[10];
                for (int i = 0; i < names.Length; i++)
                {
                    string name = zk.create("/tc-", "initialvalue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                    names[i] = name;

                    Stat stat = new Stat();
                    zk.getData(name, watcher, stat);
                    zk.setData(name, "new".getBytes(), stat.getVersion());
                    stat = zk.exists(name, watcher);
                    zk.delete(name, stat.getVersion());
                }

                for (int i = 0; i < names.Length; i++)
                {
                    string       name   = names[i];
                    WatchedEvent @event = watcher.events.poll(10 * 1000);
                    Assert.assertEquals(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(name, @event.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, @event.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());
                }
            }
            finally
            {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
示例#13
0
        public void testHammerSuper()
        {
            try
            {
                const int threadCount = 5;
                const int childCount  = 10;

                HammerThread[] threads = new HammerThread[threadCount];
                long           start   = TimeHelper.ElapsedMiliseconds;
                for (int i = 0; i < threads.Length; i++)
                {
                    string prefix = "/test-" + i;
                    {
                        ZooKeeper zk = createClient();
                        try
                        {
                            zk.create(prefix, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        }
                        finally
                        {
                            zk.close();
                        }
                    }
                    prefix += "/";
                    HammerThread thread = new SuperHammerThread("SuperHammerThread-" + i, this, prefix, childCount);
                    thread.start();

                    threads[i] = thread;
                }

                verifyHammer(start, threads, childCount);
            }
            catch (Exception t)
            {
                LOG.error("test Assert.failed", t);
                throw;
            }
        }
示例#14
0
        public void testPing()
        {
            ZooKeeper zkIdle         = null;
            ZooKeeper zkWatchCreator = null;

            try {
                CountdownWatcher watcher = new CountdownWatcher();
                zkIdle = createClient(watcher, 10000);

                zkWatchCreator = createClient();

                for (int i = 0; i < 10; i++)
                {
                    zkWatchCreator.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
                for (int i = 0; i < 10; i++)
                {
                    zkIdle.exists("/" + i, true);
                }
                for (int i = 0; i < 10; i++)
                {
                    Thread.Sleep(1000);
                    zkWatchCreator.delete("/" + i, -1);
                }
                // The bug will manifest itself here because zkIdle will expire
                zkIdle.exists("/0", false);
            }
            finally {
                if (zkIdle != null)
                {
                    zkIdle.close();
                }
                if (zkWatchCreator != null)
                {
                    zkWatchCreator.close();
                }
            }
        }
示例#15
0
        public void testNullData()
        {
            const string path = "/SIZE";
            ZooKeeper    zk   = null;

            zk = createClient();
            try {
                ManualResetEventSlim cn = new ManualResetEventSlim(false);
                zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                // try sync zk exists
                zk.exists(path, false);
                zk.existsAsync(path, false).ContinueWith(t => { cn.Set(); });
                cn.Wait(10 * 1000);
                Assert.assertTrue(cn.IsSet);
            }
            finally
            {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
示例#16
0
        public void testBasic()
        {
            const string name = "/foo";

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

            var stat = newStat();

            zk.getData(name, 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(0, stat.getEphemeralOwner());
            Assert.assertEquals(name.Length, stat.getDataLength());
            Assert.assertEquals(0, stat.getNumChildren());
        }
示例#17
0
        public void testRootAcl()
        {
            ZooKeeper zk = createClient();

            try
            {
                // set auth using digest
                zk.addAuthInfo("digest", "pat:test".getBytes());
                zk.setACL("/", ZooDefs.Ids.CREATOR_ALL_ACL, -1);
                zk.getData("/", false, null);
                zk.close();
                // verify no access
                zk = createClient();
                try
                {
                    zk.getData("/", false, null);
                    Assert.fail("validate auth");
                }
                catch (KeeperException.NoAuthException)
                {
                    // expected
                }
                try
                {
                    zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                    Assert.fail("validate auth");
                }
                catch (KeeperException.InvalidACLException)
                {
                    // expected
                }
                zk.addAuthInfo("digest", "world:anyone".getBytes());
                try
                {
                    zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                    Assert.fail("validate auth");
                }
                catch (KeeperException.NoAuthException)
                {
                    // expected
                }
                zk.close();
                // verify access using original auth
                zk = createClient();
                zk.addAuthInfo("digest", "pat:test".getBytes());
                zk.getData("/", false, null);
                zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                zk.delete("/apps", -1);
                // reset acl (back to open) and verify accessible again
                zk.setACL("/", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
                zk.close();
                zk = createClient();
                zk.getData("/", false, null);
                zk.create("/apps", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                try
                {
                    zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                    Assert.fail("validate auth");
                }
                catch (KeeperException.InvalidACLException)
                {
                    // expected
                }
                zk.delete("/apps", -1);
                zk.addAuthInfo("digest", "world:anyone".getBytes());
                zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
                zk.close();
                zk = createClient();
                zk.delete("/apps", -1);
            }
            finally
            {
                zk.close();
            }
        }
示例#18
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);
					}
				}

			}
示例#19
0
        public void testExistsSync()
        {
            Assert.assertNull(lsnr.exists("/foo", true));
            Assert.assertNull(lsnr.exists("/foo/bar", true));

            client.create("/foo", "parent".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            expected.Add(Watcher.Event.EventType.NodeCreated);
            client.create("/foo/bar", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            expected.Add(Watcher.Event.EventType.NodeCreated);

            verify();

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

            try
            {
                Assert.assertNull(lsnr.exists("/car", true));
                client.setData("/car", "missing".getBytes(), -1);
                Assert.fail();
            }
            catch (KeeperException e)
            {
                Assert.assertEquals(KeeperException.Code.NONODE, e.getCode());
                Assert.assertEquals("/car", e.getPath());
            }

            try
            {
                Assert.assertNull(lsnr.exists("/foo/car", true));
                client.setData("/foo/car", "missing".getBytes(), -1);
                Assert.fail();
            }
            catch (KeeperException e)
            {
                Assert.assertEquals(KeeperException.Code.NONODE, e.getCode());
                Assert.assertEquals("/foo/car", e.getPath());
            }

            client.setData("/foo", "parent".getBytes(), -1);
            expected.Add(Watcher.Event.EventType.NodeDataChanged);
            client.setData("/foo/bar", "child".getBytes(), -1);
            expected.Add(Watcher.Event.EventType.NodeDataChanged);

            verify();

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

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

            verify();
        }
示例#20
0
        public void testMutipleWatcherObjs()
        {
            ZooKeeper zk = createClient(new CountdownWatcher());

            try {
                MyWatcher[] watchers  = new MyWatcher[100];
                MyWatcher[] watchers2 = new MyWatcher[watchers.Length];
                for (int i = 0; i < watchers.Length; i++)
                {
                    watchers[i]  = new MyWatcher();
                    watchers2[i] = new MyWatcher();
                    zk.create("/foo-" + i, ("foodata" + i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                              CreateMode.PERSISTENT);
                }
                Stat stat = new Stat();

                //
                // test get/exists with single set of watchers
                //   get all, then exists all
                //
                for (int i = 0; i < watchers.Length; i++)
                {
                    Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat));
                }
                for (int i = 0; i < watchers.Length; i++)
                {
                    Assert.assertNotNull(zk.exists("/foo-" + i, watchers[i]));
                }
                // trigger the watches
                for (int i = 0; i < watchers.Length; i++)
                {
                    zk.setData("/foo-" + i, ("foodata2-" + i).getBytes(), -1);
                    zk.setData("/foo-" + i, ("foodata3-" + i).getBytes(), -1);
                }
                for (int i = 0; i < watchers.Length; i++)
                {
                    WatchedEvent @event = watchers[i].events.poll(10 * 1000);
                    Assert.assertEquals("/foo-" + i, @event.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());

                    // small chance that an unexpected message was delivered
                    //  after this check, but we would catch that next time
                    //  we check events
                    Assert.assertEquals(0, watchers[i].events.size());
                }

                //
                // test get/exists with single set of watchers
                //  get/exists together
                //
                for (int i = 0; i < watchers.Length; i++)
                {
                    Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat));
                    Assert.assertNotNull(zk.exists("/foo-" + i, watchers[i]));
                }
                // trigger the watches
                for (int i = 0; i < watchers.Length; i++)
                {
                    zk.setData("/foo-" + i, ("foodata4-" + i).getBytes(), -1);
                    zk.setData("/foo-" + i, ("foodata5-" + i).getBytes(), -1);
                }
                for (int i = 0; i < watchers.Length; i++)
                {
                    WatchedEvent @event = watchers[i].events.poll(10 * 1000);
                    Assert.assertEquals("/foo-" + i, @event.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());

                    // small chance that an unexpected message was delivered
                    //  after this check, but we would catch that next time
                    //  we check events
                    Assert.assertEquals(0, watchers[i].events.size());
                }

                //
                // test get/exists with two sets of watchers
                //
                for (int i = 0; i < watchers.Length; i++)
                {
                    Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat));
                    Assert.assertNotNull(zk.exists("/foo-" + i, watchers2[i]));
                }
                // trigger the watches
                for (int i = 0; i < watchers.Length; i++)
                {
                    zk.setData("/foo-" + i, ("foodata6-" + i).getBytes(), -1);
                    zk.setData("/foo-" + i, ("foodata7-" + i).getBytes(), -1);
                }
                for (int i = 0; i < watchers.Length; i++)
                {
                    WatchedEvent @event = watchers[i].events.poll(10 * 1000);
                    Assert.assertEquals("/foo-" + i, @event.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState());

                    // small chance that an unexpected message was delivered
                    //  after this check, but we would catch that next time
                    //  we check events
                    Assert.assertEquals(0, watchers[i].events.size());

                    // watchers2
                    WatchedEvent event2 = watchers2[i].events.poll(10 * 1000);
                    Assert.assertEquals("/foo-" + i, event2.getPath());
                    Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, event2.get_Type());
                    Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, event2.getState());

                    // small chance that an unexpected message was delivered
                    //  after this check, but we would catch that next time
                    //  we check events
                    Assert.assertEquals(0, watchers2[i].events.size());
                }
            }
            finally {
                if (zk != null)
                {
                    zk.close();
                }
            }
        }
示例#21
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();
                }
            }
        }
示例#22
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();
                }
            }
        }
示例#23
0
        public void testPathValidation()
        {
            ZooKeeper zk = createClient();

            verifyCreateFails(null, zk);
            verifyCreateFails("", zk);
            verifyCreateFails("//", zk);
            verifyCreateFails("///", zk);
            verifyCreateFails("////", zk);
            verifyCreateFails("/.", zk);
            verifyCreateFails("/..", zk);
            verifyCreateFails("/./", zk);
            verifyCreateFails("/../", zk);
            verifyCreateFails("/foo/./", zk);
            verifyCreateFails("/foo/../", zk);
            verifyCreateFails("/foo/.", zk);
            verifyCreateFails("/foo/..", zk);
            verifyCreateFails("/./.", zk);
            verifyCreateFails("/../..", zk);
            verifyCreateFails("/\u0001foo", zk);
            verifyCreateFails("/foo/bar/", zk);
            verifyCreateFails("/foo//bar", zk);
            verifyCreateFails("/foo/bar//", zk);

            verifyCreateFails("foo", zk);
            verifyCreateFails("a", zk);

            zk.create("/createseqpar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            // next two steps - related to sequential processing
            // 1) verify that empty child name Assert.fails if not sequential
            try {
                zk.create("/createseqpar/", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                Assert.assertTrue(false);
            }
            catch (ArgumentException) {
                // catch this.
            }

            // 2) verify that empty child name success if sequential
            zk.create("/createseqpar/", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            zk.create("/createseqpar/.", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            zk.create("/createseqpar/..", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            try {
                zk.create("/createseqpar//", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                Assert.assertTrue(false);
            }
            catch (ArgumentException) {
                // catch this.
            }
            try {
                zk.create("/createseqpar/./", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                Assert.assertTrue(false);
            }
            catch (ArgumentException) {
                // catch this.
            }
            try {
                zk.create("/createseqpar/../", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                Assert.assertTrue(false);
            }
            catch (ArgumentException) {
                // catch this.
            }
        }