Пример #1
0
        /// <summary>
        /// Adds contact points as hosts and resolving host names if necessary.
        /// </summary>
        /// <exception cref="NoHostAvailableException">When no host can be resolved and no other contact point is an address</exception>
        private async Task AddHosts(IEnumerable <object> contactPoints)
        {
            var resolvedContactPoints = new Dictionary <string, ICollection <IPEndPoint> >();
            var hostNames             = new List <string>();

            foreach (var contactPoint in contactPoints)
            {
                if (contactPoint is IPEndPoint endpoint)
                {
                    resolvedContactPoints.CreateOrAdd(endpoint.ToString(), endpoint);
                    _metadata.AddHost(endpoint);
                    continue;
                }

                if (!(contactPoint is string contactPointText))
                {
                    throw new InvalidOperationException("Contact points should be either string or IPEndPoint instances");
                }

                if (IPAddress.TryParse(contactPointText, out var ipAddress))
                {
                    var ipEndpoint = new IPEndPoint(ipAddress, Configuration.ProtocolOptions.Port);
                    resolvedContactPoints.CreateOrAdd(contactPointText, ipEndpoint);
                    _metadata.AddHost(ipEndpoint);
                    continue;
                }

                hostNames.Add(contactPointText);
                IPHostEntry hostEntry = null;
                try
                {
                    hostEntry = await Dns.GetHostEntryAsync(contactPointText).ConfigureAwait(false);
                }
                catch (Exception)
                {
                    _logger.Warning($"Host '{contactPointText}' could not be resolved");
                }

                if (hostEntry != null && hostEntry.AddressList.Length > 0)
                {
                    foreach (var resolvedAddress in hostEntry.AddressList)
                    {
                        var ipEndpoint = new IPEndPoint(resolvedAddress, Configuration.ProtocolOptions.Port);
                        _metadata.AddHost(ipEndpoint);
                        resolvedContactPoints.CreateOrAdd(contactPointText, ipEndpoint);
                    }
                }
                else
                {
                    resolvedContactPoints.CreateIfDoesNotExist(contactPointText);
                }
            }

            _metadata.SetResolvedContactPoints(resolvedContactPoints.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.AsEnumerable()));

            if (_metadata.Hosts.Count == 0)
            {
                throw new NoHostAvailableException($"No host name could be resolved, attempted: {string.Join(", ", hostNames)}");
            }
        }
Пример #2
0
        /// <summary>
        /// Adds contact points as hosts and resolving host names if necessary.
        /// </summary>
        /// <exception cref="NoHostAvailableException">When no host can be resolved and no other contact point is an address</exception>
        private async Task AddHosts(IEnumerable <object> contactPoints)
        {
            var hostNames = new List <string>();

            foreach (var contactPoint in contactPoints)
            {
                if (contactPoint is IPEndPoint endpoint)
                {
                    _metadata.AddHost(endpoint);
                    continue;
                }

                if (!(contactPoint is string contactPointText))
                {
                    throw new InvalidOperationException("Contact points should be either string or IPEndPoint instances");
                }

                if (IPAddress.TryParse(contactPointText, out var ipAddress))
                {
                    _metadata.AddHost(new IPEndPoint(ipAddress, Configuration.ProtocolOptions.Port));
                    continue;
                }

                hostNames.Add(contactPointText);
                IPHostEntry hostEntry = null;
                try
                {
                    hostEntry = await Dns.GetHostEntryAsync(contactPointText).ConfigureAwait(false);
                }
                catch (Exception)
                {
                    _logger.Warning($"Host '{contactPointText}' could not be resolved");
                }

                if (hostEntry != null)
                {
                    foreach (var resolvedAddress in hostEntry.AddressList)
                    {
                        _metadata.AddHost(new IPEndPoint(resolvedAddress, Configuration.ProtocolOptions.Port));
                    }
                }
            }

            if (_metadata.Hosts.Count == 0)
            {
                throw new NoHostAvailableException($"No host name could be resolved, attempted: {string.Join(", ", hostNames)}");
            }
        }
Пример #3
0
        internal void UpdatePeersInfo(IEnumerable <Row> rs)
        {
            var foundPeers = new HashSet <IPEndPoint>();

            foreach (var row in rs)
            {
                var address = GetAddressForPeerHost(row, _config.AddressTranslator, _config.ProtocolOptions.Port);
                if (address == null)
                {
                    _logger.Error("No address found for host, ignoring it.");
                    continue;
                }
                foundPeers.Add(address);
                var host = Metadata.GetHost(address);
                if (host == null)
                {
                    host = Metadata.AddHost(address);
                }
                host.SetLocationInfo(row.GetValue <string>("data_center"), row.GetValue <string>("rack"));
                host.Tokens = row.GetValue <IEnumerable <string> >("tokens") ?? new string[0];
            }

            // Removes all those that seems to have been removed (since we lost the control connection or not valid contact point)
            foreach (var address in Metadata.AllReplicas())
            {
                if (!address.Equals(_host.Address) && !foundPeers.Contains(address))
                {
                    Metadata.RemoveHost(address);
                }
            }
        }
Пример #4
0
        private Cluster(IEnumerable <IPAddress> contactPoints, Configuration configuration)
        {
            this._contactPoints = contactPoints;
            this._configuration = configuration;
            this._metadata      = new Metadata(configuration.Policies.ReconnectionPolicy);

            var controlpolicies = new Cassandra.Policies(
                //new ControlConnectionLoadBalancingPolicy(_configuration.Policies.LoadBalancingPolicy),
                _configuration.Policies.LoadBalancingPolicy,
                new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000),
                Cassandra.Policies.DefaultRetryPolicy);

            foreach (var ep in _contactPoints)
            {
                Metadata.AddHost(ep);
            }

            var poolingOptions = new PoolingOptions()
                                 .SetCoreConnectionsPerHost(HostDistance.Local, 0)
                                 .SetMaxConnectionsPerHost(HostDistance.Local, 1)
                                 .SetMinSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 0)
                                 .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 127)
            ;

            var controlConnection = new ControlConnection(this, new List <IPAddress>(), controlpolicies,
                                                          new ProtocolOptions(_configuration.ProtocolOptions.Port, configuration.ProtocolOptions.SslOptions),
                                                          poolingOptions, _configuration.SocketOptions,
                                                          new ClientOptions(
                                                              true,
                                                              _configuration.ClientOptions.QueryAbortTimeout, null),
                                                          _configuration.AuthProvider,
                                                          _configuration.AuthInfoProvider);

            _metadata.SetupControllConnection(controlConnection);
        }
Пример #5
0
 private Cluster(IEnumerable <IPEndPoint> contactPoints, Configuration configuration)
 {
     Configuration = configuration;
     _metadata     = new Metadata(configuration.Policies.ReconnectionPolicy);
     foreach (var ep in contactPoints)
     {
         _metadata.AddHost(ep);
     }
 }
Пример #6
0
        private Cluster(IEnumerable <IPEndPoint> contactPoints, Configuration configuration)
        {
            Configuration = configuration;
            _metadata     = new Metadata(configuration);
            foreach (var ep in contactPoints)
            {
                _metadata.AddHost(ep);
            }
            var protocolVersion = _maxProtocolVersion;

            if (Configuration.ProtocolOptions.MaxProtocolVersionValue != null &&
                Configuration.ProtocolOptions.MaxProtocolVersionValue.Value.IsSupported())
            {
                protocolVersion = Configuration.ProtocolOptions.MaxProtocolVersionValue.Value;
            }
            _controlConnection          = new ControlConnection(protocolVersion, Configuration, _metadata);
            _metadata.ControlConnection = _controlConnection;
            _serializer = _controlConnection.Serializer;
        }
Пример #7
0
 private Cluster(IEnumerable<IPEndPoint> contactPoints, Configuration configuration)
 {
     Configuration = configuration;
     _metadata = new Metadata(configuration);
     foreach (var ep in contactPoints)
     {
         _metadata.AddHost(ep);
     }
 }