public HostConnectionPool(Host host, HostDistance hostDistance, Configuration configuration)
 {
     Host = host;
     Host.Down += OnHostDown;
     HostDistance = hostDistance;
     Configuration = configuration;
 }
 public HostConnectionPool(Host host, HostDistance hostDistance, byte protocolVersion, Configuration configuration)
 {
     this.Host = host;
     this.HostDistance = hostDistance;
     this.ProtocolVersion = protocolVersion;
     this.Configuration = configuration;
 }
 public long NextExecution(Host lastQueried)
 {
     if (_executions++ < _maxSpeculativeExecutions)
     {
         return _delay;
     }
     return 0L;
 }
 public HostConnectionPool(Host host, HostDistance distance, Configuration config)
 {
     _host = host;
     _host.Down += OnHostDown;
     _host.Up += OnHostUp;
     _distance = distance;
     _config = config;
     _timer = config.Timer;
 }
 /// <summary>
 /// Returns the distance as determined by the child policy.
 /// </summary>
 public HostDistance Distance(Host host)
 {
     var lastPreferredHost = _lastPreferredHost;
     if (lastPreferredHost != null && host == lastPreferredHost)
     {
         // Set the last preferred host as local.
         // It's somewhat "hacky" but ensures that the pool for the graph analytics host has the appropriate size
         return HostDistance.Local;
     }
     return _childPolicy.Distance(host);
 }
 public HostConnectionPool(Host host, HostDistance distance, Configuration config, Serializer serializer)
 {
     _host = host;
     _host.CheckedAsDown += OnHostCheckedAsDown;
     _host.Down += OnHostDown;
     _host.Up += OnHostUp;
     _host.Remove += OnHostRemoved;
     _distance = distance;
     _config = config;
     _serializer = serializer;
     _timer = config.Timer;
 }
        /// <summary>
        ///  Return the HostDistance for the provided host. <p> This policy consider nodes
        ///  in the local datacenter as <code>Local</code>. For each remote datacenter, it
        ///  considers a configurable number of hosts as <code>Remote</code> and the rest
        ///  is <code>Ignored</code>. </p><p> To configure how many host in each remote
        ///  datacenter is considered <code>Remote</code>, see
        ///  <link>#DCAwareRoundRobinPolicy(String, int)</link>.</p>
        /// </summary>
        /// <param name="host"> the host of which to return the distance of. </param>
        /// <returns>the HostDistance to <code>host</code>.</returns>
        public HostDistance Distance(Host host)
        {
            string dc = DC(host);
            if (dc.Equals(_localDc))
                return HostDistance.Local;

            int ix = 0;
            foreach (var h in _cluster.Metadata.AllHosts())
            {
                if (h == host)
                    return ix < _usedHostsPerRemoteDc ? HostDistance.Ignored : HostDistance.Remote;
                else if (dc.Equals(DC(h)))
                    ix++;
            }
            return HostDistance.Ignored;
        }
示例#8
0
 private void OnHostDown(Host h, long reconnectionDelay)
 {
     if (HostsEvent != null)
     {
         HostsEvent(this, new HostsEventArgs { Address = h.Address, What = HostsEventArgs.Kind.Down });
     }
 }
示例#9
0
 private void OnHostDown(Host h, DateTimeOffset nextUpTime)
 {
     if (HostsEvent != null)
     {
         HostsEvent(this, new HostsEventArgs { Address = h.Address, What = HostsEventArgs.Kind.Down });
     }
 }
 /// <summary>
 /// Tries to create the a connection to the cluster
 /// </summary>
 /// <exception cref="NoHostAvailableException" />
 /// <exception cref="DriverInternalError" />
 private void Connect(bool firstTime)
 {
     var triedHosts = new Dictionary<IPEndPoint, Exception>();
     IEnumerable<Host> hostIterator = Metadata.Hosts;
     if (!firstTime)
     {
         _logger.Info("Trying to reconnect the ControlConnection");
         //Use the load balancing policy to determine which host to use
         hostIterator = _config.Policies.LoadBalancingPolicy.NewQueryPlan(null, null);
     }
     foreach (var host in hostIterator)
     {
         var address = host.Address;
         var c = new Connection(ProtocolVersion, address, _config);
         try
         {
             c.Init();
             _connection = c;
             _host = host;
             return;
         }
         catch (UnsupportedProtocolVersionException)
         {
             //Use the protocol version used to parse the response message
             var nextVersion = c.ProtocolVersion;
             if (nextVersion >= ProtocolVersion)
             {
                 //Processor could reorder instructions in such way that the connection protocol version is not up to date.
                 nextVersion = (byte)(ProtocolVersion - 1);
             }
             _logger.Info(String.Format("Unsupported protocol version {0}, trying with version {1}", ProtocolVersion, nextVersion));
             ProtocolVersion = nextVersion;
             c.Dispose();
             if (ProtocolVersion < 1)
             {
                 throw new DriverInternalError("Invalid protocol version");
             }
             //Retry using the new protocol version
             Connect(firstTime);
             return;
         }
         catch (Exception ex)
         {
             //There was a socket exception or an authentication exception
             triedHosts.Add(host.Address, ex);
             c.Dispose();
         }
     }
     throw new NoHostAvailableException(triedHosts);
 }
        private void OnHostDown(Host h, DateTimeOffset nextUpTime)
        {
            h.Down -= OnHostDown;
            _logger.Warning("Host " + h.Address + " used by the ControlConnection DOWN");

            Task.Factory.StartNew(() => Refresh(true), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        }
示例#12
0
 private void OnHostDown(Host h, DateTimeOffset nextTimeUp)
 {
     //Dispose all current connections
     //Connection allows multiple calls to Dispose
     var currentPool = _connections;
     if (currentPool != null)
     {
         foreach (var c in currentPool)
         {
             c.Dispose();
         }
     }
 }
示例#13
0
 private void OnHostUp(Host host)
 {
     _isShuttingDown = false;
     SetReconnectionTimeout(null);
     //The host is back up, we can start creating the pool (if applies)
     MaybeCreateFirstConnection();
 }
 /// <summary>
 ///  Return the HostDistance for the provided host.
 /// </summary>
 /// <param name="host"> the host of which to return the distance of. </param>
 /// 
 /// <returns>the HostDistance to <code>host</code> as returned by the wrapped
 ///  policy.</returns>
 public HostDistance Distance(Host host)
 {
     return _childPolicy.Distance(host);
 }
示例#15
0
 private Task<bool> IterateAndConnect(IEnumerator<Host> hostsEnumerator, Dictionary<IPEndPoint, Exception> triedHosts)
 {
     var available = hostsEnumerator.MoveNext();
     if (!available)
     {
         return TaskHelper.FromException<bool>(new NoHostAvailableException(triedHosts));
     }
     var host = hostsEnumerator.Current;
     var c = new Connection(_serializer, host.Address, _config);
     return ((Task) c
         .Open())
         .ContinueWith(t =>
         {
             if (t.Status == TaskStatus.RanToCompletion)
             {
                 _connection = c;
                 _host = host;
                 _logger.Info("Connection established to {0}", c.Address);
                 return TaskHelper.ToTask(true);
             }
             if (t.IsFaulted && t.Exception != null)
             {
                 var ex = t.Exception.InnerException;
                 if (ex is UnsupportedProtocolVersionException)
                 {
                     //Use the protocol version used to parse the response message
                     var nextVersion = _serializer.ProtocolVersion;
                     _logger.Info(string.Format("{0}, trying with version {1}", ex.Message, nextVersion));
                     c.Dispose();
                     if (ProtocolVersion < 1)
                     {
                         throw new DriverInternalError("Invalid protocol version");
                     }
                     //Retry using the new protocol version
                     return Connect(true);
                 }
                 //There was a socket exception or an authentication exception
                 triedHosts.Add(host.Address, ex);
                 c.Dispose();
                 return IterateAndConnect(hostsEnumerator, triedHosts);
             }
             throw new TaskCanceledException("The ControlConnection could not be connected.");
         }, TaskContinuationOptions.ExecuteSynchronously)
         .Unwrap();
 }
示例#16
0
 private void OnCoreHostAdded(Host h)
 {
     if (HostAdded != null)
     {
         HostAdded(h);
     }
 }
 private string GetDatacenter(Host host)
 {
     var dc = host.Datacenter;
     return dc ?? _localDc;
 }
 public HostDistance Distance(Host host)
 {
     return _loadBalancingPolicy.Distance(host);
 }
示例#19
0
 internal static void SetCassandraVersion(Host host, Row row)
 {
     try
     {
         var releaseVersion = row.GetValue<string>("release_version");
         if (releaseVersion != null)
         {
             host.CassandraVersion = Version.Parse(releaseVersion.Split('-')[0]);
         }
     }
     catch (Exception ex)
     {
         _logger.Error("There was an error while trying to retrieve the Cassandra version", ex);
     }
 }
示例#20
0
 private void OnHostDown(Host h, long reconnectionDelay)
 {
     h.Down -= OnHostDown;
     _logger.Warning("Host {0} used by the ControlConnection DOWN", h.Address);
     Task.Factory.StartNew(() => Reconnect(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
 }
示例#21
0
 private void OnHostUp(Host h)
 {
     if (HostsEvent != null)
     {
         HostsEvent(h, new HostsEventArgs { Address = h.Address, What = HostsEventArgs.Kind.Up });
     }
 }
示例#22
0
 private void OnHostDown(Host h, long delay)
 {
     Shutdown();
 }
示例#23
0
 private void OnCoreHostRemoved(Host h)
 {
     if (HostRemoved != null)
     {
         HostRemoved(h);
     }
 }
 public long NextExecution(Host lastQueried)
 {
     return 0L;
 }
 /// <summary>
 ///  Return the HostDistance for the provided host. <p> This policy consider nodes
 ///  in the local datacenter as <c>Local</c>. For each remote datacenter, it
 ///  considers a configurable number of hosts as <c>Remote</c> and the rest
 ///  is <c>Ignored</c>. </p><p> To configure how many host in each remote
 ///  datacenter is considered <c>Remote</c>.</p>
 /// </summary>
 /// <param name="host"> the host of which to return the distance of. </param>
 /// <returns>the HostDistance to <c>host</c>.</returns>
 public HostDistance Distance(Host host)
 {
     var dc = GetDatacenter(host);
     if (dc == _localDc)
     {
         return HostDistance.Local;
     }
     return HostDistance.Remote;
 }
        /// <summary>
        ///  Return the HostDistance for the provided host. <p> This policy consider nodes
        ///  in the local datacenter as <c>Local</c>. For each remote datacenter, it
        ///  considers a configurable number of hosts as <c>Remote</c> and the rest
        ///  is <c>Ignored</c>. </p><p> To configure how many host in each remote
        ///  datacenter is considered <c>Remote</c>, see
        ///  <link>#DCAwareRoundRobinPolicy(String, int)</link>.</p>
        /// </summary>
        /// <param name="host"> the host of which to return the distance of. </param>
        /// <returns>the HostDistance to <c>host</c>.</returns>
        public HostDistance Distance(Host host)
        {
            string dc = DC(host);
            if (dc.Equals(_localDc))
                return HostDistance.Local;

            int ix = 0;
            foreach (var h in _cluster.AllHosts())
            {
                if (h.Address.Equals(host.Address))
                {
                    if (ix < _usedHostsPerRemoteDc)
                        return HostDistance.Remote;
                    else
                        return HostDistance.Ignored;
                }
                else if (dc.Equals(DC(h)))
                    ix++;
            }
            return HostDistance.Ignored;
        }
示例#27
0
 public bool TryGet(IPAddress endpoint, out Host host)
 {
     return _hosts.TryGetValue(endpoint, out host);
 }
 private string DC(Host host)
 {
     string dc = host.Datacenter;
     return dc ?? this._localDc;
 }
示例#29
0
 /// <summary>
 /// Gets or creates the connection pool for a given host
 /// </summary>
 internal HostConnectionPool GetOrCreateConnectionPool(Host host, HostDistance distance)
 {
     var hostPool = _connectionPool.GetOrAdd(host.Address, address => 
         new HostConnectionPool(host, distance, Configuration, _serializer));
     return hostPool;
 }
示例#30
0
 /// <summary>
 ///  Return the HostDistance for the provided host. <p> This policy consider all
 ///  nodes as local. This is generally the right thing to do in a single
 ///  datacenter deployment. If you use multiple datacenter, see
 ///  <link>DCAwareRoundRobinPolicy</link> instead.</p>
 /// </summary>
 /// <param name="host"> the host of which to return the distance of. </param>
 /// <returns>the HostDistance to <c>host</c>.</returns>
 public HostDistance Distance(Host host)
 {
     return HostDistance.Local;
 }