internal ClosestToLineQuery(
     double maxDistance, double maxDistancePlusEps,
     int maxCount, List <IndexDist <double> > list)
     : base(maxDistance, maxDistancePlusEps, maxCount, list)
 {
     IndexSet = null;
 }
 public override void Clear()
 {
     base.Clear();
     IndexSet = new DictSet <long>();
 }
Exemple #3
0
        public static TokenMap Build(String partitioner, Dictionary<IPAddress, DictSet<string>> allTokens)
        {
            TokenFactory factory = TokenFactory.GetFactory(partitioner);
            if (factory == null)
                return null;

            Dictionary<IToken, DictSet<IPAddress>> tokenToCassandraClusterHosts = new Dictionary<IToken, DictSet<IPAddress>>();
            DictSet<IToken> allSorted = new DictSet<IToken>();

            foreach (var entry in allTokens)
            {
                var cassandraClusterHost = entry.Key;
                foreach (string tokenStr in entry.Value)
                {
                    try
                    {
                        IToken t = factory.Parse(tokenStr);
                        allSorted.Add(t);
                        if (!tokenToCassandraClusterHosts.ContainsKey(t))
                            tokenToCassandraClusterHosts.Add(t, new DictSet<IPAddress>());
                        tokenToCassandraClusterHosts[t].Add(cassandraClusterHost);
                    }
                    catch (ArgumentException)
                    {
                        // If we failed parsing that token, skip it
                    }
                }
            }
            return new TokenMap(factory, tokenToCassandraClusterHosts, new List<IToken>(allSorted));
        }
        /// <summary>
        ///  Returns the hosts to use for a new query. <p> The returned plan will first
        ///  return replicas (whose <code>HostDistance</code> for the child policy is
        ///  <code>Local</code>) for the query if it can determine them (i.e. mainly if
        ///  <code>query.getRoutingKey()</code> is not <code>null</code>). Following what
        ///  it will return the plan of the child policy.</p>
        /// </summary>
        /// <param name="query"> the query for which to build the plan. </param>
        /// 
        /// <returns>the new query plan.</returns>
        public IEnumerable<Host> NewQueryPlan(Query query)
        {
            var routingKey = query == null ? null : query.RoutingKey;
            if (routingKey == null)
            {
                foreach (var iter in _childPolicy.NewQueryPlan(null))
                    yield return iter;
                yield break;
            }

            var replicas = _cluster.Metadata.GetReplicas(routingKey.RawRoutingKey);
            if (replicas.Count == 0)
            {
                foreach (var iter in _childPolicy.NewQueryPlan(query))
                    yield return iter;
                yield break;
            }

            var iterator = replicas.GetEnumerator();
            while (iterator.MoveNext())
            {
                var host = _cluster.Metadata.GetHost(iterator.Current);
                if (host != null && host.IsConsiderablyUp && _childPolicy.Distance(host) == HostDistance.Local)
                    yield return host;
            }

            var childIterator = _childPolicy.NewQueryPlan(query);
            var remoteChildren = new DictSet<Host>();
            foreach (var host in childIterator)
            {
                if (!replicas.Contains(host.Address))
                {
                    if (_childPolicy.Distance(host) != HostDistance.Local)
                        remoteChildren.Add(host);
                    else
                        yield return host;
                }
            }

            foreach (var host in remoteChildren)
            {
                yield return host;
            }
        }
        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!");
        }
        internal bool WaitForSchemaAgreement()
        {
            var start = DateTimeOffset.Now;
            long elapsed = 0;
            while (elapsed < MaxSchemaAgreementWaitMs)
            {
                var versions = new DictSet<Guid>();

                using (var rowset = _session.Query(SelectSchemaLocal, ConsistencyLevel.Default))
                {
                    foreach (var localRow in rowset.GetRows())
                        if (!localRow.IsNull("schema_version"))
                        {
                            versions.Add(localRow.GetValue<Guid>("schema_version"));
                            break;
                        }
                }

                using (var rowset = _session.Query(SelectSchemaPeers, ConsistencyLevel.Default))
                {
                    foreach (var row in rowset.GetRows())
                    {
                        if (row.IsNull("peer") || row.IsNull("schema_version"))
                            continue;

                        Host peer = _cluster.Metadata.GetHost(row.GetValue<IPEndPoint>("peer").Address);
                        if (peer != null && peer.IsConsiderablyUp)
                            versions.Add(row.GetValue<Guid>("schema_version"));
                    }
                }

                if (versions.Count <= 1)
                    return true;

                // let's not flood the node too much
                Thread.Sleep(200);
                elapsed = (long) (DateTimeOffset.Now - start).TotalMilliseconds;
            }

            return false;
        }
        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, DictSet<string>>();
            string partitioner = null;

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

            using (var rowset = _session.Query(SelectPeers, ConsistencyLevel.Quorum))
            {
                queriedHost = rowset.Info.QueriedHost;
                foreach (var row in rowset.GetRows())
                {
                    IPAddress hstip = null;
                    if(!row.IsNull("rpc_address"))
                         hstip = row.GetValue<IPEndPoint>("rpc_address").Address;
                    if (hstip == null)
                    {
                        if (!row.IsNull("peer"))
                            hstip = row.GetValue<IPEndPoint>("peer").Address;
                        _logger.Error("No rpc_address found for host in peers system table. ");
                    }
                    else if (hstip.Equals(bindAllAddress))
                    {
                        if (!row.IsNull("peer"))
                            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"));
                        var col = row.GetValue<IEnumerable<string>>("tokens");
                        if (col == null)
                            allTokens.Add(new DictSet<string>());
                        else
                            allTokens.Add(new DictSet<string>(col));
                    }
                }
            }

            var localhost = _cluster.Metadata.GetHost(queriedHost);
            var iterLiof = new List<Host>() { localhost }.GetEnumerator();
            iterLiof.MoveNext();
            List<IPAddress> tr = new List<IPAddress>();
            Dictionary<IPAddress, List<Exception>> exx = new Dictionary<IPAddress, List<Exception>>();
            var cn = _session.Connect(null, iterLiof, tr, exx);

            using (var outp = cn.Query(SelectLocal, ConsistencyLevel.Default,false))
            {
                if (outp is OutputRows)
                {
                    var rowset = new RowSet((outp as OutputRows), null, false);
                    // 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;

                        // 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 DictSet<string>());
                                tokenMap[localhost.Address].AddRange(tokens);
                            }
                        }

                        break; //fetch only one row
                    }
                }
            }

            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]);
                }
                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(queriedHost) && !foundHostsSet.Contains(host))
                    _cluster.Metadata.RemoveHost(host);

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

            _logger.Info("NodeList and TokenMap have been successfully refreshed!");
        }
        internal bool WaitForSchemaAgreement(IPAddress forHost = null)
        {
            var start = DateTimeOffset.Now;
            long elapsed = 0;
            while (elapsed < MaxSchemaAgreementWaitMs)
            {
                var versions = new DictSet<Guid>();

                IPAddress queriedHost;

                if (forHost == null)
                {
                    using (var rowset = _session.Query(SelectSchemaPeers, ConsistencyLevel.Default))
                    {
                        queriedHost = rowset.Info.QueriedHost;
                        foreach (var row in rowset.GetRows())
                        {
                            if (row.IsNull("rpc_address") || row.IsNull("schema_version"))
                                continue;

                            var rpc = row.GetValue<IPEndPoint>("rpc_address").Address;
                            if (rpc.Equals(bindAllAddress))
                                if (!row.IsNull("peer"))
                                    rpc = row.GetValue<IPEndPoint>("peer").Address;

                            Host peer = _cluster.Metadata.GetHost(rpc);
                            if (peer != null && peer.IsConsiderablyUp)
                                versions.Add(row.GetValue<Guid>("schema_version"));
                        }
                    }
                }
                else
                {
                    queriedHost = forHost;
                    var localhost = _cluster.Metadata.GetHost(queriedHost);
                    var iterLiof = new List<Host>() { localhost }.GetEnumerator();
                    iterLiof.MoveNext();
                    List<IPAddress> tr = new List<IPAddress>();
                    Dictionary<IPAddress, List<Exception>> exx = new Dictionary<IPAddress, List<Exception>>();
                    var cn = _session.Connect(null, iterLiof, tr, exx);

                    using (var outp = cn.Query(SelectSchemaPeers, ConsistencyLevel.Default, false))
                    {
                        if (outp is OutputRows)
                        {
                            var rowset = new RowSet((outp as OutputRows), null, false);
                            foreach (var row in rowset.GetRows())
                            {
                                if (row.IsNull("rpc_address") || row.IsNull("schema_version"))
                                    continue;

                                var rpc = row.GetValue<IPEndPoint>("rpc_address").Address;
                                if (rpc.Equals(bindAllAddress))
                                {
                                    if (!row.IsNull("peer"))
                                        rpc = row.GetValue<IPEndPoint>("peer").Address;
                                }

                                Host peer = _cluster.Metadata.GetHost(rpc);
                                if (peer != null && peer.IsConsiderablyUp)
                                    versions.Add(row.GetValue<Guid>("schema_version"));
                            }
                        }
                    }
                }

                {
                    var localhost = _cluster.Metadata.GetHost(queriedHost);
                    var iterLiof = new List<Host>() { localhost }.GetEnumerator();
                    iterLiof.MoveNext();
                    List<IPAddress> tr = new List<IPAddress>();
                    Dictionary<IPAddress, List<Exception>> exx = new Dictionary<IPAddress, List<Exception>>();
                    var cn = _session.Connect(null, iterLiof, tr, exx);

                    using (var outp = cn.Query(SelectSchemaLocal, ConsistencyLevel.Default, false))
                    {
                        if (outp is OutputRows)
                        {
                            var rowset = new RowSet((outp as OutputRows), null, false);
                            // Update cluster name, DC and rack for the one node we are connected to
                            foreach (var localRow in rowset.GetRows())
                                if (!localRow.IsNull("schema_version"))
                                {
                                    versions.Add(localRow.GetValue<Guid>("schema_version"));
                                    break;
                                }
                        }
                    }
                }

                if (versions.Count <= 1)
                    return true;

                // let's not flood the node too much
                Thread.Sleep(200);
                elapsed = (long)(DateTimeOffset.Now - start).TotalMilliseconds;
            }

            return false;
        }
        public SymMapBase Traverse(SymMapBase map)
        {
            // choose visitor
            SymMapBaseVisitor visitor = OnDefaultVisit;

            if (map.TypeName != null && m_perTypenameVisitors.ContainsKey(map.TypeName))
            {
                visitor = m_perTypenameVisitors[map.TypeName];
            }

            // pre visit
            if ((m_visit & Visit.Pre) != 0)
            {
                if (visitor != null)
                {
                    if (m_mode == Mode.Modifying)
                    {
                        map = visitor(map, Visit.Pre);
                    }
                    else
                    {
                        SymMapBase tmp = visitor(map, Visit.Pre);
                        if (tmp != map)
                        {
                            throw new ArgumentException(
                                      "The node returned by a non-modifying pre-visit is " +
                                      "different from the node that has been visited. " +
                                      "This makes no sense!"
                                      );
                        }
                    }
                }
            }

            // traverse children
            if (m_mode == Mode.Modifying)
            {
                SymbolDict <object> tmp = new SymbolDict <object>();
                foreach (var e in map.MapItems)
                {
                    tmp[e.Key] = e.Value;
                }

                foreach (var e in tmp)
                {
                    object o = e.Value;
                    if (o is SymMapBase)
                    {
                        map[e.Key] = Traverse((SymMapBase)o);
                    }
                    else if (o is SymMapBase[])
                    {
                        SymMapBase[] array = (SymMapBase[])o;
                        for (int i = 0; i < array.Length; i++)
                        {
                            array[i] = Traverse(array[i]);
                        }
                    }
                    else if (o is List <SymMapBase> )
                    {
                        List <SymMapBase> list = (List <SymMapBase>)o;
                        for (int i = 0; i < list.Count; i++)
                        {
                            list[i] = Traverse(list[i]);
                        }
                    }
                    else if (o is Dict <string, SymMapBase> )
                    {
                        var dict = new Dict <string, SymMapBase>();
                        foreach (var kvp in (Dict <string, SymMapBase>)o)
                        {
                            dict[kvp.Key] = Traverse(kvp.Value);
                        }
                        map[e.Key] = dict;
                    }
                    else if (o is SymbolDict <SymMapBase> )
                    {
                        var dict = new SymbolDict <SymMapBase>();
                        foreach (var kvp in (SymbolDict <SymMapBase>)o)
                        {
                            dict[kvp.Key] = Traverse(kvp.Value);
                        }
                        map[e.Key] = dict;
                    }
                    else if (o is DictSet <SymMapBase> )
                    {
                        var set = new DictSet <SymMapBase>();
                        foreach (var m in (DictSet <SymMapBase>)o)
                        {
                            set.Add(Traverse(m));
                        }
                        map[e.Key] = set;
                    }
                }
            }
            else
            {
                foreach (var e in map.MapItems)
                {
                    object o = e.Value;
                    if (o is SymMapBase)
                    {
                        Traverse((SymMapBase)o);
                    }
                    else if (o is SymMapBase[])
                    {
                        foreach (var m in (SymMapBase[])o)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is List <SymMapBase> )
                    {
                        foreach (var m in (List <SymMapBase>)o)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is Dict <string, SymMapBase> )
                    {
                        foreach (var m in ((Dict <string, SymMapBase>)o).Values)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is SymbolDict <SymMapBase> )
                    {
                        foreach (var m in ((SymbolDict <SymMapBase>)o).Values)
                        {
                            Traverse(m);
                        }
                    }
                    else if (o is DictSet <SymMapBase> )
                    {
                        foreach (var m in (DictSet <SymMapBase>)o)
                        {
                            Traverse(m);
                        }
                    }
                }
            }

            // post visit
            if ((m_visit & Visit.Post) != 0)
            {
                if (visitor != null)
                {
                    if (m_mode == Mode.Modifying)
                    {
                        map = visitor(map, Visit.Post);
                    }
                    else
                    {
                        var tmp = visitor(map, Visit.Post);
                        if (tmp != map)
                        {
                            throw new ArgumentException(
                                      "The node returned by a non-modifying post-visit is " +
                                      "different from the node that has been visited. " +
                                      "This makes no sense!"
                                      );
                        }
                    }
                }
            }

            return(map);
        }