コード例 #1
0
 public ResourcesZnode()
 {
     ResourceAssignments = new ResourcesZnodeData();
 }
コード例 #2
0
        public async Task <ResourcesZnode> GetResourcesAsync(Watcher childWatcher, Watcher dataWatcher)
        {
            var actionToPerform = "get the list of resources";

            while (true)
            {
                await BlockUntilConnected(actionToPerform);

                try
                {
                    DataResult dataResult = null;
                    if (dataWatcher != null)
                    {
                        dataResult = await this.zookeeper.getDataAsync(this.resourcesPath, dataWatcher);
                    }
                    else
                    {
                        dataResult = await this.zookeeper.getDataAsync(this.resourcesPath);
                    }

                    ChildrenResult childrenResult = null;
                    if (childWatcher != null)
                    {
                        childrenResult = await this.zookeeper.getChildrenAsync(this.resourcesPath, childWatcher);
                    }
                    else
                    {
                        childrenResult = await this.zookeeper.getChildrenAsync(this.resourcesPath);
                    }

                    var resourcesZnodeData = JSONSerializer <ResourcesZnodeData> .DeSerialize(
                        System.Text.Encoding.UTF8.GetString(dataResult.Data));

                    if (resourcesZnodeData == null)
                    {
                        resourcesZnodeData = new ResourcesZnodeData();
                    }

                    return(new ResourcesZnode()
                    {
                        ResourceAssignments = resourcesZnodeData,
                        Resources = childrenResult.Children,
                        Version = dataResult.Stat.getVersion()
                    });
                }
                catch (KeeperException.NoNodeException e)
                {
                    throw new ZkInvalidOperationException(
                              $"Could not {actionToPerform} as the resources node does not exist.", e);
                }
                catch (KeeperException.ConnectionLossException)
                {
                    // do nothing, the next iteration will try again
                }
                catch (KeeperException.SessionExpiredException e)
                {
                    throw new ZkSessionExpiredException($"Could not {actionToPerform} as the session has expired: ", e);
                }
                catch (Exception e)
                {
                    throw new ZkInvalidOperationException($"Could not {actionToPerform} due to an unexpected error", e);
                }
            }
        }