Beispiel #1
0
        // schema

        private void RefreshNodeListAndTokenMap()
        {
            _logger.Info("Refreshing NodeList and TokenMap..");
            // Make sure we're up to date on nodes and tokens
            var    tokenMap    = new Dictionary <IPAddress, HashSet <string> >();
            string partitioner = null;

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

            {
                int sessionId = _activeConnection.Value.AllocateStreamId();
                var rowset    = ProcessRowset(
                    _activeConnection.Value.Query(
                        sessionId, SelectPeers, false, QueryProtocolOptions.Default, _cluster.Configuration.QueryOptions.GetConsistencyLevel()), SelectPeers);

                foreach (Row row in rowset.GetRows())
                {
                    IPAddress hstip = null;
                    if (!row.IsNull("rpc_address"))
                    {
                        hstip = row.GetValue <IPAddress>("rpc_address");
                    }
                    if (hstip == null)
                    {
                        if (!row.IsNull("peer"))
                        {
                            hstip = row.GetValue <IPAddress>("peer");
                        }
                        _logger.Error("No rpc_address found for host in peers system table. ");
                    }
                    else if (hstip.Equals(Session.BindAllAddress))
                    {
                        if (!row.IsNull("peer"))
                        {
                            hstip = row.GetValue <IPAddress>("peer");
                        }
                    }

                    if (hstip != null)
                    {
                        foundHosts.Add(hstip);
                        dcs.Add(row.GetValue <string>("data_center"));
                        racks.Add(row.GetValue <string>("rack"));
                        var col = row.GetValue <IEnumerable <string> >("tokens");
                        if (col == null)
                        {
                            allTokens.Add(new HashSet <string>());
                        }
                        else
                        {
                            allTokens.Add(new HashSet <string>(col));
                        }
                    }
                }
            }
            {
                int  streamId  = _activeConnection.Value.AllocateStreamId();
                Host localhost = _cluster.Metadata.GetHost(_activeConnection.Value.GetHostAdress());

                var rowset = ProcessRowset(
                    _activeConnection.Value.Query(
                        streamId, SelectLocal, false, QueryProtocolOptions.Default, _cluster.Configuration.QueryOptions.GetConsistencyLevel()), SelectLocal);
                // Update cluster name, DC and rack for the one node we are connected to
                foreach (Row localRow in rowset.GetRows())
                {
                    var clusterName = localRow.GetValue <string>("cluster_name");
                    if (clusterName != null)
                    {
                        _cluster.Metadata.ClusterName = clusterName;
                    }

                    // 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 (localhost != null)
                    {
                        localhost.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(localhost.Address))
                            {
                                tokenMap.Add(localhost.Address, new HashSet <string>());
                            }
                            tokenMap[localhost.Address].UnionWith(tokens);
                        }
                    }

                    break; //fetch only one row
                }
            }

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

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

            // Removes all those that seems to have been removed (since we lost the control connection)
            var foundHostsSet = new HashSet <IPAddress>(foundHosts);

            foreach (IPAddress host in _cluster.Metadata.AllReplicas())
            {
                if (!host.Equals(_activeConnection.Value.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!");
        }
        // schema

        private void RefreshNodeListAndTokenMap()
        {
            _logger.Info("Refreshing NodeList and TokenMap..");
            // Make sure we're up to date on nodes and tokens
            var    tokenMap    = new Dictionary <IPEndPoint, HashSet <string> >();
            string partitioner = null;

            var foundHosts = new List <IPEndPoint>();
            var dcs        = new List <string>();
            var racks      = new List <string>();
            var allTokens  = new List <HashSet <string> >();

            {
                var rowset = Query(SelectPeers);

                foreach (Row row in rowset.GetRows())
                {
                    IPAddress address = null;
                    if (!row.IsNull("rpc_address"))
                    {
                        address = row.GetValue <IPAddress>("rpc_address");
                    }
                    if (address == null)
                    {
                        if (!row.IsNull("peer"))
                        {
                            address = row.GetValue <IPAddress>("peer");
                        }
                        _logger.Error("No rpc_address found for host in peers system table. ");
                    }
                    else if (address.Equals(BindAllAddress))
                    {
                        if (!row.IsNull("peer"))
                        {
                            address = row.GetValue <IPAddress>("peer");
                        }
                    }

                    if (address != null)
                    {
                        // Translate discovered node address
                        var originalAddress   = new IPEndPoint(address, ProtocolOptions.DefaultPort);
                        var translatedAddress = TranslateAddress(originalAddress);

                        foundHosts.Add(translatedAddress);
                        dcs.Add(row.GetValue <string>("data_center"));
                        racks.Add(row.GetValue <string>("rack"));
                        var col = row.GetValue <IEnumerable <string> >("tokens");
                        if (col == null)
                        {
                            allTokens.Add(new HashSet <string>());
                        }
                        else
                        {
                            allTokens.Add(new HashSet <string>(col));
                        }
                    }
                }
            }
            {
                Host localhost = _cluster.Metadata.GetHost(_activeConnection.Value.Address);

                var rowset = Query(SelectLocal);
                // Update cluster name, DC and rack for the one node we are connected to
                var localRow    = rowset.First();
                var clusterName = localRow.GetValue <string>("cluster_name");
                if (clusterName != null)
                {
                    _cluster.Metadata.ClusterName = clusterName;
                }
                int protocolVersion;
                if (rowset.Columns.Any(c => c.Name == "native_protocol_version") &&
                    Int32.TryParse(localRow.GetValue <string>("native_protocol_version"), out protocolVersion))
                {
                    //In Cassandra < 2
                    //  there is no native protocol version column, it will get the default value
                    _protocolVersion = protocolVersion;
                }
                // 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 (localhost != null)
                {
                    localhost.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(localhost.Address))
                        {
                            tokenMap.Add(localhost.Address, new HashSet <string>());
                        }
                        tokenMap[localhost.Address].UnionWith(tokens);
                    }
                }
            }

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

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

            // Removes all those that seems to have been removed (since we lost the control connection)
            var foundHostsSet = new HashSet <IPEndPoint>(foundHosts);

            foreach (IPEndPoint host in _cluster.Metadata.AllReplicas())
            {
                if (!host.Equals(_activeConnection.Value.Address) && !foundHostsSet.Contains(host))
                {
                    _cluster.Metadata.RemoveHost(host);
                }
            }

            if (partitioner != null)
            {
                _cluster.Metadata.RebuildTokenMap(partitioner, tokenMap);
            }

            _logger.Info("NodeList and TokenMap have been successfully refreshed!");
        }