Beispiel #1
0
        void FreeConnection(CassandraConnection connection)
        {
            connection.Dispose();
            AtomicValue <int> val;

            _allocatedConnections.TryGetValue(connection.GetHostAdress(), out val);
            var no = Interlocked.Decrement(ref val.RawValue);
        }
Beispiel #2
0
        void TrashcanPut(CassandraConnection conn)
        {
RETRY:
            if (!_trashcan.ContainsKey(conn.GetHostAdress()))
            {
                _trashcan.TryAdd(conn.GetHostAdress(), new ConcurrentDictionary <Guid, CassandraConnection>());
            }

            ConcurrentDictionary <Guid, CassandraConnection> trashes;

            if (_trashcan.TryGetValue(conn.GetHostAdress(), out trashes))
            {
                trashes.TryAdd(conn.Guid, conn);
            }
            else
            {
                goto RETRY;
            }

            _trashcanCleaner.Change(10000, Timeout.Infinite);
        }
        private void RefreshNodeListAndTokenMap(CassandraConnection connection)
        {
            _logger.Info("Refreshing NodeList and TokenMap..");
            // Make sure we're up to date on nodes and tokens
            var tokenMap = new Dictionary<IPAddress, DictSet<string>>();
            string partitioner = null;

            using (var rowset = _session.Query(SelectLocal, ConsistencyLevel.Default))
            {
                // Update cluster name, DC and rack for the one node we are connected to
                foreach (var localRow in rowset.GetRows())
                {
                    var clusterName = localRow.GetValue<string>("cluster_name");
                    if (clusterName != null)
                        _cluster.Metadata.ClusterName = clusterName;

                    var host = _cluster.Metadata.GetHost(connection.GetHostAdress());
                    // In theory host can't be null. However there is no point in risking a NPE in case we
                    // have a race between a node removal and this.
                    if (host != null)
                    {
                        host.SetLocationInfo(localRow.GetValue<string>("data_center"), localRow.GetValue<string>("rack"));

                        partitioner = localRow.GetValue<string>("partitioner");
                        var tokens = localRow.GetValue<IList<string>>("tokens");
                        if (partitioner != null && tokens.Count > 0)
                        {
                            if (!tokenMap.ContainsKey(host.Address))
                                tokenMap.Add(host.Address, new DictSet<string>());
                            tokenMap[host.Address].AddRange(tokens);
                        }
                    }
                    break;
                }
            }

            var foundHosts = new List<IPAddress>();
            var dcs = new List<string>();
            var racks = new List<string>();
            var allTokens = new List<DictSet<string>>();

            using (var rowset = _session.Query(SelectPeers, ConsistencyLevel.Default))
            {
                foreach (var row in rowset.GetRows())
                {
                    var hstip = row.GetValue<IPEndPoint>("peer").Address;
                    if (hstip != null)
                    {
                        foundHosts.Add(hstip);
                        dcs.Add(row.GetValue<string>("data_center"));
                        racks.Add(row.GetValue<string>("rack"));
                        allTokens.Add(new DictSet<string>(row.GetValue<IEnumerable<string>>("tokens")));
                    }
                }
            }

            for (int i = 0; i < foundHosts.Count; i++)
            {
                var host = _cluster.Metadata.GetHost(foundHosts[i]);
                if (host == null)
                {
                    // We don't know that node, add it.
                    host = _cluster.Metadata.AddHost(foundHosts[i], _session._policies.ReconnectionPolicy);
                }
                host.SetLocationInfo(dcs[i], racks[i]);

                if (partitioner != null && !allTokens[i].IsEmpty)
                    tokenMap.Add(host.Address, allTokens[i]);
            }

            // Removes all those that seems to have been removed (since we lost the control connection)
            var foundHostsSet = new DictSet<IPAddress>(foundHosts);
            foreach (var host in _cluster.Metadata.AllReplicas())
                if (!host.Equals(connection.GetHostAdress()) && !foundHostsSet.Contains(host))
                    _cluster.Metadata.RemoveHost(host);

            if (partitioner != null)
                _cluster.Metadata.RebuildTokenMap(partitioner, tokenMap);
            _logger.Info("NodeList and TokenMap have been successfully refreshed!");
        }
Beispiel #4
0
        void TrashcanPut(CassandraConnection conn)
        {
            RETRY:
            if (!_trashcan.ContainsKey(conn.GetHostAdress()))
                _trashcan.TryAdd(conn.GetHostAdress(), new ConcurrentDictionary<Guid, CassandraConnection>());

            ConcurrentDictionary<Guid, CassandraConnection> trashes;
            if (_trashcan.TryGetValue(conn.GetHostAdress(), out trashes))
                trashes.TryAdd(conn.Guid,conn);
            else
                goto RETRY;

            _trashcanCleaner.Change(10000, Timeout.Infinite);
        }
Beispiel #5
0
 void FreeConnection(CassandraConnection connection)
 {
     connection.Dispose();
     AtomicValue<int> val;
     _allocatedConnections.TryGetValue(connection.GetHostAdress(), out val);
     var no = Interlocked.Decrement(ref val.RawValue);
 }