private async Task Watcher_OnChange(string path, Watcher.Event.KeeperState keeperState, Watcher.Event.EventType eventType) { switch (keeperState) { case Watcher.Event.KeeperState.Expired: Logger.LogWarning($"ZooKeeper has been {keeperState},Reconnecting..."); ZooKeeperSessionId = 0; CreateZooKeeperClient(); break; case Watcher.Event.KeeperState.Disconnected: Logger.LogWarning($"ZooKeeper has been {keeperState},Reconnecting..."); CreateZooKeeperClient(); break; case Watcher.Event.KeeperState.SyncConnected: if (ZooKeeperSessionId == 0) { ZooKeeperSessionId = ZooKeeper.getSessionId(); } await SubscribeNodes(path, eventType); break; } }
/// <summary> /// 初始化连接 /// </summary> public void InitClientConnection() { if (connectionString.IsNullOrEmpty()) { return; } zkClient = new ZooKeeper(connectionString, zkTimeOut, watcher); Thread.Sleep(zkTimeOut); sessionId = zkClient.getSessionId(); }
/** * 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(); } }
/** * 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(); } }
private ZookeeperClient(string[] hosts, string[] ports) { if ((hosts.Length != ports.Length) || (hosts.Length <= 0 && ports.Length <= 0)) { throw new ApplicationException("the length of hosts and ports must same and must bigger than 0"); } connectionString = ""; for (var i = 0; i < hosts.Length; i++) { connectionString += hosts[i] + ":" + ports[i] + ","; } watcher = new ConnectionStatusWatcher(this); connectionString = connectionString.Substring(0, connectionString.Length - 1); zkClient = new ZooKeeper(connectionString, zkTimeOut, watcher); // waiting for the connect finish Thread.Sleep(zkTimeOut); sessionId = zkClient.getSessionId(); }
public void testChild() { const string name = "/foo"; zk.create(name, name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); const string childname = name + "/bar"; zk.create(childname, childname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); var stat = newStat(); zk.getData(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()); stat = newStat(); zk.getData(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()); }
public long GetSessionId() { ValidateState(); return(_zk.getSessionId()); }