Beispiel #1
0
        internal async Task <bool> RebuildTokenMapAsync(bool retry)
        {
            var currentCounter = Interlocked.Read(ref _counterRebuild);

            Metadata.Logger.Info("Retrieving keyspaces metadata");
            // running this statement synchronously inside the exclusive scheduler deadlocks
            var ksList = await _schemaParser.GetKeyspaces(retry).ConfigureAwait(false);

            var task = _tokenMapTaskFactory.StartNew(() =>
            {
                if (!RebuildNecessary(currentCounter))
                {
                    return(true);
                }

                Metadata.Logger.Info("Updating keyspaces metadata");
                var ksMap  = ksList.Select(ks => new KeyValuePair <string, KeyspaceMetadata>(ks.Name, ks));
                _keyspaces = new ConcurrentDictionary <string, KeyspaceMetadata>(ksMap);
                Metadata.Logger.Info("Rebuilding token map");
                if (Partitioner == null)
                {
                    throw new DriverInternalError("Partitioner can not be null");
                }

                _tokenMap = TokenMap.Build(Partitioner, Hosts.ToCollection(), _keyspaces.Values);
                return(true);
            });

            return(await task.ConfigureAwait(false));
        }
Beispiel #2
0
        public static TokenMap Build(string partitioner, ICollection <Host> hosts, ICollection <KeyspaceMetadata> keyspaces)
        {
            var sw = new Stopwatch();

            sw.Start();
            var factory = TokenFactory.GetFactory(partitioner);

            if (factory == null)
            {
                return(null);
            }

            var primaryReplicas = new Dictionary <IToken, Host>();
            var allSorted       = new SortedSet <IToken>();
            var datacenters     = new Dictionary <string, DatacenterInfo>();

            foreach (var host in hosts)
            {
                if (host.Datacenter != null)
                {
                    if (!datacenters.TryGetValue(host.Datacenter, out var dc))
                    {
                        datacenters[host.Datacenter] = dc = new DatacenterInfo();
                    }
                    dc.HostLength++;
                    dc.AddRack(host.Rack);
                }
                foreach (var tokenStr in host.Tokens)
                {
                    try
                    {
                        var token = factory.Parse(tokenStr);
                        allSorted.Add(token);
                        primaryReplicas[token] = host;
                    }
                    catch (Exception ex)
                    {
                        TokenMap.Logger.Error($"Token {tokenStr} could not be parsed using {partitioner} partitioner implementation. Exception: {ex}");
                    }
                }
            }
            var ring          = new List <IToken>(allSorted);
            var tokenToHosts  = new Dictionary <string, IReadOnlyDictionary <IToken, ISet <Host> > >(keyspaces.Count);
            var ksTokensCache = new Dictionary <IReplicationStrategy, IReadOnlyDictionary <IToken, ISet <Host> > >();
            //Only consider nodes that have tokens
            var numberOfHostsWithTokens = hosts.Count(h => h.Tokens.Any());

            foreach (var ks in keyspaces)
            {
                TokenMap.UpdateKeyspace(ks, tokenToHosts, ring, primaryReplicas, ksTokensCache, datacenters, numberOfHostsWithTokens);
            }

            sw.Stop();
            TokenMap.Logger.Info(
                "Finished building TokenMap for {0} keyspaces and {1} hosts. It took {2:0} milliseconds.",
                keyspaces.Count,
                hosts.Count,
                sw.Elapsed.TotalMilliseconds);
            return(new TokenMap(factory, tokenToHosts, ring, primaryReplicas, ksTokensCache, datacenters, numberOfHostsWithTokens));
        }
Beispiel #3
0
        internal async Task RebuildTokenMapAsync(bool retry, bool fetchKeyspaces)
        {
            IEnumerable <KeyspaceMetadata> ksList = null;

            if (fetchKeyspaces)
            {
                Metadata.Logger.Info("Retrieving keyspaces metadata");
                ksList = await _schemaParser.GetKeyspacesAsync(retry).ConfigureAwait(false);
            }

            ConcurrentDictionary <string, KeyspaceMetadata> keyspaces;

            if (ksList != null)
            {
                Metadata.Logger.Info("Updating keyspaces metadata");
                var ksMap = ksList.Select(ks => new KeyValuePair <string, KeyspaceMetadata>(ks.Name, ks));
                keyspaces = new ConcurrentDictionary <string, KeyspaceMetadata>(ksMap);
            }
            else
            {
                keyspaces = _keyspaces;
            }

            Metadata.Logger.Info("Rebuilding token map");
            if (Partitioner == null)
            {
                throw new DriverInternalError("Partitioner can not be null");
            }

            var tokenMap = TokenMap.Build(Partitioner, Hosts.ToCollection(), keyspaces.Values);

            _keyspaces = keyspaces;
            _tokenMap  = tokenMap;
        }
Beispiel #4
0
 internal void RebuildTokenMap()
 {
     Logger.Info("Rebuilding token map");
     if (Partitioner == null)
     {
         throw new DriverInternalError("Partitioner can not be null");
     }
     _tokenMap = TokenMap.Build(Partitioner, Hosts.ToCollection(), _keyspaces.Values);
 }
 internal void RebuildTokenMap()
 {
     if (!Configuration.Policies.LoadBalancingPolicy.RequiresTokenMap)
     {
         Logger.Info("Skip rebuilding token map");
         return;
     }
     Logger.Info("Rebuilding token map");
     if (Partitioner == null)
     {
         throw new DriverInternalError("Partitioner can not be null");
     }
     _tokenMap = TokenMap.Build(Partitioner, Hosts.ToCollection(), _keyspaces.Values);
 }
Beispiel #6
0
        public void UpdateKeyspace(KeyspaceMetadata ks)
        {
            var sw = new Stopwatch();

            sw.Start();

            TokenMap.UpdateKeyspace(
                ks, _tokenToHostsByKeyspace, _ring, _primaryReplicas, _keyspaceTokensCache, _datacenters, _numberOfHostsWithTokens);

            sw.Stop();
            TokenMap.Logger.Info(
                "Finished updating TokenMap for the '{0}' keyspace. It took {1:0} milliseconds.",
                ks.Name,
                sw.Elapsed.TotalMilliseconds);
        }
 internal void RebuildTokenMap(string partitioner, Dictionary <IPAddress, DictSet <string> > allTokens)
 {
     this._tokenMap = TokenMap.Build(partitioner, allTokens);
 }
Beispiel #8
0
 internal void RebuildTokenMap(string partitioner, Dictionary<IPAddress, HashSet<string>> allTokens)
 {
     this._tokenMap = TokenMap.Build(partitioner, allTokens);
 }
 internal void RebuildTokenMap()
 {
     Logger.Info("Rebuilding token map");
     if (Partitioner == null)
     {
         throw new DriverInternalError("Partitioner can not be null");
     }
     _tokenMap = TokenMap.Build(Partitioner, Hosts.ToCollection(), _keyspaces.Values);
 }
Beispiel #10
0
 internal void RebuildTokenMap(string partitioner, Dictionary <IPEndPoint, HashSet <string> > allTokens)
 {
     _tokenMap = TokenMap.Build(partitioner, allTokens);
 }