Exemple #1
0
        /// <summary>
        /// Initiates a schema agreement check.
        /// <para/>
        /// Schema changes need to be propagated to all nodes in the cluster.
        /// Once they have settled on a common version, we say that they are in agreement.
        /// <para/>
        /// This method does not perform retries so
        /// <see cref="ProtocolOptions.MaxSchemaAgreementWaitSeconds"/> does not apply.
        /// </summary>
        /// <returns>True if schema agreement was successful and false if it was not successful.</returns>
        public async Task <bool> CheckSchemaAgreementAsync()
        {
            if (Hosts.Count == 1)
            {
                // If there is just one node, the schema is up to date in all nodes :)
                return(true);
            }

            try
            {
                var queries = new[]
                {
                    ControlConnection.QueryAsync(SelectSchemaVersionLocal),
                    ControlConnection.QueryAsync(SelectSchemaVersionPeers)
                };

                await Task.WhenAll(queries).ConfigureAwait(false);

                return(CheckSchemaVersionResults(queries[0].Result, queries[1].Result));
            }
            catch (Exception ex)
            {
                Logger.Error("Error while checking schema agreement.", ex);
            }

            return(false);
        }
        /// <summary>
        /// Refresh the partition map for all tables.
        /// </summary>
        internal async Task RefreshPartitionMap()
        {
            if (!Configuration.Policies.LoadBalancingPolicy.RequiresPartitionMap)
            {
                return;
            }
            var addressToHosts = new Dictionary <IPAddress, Host>();

            foreach (var host in AllHosts())
            {
                addressToHosts.Add(host.Address.Address, host);
            }
            var rows = await ControlConnection.QueryAsync(SelectPartitions);

            var splitsSource = new Dictionary <string, List <PartitionMetadata> >();

            foreach (var row in rows)
            {
                var keyspace      = row.GetValue <string>("keyspace_name");
                var tableName     = row.GetValue <string>("table_name");
                var fullTableName = keyspace + "." + tableName;
                var replicas      = row.GetValue <IDictionary <IPAddress, string> >("replica_addresses");
                var hosts         = new List <Host>(replicas.Count);
                foreach (var entry in replicas)
                {
                    Host host;
                    if (!addressToHosts.TryGetValue(entry.Key, out host))
                    {
                        continue;
                    }
                    var role = entry.Value;
                    if (role == "LEADER")
                    {
                        hosts.Insert(0, host);
                    }
                    else if (role == "READ_REPLICA" || role == "FOLLOWER")
                    {
                        hosts.Add(host);
                    }
                }

                var startKey = BytesToHashCode(row.GetValue <byte[]>("start_key"));
                var endKey   = BytesToHashCode(row.GetValue <byte[]>("end_key"));
                List <PartitionMetadata> partitions;
                if (!splitsSource.TryGetValue(fullTableName, out partitions))
                {
                    partitions = new List <PartitionMetadata>();
                    splitsSource.Add(fullTableName, partitions);
                }
                partitions.Add(new PartitionMetadata(startKey, endKey, hosts));
            }
            var splits = new Dictionary <string, TableSplitMetadata>();

            foreach (var entry in splitsSource)
            {
                splits.Add(entry.Key, new TableSplitMetadata(entry.Value));
            }
            TableSplitMetadata = splits;
        }