Beispiel #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);
            }


        }
Beispiel #2
0
        public ZkAdaptor()
        {
            watcher = new ZkWatcher()
            {
                DataChangedHandler = OnDataChanged,
            };

            // zk handle
            // zkServer addr, session timeout, watcher
            handle = new ZooKeeper("192.168.0.103:2181,192.168.0.103:2182,192.168.0.103:2183,192.168.0.103:2184,192.168.0.103:2185", new TimeSpan(0, 0, 0, 50000), watcher);

            try
            {
                // create root node
                // no ACL
                // Persistent node
                handle.Create(RoutingRule.ZkRoutingRule.GetServiceRoot(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
            }
            catch (KeeperException.NodeExistsException e)
            {
                // ignore
            }

            watcher.Handle = handle;
        }
        /**
         * Barrier constructor
         *
         * @param address
         * @param root
         * @param size
         */
        public Barrier(ZooKeeper zk, String root, int size, string nodeName)
        {
            _root = root;
            _size = size;

            _nodeName = nodeName;

            _zk = zk;

            // Create barrier node
            if (_zk == null) return;

            try
            {
                var s = _zk.Exists(root, false);
                if (s == null)
                    _zk.Create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
            }
            catch (KeeperException e)
            {
                Console.WriteLine("Keeper exception when instantiating queue: " + e);
            }
            catch (Exception e)
            {
                //TODO
            }
        }
 public ProtocolSupport(ZooKeeper zookeeper)
 {
     RetryDelay = new TimeSpan(0, 0, 0, 0, 500);
     Acl = Ids.OPEN_ACL_UNSAFE;
     RetryCount = 10;
     Zookeeper = zookeeper;
 }
Beispiel #5
0
 public WriteLock(ZooKeeper zookeeper, string dir, List<ACL> acl)
     : base(zookeeper)
 {
     this.dir = dir;
     if (acl != null) Acl = acl;
     lockOperation = LockOperation;
 }
 public LeaderElection(ZooKeeper zookeeper, string path, ILeaderWatcher watcher, byte[] data)
     : base(zookeeper)
 {
     this._path = path;
     this._watcher = watcher;
     this._data = data;
 }
Beispiel #7
0
        public void Connect(IWatcher watcher) 
        {
            Monitor.Enter(this._zookeeperLock);
            try 
            {
                if (this._zk != null) 
                {
                    throw new Exception("zk client has already been started");
                }

                try 
                {
                    Logger.Debug("Creating new ZookKeeper instance to connect to " + this._servers + ".");
                    this._zk = new ZooKeeper(this._servers, this._sessionTimeOut, watcher);
                } 
                catch (Exception e) 
                {
                    throw new ZkException("Unable to connect to " + this._servers, e);
                }
            } 
            finally 
            {
                Monitor.Exit(this._zookeeperLock);
            }
        }
 public ClientConnectionRequestProducer(ClientConnection conn)
 {
     this.conn = conn;
     zooKeeper = conn.zooKeeper;
     zkEndpoints = new ZooKeeperEndpoints(conn.serverAddrs);
     requestThread = new Thread(new SafeThreadStart(SendRequests).Run) { Name = new StringBuilder("ZK-SendThread ").Append(conn.zooKeeper.Id).ToString(), IsBackground = true };
 }
        public void Subscribe()
        {
            var w = new watcher();
            var zk = new ZooKeeper("", TimeSpan.FromSeconds(1), w);
            w.WaitUntilConnected(1);

            zk.GetData("", w, new Stat());
        }
Beispiel #10
0
 protected BaseZkTest(string zkConnectionString,
                         int sessionTimeout,
                         Watcher watcher,
                         bool readOnly)
 {
     ZkConnectionString = zkConnectionString;
     Zookeeper = new ZooKeeper(zkConnectionString, sessionTimeout, watcher, readOnly);
 }
Beispiel #11
0
 public void Dispose()
 {
     if (_zk != null)
     {
         _zk.Dispose();
         _zk = null;
     }
 }
Beispiel #12
0
		/// <summary>
		/// zookeeper contructor for writelock
		/// </summary>
		/// <param name="zookeeper"> zookeeper client instance </param>
		/// <param name="dir">       the parent path you want to use for locking </param>
		/// <param name="acls">      the acls that you want to use for all the paths,
		///                  if null world read/write is used. </param>
		public WriteLock(ZooKeeper zookeeper, string dir, IList<ACL> acl) : base(zookeeper)
		{
			this.dir = dir;
			if (acl != null)
			{
				Acl = acl;
			}
			this.zop = new LockZooKeeperOperation(this);
		}
 public DataMonitor(ZooKeeper zk, String zPodePath, Watcher chainedWatcher,
     IDataMonitorListener listener)
 {
     _zk = zk;
     _znode = zPodePath;
     _chainedWatcher = chainedWatcher;
     _listener = listener;
     _zk.Exists(_znode, this);
 }
Beispiel #14
0
        static void Main(string[] args)
        {
            DemoWatcher watcher = new DemoWatcher();
            ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(16000), watcher);
            //zk.Create("/master", BitConverter.GetBytes(230), ACL, CreateMode.Ephemeral);
            checkMaster(zk);

            Console.ReadLine();
        }
Beispiel #15
0
		/// <param name="hosts">
		/// </param>
		/// <returns> void
		/// </returns>
		/// <exception cref="IOException"> </exception>
		/// <exception cref="InterruptedException">
		/// @Description: 连接ZK
		/// @author liaoqiqi
		/// @date 2013-6-14 </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void connect(String hosts) throws java.io.IOException, InterruptedException
		public virtual void connect(string hosts)
		{
			internalHost = hosts;
			zk = new ZooKeeper(internalHost, SESSION_TIMEOUT, this);

			// 连接有超时哦
			connectedSignal.await(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);

			LOGGER.info("zookeeper: " + hosts + " , connected.");
		}
        public static void Connect(ref ZooKeeper zk, string address, string path)
        {
            if (zk == null)
                zk = new ZooKeeper(address, new TimeSpan(0, 0, 0, 30), new Watcher());

            var stats = zk.Exists(path, false);

            if (stats == null)
                zk.Create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

            var list = zk.GetChildren(path, true);
        }
Beispiel #17
0
        /**
         * Kill the given ZK session
         *
         * @param client the client to kill
         * @param connectString server connection string
         * @param maxMs max time ms to wait for kill
         * @throws Exception errors
         */
        public static void kill(IZooKeeper client, String connectString, int maxMs) 
        {
			System.Diagnostics.Debug.WriteLine ("Kill Start");
            long startTicks = (long)TimeSpan.FromTicks(System.DateTime.Now.Ticks).TotalMilliseconds;

			var sessionLostLatch = new AutoResetEvent(false);
			IWatcher sessionLostWatch = new CuratorWatcher((e)=> { if(e.State == KeeperState.Expired) sessionLostLatch.Set();});
            client.Exists("/___CURATOR_KILL_SESSION___" + System.DateTime.Now.Ticks, sessionLostWatch);

			var connectionLatch = new AutoResetEvent(false);
			var connectionWatcher = new CuratorWatcher((e)=> {
				if(e.State == KeeperState.SyncConnected){
					connectionLatch.Set();
				}
			});

             
            IZooKeeper zk = new ZooKeeper(connectString, TimeSpan.FromMilliseconds(maxMs), connectionWatcher, client.SessionId, client.SesionPassword);
            try
            {
                if ( !connectionLatch.WaitOne(maxMs) )
                {
                    throw new Exception("KillSession could not establish duplicate session");
                }
                try
                {
                        zk.Dispose();
                }
                finally
                {
                    zk = null;
                }

				while ( client.State.IsAlive() && !sessionLostLatch.WaitOne(100) )
                {
                    long elapsed = (long)TimeSpan.FromTicks(System.DateTime.Now.Ticks).TotalMilliseconds - startTicks;
                    if ( elapsed > maxMs )
                    {
                        throw new Exception("KillSession timed out waiting for session to expire");
                    }
                }
            }
            finally
            {
                if ( zk != null )
                {
                    zk.Dispose();
                }
            }
			System.Diagnostics.Debug.WriteLine ("Kill End");
    }
        public CollectionClusterState GetClusterState(string zkHost, string collectionName)
        {
            var w = new watcher();
            var zk = new ZooKeeper(zkHost, TimeSpan.FromSeconds(1), w);
            w.WaitUntilConnected(1);

            //var nodes = zk.GetChildren("/live_nodes", false).ToList();

            var clusterState = zk.GetData("/clusterstate.json", false, null);
            var json = Encoding.Default.GetString(clusterState);

            ClusterState state = ClusterState.FromJson(json);

            return new CollectionClusterState(state.Collections[collectionName]);
        }
Beispiel #19
0
        /**
         * Kill the given ZK session
         *
         * @param client the client to kill
         * @param connectString server connection string
         * @param maxMs max time ms to wait for kill
         * @throws Exception errors
         */
        public static void kill(ZooKeeper client, String connectString, int maxMs)
        {
            //            long startTicks = DateTime.Now.Ticks / 1000;

            //            Barrier sessionLostLatch = new Barrier(2);
            //            Watcher sessionLostWatch = new BarrierWatcher(sessionLostLatch);
            //            client.existsAsync("/___CURATOR_KILL_SESSION___" + DateTime.Now.Ticks,
            //                                     sessionLostWatch)
            //                  .Wait();

            Barrier connectionLatch = new Barrier(2);
            Watcher connectionWatcher = new SyncWatcher(connectionLatch);
            ZooKeeper zk = new ZooKeeper(connectString,
                                            maxMs,
                                            connectionWatcher,
                                            client.getSessionId(),
                                            client.getSessionPasswd());
            try
            {
                if ( !connectionLatch.SignalAndWait(maxMs) )
                {
                    throw new Exception("KillSession could not establish duplicate session");
                }
                try
                {
                    zk.closeAsync().Wait();
                }
                finally
                {
                    zk = null;
                }

            //                while ( client.getState() == ZooKeeper.States.CONNECTED
            //                            &&  !sessionLostLatch.SignalAndWait(100) )
            //                {
            //                    long elapsed = (DateTime.Now.Ticks / 1000) - startTicks;
            //                    if ( elapsed > maxMs )
            //                    {
            //                        throw new Exception("KillSession timed out waiting for session to expire");
            //                    }
            //                }
            }
            finally
            {
                zk?.closeAsync().Wait();
            }
        }
Beispiel #20
0
 public void Dispose() 
 {
     Monitor.Enter(this._zookeeperLock);
     try 
     {
         if (this._zk != null) 
         {
             Logger.Debug("Closing ZooKeeper connected to " + this._servers);
             this._zk.Dispose();
             this._zk = null;
         }
     } 
     finally 
     {
         Monitor.Exit(this._zookeeperLock);
     }
 }
Beispiel #21
0
		public void SessionTermination(){

			var latch = new AutoResetEvent (false);
			var sxl = new AutoResetEvent (false);
			var conlatch = new AutoResetEvent (false);
			var w1 = new CuratorWatcher ((e) => {
				if(e.State == KeeperState.SyncConnected){
					latch.Set();
				}
				else if(e.State == KeeperState.Expired){
					sxl.Set();
				}
			});
			var zookeeper = new ZooKeeper(connectionString,TimeSpan.FromMilliseconds(10000),w1);
			latch.WaitOne (5000);

			using (var zk = new ZooKeeper (connectionString, TimeSpan.FromMilliseconds (2000), new CuratorWatcher ((e) => {
				if (e.State == KeeperState.SyncConnected)
					conlatch.Set ();
			}), zookeeper.SessionId, zookeeper.SesionPassword)) {

				if (!conlatch.WaitOne (5000)) {
					Assert.Fail ();
				} 
			};

			if (!sxl.WaitOne (20000)) {
				Assert.Fail ();
			} 

			try{
				var stat = zookeeper.Exists ("/test", false);
				if (stat == null) {
					System.Diagnostics.Debug.WriteLine ("Node does not exits");

				}
			}
			catch(KeeperException e){

				System.Diagnostics.Debug.WriteLine ("Session Expired");

			}

		}
Beispiel #22
0
 internal Packet(RequestHeader header, ReplyHeader replyHeader, IRecord request, IRecord response, byte[] data, ZooKeeper.WatchRegistration watchRegistration, string serverPath, string clientPath)
 {
     this.header = header;
     this.replyHeader = replyHeader;
     this.request = request;
     this.response = response;
     this.serverPath = serverPath;
     this.clientPath = clientPath;
     if (data != null)
     {
         this.data = data;
     } 
     else
     {
         try
         {
             MemoryStream ms = new MemoryStream();
             using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, ms, Encoding.UTF8))
             {
                 BinaryOutputArchive boa = BinaryOutputArchive.getArchive(writer);
                 boa.WriteInt(-1, "len"); // We'll fill this in later
                 if(header != null)
                 {
                     header.Serialize(boa, "header");
                 }
                 if (request != null)
                 {
                     request.Serialize(boa, "request");
                 }                        
                 ms.Position = 0;
                 int len = Convert.ToInt32(ms.Length); // now we have the real length
                 writer.Write(len - 4); // update the length info
                 this.data = ms.ToArray();
             }
         }
         catch (IOException e)
         {
             LOG.Warn("Ignoring unexpected exception", e);
         }
     }
     this.watchRegistration = watchRegistration;
 }
Beispiel #23
0
        /// <summary>
        /// 定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Enabled = false;

            if (Monitor.TryEnter(timer))//每次只能一个线程进去
            {
                if (!isClose)
                {
                    //Thread.Sleep(SessionTimeout);
                    if (!Connected)
                    {
                        try
                        {
                            zookeeper?.closeAsync();
                            are.Reset();
                            zookeeper = new ZooKeeper(string.Join(",", address), SessionTimeout, defaultWatcher);
                            if (are.WaitOne(SessionTimeout) && Connected)//会话未超时,表示成功连接
                            {
                                //挂载监听器
                                foreach (var key in nodeWatchers.Keys)
                                {
                                    NodeWatcher watcher;
                                    if (nodeWatchers.TryGetValue(key, out watcher))
                                    {
                                        WatchAsync(key, watcher, true).Wait();
                                    }
                                }
                                OnConnected?.Invoke();
                                Monitor.Exit(timer);
                                return;
                            }
                        }
                        catch { }
                        timer.Enabled = true;
                    }
                }

                Monitor.Exit(timer);
            }
        }
Beispiel #24
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();
                }
            }
        }
Beispiel #25
0
 private async Task CreateZooKeeper()
 {
     if (_zooKeeper != null)
     {
         await _zooKeeper.closeAsync();
     }
     _zooKeeper = new ZooKeeper(_configInfo.ConnectionString, (int)_configInfo.SessionTimeout.TotalMilliseconds
                                , new ReconnectionWatcher(
                                    () =>
     {
         _connectionWait.Set();
     },
                                    () =>
     {
         _connectionWait.Close();
     },
                                    async() =>
     {
         _connectionWait.Reset();
         await CreateZooKeeper();
     }));
 }
Beispiel #26
0
        private static bool checkMaster(ZooKeeper zk)
        {
            while (true)
            {
                try
                {
                    Stat stat = new Stat();
                    byte[] data = zk.GetData("/master", false, stat);
                    var isLeader = System.Text.Encoding.UTF8.GetString(data).Equals("230");
                    return true;
                }
                catch (KeeperException.NoNodeException)
                {

                    return false;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
Beispiel #27
0
        public void testChRootCreateDelete()
        {
            // creating the subtree for chRoot clients.
            string chRoot = createNameSpace();

            // Creating child using chRoot client.
            zk_chroot = createClient(chRoot);
            Op createChild = Op.create("/myid", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            zk_chroot.multi(Arrays.asList(createChild));

            Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk.exists(chRoot + "/myid", false));
            Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk_chroot.exists("/myid", false));
            Assert.assertNull("zNode is created directly under '/', ignored configured chroot", zk.exists("/myid", false));

            // Deleting child using chRoot client.
            Op deleteChild = Op.delete("/myid", 0);

            zk_chroot.multi(Arrays.asList(deleteChild));
            Assert.assertNull("zNode exists under chroot:" + chRoot, zk.exists(chRoot + "/myid", false));
            Assert.assertNull("zNode exists under chroot:" + chRoot, zk_chroot.exists("/myid", false));
        }
Beispiel #28
0
 public bool ConnectZookeeper(string connectionString, TimeSpan timeout)
 {
     try
     {
         if (_zk == null)
         {
             _zk = new ZooKeeper(connectionString, timeout, new ZookeeperWatcher(this));
             ZooKeeper.WaitUntilConnected(_zk);
         }
         return(true);
     }
     catch (Exception ex)
     {
         this.AddLog(LogType.Fatal, ex.Message);
         if (_zk != null)
         {
             _zk.Dispose();
         }
         _zk = null;
     }
     return(false);
 }
 internal virtual bool Verify(ZooKeeper zkc, string path)
 {
     try
     {
         Org.Apache.Hadoop.Contrib.Bkjournal.EditLogLedgerMetadata other = Read(zkc, path);
         if (Log.IsTraceEnabled())
         {
             Log.Trace("Verifying " + this.ToString() + " against " + other);
         }
         return(other.Equals(this));
     }
     catch (KeeperException e)
     {
         Log.Error("Couldn't verify data in " + path, e);
         return(false);
     }
     catch (IOException ie)
     {
         Log.Error("Couldn't verify data in " + path, ie);
         return(false);
     }
 }
Beispiel #30
0
        public static async Task <bool> EnsureZooKeeperAsync()
        {
            var connectionString = TestDefaultConfiguration.ZooKeeperConnectionString;

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                return(false);
            }

            return(await ZooKeeper.Using(connectionString, 2000, new ZooKeeperWatcher(), async zk =>
            {
                try
                {
                    await zk.existsAsync("/test", false);
                    return true;
                }
                catch (KeeperException.ConnectionLossException)
                {
                    return false;
                }
            }));
        }
Beispiel #31
0
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure(new FileInfo("zookeeperDemo.log4net"));

            zookeeper = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 10), new ZooKeeper_Watch_Usage());
            manualWaitHandler.WaitOne();

            //ASYNC_VerifyZookeeper();

            string path     = "/zk-book";
            var    testStat = zookeeper.Exists(path, false);

            if (testStat == null)
            {
                zookeeper.Create(path, "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
            }


            var pathData = zookeeper.GetData(path, true, null);

            Console.WriteLine("The path's data(watched):{0}", System.Text.UTF8Encoding.UTF8.GetString(pathData));

            Console.WriteLine("begin to changed the data of path:{0}, which is watched.", path);
            zookeeper.SetData(path, "f**k you".GetBytes(), -1);
            Console.WriteLine("change data of path:{0} is finished, which is watched.", path);

            var newPath = "/ACID";

            zookeeper.Create(newPath, "AAAA".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
            var acidData = zookeeper.GetData(newPath, false, null);

            Console.WriteLine("The new created node's data(Unwatched):{0}", System.Text.UTF8Encoding.UTF8.GetString(acidData));

            Console.WriteLine("begin to changed the data of path:{0}, which is UNwatched.", newPath);
            zookeeper.SetData(newPath, "laoHUYou".GetBytes(), -1);
            Console.WriteLine("change data of path:{0} is finished, which is Unwatched.", newPath);

            Console.ReadLine();
        }
Beispiel #32
0
        /// <summary>
        /// 建立与ZooKeeper服务器的链接
        /// </summary>
        /// <exception cref="TimeoutException"></exception>
        public async Task OpenAsync()
        {
            var tcs     = new TaskCompletionSource <Void>();
            var watcher = new DefaultWatcher(this, tcs);

            _connection = new ZooKeeper(_connectionString, _sessionTimeout, watcher);
            var task = await Task.WhenAny(tcs.Task, Task.Delay(_connectionTimeout)).ConfigureAwait(false);

            if (task != tcs.Task)
            {
                try
                {
                    tcs.SetException(new TimeoutException($"Connecting to '{_connectionString}' timeout"));
                    await tcs.Task;
                }
                catch (InvalidOperationException)
                {
                    //ignore
                    //连接即将timeout的时候被抢救
                }
            }
        }
Beispiel #33
0
        public async Task testRemove1()
        {
            const string dir         = "/testRemove1";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = await createClient();

                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            try {
                await queueHandles[0].remove();
            }
            catch (InvalidOperationException) {
                return;
            }
            Assert.assertTrue(false);
        }
        void GetNodeListLoop(ZooKeeper zk, TreeZNode root, List <TreeZNode> nodeList)
        {
            int i = 0;

            try
            {
                foreach (string child in zk.GetChildren(root.NodePath, true))
                {
                    ++i;
                    int       id        = int.Parse(string.Format("{0}{1}", root.id, i));
                    TreeZNode childnode = new TreeZNode(id, true, string.Concat(root.joinNodePath, "/", child), child, root.id);
                    root.ChildNodeList.Add(childnode);
                    nodeList.Add(childnode);
                    GetNodeListLoop(zk, childnode, nodeList);
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("JinRi.Fx.Manage:{0}GetNodeListLoop()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString());
                log.Error(message);
            }
        }
Beispiel #35
0
        public async Task testSequentialNodeNames()
        {
            string path     = "/SEQUENCE";
            string file     = "TEST";
            string filepath = path + "/" + file;

            ZooKeeper zk = await createClient();

            await zk.createAsync(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            await zk.createAsync(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

            IList <string> children = (await zk.getChildrenAsync(path, false)).Children;

            Assert.assertEquals(1, children.Count);
            Assert.assertEquals(file + "0000000000", children[0]);

            await zk.createAsync(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

            children = (await zk.getChildrenAsync(path, false)).Children;
            Assert.assertEquals(2, children.Count);
            Assert.assertTrue("contains child 1", children.Contains(file + "0000000001"));

            await zk.createAsync(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

            children = (await zk.getChildrenAsync(path, false)).Children;
            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++)
            {
                await zk.createAsync(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            }

            children = (await zk.getChildrenAsync(path, false)).Children;
            Assert.assertTrue("contains child 104", children.Contains(file + "0000000104"));
        }
        public ZookeeperReadWriteLock(ZooKeeper zkClient, string path, int lockTimeout)
        {
            _writeMutex = new ZookeeperMutexLock
                          (
                zkClient,
                path,
                WRITE_LOCK_NAME,
                1,
                new WriteLockInternalsDriver(),
                lockTimeout
                          );

            _readMutex = new ZookeeperMutexLock
                         (
                zkClient,
                path,
                READ_LOCK_NAME,
                int.MaxValue,
                new ReadLockInternalsDriver(_writeMutex),
                lockTimeout
                         );
        }
Beispiel #37
0
        public async Task testSequentialNodeData()
        {
            const string queue_handle = "/queue";
            ZooKeeper    zk           = await createClient();

            await zk.createAsync(queue_handle, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                 CreateMode.PERSISTENT);

            await zk.createAsync(queue_handle + "/element", "0".UTF8getBytes(),
                                 ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

            await zk.createAsync(queue_handle + "/element", "1".UTF8getBytes(),
                                 ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

            IList <string> children = (await zk.getChildrenAsync(queue_handle, true)).Children;

            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 = (await zk.getDataAsync(queue_handle
                                                       + "/" + child1, false)).Data.UTF8bytesToString();
            string child2data = (await zk.getDataAsync(queue_handle
                                                       + "/" + child2, false)).Data.UTF8bytesToString();

            Assert.assertEquals(child1data, "0");
            Assert.assertEquals(child2data, "1");
        }
Beispiel #38
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 != "/")
         {
             zk.Delete(path, -1);
         }
     }
 }
Beispiel #39
0
        public async Task testHammerSuper()
        {
            const int threadCount = 5;
            const int childCount  = 10;

            Task[] tasks = new Task[threadCount];
            for (int i = 0; i < tasks.Length; i++)
            {
                string    prefix = "/test-" + i;
                ZooKeeper zk     = await createClient();

                await zk.createAsync(prefix, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

                await zk.closeAsync();

                LOG.warn("created:" + prefix);
                prefix  += "/";
                tasks[i] = GetSuperHammerTask(prefix, childCount);
            }

            await verifyHammer(tasks, childCount);
        }
Beispiel #40
0
        public DialogResult ShowDialog(ZooKeeper zooKeeper)
        {
            Label label = new Label();

            label.Size      = new Size(250, 50);
            label.Dock      = DockStyle.Top;
            label.TextAlign = ContentAlignment.MiddleCenter;
            label.Text      = "If you deleted " + zooKeeper.FullName + "\nalso will be deleted:";

            ListBox listBox = new ListBox();

            foreach (var animal in zooKeeper.Animals)
            {
                listBox.Items.Add(animal.Name);
            }
            listBox.Size     = new Size(150, 125);
            listBox.Location = new Point(50 - 5, 50);

            this.Controls.Add(listBox);
            this.Controls.Add(label);
            return(ShowDialog());
        }
Beispiel #41
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();
                }
            }
        }
Beispiel #42
0
        public ZookeeperClient(string Connstring, int SessionTimeout = -1)
        {
            var watcher = new SingleWatcher();

            Watcher            = watcher;
            Watcher.RunProcess = new Action <WatchedEvent>((e) =>
            {
                if (e.getState() == org.apache.zookeeper.Watcher.Event.KeeperState.Disconnected)
                {
                    lock (_lockerList)
                    {
                        foreach (var item in _lockerList)
                        {
                            item.ZooEvent = WatcherResult.Disconnected;
                            item.Event.Set();
                        }
                        _lockerList.Clear();
                    }
                }
                else if (e.get_Type() == org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted)
                {
                    var path = e.getPath();
                    lock (_lockerList)
                    {
                        var node = _lockerList.FirstOrDefault(w => $"/locks/{w.Name}" == path);
                        if (!string.IsNullOrWhiteSpace(node.Name))
                        {
                            node.ZooEvent = WatcherResult.UnLocked;
                            node.Event?.Set();
                            _lockerList.Remove(node);
                        }
                    }
                }
            });
            //初始化客户端
            _client = new ZooKeeper(Connstring, SessionTimeout, watcher, false);
            //初始化root节点
        }
Beispiel #43
0
        public async Task testTakeWait1()
        {
            const string dir         = "/testTakeWait1";
            const string testString  = "Hello World";
            const int    num_clients = 1;

            ZooKeeper[]        clients      = new ZooKeeper[num_clients];
            DistributedQueue[] queueHandles = new DistributedQueue[num_clients];
            for (int i = 0; i < clients.Length; i++)
            {
                clients[i] = await createClient();

                queueHandles[i] = new DistributedQueue(clients[i], dir, null);
            }

            byte[][] takeResult = new byte[1][];
            Task     takeTask   = Task.Run(async() => { try {
                                                            takeResult[0] = await queueHandles[0].take();
                                                        }
                                                        catch (KeeperException) {
                                                        } });
            await Task.Delay(1000);

            Task offerTask = Task.Run(async() => {
                try {
                    await queueHandles[0].offer(testString.UTF8getBytes());
                }
                catch (KeeperException) {
                }
            });
            await offerTask;

            await takeTask;

            Assert.assertTrue(takeResult[0] != null);
            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.assertEquals(takeResult[0].UTF8bytesToString(), testString);
        }
        public async Task <IReadOnlyList <ServiceDiscoveryInfo> > QueryServiceAsync(string serviceName, CancellationToken cancellationToken = default)
        {
            try
            {
                if (await ZooKeeper.existsAsync($"{Root}/{serviceName}", true) == null)
                {
                    return(new List <ServiceDiscoveryInfo>());
                }
                var nodes = await ZooKeeper.getChildrenAsync($"{Root}/{serviceName}", true);

                if (nodes == null)
                {
                    return(new List <ServiceDiscoveryInfo>());
                }
                var result = new List <ServiceDiscoveryInfo>();
                foreach (var node in nodes.Children)
                {
                    var data = await ZooKeeper.getDataAsync($"{Root}/{serviceName}/{node}");

                    if (data?.Data == null)
                    {
                        throw new ArgumentNullException("data", "The node data is null.");
                    }
                    var serviceData = Codec.Deserialize <ZooKeeperNodeInfo>(data.Data);
                    if (serviceData == null)
                    {
                        continue;
                    }
                    result.Add(new ServiceDiscoveryInfo(node, serviceData.Address, serviceData.Port, serviceData.Weight, serviceData.EnableTls, null));
                }
                return(result);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Query zookeeper services error:{0}", e.Message);
                throw;
            }
        }
Beispiel #45
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;
            }
        }
Beispiel #46
0
 private void Run(object sender)
 {
     try
     {
         CancellationToken token = (CancellationToken)sender;
         using (ZooKeeper zooKeeper = CreateClient(this))
         {
             Stat stat = new Stat();
             if (zooKeeper.Exists("/unittests/recoverytests", false) == null)
             {
                 zooKeeper.Create("/unittests", new byte[] { 0 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                 zooKeeper.Create("/unittests/recoverytests", new byte[] { 0 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
             }
             if (zooKeeper.Exists(testPath, false) == null)
             {
                 zooKeeper.Create(testPath, new byte[] { 0 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
             }
             while (zooKeeper.State.IsAlive() && !token.IsCancellationRequested)
             {
                 try
                 {
                     zooKeeper.GetData(testPath, true, stat);
                     zooKeeper.SetData(testPath, Guid.NewGuid().ToByteArray(), -1);
                     setDataCount++;
                 }
                 catch (KeeperException ke)
                 {
                     LOG.Error(ke);
                 }
             }
             LOG.Error("Waiting for dispose.");
         }
     }
     catch (Exception ex)
     {
         LOG.Error(ex);
     }
 }
Beispiel #47
0
        /// <summary>
        /// 创建分布式锁
        /// </summary>
        /// <param name="lockName">竞争资源标志,lockName中不能包含单词lock</param>
        public ZooKeeprDistributedLock(string lockName)
        {
            this._lockName = lockName;
            // 创建一个与服务器的连接
            try
            {
                _zk = new ZooKeeper(_connectString, _sessionTimeout, this);
                var sw = new Stopwatch();
                sw.Start();

                while (true)
                {
                    if (Equals(_zk.State, States.CONNECTING))
                    {
                        break;
                    }
                    if (Equals(_zk.State, States.CONNECTED))
                    {
                        break;
                    }
                }

                sw.Stop();
                var ts2 = sw.Elapsed;
                Console.WriteLine($@"zoo连接总共花费{ts2.TotalMilliseconds}ms.");

                var stat = _zk.Exists(_root, false);
                if (stat == null)
                {
                    // 创建根节点
                    _zk.Create(_root, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                }
            }
            catch (KeeperException e)
            {
                throw e;
            }
        }
Beispiel #48
0
        static ManualResetEvent manualWaitHandler = new ManualResetEvent(false);//false 即非终止,未触发。

        static void Main(string[] args)
        {
            ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), new ZooKeeper_Constructor_Usage_Simple());

            try
            {
                manualWaitHandler.WaitOne();

                //临时节点,API的返回值就是传入的path
                string path1 = zookeeper.Create("/zk-test-ephemeral-", "".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
                Console.WriteLine("Success create znode: {0}", path1);

                //临时顺序节点,API的返回值会在传入的path后边加上一个数字
                string path2 = zookeeper.Create("/zk-test-ephemeral-", "".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential);
                Console.WriteLine("Success create znode: {0}", path2);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
Beispiel #49
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();
                }
            }
        }
Beispiel #50
0
        public async Task <string> createAsync(string node, string name, string port, Watcher watcher)
        {
            ZooKeeper zk = createClient(watcher);

            //await zk.setDataAsync("/", Encoding.UTF8.GetBytes("t"), -1);
            try
            {
                if (zk.existsAsync(node).Result == null)
                {
                    await zk.createAsync($"{node}", Encoding.UTF8.GetBytes($"{name}:{port}"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }//$"{node}/a"
                //need watcher else excption EPHEMERAL disconnected
                return(await zk.createAsync($"{node}/a", Encoding.UTF8.GetBytes($"{name}:{port}"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL));
            }
            catch (KeeperException ke)
            {
                return(await Task.FromResult($"Keeper error:{ke.Message}"));
            }
            catch (Exception ex)
            {
                return(await Task.FromResult($"error:{ex.Message}"));
            }
        }
Beispiel #51
0
        public async Task testPing()
        {
            ZooKeeper zkIdle = await createClient();

            ZooKeeper zkWatchCreator = await createClient();

            for (int i = 0; i < 10; i++)
            {
                await zkWatchCreator.createAsync("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            for (int i = 0; i < 10; i++)
            {
                await zkIdle.existsAsync("/" + i, true);
            }
            for (int i = 0; i < 10; i++)
            {
                await Task.Delay(1000);

                await zkWatchCreator.deleteAsync("/" + i, -1);
            }
            // The bug will manifest itself here because zkIdle will expire
            await zkIdle.existsAsync("/0", false);
        }
Beispiel #52
0
        public void Get_Value_From_Node()
        {
            string usd = "6.35";
            var    zk  = new ZooKeeper("localhost:2181", 3000, null);

            zk.createAsync("/AccountApp", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).GetAwaiter().GetResult();
            zk.createAsync("/AccountApp/Rate", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).GetAwaiter().GetResult();
            zk.createAsync("/AccountApp/Rate/USD", Encoding.UTF8.GetBytes(usd), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).GetAwaiter().GetResult();
            zk.closeAsync().GetAwaiter().GetResult();

            IConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddZookeeper(option =>
            {
                option.ConnectionString  = "localhost:2181";
                option.ConnectionTimeout = 10000;
                option.RootPath          = "/AccountApp";
                option.SessionTimeout    = 30000;
            });
            var configuration = builder.Build();

            Assert.Equal(usd, configuration["Rate:USD"]);
        }
Beispiel #53
0
        public void testChRootSetData()
        {
            // creating the subtree for chRoot clients.
            string chRoot = createNameSpace();

            // setData using chRoot client.
            zk_chroot = createClient(chRoot);
            string[]  names = new string[] { "/multi0", "/multi1", "/multi2" };
            List <Op> ops   = new List <Op>();

            for (int i = 0; i < names.Length; i++)
            {
                ops.Add(Op.create(names[i], new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
                ops.Add(Op.setData(names[i], names[i].getBytes(), 0));
            }

            zk_chroot.multi(ops);

            for (int i = 0; i < names.Length; i++)
            {
                Assert.assertEquals("zNode data not matching", names[i].getBytes(), zk_chroot.getData(names[i], false, null));
            }
        }
Beispiel #54
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void connect(String hosts) throws java.io.IOException, InterruptedException
		public virtual void connect(string hosts)
		{

			zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
			connectedSignal.await();
		}
Beispiel #55
0
            public ZooKeeper getZooKeeper()
            {
                lock(this)
                {
                    if (zooKeeperHandle == null)
                    {
                        connectionString = _handleHolder.ensembleProvider
                                                        .getConnectionString();
                        zooKeeperHandle = _handleHolder.zookeeperFactory
                                                       .newZooKeeper(connectionString,
                                                                        _handleHolder.sessionTimeout,
                                                                        _handleHolder.watcher,
                                                                        _handleHolder.canBeReadOnly);
                    }

                    _handleHolder.helper = new UnsyncHelper(this);

                    return zooKeeperHandle;
                }
            }
Beispiel #56
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientConnection"/> class.
 /// </summary>
 /// <param name="connectionString">The connection string.</param>
 /// <param name="sessionTimeout">The session timeout.</param>
 /// <param name="zooKeeper">The zoo keeper.</param>
 /// <param name="watcher">The watch manager.</param>
 /// <param name="connectTimeout">Connection Timeout.</param>
 public ClientConnection(string connectionString, TimeSpan sessionTimeout, ZooKeeper zooKeeper, ZKWatchManager watcher, TimeSpan connectTimeout) :
     this(connectionString, sessionTimeout, zooKeeper, watcher, 0, new byte[16], connectTimeout)
 {
 }
Beispiel #57
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientConnection"/> class.
 /// </summary>
 /// <param name="hosts">The hosts.</param>
 /// <param name="sessionTimeout">The session timeout.</param>
 /// <param name="zooKeeper">The zoo keeper.</param>
 /// <param name="watcher">The watch manager.</param>
 /// <param name="sessionId">The session id.</param>
 /// <param name="sessionPasswd">The session passwd.</param>
 public ClientConnection(string hosts, TimeSpan sessionTimeout, ZooKeeper zooKeeper, ZKWatchManager watcher, long sessionId, byte[] sessionPasswd)
     : this(hosts, sessionTimeout, zooKeeper, watcher, 0, new byte[16], DefaultConnectTimeout)
 {
 }
Beispiel #58
0
 public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration, object callback, object ctx)
 {
     return producer.QueuePacket(h, r, request, response, clientPath, serverPath, watchRegistration);
 }
Beispiel #59
0
 public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration)
 {
     ReplyHeader r = new ReplyHeader();
     Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null);
     
     if (!p.WaitUntilFinishedSlim(SessionTimeout))
     {
         throw new TimeoutException(new StringBuilder("The request ").Append(request).Append(" timed out while waiting for a response from the server.").ToString());
     }
     return r;
 }
Beispiel #60
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientConnection"/> class.
        /// </summary>
        /// <param name="hosts">The hosts.</param>
        /// <param name="sessionTimeout">The session timeout.</param>
        /// <param name="zooKeeper">The zoo keeper.</param>
        /// <param name="watcher">The watch manager.</param>
        /// <param name="sessionId">The session id.</param>
        /// <param name="sessionPasswd">The session passwd.</param>
        /// <param name="connectTimeout">Connection Timeout.</param>
        public ClientConnection(string hosts, TimeSpan sessionTimeout, ZooKeeper zooKeeper, ZKWatchManager watcher, long sessionId, byte[] sessionPasswd, TimeSpan connectTimeout)
        {
            this.hosts = hosts;
            this.zooKeeper = zooKeeper;
            this.watcher = watcher;
            SessionTimeout = sessionTimeout;
            SessionId = sessionId;
            SessionPassword = sessionPasswd;
            ConnectionTimeout = connectTimeout;

            // parse out chroot, if any
            hosts = SetChrootPath();
            GetHosts(hosts);
            SetTimeouts(sessionTimeout);
            CreateConsumer();
            CreateProducer();
        }