Beispiel #1
0
 internal TokenMap(TokenFactory factory, Dictionary<string, Dictionary<IToken, HashSet<Host>>> tokenToHostsByKeyspace, List<IToken> ring, Dictionary<IToken, Host> primaryReplicas)
 {
     Factory = factory;
     _tokenToHostsByKeyspace = tokenToHostsByKeyspace;
     _ring = ring;
     _primaryReplicas = primaryReplicas;
 }
Beispiel #2
0
 private TokenMap(TokenFactory factory, Dictionary<IToken, HashSet<IPAddress>> tokenToCassandraClusterHosts, List<IToken> ring)
 {
     Factory = factory;
     _tokenToCassandraClusterHosts = tokenToCassandraClusterHosts;
     _ring = ring.ToArray();
     Array.Sort(_ring);
 }
        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));
        }
Beispiel #4
0
 private TokenMap(TokenFactory factory, Dictionary<IToken, DictSet<IPAddress>> tokenToCassandraClusterHosts, List<IToken> ring)
 {
     this.Factory = factory;
     this._tokenToCassandraClusterHosts = tokenToCassandraClusterHosts;
     this._ring = ring;
 }
        public static TokenMap Build(string partitioner, IEnumerable <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);

            foreach (var ks in keyspaces)
            {
                Dictionary <IToken, HashSet <Host> > replicas;
                if (ks.StrategyClass == ReplicationStrategies.SimpleStrategy)
                {
                    replicas = ComputeTokenToReplicaSimple(ks.Replication["replication_factor"], 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));
        }