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, Dictionary <IPAddress, DictSet <string> > allTokens) { TokenFactory factory = TokenFactory.GetFactory(partitioner); if (factory == null) { return(null); } Dictionary <IToken, DictSet <IPAddress> > tokenToCassandraClusterHosts = new Dictionary <IToken, DictSet <IPAddress> >(); DictSet <IToken> allSorted = new DictSet <IToken>(); foreach (var entry in allTokens) { var cassandraClusterHost = entry.Key; foreach (string tokenStr in entry.Value) { try { IToken t = factory.Parse(tokenStr); allSorted.Add(t); if (!tokenToCassandraClusterHosts.ContainsKey(t)) { tokenToCassandraClusterHosts.Add(t, new DictSet <IPAddress>()); } tokenToCassandraClusterHosts[t].Add(cassandraClusterHost); } catch (ArgumentException e) { // If we failed parsing that token, skip it } } } return(new TokenMap(factory, tokenToCassandraClusterHosts, new List <IToken>(allSorted))); }
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, int>(); foreach (var host in hosts) { if (host.Datacenter != null) { if (!datacenters.ContainsKey(host.Datacenter)) { datacenters[host.Datacenter] = 1; } else { datacenters[host.Datacenter]++; } } 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, HashSet <Host> > >(keyspaces.Count); //Only consider nodes that have tokens var hostCount = hosts.Count(h => h.Tokens.Any()); foreach (var ks in keyspaces) { Dictionary <IToken, HashSet <Host> > replicas; if (ks.StrategyClass == ReplicationStrategies.SimpleStrategy) { replicas = ComputeTokenToReplicaSimple(ks.Replication["replication_factor"], hostCount, ring, primaryReplicas); } else if (ks.StrategyClass == ReplicationStrategies.NetworkTopologyStrategy) { replicas = ComputeTokenToReplicaNetwork(ks.Replication, ring, primaryReplicas, datacenters); } else { //No replication information, use primary replicas replicas = primaryReplicas.ToDictionary(kv => kv.Key, kv => new HashSet <Host>(new [] { kv.Value })); } tokenToHosts[ks.Name] = replicas; } return(new TokenMap(factory, tokenToHosts, ring, primaryReplicas)); }