Beispiel #1
0
        /// <summary>
        /// Waits until that the schema version in all nodes is the same or the waiting time passed.
        /// This method blocks the calling thread.
        /// </summary>
        internal void WaitForSchemaAgreement(Connection connection)
        {
            if (Hosts.Count == 1)
            {
                //If there is just one node, the schema is up to date in all nodes :)
                return;
            }
            var start       = DateTime.Now;
            var waitSeconds = _config.ProtocolOptions.MaxSchemaAgreementWaitSeconds;

            Logger.Info("Waiting for schema agreement");
            try
            {
                var totalVersions = 0;
                while (DateTime.Now.Subtract(start).TotalSeconds < waitSeconds)
                {
                    var schemaVersionLocalQuery = new QueryRequest(connection.ProtocolVersion, SelectSchemaVersionLocal, false, QueryProtocolOptions.Default);
                    var schemaVersionPeersQuery = new QueryRequest(connection.ProtocolVersion, SelectSchemaVersionPeers, false, QueryProtocolOptions.Default);
                    var queries = new [] { connection.Send(schemaVersionLocalQuery), connection.Send(schemaVersionPeersQuery) };
                    // ReSharper disable once CoVariantArrayConversion
                    Task.WaitAll(queries, _config.ClientOptions.QueryAbortTimeout);
                    var versions = new HashSet <Guid>
                    {
                        ControlConnection.GetRowSet(queries[0].Result).First().GetValue <Guid>("schema_version")
                    };
                    var peerVersions = ControlConnection.GetRowSet(queries[1].Result).Select(r => r.GetValue <Guid>("schema_version"));
                    foreach (var v in peerVersions)
                    {
                        versions.Add(v);
                    }
                    totalVersions = versions.Count;
                    if (versions.Count == 1)
                    {
                        return;
                    }
                    Thread.Sleep(500);
                }
                Logger.Info(String.Format("Waited for schema agreement, still {0} schema versions in the cluster.", totalVersions));
            }
            catch (Exception ex)
            {
                //Exceptions are not fatal
                Logger.Error("There was an exception while trying to retrieve schema versions", ex);
            }
        }