Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 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;
     }
 }