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)); }
public static TokenMap Build(string partitioner, ICollection <Host> hosts, ICollection <KeyspaceMetadata> keyspaces) { 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) { DatacenterInfo dc; if (!datacenters.TryGetValue(host.Datacenter, out 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 { Logger.Error(string.Format("Token {0} could not be parsed using {1} partitioner implementation", tokenStr, partitioner)); } } } var ring = new List <IToken>(allSorted); var tokenToHosts = new Dictionary <string, Dictionary <IToken, ISet <Host> > >(keyspaces.Count); var ksTokensCache = new Dictionary <string, Dictionary <IToken, ISet <Host> > >(); //Only consider nodes that have tokens var hostCount = hosts.Count(h => h.Tokens.Any()); foreach (var ks in keyspaces) { Dictionary <IToken, ISet <Host> > replicas; if (ks.StrategyClass == ReplicationStrategies.SimpleStrategy) { replicas = ComputeTokenToReplicaSimple(ks.Replication["replication_factor"], hostCount, ring, primaryReplicas); } else if (ks.StrategyClass == ReplicationStrategies.NetworkTopologyStrategy) { var key = GetReplicationKey(ks.Replication); if (!ksTokensCache.TryGetValue(key, out replicas)) { replicas = ComputeTokenToReplicaNetwork(ks.Replication, ring, primaryReplicas, datacenters); ksTokensCache.Add(key, replicas); } } else { //No replication information, use primary replicas replicas = primaryReplicas.ToDictionary(kv => kv.Key, kv => (ISet <Host>) new HashSet <Host>(new [] { kv.Value })); } tokenToHosts[ks.Name] = replicas; } return(new TokenMap(factory, tokenToHosts, ring, primaryReplicas)); }