public static bool AmILeader(Server server) { try { bool result = helper.RetryOperation(() => { IZooKeeper zookeeper = _zookeeper; string shardLeaderPath = ZkPath.LeaderPath(ZkPath.CollectionName, server.ShardName); Stat stat = zookeeper.Exists(shardLeaderPath, null); if (stat == null) { throw new NotProcessLeaderElectionException(shardLeaderPath); } Server dataServer = getDataSever(shardLeaderPath); if (dataServer == server) { return(true); } else { return(false); } }); return(result); } catch (KeeperException.SessionExpiredException ex) { helper.Dispose(); helper = new LeaderHelper(_zookeeper); throw ex; } }
/** * 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 Process(WatchedEvent ev) { var path = ev.Path; var value = string.Empty; //debug switch (ev.Type) { case EventType.None: break; case EventType.NodeCreated: value = Zookeeper.GetData(path, this, null).GetString(); zkCache.SetValue(path, value); Zookeeper.Exists(path, this); LOG.Debug(" >>NodeCreated:" + value); System.Console.WriteLine(" >>NodeCreated:" + value); break; case EventType.NodeDeleted: Zookeeper.Exists(path, this); LOG.Debug(" >>NodeDeleted:" + value); System.Console.WriteLine(" >>NodeDeleted:" + value); break; case EventType.NodeDataChanged: value = Zookeeper.GetData(path, this, null).GetString(); zkCache.SetValue(path, value); Zookeeper.Exists(path, this); LOG.Debug(" >>NodeDataChanged:" + value); System.Console.WriteLine(" >>NodeDataChanged:" + value); break; case EventType.NodeChildrenChanged: break; default: break; } }
/** * 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 static Server ShardLeader(string shardName) { IZooKeeper zookeeper = _zookeeper; string shardLeaderPath = ZkPath.LeaderPath(ZkPath.CollectionName, shardName); Stat stat = zookeeper.Exists(shardLeaderPath, null); if (stat == null) { return(null); } Server dataServer = getDataSever(shardLeaderPath); return(dataServer); }
public void TestExpiredSession() { var timing = new Timing(); var latch = new AutoResetEvent(false); var watcher = new CuratorWatcher((e) => { if (e.State == KeeperState.Expired) { latch.Set(); } }); using (var client = new CuratorZookeeperClient(server.GetConnectionString(), timing.session(), timing.connection(), watcher, new RetryOneTime(TimeSpan.FromMilliseconds(2)))) { client.Start(); bool firstTime = true; RetryLoop.CallWithRetry <bool>(client, () => { if (firstTime) { try { var stat = client.GetZooKeeper().Exists("/foo", false); if (stat == null) { client.GetZooKeeper().Create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } } catch (KeeperException.NodeExistsException ignore) { } KillSession.kill(client.GetZooKeeper(), server.GetConnectionString()); Assert.IsFalse(timing.awaitLatch(latch)); } IZooKeeper zooKeeper = client.GetZooKeeper(); client.BlockUntilConnectedOrTimedOut(); Assert.IsNotNull(zooKeeper.Exists("/foo", false)); return(true); }); } }
/// <summary> /// Attempt to delete the lock node so that sequence numbers get reset. /// </summary> public void Clean() { try { // Don't blindly do delete or things get noisy in the INFO logs on the // server. var stat = _client.Exists(_basePath, false); if (null != stat && stat.NumChildren < 1) { _client.Delete(_basePath, -1); } } catch (KeeperException.BadVersionException) { // Intentially ignore this - another thread/process got the lock } catch (KeeperException.NotEmptyException) { // Intentially ignore this - other threads/processes are waiting } }
/** * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * * @param zookeeper the client * @param path path to ensure * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException * Zookeeper errors */ public static void Mkdirs(IZooKeeper zookeeper, String path, bool makeLastNode) { PathUtils.ValidatePath(path); var pos = 1; // skip first slash, root is guaranteed to exist do { pos = path.IndexOf(PathUtils.PathSeparatorChar, pos + 1); if (pos == -1) { if (makeLastNode) { pos = path.Length; } else { break; } } var subPath = path.Substring(0, pos); if (zookeeper.Exists(subPath, false) == null) { try { zookeeper.Create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (KeeperException.NodeExistsException e) { // ignore... someone else has created it since we checked } } }while (pos < path.Length); }
/** * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * * @param zookeeper the client * @param path path to ensure * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException * Zookeeper errors */ public static void Mkdirs(IZooKeeper zookeeper, String path, bool makeLastNode) { PathUtils.ValidatePath(path); var pos = 1; // skip first slash, root is guaranteed to exist do { pos = path.IndexOf(PathUtils.PathSeparatorChar, pos + 1); if (pos == -1) { if (makeLastNode) { pos = path.Length; } else { break; } } var subPath = path.Substring(0, pos); if (zookeeper.Exists(subPath, false) == null) { try { zookeeper.Create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (KeeperException.NodeExistsException e) { // ignore... someone else has created it since we checked } } } while (pos < path.Length); }