/** * 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 void Closing(int rc) { lock (_lockObj) { _zk.Dispose(); } }
public void Close() { zk.Dispose(); zk = null; GC.Collect(); }
/// <summary> /// Closes underlying ZooKeeper client /// </summary> public void Dispose() { if (disposed) { return; } lock (shuttingDownLock) { if (disposed) { return; } disposed = true; } try { if (_zkclient != null) { Logger.Debug("Closing ZooKeeper client connected to " + Servers); _zkclient.Dispose(); _zkclient = null; Logger.Debug("ZooKeeper client connection closed"); } } catch (Exception exc) { Logger.WarnFormat("Ignoring unexpected errors on closing {0}", exc.FormatException()); } }
/// <summary> /// 关闭zk client /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_zk != null) { _zk.Dispose(); } }
public void Dispose() { if (_zk != null) { _zk.Dispose(); _zk = null; } }
public void CloseConnection() { if (Zk == null) { return; } Status = "Connection Closed"; Zk.Dispose(); }
private void disposeZk() { var cancellationToken = new CancellationTokenSource(); var task = Task.Factory.StartNew(() => _zk.Dispose(), cancellationToken.Token); var timeout = Task.Delay(3000, cancellationToken.Token); Task.WhenAny(task, timeout); cancellationToken.Cancel(); }
public void close() { if (zk.State == ZooKeeper.States.CONNECTED) { zk.Dispose(); } zk = null; }
public void Close() { if (Equals(zk.State, ZooKeeper.States.CONNECTED)) { zk.Dispose(); } zk = null; }
/** * 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"); }
/// <summary> /// 解除锁 /// </summary> public virtual void Unlock() { try { Console.WriteLine("unlock " + _myZnode); _zk.Delete(_myZnode, -1); _myZnode = null; _zk.Dispose(); } catch (KeeperException e) { throw e; } }
static void Main(string[] args) { var zookeeper = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), null); //添加权限信息后创建的节点就 受到了权限控制 zookeeper.AddAuthInfo("digest", "foo:true".GetBytes()); zookeeper.Create(PATH, "init".GetBytes(), Ids.CREATOR_ALL_ACL, CreateMode.Ephemeral); try { var zooKeeper2 = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), null); //这儿会抛出异常,无权限访问 zooKeeper2.GetData(PATH, false, null); } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine(); } try { var zooKeeper3 = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), null); zooKeeper3.AddAuthInfo("digest", "foo:false".GetBytes()); zooKeeper3.GetData(PATH, false, null); } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine(); } try { var zooKeeper4 = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), null); zooKeeper4.AddAuthInfo("digest5555555", "foo:true".GetBytes()); zooKeeper4.GetData(PATH, false, null); } catch (Exception ex) { Console.WriteLine(ex); } zookeeper.GetData(PATH, null, null); zookeeper.Dispose(); Console.ReadLine(); }
private void CreateZooKeeper() { _zooKeeper?.Dispose(); _zooKeeper = new ZooKeeper(_configInfo.ConnectionString, _configInfo.SessionTimeout , new ReconnectionWatcher( () => { _connectionWait.Set(); }, () => { _connectionWait.Reset(); CreateZooKeeper(); })); }
/// <summary> /// 关闭ZooKeeper /// </summary> public void Close() { try { if (_zooKeeper != null) { LogManager.GetLogger().Info(string.Format("DisconfClient.ConnectionWatcher.Close,Begin,State:{0},IsActive:{1}", _zooKeeper.State.State, _zooKeeper.State.IsAlive())); _zooKeeper.Dispose(); LogManager.GetLogger().Info(string.Format("DisconfClient.ConnectionWatcher.Close,End,State:{0},IsActive:{1}", _zooKeeper.State.State, _zooKeeper.State.IsAlive())); } } catch (Exception ex) { LogManager.GetLogger().Error("DisconfClient.ConnectionWatcher.Close", ex); } }
/// <summary> /// 注销 /// </summary> public void Dispose() { try { if (_zk != null) { _zk.Dispose(); } Logger.Info("ZooKeeper注销..."); } finally { _zk = null; } }
public void Disconnect() { if (zk != null) { try { zk.Dispose(); isDisposed = true; } catch (Exception ex) { Console.WriteLine("Disposal of old Zookeeper failed, I still gonna kick the shit out of him"); Console.WriteLine("Error message: " + ex.Message); } zk = null; } }
public void testDeleteWithChildren() { ZooKeeper zk = CreateClient(); zk.Create("/parent", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create("/parent/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); try { zk.Delete("/parent", -1); Assert.Fail("Should have received a not equals message"); } catch (KeeperException e) { Assert.AreEqual(KeeperException.Code.NOTEMPTY, e.GetCode()); } zk.Delete("/parent/child", -1); zk.Delete("/parent", -1); zk.Dispose(); }
public void testPing() { ZooKeeper zkIdle = null; ZooKeeper zkWatchCreator = null; try { CountdownWatcher watcher = new CountdownWatcher(); zkIdle = CreateClient(); zkWatchCreator = CreateClient(watcher); var node = Guid.NewGuid(); for (int i = 0; i < 10; i++) { zkWatchCreator.Create("/" + node + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } for (int i = 0; i < 10; i++) { zkIdle.Exists("/" + node + i, true); } for (int i = 0; i < 10; i++) { Thread.Sleep(1000); zkWatchCreator.Delete("/" + node + i, -1); } // The bug will manifest itself here because zkIdle will expire zkIdle.Exists("/0", false); } finally { if (zkIdle != null) { zkIdle.Dispose(); } if (zkWatchCreator != null) { zkWatchCreator.Dispose(); } } }
public bool ConnectZookeeper(string connectionString, TimeSpan timeout) { try { if (_zk == null) { _zk = new ZooKeeper(connectionString, timeout, new ZookeeperWatcher(this)); } return(true); } catch (Exception ex) { this.AddLog(LogType.Fatal, ex.Message); if (_zk != null) { _zk.Dispose(); } _zk = null; } return(false); }
public void Dispose() { zk.Dispose(); }
public void Close() { zkWatchCreator.Dispose(); }
public void testMutipleWatcherObjs() { ZooKeeper zk = CreateClient(new CountdownWatcher()); try { MyWatcher[] watchers = new MyWatcher[100]; MyWatcher[] watchers2 = new MyWatcher[watchers.Length]; string name = "/" + Guid.NewGuid() + "foo-"; for (int i = 0; i < watchers.Length; i++) { watchers[i] = new MyWatcher(); watchers2[i] = new MyWatcher(); zk.Create(name + i, ("foodata" + i).GetBytes(), 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.NotNull(zk.GetData(name + i, watchers[i], stat)); } for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.Exists(name + i, watchers[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.SetData(name + i, ("foodata2-" + i).GetBytes(), -1); zk.SetData(name + i, ("foodata3-" + i).GetBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event; watchers[i].events.TryTake(out @event, TimeSpan.FromSeconds(3d)); Assert.Equal(name + i, @event.Path); Assert.Equal(EventType.NodeDataChanged, @event.Type); Assert.Equal(KeeperState.SyncConnected, @event.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers[i].events.Count); } // // test get/Exists with single set of watchers // get/Exists together // for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.GetData(name + i, watchers[i], stat)); Assert.NotNull(zk.Exists(name + i, watchers[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.SetData(name + i, ("foodata4-" + i).GetBytes(), -1); zk.SetData(name + i, ("foodata5-" + i).GetBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event; watchers[i].events.TryTake(out @event, TimeSpan.FromSeconds(10d)); Assert.Equal(name + i, @event.Path); Assert.Equal(EventType.NodeDataChanged, @event.Type); Assert.Equal(KeeperState.SyncConnected, @event.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers[i].events.Count); } // // test get/Exists with two sets of watchers // for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.GetData(name + i, watchers[i], stat)); Assert.NotNull(zk.Exists(name + i, watchers2[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.SetData(name + i, ("foodata6-" + i).GetBytes(), -1); zk.SetData(name + i, ("foodata7-" + i).GetBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event; watchers[i].events.TryTake(out @event, TimeSpan.FromSeconds(3000)); Assert.Equal(name + i, @event.Path); Assert.Equal(EventType.NodeDataChanged, @event.Type); Assert.Equal(KeeperState.SyncConnected, @event.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers[i].events.Count); // watchers2 WatchedEvent event2; watchers2[i].events.TryTake(out @event2, TimeSpan.FromSeconds(3000)); Assert.Equal(name + i, event2.Path); Assert.Equal(EventType.NodeDataChanged, event2.Type); Assert.Equal(KeeperState.SyncConnected, event2.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers2[i].events.Count); } } finally { if (zk != null) { zk.Dispose(); } } }
void Dispose() { zk.Dispose(); }
/// <summary> /// 关闭ZooKeeper客户端连接 /// </summary> /// <param name="zookeeper"></param> public void close(ZooKeeper zookeeper) { zookeeper.Dispose(); }
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> public void Dispose() { _connectionWait.Dispose(); _zooKeeper.Dispose(); }
public void TearDown() { zk.Dispose(); }
public void testRootAcl() { ZooKeeper zk = CreateClient(); // set auth using digest zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("pat:test")); zk.SetACL("/", Ids.CREATOR_ALL_ACL, -1); zk.GetData("/", false, null); zk.Dispose(); // verify no access zk = CreateClient(); try { zk.GetData("/", false, null); Assert.Fail("validate auth"); } catch (KeeperException.NoAuthException e) { // expected } string path = "/" + Guid.NewGuid() + "apps"; try { zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); Assert.Fail("validate auth"); } catch (KeeperException.InvalidACLException e) { // expected } zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("world:anyone")); try { zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); Assert.Fail("validate auth"); } catch (KeeperException.NoAuthException e) { // expected } zk.Dispose(); // verify access using original auth zk = CreateClient(); zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("pat:test")); zk.GetData("/", false, null); zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); zk.Delete(path, -1); // reset acl (back to open) and verify accessible again zk.SetACL("/", Ids.OPEN_ACL_UNSAFE, -1); zk.Dispose(); zk = CreateClient(); zk.GetData("/", false, null); zk.Create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); try { zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); Assert.Fail("validate auth"); } catch (KeeperException.InvalidACLException e) { // expected } zk.Delete(path, -1); zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("world:anyone")); zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); zk.Dispose(); zk = CreateClient(); zk.Delete(path, -1); }
public void Teardown() { _zk.Dispose(); _zk = null; }
public void Stop() { zookeeper.Dispose(); zookeeper = null; }
public void Close() { zk.Dispose(); }