Exemplo n.º 1
0
            virtual public void Connect(Session owner, bool moveNext, out int streamId)
            {
                if (_hostsIter == null)
                {
                    _hostsIter = owner._policies.LoadBalancingPolicy.NewQueryPlan(Query).GetEnumerator();
                    if (!_hostsIter.MoveNext())
                    {
                        var ex = new NoHostAvailableException(new Dictionary <IPAddress, List <Exception> >());
                        _logger.Error(ex);
                        throw ex;
                    }
                }
                else
                {
                    if (moveNext)
                    {
                        if (!_hostsIter.MoveNext())
                        {
                            var ex = new NoHostAvailableException(InnerExceptions);
                            _logger.Error(ex);
                            throw ex;
                        }
                    }
                }

                Connection = owner.Connect(Query, _hostsIter, TriedHosts, InnerExceptions, out streamId);
            }
Exemplo n.º 2
0
        internal void Init()
        {
            var ci = this._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!ci.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary <IPAddress, Exception>());
                _logger.Error(ex.Message);
                throw ex;
            }

            var triedHosts      = new List <IPAddress>();
            var innerExceptions = new Dictionary <IPAddress, Exception>();

            Connect(null, ci, triedHosts, innerExceptions);
        }
Exemplo n.º 3
0
        private void SetupEventListener()
        {
            var triedHosts      = new List <IPAddress>();
            var innerExceptions = new Dictionary <IPAddress, List <Exception> >();

            var hostsIter = _session._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary <IPAddress, List <Exception> >());
                _logger.Error(ex);
                throw ex;
            }

            int streamId;
            var nconn = _session.Connect(null, hostsIter, triedHosts, innerExceptions, out streamId);

            listeningOnHost = nconn.GetHostAdress();

            Exception theExc = null;

            nconn.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = nconn.RegisterForCassandraEvent(streamId,
                                                             CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                    {
                        theExc = (ret as OutputError).CreateException();
                    }
                    else
                    {
                        theExc = new DriverInternalError("Expected Error on Output");
                    }
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 4
0
        private void SetupEventListener()
        {
            var triedHosts      = new List <IPAddress>();
            var innerExceptions = new Dictionary <IPAddress, List <Exception> >();

            IEnumerator <Host> hostsIter = _session.Policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary <IPAddress, List <Exception> >());
                _logger.Error(ex);
                throw ex;
            }

            _activeConnection.Value = _session.Connect(hostsIter, triedHosts, innerExceptions, out _lockingStreamId);

            int streamId = _activeConnection.Value.AllocateStreamId();

            Exception theExc = null;

            _activeConnection.Value.CassandraEvent += conn_CassandraEvent;
            using (IOutput ret = _activeConnection.Value.RegisterForCassandraEvent(streamId,
                                                                                   CassandraEventType.TopologyChange | CassandraEventType.StatusChange |
                                                                                   CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                    {
                        theExc = (ret as OutputError).CreateException();
                    }
                    else
                    {
                        theExc = new DriverInternalError("Expected Error on Output");
                    }
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 5
0
        internal void Init(bool allock = true)
        {
            if (allock)
            {
                var ci = Policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();
                if (!ci.MoveNext())
                {
                    var ex = new NoHostAvailableException(new Dictionary <IPAddress, List <Exception> >());
                    _logger.Error(ex.Message);
                    throw ex;
                }

                var triedHosts      = new List <IPAddress>();
                var innerExceptions = new Dictionary <IPAddress, List <Exception> >();
                int streamId;
                var con = Connect(ci, triedHosts, innerExceptions, out streamId);
                con.FreeStreamId(streamId);
            }

            _trashcanCleaner = new Timer(TranscanCleanup, null, Timeout.Infinite, Timeout.Infinite);
        }
Exemplo n.º 6
0
        internal CassandraConnection Connect(Query query, IEnumerator <Host> hostsIter, List <IPAddress> triedHosts, Dictionary <IPAddress, List <Exception> > innerExceptions, out int streamId)
        {
            CheckDisposed();

            lock (_connectionPool)
            {
                List <CassandraConnection> toDel = null;
                foreach (var conn in _trashcan)
                {
                    if (conn.IsEmpty())
                    {
                        _logger.Info("Connection trashed");
                        conn.Dispose();
                        if (toDel == null)
                        {
                            toDel = new List <CassandraConnection>();
                        }
                        toDel.Add(conn);
                    }
                }
                if (toDel != null)
                {
                    foreach (var d in toDel)
                    {
                        _trashcan.Remove(d);
                    }
                }

                while (true)
                {
                    var currentHost = hostsIter.Current;
                    if (currentHost == null)
                    {
                        var ex = new NoHostAvailableException(innerExceptions);
                        _logger.Error("All hosts are not responding.", ex);
                        throw ex;
                    }
                    if (currentHost.IsConsiderablyUp)
                    {
                        triedHosts.Add(currentHost.Address);
                        var hostDistance = _policies.LoadBalancingPolicy.Distance(currentHost);
                        if (!_connectionPool.ContainsKey(currentHost.Address))
                        {
                            _connectionPool.Add(currentHost.Address, new List <CassandraConnection>());
                        }

                        var pool    = _connectionPool[currentHost.Address];
                        var poolCpy = new List <CassandraConnection>(pool);
                        CassandraCounters.SetConnectionsCount(currentHost.Address, poolCpy.Count);
                        CassandraConnection toReturn = null;
                        foreach (var conn in poolCpy)
                        {
                            if (!conn.IsHealthy)
                            {
                                pool.Remove(conn);
                                conn.Dispose();
                            }
                            else
                            {
                                if (toReturn == null)
                                {
                                    if (!conn.IsBusy(_poolingOptions.GetMaxSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                                    {
                                        toReturn = conn;
                                    }
                                }
                                else
                                {
                                    if (pool.Count > _poolingOptions.GetCoreConnectionsPerHost(hostDistance))
                                    {
                                        if (conn.IsFree(_poolingOptions.GetMinSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                                        {
                                            _trashcan.Add(conn);
                                            pool.Remove(conn);
                                        }
                                    }
                                }
                            }
                        }
                        if (toReturn != null)
                        {
                            streamId = toReturn.AllocateStreamId();
                            return(toReturn);
                        }
                        if (pool.Count < _poolingOptions.GetMaxConnectionPerHost(hostDistance) - 1)
                        {
                            bool error = false;
                            CassandraConnection conn = null;
                            do
                            {
                                Exception outExc;
                                conn = AllocateConnection(currentHost.Address, out outExc);
                                if (conn != null)
                                {
                                    if (_cluster.Metadata != null)
                                    {
                                        _cluster.Metadata.BringUpHost(currentHost.Address, this);
                                    }
                                    pool.Add(conn);
                                }
                                else
                                {
                                    if (!innerExceptions.ContainsKey(currentHost.Address))
                                    {
                                        innerExceptions.Add(currentHost.Address, new List <Exception>());
                                    }
                                    innerExceptions[currentHost.Address].Add(outExc);
                                    _logger.Info("New connection attempt failed - goto another host.");
                                    error = true;
                                    break;
                                }
                            }while (pool.Count < _poolingOptions.GetCoreConnectionsPerHost(hostDistance));
                            if (!error)
                            {
                                streamId = conn.AllocateStreamId();
                                return(conn);
                            }
                        }
                    }

                    _logger.Verbose(string.Format("Currently tried host: {0} have all of connections busy. Switching to the next host.", currentHost.Address));

                    if (!hostsIter.MoveNext())
                    {
                        var ex = new NoHostAvailableException(innerExceptions);
                        _logger.Error("Cannot connect to any host from pool.", ex);
                        throw ex;
                    }
                }
            }
        }
Exemplo n.º 7
0
        private void SetupEventListener()
        {
            var triedHosts = new List<IPAddress>();
            var innerExceptions = new Dictionary<IPAddress, List<Exception>>();

            var hostsIter = _session._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary<IPAddress, List<Exception>>());
                _logger.Error(ex);
                throw ex;
            }

            _activeConnection.Value = _session.Connect(hostsIter, triedHosts, innerExceptions, out _lockingStreamId);

            int streamId = _activeConnection.Value.AllocateStreamId();

            Exception theExc = null;

            _activeConnection.Value.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = _activeConnection.Value.RegisterForCassandraEvent(streamId,
                CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                        theExc = (ret as OutputError).CreateException();
                    else
                        theExc = new DriverInternalError("Expected Error on Output");
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 8
0
        internal CassandraConnection Connect(IEnumerator<Host> hostsIter, List<IPAddress> triedHosts, Dictionary<IPAddress, List<Exception>> innerExceptions, out int streamId)
        {
            CheckDisposed();

            while (true)
            {
                var currentHost = hostsIter.Current;
                if (currentHost == null)
                {
                    var ex = new NoHostAvailableException(innerExceptions);
                    _logger.Error("All hosts are not responding.", ex);
                    throw ex;
                }
                if (currentHost.IsConsiderablyUp)
                {
                    triedHosts.Add(currentHost.Address);
                    var hostDistance = _policies.LoadBalancingPolicy.Distance(currentHost);
                RETRY_GET_POOL:
                    if (!_connectionPool.ContainsKey(currentHost.Address))
                        _connectionPool.TryAdd(currentHost.Address, new ConcurrentDictionary<Guid, CassandraConnection>());

                    ConcurrentDictionary<Guid, CassandraConnection> pool;

                    if (!_connectionPool.TryGetValue(currentHost.Address, out pool))
                        goto RETRY_GET_POOL;

            //                    CassandraCounters.SetConnectionsCount(currentHost.Address, pool.Count);
                    foreach (var kv in pool)
                    {
                        CassandraConnection conn = kv.Value;
                        if (!conn.IsHealthy)
                        {
                            CassandraConnection cc;
                            if(pool.TryRemove(conn.Guid, out cc))
                                FreeConnection(cc);
                        }
                        else
                        {
                            if (!conn.IsBusy(_poolingOptions.GetMaxSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                            {
                                streamId = conn.AllocateStreamId();
                                return conn;
                            }
                            else
                            {
                                if (pool.Count > _poolingOptions.GetCoreConnectionsPerHost(hostDistance))
                                {
                                    if (conn.IsFree(_poolingOptions.GetMinSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                                    {
                                        CassandraConnection cc;
                                        if (pool.TryRemove(conn.Guid, out cc))
                                            TrashcanPut(cc);
                                    }
                                }
                            }
                        }
                    }
                    {

                        var conn = TrashcanRecycle(currentHost.Address);
                        if (conn != null)
                        {
                            if (!conn.IsHealthy)
                                FreeConnection(conn);
                            else
                            {
                                pool.TryAdd(conn.Guid, conn);
                                streamId = conn.AllocateStreamId();
                                return conn;
                            }
                        }
                        // if not recycled
                        {
                            Exception outExc;
                            conn = AllocateConnection(currentHost.Address, hostDistance, out outExc);
                            if (conn != null)
                            {
                                if (_cluster.Metadata != null)
                                    _cluster.Metadata.BringUpHost(currentHost.Address, this);
                                pool.TryAdd(conn.Guid, conn);
                                streamId = conn.AllocateStreamId();
                                return conn;
                            }
                            else
                            {
                                if (!innerExceptions.ContainsKey(currentHost.Address))
                                    innerExceptions.Add(currentHost.Address, new List<Exception>());
                                innerExceptions[currentHost.Address].Add(outExc);
                                _logger.Info("New connection attempt failed - goto another host.");
                            }
                        }
                    }
                }

                _logger.Verbose(string.Format("Currently tried host: {0} have all of connections busy. Switching to the next host.", currentHost.Address));

                if (!hostsIter.MoveNext())
                {
                    var ex = new NoHostAvailableException(innerExceptions);
                    _logger.Error("Cannot connect to any host from pool.", ex);
                    throw ex;
                }
            }
        }
Exemplo n.º 9
0
        internal Session(Cluster cluster,
                         Policies policies,
                         ProtocolOptions protocolOptions,
                         PoolingOptions poolingOptions,
                         SocketOptions socketOptions,
                         ClientOptions clientOptions,
                         IAuthInfoProvider authProvider,
                         string keyspace, bool init)
        {
            try
            {
                this._cluster = cluster;

                this._protocolOptions = protocolOptions;
                this._poolingOptions = poolingOptions;
                this._socketOptions = socketOptions;
                this._clientOptions = clientOptions;
                this._authProvider = authProvider;

                this._policies = policies ?? Policies.DefaultPolicies;

                this._policies.LoadBalancingPolicy.Initialize(_cluster);

                _keyspace = keyspace ?? clientOptions.DefaultKeyspace;

                Guid = Guid.NewGuid();

                _trashcanCleaner = new Timer(TranscanCleanup, null, Timeout.Infinite, Timeout.Infinite);

                if (init)
                {
                    var ci = this._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();
                    if (!ci.MoveNext())
                    {
                        var ex = new NoHostAvailableException(new Dictionary<IPAddress, List<Exception>>());
                        _logger.Error(ex.Message);
                        throw ex;
                    }

                    var triedHosts = new List<IPAddress>();
                    var innerExceptions = new Dictionary<IPAddress, List<Exception>>();
                    int streamId;
                    var con = Connect(ci, triedHosts, innerExceptions, out streamId);
                    con.FreeStreamId(streamId);
                }
            }
            catch
            {
                InternalDispose();
                throw;
            }
        }
Exemplo n.º 10
0
            public virtual void Connect(Session owner, bool moveNext, out int streamId)
            {
                if (_hostsIter == null)
                {
                    _hostsIter = owner._policies.LoadBalancingPolicy.NewQueryPlan(Query).GetEnumerator();
                    if (!_hostsIter.MoveNext())
                    {
                        var ex = new NoHostAvailableException(new Dictionary<IPAddress, List<Exception>>());
                        _logger.Error(ex);
                        throw ex;
                    }
                }
                else
                {
                    if (moveNext)
                        if (!_hostsIter.MoveNext())
                        {
                            var ex = new NoHostAvailableException(InnerExceptions);
                            _logger.Error(ex);
                            throw ex;
                        }
                }

                Connection = owner.Connect(_hostsIter, TriedHosts, InnerExceptions, out streamId);
            }
Exemplo n.º 11
0
        internal void Init()
        {
            var ci = this._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();
            if (!ci.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary<IPAddress, List<Exception>>());
                _logger.Error(ex.Message);
                throw ex;
            }

            var triedHosts = new List<IPAddress>();
            var innerExceptions = new Dictionary<IPAddress, List<Exception>>();
            Connect(null, ci, triedHosts, innerExceptions);
        }
Exemplo n.º 12
0
        internal CassandraConnection Connect(Query query, IEnumerator<Host> hostsIter, List<IPAddress> triedHosts, Dictionary<IPAddress, List<Exception>> innerExceptions)
        {
            CheckDisposed();
            lock (_trashcan)
            {
                foreach (var conn in _trashcan)
                {
                    if (conn.IsEmpty())
                    {
                        _logger.Error("Connection trashed");
                        conn.Dispose();
                    }
                }
            }
            lock (_connectionPool)
            {
                while (true)
                {
                    var currentHost = hostsIter.Current;
                    if (currentHost == null)
                    {
                        var ex = new NoHostAvailableException(innerExceptions);
                        _logger.Error("All hosts are not responding.", ex);
                        throw ex;
                    }
                    if (currentHost.IsConsiderablyUp)
                    {
                        triedHosts.Add(currentHost.Address);
                        var hostDistance = _policies.LoadBalancingPolicy.Distance(currentHost);
                        if (!_connectionPool.ContainsKey(currentHost.Address))
                            _connectionPool.Add(currentHost.Address, new List<CassandraConnection>());

                        var pool = _connectionPool[currentHost.Address];
                        var poolCpy = new List<CassandraConnection>(pool);
                        CassandraConnection toReturn = null;
                        foreach (var conn in poolCpy)
                        {
                            if (!conn.IsHealthy)
                            {
                                pool.Remove(conn);
                                conn.Dispose();
                            }
                            else
                            {
                                if (toReturn == null)
                                {
                                    if (!conn.IsBusy(_poolingOptions.GetMaxSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                                        toReturn = conn;
                                }
                                else
                                {
                                    if (pool.Count > _poolingOptions.GetCoreConnectionsPerHost(hostDistance))
                                    {
                                        if (conn.IsFree(_poolingOptions.GetMinSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                                        {
                                            lock (_trashcan)
                                                _trashcan.Add(conn);
                                            pool.Remove(conn);
                                        }
                                    }
                                }
                            }
                        }
                        if (toReturn != null)
                            return toReturn;
                        if (pool.Count < _poolingOptions.GetMaxConnectionPerHost(hostDistance) - 1)
                        {
                            bool error = false;
                            CassandraConnection conn = null;
                            do
                            {
                                Exception outExc;
                                conn = AllocateConnection(currentHost.Address, out outExc);
                                if (conn != null)
                                {
                                    if (_cluster.Metadata != null)
                                        _cluster.Metadata.BringUpHost(currentHost.Address, this);
                                    pool.Add(conn);
                                }
                                else
                                {
                                    if (!innerExceptions.ContainsKey(currentHost.Address))
                                        innerExceptions.Add(currentHost.Address, new List<Exception>());
                                    innerExceptions[currentHost.Address].Add(outExc);
                                    _logger.Info("New connection attempt failed - goto another host.");
                                    error = true;
                                    break;
                                }
                            }
                            while (pool.Count < _poolingOptions.GetCoreConnectionsPerHost(hostDistance));
                            if (!error)
                                return conn;
                        }
                    }

                    if (!hostsIter.MoveNext())
                    {
                        var ex = new NoHostAvailableException(innerExceptions);
                        _logger.Error("Cannot connect to any host from pool.", ex);
                        throw ex;
                    }
                }
            }
        }
Exemplo n.º 13
0
        internal void Init(bool allock=true)
        {
            if (allock)
            {
                var ci = this._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();
                if (!ci.MoveNext())
                {
                    var ex = new NoHostAvailableException(new Dictionary<IPAddress, List<Exception>>());
                    _logger.Error(ex.Message);
                    throw ex;
                }

                var triedHosts = new List<IPAddress>();
                var innerExceptions = new Dictionary<IPAddress, List<Exception>>();
                int streamId;
                var con = Connect(null, ci, triedHosts, innerExceptions, out streamId);
                con.FreeStreamId(streamId);
            }

            _trashcanCleaner = new Timer(TranscanCleanup, null, Timeout.Infinite, Timeout.Infinite);
        }
Exemplo n.º 14
0
        internal void Init()
        {
            var ci = this._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();
            if (!ci.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary<IPAddress, Exception>());
                _logger.Error(ex.Message);
                throw ex;
            }

            Connect(null, ci);
        }
Exemplo n.º 15
0
        internal CassandraConnection Connect(IEnumerator <Host> hostsIter, List <IPAddress> triedHosts, Dictionary <IPAddress, List <Exception> > innerExceptions, out int streamId)
        {
            CheckDisposed();

            while (true)
            {
                var currentHost = hostsIter.Current;
                if (currentHost == null)
                {
                    var ex = new NoHostAvailableException(innerExceptions);
                    _logger.Error("All hosts are not responding.", ex);
                    throw ex;
                }
                if (currentHost.IsConsiderablyUp)
                {
                    triedHosts.Add(currentHost.Address);
                    var hostDistance = Policies.LoadBalancingPolicy.Distance(currentHost);
RETRY_GET_POOL:
                    if (!_connectionPool.ContainsKey(currentHost.Address))
                    {
                        _connectionPool.TryAdd(currentHost.Address, new ConcurrentDictionary <Guid, CassandraConnection>());
                    }

                    ConcurrentDictionary <Guid, CassandraConnection> pool;

                    if (!_connectionPool.TryGetValue(currentHost.Address, out pool))
                    {
                        goto RETRY_GET_POOL;
                    }

//                    CassandraCounters.SetConnectionsCount(currentHost.Address, pool.Count);
                    foreach (var kv in pool)
                    {
                        CassandraConnection conn = kv.Value;
                        if (!conn.IsHealthy)
                        {
                            CassandraConnection cc;
                            if (pool.TryRemove(conn.Guid, out cc))
                            {
                                FreeConnection(cc);
                            }
                        }
                        else
                        {
                            if (!conn.IsBusy(_poolingOptions.GetMaxSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                            {
                                streamId = conn.AllocateStreamId();
                                return(conn);
                            }
                            else
                            {
                                if (pool.Count > _poolingOptions.GetCoreConnectionsPerHost(hostDistance))
                                {
                                    if (conn.IsFree(_poolingOptions.GetMinSimultaneousRequestsPerConnectionTreshold(hostDistance)))
                                    {
                                        CassandraConnection cc;
                                        if (pool.TryRemove(conn.Guid, out cc))
                                        {
                                            TrashcanPut(cc);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    {
                        var conn = TrashcanRecycle(currentHost.Address);
                        if (conn != null)
                        {
                            if (!conn.IsHealthy)
                            {
                                FreeConnection(conn);
                            }
                            else
                            {
                                pool.TryAdd(conn.Guid, conn);
                                streamId = conn.AllocateStreamId();
                                return(conn);
                            }
                        }
                        // if not recycled
                        {
                            Exception outExc;
                            conn = AllocateConnection(currentHost.Address, hostDistance, out outExc);
                            if (conn != null)
                            {
                                if (_cluster.Metadata != null)
                                {
                                    _cluster.Metadata.BringUpHost(currentHost.Address, this);
                                }
                                pool.TryAdd(conn.Guid, conn);
                                streamId = conn.AllocateStreamId();
                                return(conn);
                            }
                            else
                            {
                                if (!innerExceptions.ContainsKey(currentHost.Address))
                                {
                                    innerExceptions.Add(currentHost.Address, new List <Exception>());
                                }
                                innerExceptions[currentHost.Address].Add(outExc);
                                _logger.Info("New connection attempt failed - goto another host.");
                            }
                        }
                    }
                }

                _logger.Verbose(string.Format("Currently tried host: {0} have all of connections busy. Switching to the next host.", currentHost.Address));

                if (!hostsIter.MoveNext())
                {
                    var ex = new NoHostAvailableException(innerExceptions);
                    _logger.Error("Cannot connect to any host from pool.", ex);
                    throw ex;
                }
            }
        }
        private void SetupEventListener()
        {
            var triedHosts = new List<IPAddress>();
            var innerExceptions = new Dictionary<IPAddress, Exception>();

            var hostsIter = _session._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary<IPAddress, Exception>());
                _logger.Error(ex);
                throw ex;
            }

            var nconn = _session.Connect(null, hostsIter, triedHosts, innerExceptions);
            listeningOnHost = nconn.GetHostAdress();

            Exception theExc = null;

            nconn.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = nconn.RegisterForCassandraEvent(
                CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                        theExc = (ret as OutputError).CreateException();
                    else
                        theExc = new DriverInternalError("Expected Error on Output");
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }