Exemplo 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);
        }
Exemplo 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);
        }
Exemplo n.º 3
0
        public override async Task <bool> UpdateTopologyAsync(UpdateTopologyParameters parameters)
        {
            if (parameters is null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (Disposed)
            {
                return(false);
            }
            var lockTaken = await _clusterTopologySemaphore.WaitAsync(parameters.TimeoutInMs).ConfigureAwait(false);

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

                using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
                {
                    var command = new GetClusterTopologyCommand(parameters.DebugTag);
                    await ExecuteAsync(parameters.Node, null, context, command, shouldRetry : false, sessionInfo : null, token : CancellationToken.None).ConfigureAwait(false);

                    ClusterTopologyLocalCache.TrySaving(TopologyHash, command.Result, Conventions, 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
                        }
                            ),
                        Etag = results.Etag
                    };

                    TopologyEtag = results.Etag;

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

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

                    OnTopologyUpdatedInvoke(newTopology);
                }
            }
            catch (Exception)
            {
                if (Disposed == false)
                {
                    throw;
                }
            }
            finally
            {
                _clusterTopologySemaphore.Release();
            }

            return(true);
        }