Пример #1
0
 override public void Complete(Session owner, object value, Exception exc = null)
 {
     try
     {
         var ar = LongActionAc as AsyncResult <CqlRowSet>;
         if (exc != null)
         {
             ar.Complete(exc);
         }
         else
         {
             CqlRowSet rowset = value as CqlRowSet;
             if (rowset == null)
             {
                 rowset = new CqlRowSet(null, owner, false);
             }
             rowset.SetTriedHosts(TriedHosts);
             ar.SetResult(rowset);
             ar.Complete();
         }
     }
     finally
     {
         var ts = StartedAt.ElapsedTicks;
         CassandraCounters.IncrementCqlQueryCount();
         CassandraCounters.IncrementCqlQueryBeats((ts * 1000000000));
         CassandraCounters.UpdateQueryTimeRollingAvrg((ts * 1000000000) / Stopwatch.Frequency);
         CassandraCounters.IncrementCqlQueryBeatsBase();
     }
 }
Пример #2
0
 internal CqlReader(CqlRowSet rows)
 {
     this.popul = rows;
     for (int idx = 0; idx < popul.Columns.Length; idx++)
         colidx.Add(popul.Columns[idx].Name, idx);
     enumRows = popul.GetRows();
     enumerRows = enumRows.GetEnumerator();
 }
 public void PrintResults(CqlRowSet results)
 {
     Console.WriteLine(String.Format("{0, -30}\t{1, -20}\t{2, -20}\t{3, -30}",
         "title", "album", "artist", "tags"));
     Console.WriteLine("-------------------------------+-----------------------+--------------------+-------------------------------");
     foreach (CqlRow row in results.GetRows())
     {
         Console.WriteLine(String.Format("{0, -30}\t{1, -20}\t{2, -20}\t{3}",
             row.GetValue<String>("title"), row.GetValue<String>("album"),
             row.GetValue<String>("artist"), Prettify( row.GetValue<List<String>>("tags")
             ) ));
     }
 }
 private static void DisplayKeyspace(CqlRowSet result)
 {
     try
     {
         foreach (var resKeyspace in result.GetRows())
         {
             Console.WriteLine("durable_writes={0} keyspace_name={1} strategy_Class={2} strategy_options={3}",
                               resKeyspace.GetValue<bool>("durable_writes"),
                               resKeyspace.GetValue<string>("keyspace_name"),
                               resKeyspace.GetValue<string>("strategy_class"),
                               resKeyspace.GetValue<string>("strategy_options"));
         }
         Console.WriteLine();
     }
     catch (Exception ex)
     {
         Console.WriteLine("Command failed {0}", ex.Message);
     }
 }
        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.QueriedHost;
                        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"));
                            }
                        }
                    }
                }
                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, Exception> exx = new Dictionary <IPAddress, 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 CqlRowSet((outp as OutputRows), null, false);
                            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"));
                                }
                            }
                        }
                    }
                }

                {
                    var localhost = _cluster.Metadata.GetHost(queriedHost);
                    var iterLiof  = new List <Host>()
                    {
                        localhost
                    }.GetEnumerator();
                    iterLiof.MoveNext();
                    List <IPAddress> tr = new List <IPAddress>();
                    Dictionary <IPAddress, Exception> exx = new Dictionary <IPAddress, 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 CqlRowSet((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);
        }
        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.QueriedHost;
                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"));
                        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, Exception> exx = new Dictionary <IPAddress, 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 CqlRowSet((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!");
        }
Пример #7
0
 public void valueComparator(CqlRowSet rowset, List<object[]> insertedRows)
 {
     Assert.True(rowset.RowsCount == insertedRows.Count, string.Format("Returned rows count is not equal with the count of rows that were inserted! \n Returned: {0} \n Expected: {1} \n", rowset.RowsCount, insertedRows.Count));
     int i = 0;
     foreach (var row in rowset.GetRows())
     {
         if (row.Columns.Any(col => col.GetType() == typeof(byte[])))
             for (int j = 0; j < row.Length; j++)
                 Assert.True(row[j].GetType() == typeof(byte[]) ? ArrEqual((byte[])row[j], (byte[])insertedRows[i][j]) : row[j].Equals(insertedRows[i][j]));
         else
         {
             for (int m = 0; m < row.Columns.Length; m++)
             {
                 if (!row.Columns[m].Equals(insertedRows[i][m]))
                 {
                     insertedRows.Reverse();// To check if needed and why
                     if (!row.Columns[m].Equals(insertedRows[i][m]))
                         insertedRows.Reverse();
                 }
                 Assert.True(row.Columns[m].Equals(insertedRows[i][m]), "Inserted data !Equals with returned data.");
             }
         }
         i++;
     }
 }
Пример #8
0
 public override void Complete(Session owner, object value, Exception exc = null)
 {
     try
     {
         var ar = LongActionAc as AsyncResult<CqlRowSet>;
         if (exc != null)
             ar.Complete(exc);
         else
         {
             CqlRowSet rowset = value as CqlRowSet;
             if(rowset==null)
                 rowset = new CqlRowSet(null,owner,false);
             rowset.SetTriedHosts(TriedHosts);
             ar.SetResult(rowset);
             ar.Complete();
         }
     }
     finally
     {
         var ts = StartedAt.ElapsedTicks;
         CassandraCounters.IncrementCqlQueryCount();
         CassandraCounters.IncrementCqlQueryBeats((ts * 1000000000));
         CassandraCounters.UpdateQueryTimeRollingAvrg((ts * 1000000000) / Stopwatch.Frequency);
         CassandraCounters.IncrementCqlQueryBeatsBase();
     }
 }
        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.QueriedHost;
                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"));
                        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, Exception> exx = new Dictionary<IPAddress, 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 CqlRowSet((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.QueriedHost;
                        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"));
                        }
                    }
                }
                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, Exception> exx = new Dictionary<IPAddress, 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 CqlRowSet((outp as OutputRows), null, false);
                            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"));
                            }
                        }
                    }
                }

                {
                    var localhost = _cluster.Metadata.GetHost(queriedHost);
                    var iterLiof = new List<Host>() { localhost }.GetEnumerator();
                    iterLiof.MoveNext();
                    List<IPAddress> tr = new List<IPAddress>();
                    Dictionary<IPAddress, Exception> exx = new Dictionary<IPAddress, 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 CqlRowSet((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;
        }