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; } }
public static void CreateLeaderNode(string shardName, byte[] data) { try { helper.RetryOperation(() => { string shardLeaderPath = ZkPath.LeaderPath(ZkPath.CollectionName, shardName); helper.EnsureExists(shardLeaderPath, data, null, CreateMode.Persistent); return(true); }); } catch (KeeperException.SessionExpiredException ex) { helper.Dispose(); helper = new LeaderHelper(_zookeeper); throw ex; } }