Ejemplo n.º 1
0
        protected override bool TryLoadFromCache(string url, JsonOperationContext context)
        {
            var serverHash     = ServerHash.GetServerHash(url);
            var cachedTopology = ClusterTopologyLocalCache.TryLoadClusterTopologyFromLocalCache(serverHash, context);

            if (cachedTopology == null)
            {
                return(false);
            }

            _nodeSelector = new NodeSelector(new Topology
            {
                Nodes = new List <ServerNode>(
                    from member in cachedTopology.Topology.Members
                    select new ServerNode
                {
                    Url        = member.Value,
                    ClusterTag = member.Key
                }
                    )
            });
            return(true);
        }
Ejemplo n.º 2
0
        public override async Task <bool> UpdateTopologyAsync(ServerNode node, int timeout, bool forceUpdate = false)
        {
            if (Disposed)
            {
                return(false);
            }
            var lockTaken = await _clusterTopologySemaphore.WaitAsync(timeout).ConfigureAwait(false);

            if (lockTaken == false)
            {
                return(false);
            }
            try
            {
                if (Disposed)
                {
                    return(false);
                }

                using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
                {
                    var command = new GetClusterTopologyCommand();
                    await ExecuteAsync(node, null, context, command, shouldRetry : false).ConfigureAwait(false);

                    var serverHash = ServerHash.GetServerHash(node.Url);
                    ClusterTopologyLocalCache.TrySavingTopologyToLocalCache(serverHash, command.Result, context);

                    var results     = command.Result;
                    var newTopology = new Topology
                    {
                        Nodes = new List <ServerNode>(
                            from member in results.Topology.Members
                            select new ServerNode
                        {
                            Url        = member.Value,
                            ClusterTag = member.Key
                        }
                            )
                    };

                    if (_nodeSelector == null)
                    {
                        _nodeSelector = new NodeSelector(newTopology);
                        if (_readBalanceBehavior == ReadBalanceBehavior.FastestNode)
                        {
                            _nodeSelector.ScheduleSpeedTest();
                        }
                    }
                    else if (_nodeSelector.OnUpdateTopology(newTopology, forceUpdate: forceUpdate))
                    {
                        DisposeAllFailedNodesTimers();

                        if (_readBalanceBehavior == ReadBalanceBehavior.FastestNode)
                        {
                            _nodeSelector.ScheduleSpeedTest();
                        }
                    }

                    OnTopologyUpdated(newTopology);
                }
            }
            finally
            {
                _clusterTopologySemaphore.Release();
            }
            return(true);
        }