コード例 #1
0
ファイル: Cluster.cs プロジェクト: reuzel/CqlSharp
        /// <summary>
        /// Opens the cluster for queries. Contains actual implementation and will be called only once per cluster
        /// </summary>
        /// <returns> </returns>
        /// <exception cref="CqlException">Cannot construct ring from provided seeds!</exception>
        private async Task OpenAsyncInternal(Logger logger, CancellationToken token)
        {
            logger.LogInfo("Opening Cluster with parameters: {0}", _config.ToString());

            //initialize the ring
            _nodes = new Ring();

            //retry a few times to deal with bad network conditions
            for(int attempt = 0; attempt <= _config.MaxQueryRetries && _nodes.Count == 0; attempt++)
            {
                //try to connect to the seeds in turn
                foreach(IPAddress seedAddress in _config.ServerAddresses)
                {
                    try
                    {
                        var seed = new Node(seedAddress, this);

                        await GetClusterInfoAsync(seed, logger, token).AutoConfigureAwait();

                        //break from the loop as it seems we are done
                        if(_nodes.Count > 0)
                            break;
                    }
                    catch(OperationCanceledException)
                    {
                        logger.LogWarning("Opening connection to cluster was cancelled");
                        throw;
                    }
                    catch(ProtocolException pex)
                    {
                        logger.LogWarning("Could not open cluster via {0}: {1}", seedAddress, pex.Message);

                        //node is not available, or starting up, try next, otherwise throw error
                        if(pex.Code != ErrorCode.Overloaded && pex.Code != ErrorCode.IsBootstrapping)
                            throw;
                    }
                    catch(SocketException ex)
                    {
                        //seed not reachable, try next
                        logger.LogWarning("Could not open TCP connection to seed {0}: {1}", seedAddress, ex.Message);
                    }
                    catch(IOException ex)
                    {
                        //seed not reachable, try next
                        logger.LogWarning("Could not discover nodes via seed {0}: {1}", seedAddress, ex);
                    }
                }
            }

            //check if not disposed while opening
            if(_disposed)
            {
                foreach(var node in _nodes)
                    node.Dispose();

                throw new ObjectDisposedException("Cluster", "Cluster was disposed while opening");
            }

            //check if we found any nodes
            if(_nodes.Count == 0)
            {
                var ex = new CqlException("Unable to connect to the cluster as none of the provided seeds is reachable.");
                logger.LogCritical("Unable to setup Cluster based on given configuration: {0}", ex);
                throw ex;
            }

            //setup cluster connection strategy
            switch(_config.ConnectionStrategy)
            {
                case CqlSharp.ConnectionStrategy.Balanced:
                    _connectionStrategy = new BalancedConnectionStrategy(_nodes, _config);
                    break;
                case CqlSharp.ConnectionStrategy.Random:
                    _connectionStrategy = new RandomConnectionStrategy(_nodes, _config);
                    break;
                case CqlSharp.ConnectionStrategy.Exclusive:
                    _connectionStrategy = new ExclusiveConnectionStrategy(_nodes, _config);
                    break;
                case CqlSharp.ConnectionStrategy.PartitionAware:
                    _connectionStrategy = new PartitionAwareConnectionStrategy(_nodes, _config);
                    if(_config.DiscoveryScope != DiscoveryScope.Cluster &&
                       _config.DiscoveryScope != DiscoveryScope.DataCenter)
                    {
                        logger.LogWarning(
                            "PartitionAware connection strategy performs best if DiscoveryScope is set to cluster or datacenter");
                    }
                    break;
            }

            //setup prepared query cache
            PreparedQueryCache = new ConcurrentDictionary<string, ResultFrame>();

            //setup maintenance connection
            Scheduler.RunOnThreadPool(() => SetupMaintenanceConnection(logger));
        }
コード例 #2
0
ファイル: Cluster.cs プロジェクト: priyaparul/CqlSharp
        /// <summary>
        /// Opens the cluster for queries. Contains actual implementation and will be called only once per cluster
        /// </summary>
        /// <returns></returns>
        /// <exception cref="CqlException">Cannot construct ring from provided seeds!</exception>
        private async Task OpenAsyncInternal(Logger logger)
        {
            logger.LogInfo("Opening Cluster with parameters: {0}", _config.ToString());

            //try to connect to the seeds in turn
            foreach (IPAddress seedAddress in _config.NodeAddresses)
            {
                try
                {
                    var seed = new Node(seedAddress, this);
                    _nodes = await DiscoverNodesAsync(seed, logger).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    //seed not reachable, try next
                    logger.LogWarning("Could not discover nodes via seed {0}: {1}", seedAddress, ex);
                }
            }

            if (_nodes == null)
            {
                var ex = new CqlException("Cannot construct ring from provided seeds!");
                logger.LogCritical("Unable to setup Cluster based on given configuration: {0}", ex);
                throw ex;
            }

            logger.LogInfo("Nodes detected: " + string.Join(", ", _nodes.Select(n => n.Address)));

            //setup cluster connection strategy
            switch (_config.ConnectionStrategy)
            {
                case ConnectionStrategy.Balanced:
                    _connectionSelector = new BalancedConnectionStrategy(_nodes, _config);
                    break;
                case ConnectionStrategy.Random:
                    _connectionSelector = new RandomConnectionStrategy(_nodes, _config);
                    break;
                case ConnectionStrategy.Exclusive:
                    _connectionSelector = new ExclusiveConnectionStrategy(_nodes, _config);
                    break;
                case ConnectionStrategy.PartitionAware:
                    _connectionSelector = new PartitionAwareConnectionStrategy(_nodes, _config);
                    if (_config.DiscoveryScope != DiscoveryScope.Cluster || _config.DiscoveryScope != DiscoveryScope.DataCenter) 
                        logger.LogWarning("PartitionAware connection strategy performs best if DiscoveryScope is set to cluster or datacenter");
                    break;
            }

            //setup throttle
            int concurrent = _config.MaxConcurrentQueries <= 0
                                 ? _nodes.Count * _config.MaxConnectionsPerNode * 256
                                 : _config.MaxConcurrentQueries;

            logger.LogInfo("Cluster is configured to allow {0} parallel queries", concurrent);

            _throttle = new SemaphoreSlim(concurrent, concurrent);

            //setup prepared query cache
            _prepareResultCache = new ConcurrentDictionary<string, ConcurrentDictionary<IPAddress, ResultFrame>>();

            //setup maintenance connection
            SetupMaintenanceConnection(logger);
        }
コード例 #3
0
ファイル: Cluster.cs プロジェクト: priyaparul/CqlSharp
        /// <summary>
        /// Opens the cluster for queries. Contains actual implementation and will be called only once per cluster
        /// </summary>
        /// <returns></returns>
        /// <exception cref="CqlException">Cannot construct ring from provided seeds!</exception>
        private async Task OpenAsyncInternal(Logger logger)
        {
            logger.LogInfo("Opening Cluster with parameters: {0}", _config.ToString());

            //try to connect to the seeds in turn
            foreach (IPAddress seedAddress in _config.NodeAddresses)
            {
                try
                {
                    var seed = new Node(seedAddress, this);
                    _nodes = await DiscoverNodesAsync(seed, logger).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    //seed not reachable, try next
                    logger.LogWarning("Could not discover nodes via seed {0}: {1}", seedAddress, ex);
                }
            }

            if (_nodes == null)
            {
                var ex = new CqlException("Cannot construct ring from provided seeds!");
                logger.LogCritical("Unable to setup Cluster based on given configuration: {0}", ex);
                throw ex;
            }

            logger.LogInfo("Nodes detected: " + string.Join(", ", _nodes.Select(n => n.Address)));

            //setup cluster connection strategy
            switch (_config.ConnectionStrategy)
            {
            case ConnectionStrategy.Balanced:
                _connectionSelector = new BalancedConnectionStrategy(_nodes, _config);
                break;

            case ConnectionStrategy.Random:
                _connectionSelector = new RandomConnectionStrategy(_nodes, _config);
                break;

            case ConnectionStrategy.Exclusive:
                _connectionSelector = new ExclusiveConnectionStrategy(_nodes, _config);
                break;

            case ConnectionStrategy.PartitionAware:
                _connectionSelector = new PartitionAwareConnectionStrategy(_nodes, _config);
                if (_config.DiscoveryScope != DiscoveryScope.Cluster || _config.DiscoveryScope != DiscoveryScope.DataCenter)
                {
                    logger.LogWarning("PartitionAware connection strategy performs best if DiscoveryScope is set to cluster or datacenter");
                }
                break;
            }

            //setup throttle
            int concurrent = _config.MaxConcurrentQueries <= 0
                                 ? _nodes.Count * _config.MaxConnectionsPerNode * 256
                                 : _config.MaxConcurrentQueries;

            logger.LogInfo("Cluster is configured to allow {0} parallel queries", concurrent);

            _throttle = new SemaphoreSlim(concurrent, concurrent);

            //setup prepared query cache
            _prepareResultCache = new ConcurrentDictionary <string, ConcurrentDictionary <IPAddress, ResultFrame> >();

            //setup maintenance connection
            SetupMaintenanceConnection(logger);
        }