Beispiel #1
0
        public void TokenMap_Build_NetworkTopology_Multiple_Racks_Skipping_Hosts_Test()
        {
            const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
            var          hosts    = new[]
            {
                // DC1 racks has contiguous tokens
                // DC2 racks are properly organized
                TestHelper.CreateHost("192.168.0.0", "dc1", "dc1_rack1", new HashSet <string> {
                    "0"
                }),
                TestHelper.CreateHost("192.168.0.1", "dc2", "dc2_rack1", new HashSet <string> {
                    "1"
                }),
                TestHelper.CreateHost("192.168.0.2", "dc1", "dc1_rack1", new HashSet <string> {
                    "2"
                }),
                TestHelper.CreateHost("192.168.0.3", "dc2", "dc2_rack2", new HashSet <string> {
                    "3"
                }),
                TestHelper.CreateHost("192.168.0.4", "dc1", "dc1_rack2", new HashSet <string> {
                    "4"
                }),
                TestHelper.CreateHost("192.168.0.5", "dc2", "dc2_rack1", new HashSet <string> {
                    "5"
                }),
                TestHelper.CreateHost("192.168.0.6", "dc1", "dc1_rack2", new HashSet <string> {
                    "6"
                }),
                TestHelper.CreateHost("192.168.0.7", "dc2", "dc2_rack2", new HashSet <string> {
                    "7"
                })
            };
            var ks = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int>
            {
                { "dc1", 3 },
                { "dc2", 2 }
            });
            var map    = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
            var values = new[]
            {
                Tuple.Create(0, new byte[] { 0, 1, 3, 4, 2 }),
                Tuple.Create(1, new byte[] { 1, 2, 3, 4, 6 }),
                Tuple.Create(4, new byte[] { 4, 5, 7, 0, 6 })
            };

            foreach (var v in values)
            {
                var replicas = map.GetReplicas("ks1", new M3PToken(v.Item1));
                CollectionAssert.AreEqual(v.Item2, replicas.Select(TestHelper.GetLastAddressByte));
            }
        }
Beispiel #2
0
        public void CreateKeyspaceWithPropertiesTest(string strategy_class)
        {
            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            try
            {
                Session = CCMCluster.Session;
                Cluster = CCMCluster.Cluster;

                Randomm rndm           = new Randomm(DateTime.Now.Millisecond);
                bool    durable_writes = rndm.NextBoolean();

                int?replication_factor = null;
                int?data_centers_count = null;
                Dictionary <string, int> datacenters_replication_factors = null;

                if (strategy_class == ReplicationStrategies.SimpleStrategy)
                {
                    replication_factor = rndm.Next(1, 21);
                    Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateSimpleStrategyReplicationProperty((int)replication_factor), durable_writes);
                    Session.ChangeKeyspace(Keyspace);
                }
                else
                if (strategy_class == ReplicationStrategies.NetworkTopologyStrategy)
                {
                    data_centers_count = rndm.Next(1, 11);
                    datacenters_replication_factors = new Dictionary <string, int>((int)data_centers_count);
                    for (int i = 0; i < data_centers_count; i++)
                    {
                        datacenters_replication_factors.Add("dc" + i.ToString(), rndm.Next(1, 21));
                    }
                    Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(datacenters_replication_factors), durable_writes);
                }

                KeyspaceMetadata ksmd = Cluster.Metadata.GetKeyspace(Keyspace);
                Assert.Equal(strategy_class, ksmd.StrategyClass);
                Assert.Equal(durable_writes, ksmd.DurableWrites);
                if (replication_factor != null)
                {
                    Assert.Equal(replication_factor, ksmd.Replication["replication_factor"]);
                }
                if (datacenters_replication_factors != null)
                {
                    Assert.True(datacenters_replication_factors.SequenceEqual(ksmd.Replication));
                }
            }
            finally
            {
                CCMCluster.Discard();
            }
        }
Beispiel #3
0
        public void Ctor_Should_InitializeStrategyWithNull_When_NullReplicationOptionsAndStrategyClassArePassed()
        {
            var sut = new KeyspaceMetadata(
                null,
                "name",
                false,
                null,
                null,
                new ReplicationStrategyFactory(),
                null,
                true);

            Assert.IsNull(sut.Strategy);
        }
Beispiel #4
0
        public void Ctor_Should_StrategyBeNull_When_UnrecognizedStrategyClass()
        {
            var ks = new KeyspaceMetadata(
                null,
                "ks2",
                true,
                "random",
                new Dictionary <string, string> {
                { "replication_factor", "2" }
            },
                new ReplicationStrategyFactory(),
                null);

            Assert.IsNull(ks.Strategy);
            Assert.AreEqual("random", ks.StrategyClass);
        }
Beispiel #5
0
        public void Ctor_Should_ParseSimpleStrategyClass_When_ShortClassName()
        {
            var ks = new KeyspaceMetadata(
                null,
                "ks2",
                true,
                "SimpleStrategy",
                new Dictionary <string, string> {
                { "replication_factor", "2" }
            },
                new ReplicationStrategyFactory(),
                null);

            Assert.IsNotNull(ks.Strategy);
            Assert.AreEqual(ReplicationStrategies.SimpleStrategy, ks.StrategyClass);
        }
Beispiel #6
0
        public void Ctor_Should_ParseNetworkTopologyStrategyClass_When_ShortClassName()
        {
            var ks = new KeyspaceMetadata(
                null,
                "ks2",
                true,
                "NetworkTopologyStrategy",
                new Dictionary <string, string> {
                { "dc1", "2" }, { "dc2", "1" }
            },
                new ReplicationStrategyFactory(),
                null);

            Assert.IsNotNull(ks.Strategy);
            Assert.AreEqual(ReplicationStrategies.NetworkTopologyStrategy, ks.StrategyClass);
        }
Beispiel #7
0
        private void CreateCS(KeyspaceMetadata keySpace, DataCollectionMetadata tableDef)
        {
            var columnDescriptions = new List <Utils.ColumnDescription>();
            var className          = Utils.Utils.CSharpifyName(tableDef.Name);

            this.swaggerRoot.Definitions.Add(className, new Schema());
            Utils.Utils.WriteLineGreen($" Starting table {tableDef.Name}, class {className}");
            var properties = new List <string> {
                ""
            };

            foreach (var tableColumn in tableDef.TableColumns)
            {
                this.isFrozen = false;
                Utils.Utils.WriteLineGreen($" Table {tableDef.Name}, class {className}, column {tableColumn.Name}");

                var csharpTypeName = this.GetCSharpTypeName(tableColumn);

                if (csharpTypeName != null)
                {
                    AddProperty(tableDef, columnDescriptions, properties, tableColumn, csharpTypeName);
                }
                else
                {
                    Utils.Utils.WriteLineGreen($" Table {tableDef.Name}, class {className}, column {tableColumn.Name}: DON'T KNOW HOW TO HANDLE TYPE {tableColumn.TypeCode}");
                }
            }
            PopulateIsIndexField(columnDescriptions, tableDef);
            this.CreateCSFile(
                className,
                properties,
                "using Cassandra.Mapping.Attributes;",
                $@"[Table(""{tableDef.Name}"", Keyspace = ""{keySpace.Name}"" )]");
            this.typeDescriptions.Add(
                new Utils.TypeDescription
            {
                CassandraTableName = tableDef.Name,
                CSharpName         = className,
                ColumnDescriptions = columnDescriptions
            });
        }
        public void CheckSimpleStrategyKeyspace()
        {
            ITestCluster testCluster   = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount);
            var          session       = testCluster.Session;
            bool         durableWrites = Randomm.Instance.NextBoolean();
            string       keyspaceName  = TestUtils.GetUniqueKeyspaceName();

            string strategyClass     = ReplicationStrategies.SimpleStrategy;
            int    replicationFactor = Randomm.Instance.Next(1, 21);

            session.CreateKeyspace(keyspaceName,
                                   ReplicationStrategies.CreateSimpleStrategyReplicationProperty(replicationFactor),
                                   durableWrites);
            session.ChangeKeyspace(keyspaceName);

            KeyspaceMetadata ksmd = testCluster.Cluster.Metadata.GetKeyspace(keyspaceName);

            Assert.AreEqual(strategyClass, ksmd.StrategyClass);
            Assert.AreEqual(durableWrites, ksmd.DurableWrites);
            Assert.AreEqual(replicationFactor, ksmd.Replication["replication_factor"]);
        }
Beispiel #9
0
        public void checkKSMetadata()
        {
            var clusterInfo = TestUtils.CcmSetup(2);

            try
            {
                Session = clusterInfo.Session;
                Cluster = clusterInfo.Cluster;
                Session.CreateKeyspaceIfNotExists(Keyspace);
                Session.ChangeKeyspace(Keyspace);

                string keyspacename  = "keyspace" + Guid.NewGuid().ToString("N").ToLower();
                bool   durableWrites = false;
                string strgyClass    = "SimpleStrategy";
                short  rplctnFactor  = 1;
                Session.WaitForSchemaAgreement(
                    Session.Execute(
                        string.Format(@"CREATE KEYSPACE {0} 
         WITH replication = {{ 'class' : '{1}', 'replication_factor' : {2} }}
         AND durable_writes={3};"
                                      , keyspacename, strgyClass, rplctnFactor, durableWrites))
                    );
                Session.ChangeKeyspace(keyspacename);


                for (int i = 0; i < 10; i++)
                {
                    checkPureMetadata("table" + Guid.NewGuid().ToString("N"), keyspacename);
                }

                KeyspaceMetadata ksmd = Cluster.Metadata.GetKeyspace(keyspacename);
                Assert.True(ksmd.DurableWrites == durableWrites);
                Assert.True(ksmd.Replication.Where(opt => opt.Key == "replication_factor").First().Value == rplctnFactor);
                Assert.True(ksmd.StrategyClass == strgyClass);
            }
            finally
            {
                TestUtils.CcmRemove(clusterInfo);
            }
        }
Beispiel #10
0
        public void TokenMap_Build_Should_Memorize_Tokens_Per_Replication_Test()
        {
            const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
            var          hosts    = new[]
            {
                //0 and 100 are adjacent
                TestHelper.CreateHost("192.168.0.1", "dc1", "dc1_rack1", new HashSet <string> {
                    "0", "100", "1000"
                }),
                TestHelper.CreateHost("192.168.0.2", "dc1", "dc1_rack2", new HashSet <string> {
                    "200", "2000", "20000"
                }),
                TestHelper.CreateHost("192.168.0.3", "dc1", "dc1_rack1", new HashSet <string> {
                    "300", "3000", "30000"
                }),
                TestHelper.CreateHost("192.168.0.4", "dc2", "dc2_rack1", new HashSet <string> {
                    "400", "4000", "40000"
                }),
                TestHelper.CreateHost("192.168.0.5", "dc2", "dc2_rack2", new HashSet <string> {
                    "500", "5000", "50000"
                })
            };
            var ks1 = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int> {
                { "dc1", 2 }, { "dc2", 1 }
            });
            var ks2 = new KeyspaceMetadata(null, "ks2", true, strategy, new Dictionary <string, int> {
                { "dc1", 2 }, { "dc2", 1 }
            });
            var ks3 = new KeyspaceMetadata(null, "ks3", true, strategy, new Dictionary <string, int> {
                { "dc1", 2 }
            });
            var map     = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks1, ks2, ks3 });
            var tokens1 = map.GetByKeyspace("ks1");
            var tokens2 = map.GetByKeyspace("ks2");
            var tokens3 = map.GetByKeyspace("ks3");

            Assert.AreSame(tokens1, tokens2);
            Assert.AreNotSame(tokens1, tokens3);
        }
Beispiel #11
0
        public void TokenMap_Build_NetworkTopology_Quickly_Leave_When_Dc_Not_Found()
        {
            const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
            var          hosts    = new Host[100];

            for (var i = 0; i < hosts.Length; i++)
            {
                hosts[i] = TestHelper.CreateHost("192.168.0." + i, "dc" + (i % 2), "rack1", new HashSet <string>());
            }
            for (var i = 0; i < 256 * hosts.Length; i++)
            {
                var tokens = (HashSet <string>)hosts[i % hosts.Length].Tokens;
                tokens.Add(i.ToString());
            }
            var ks = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int>
            {
                { "dc1", 3 },
                { "dc2", 2 },
                { "dc3", 1 }
            });

            TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
        }
Beispiel #12
0
        /// <summary>
        /// Initialize a Keyspace by name in association with the specified cluster.
        /// </summary>
        internal Keyspace(string name, MetaCluster metaCluster)
        {
            if (metaCluster == null)
            {
                MetaCluster = ManagerCql.DefaultCluster;
            }
            else
            {
                MetaCluster = metaCluster;
            }

            // initialize cache collections
            Tables        = new Shared <Dictionary <string, Table> >(new Dictionary <string, Table>());
            Statements    = new Shared <Dictionary <int, PreparedStatement> >(new Dictionary <int, PreparedStatement>());
            TableMetadata = new Shared <Dictionary <string, TableMetadata> >(new Dictionary <string, TableMetadata>());
            // initialize the batch related fields
            _batch     = new ArrayRig <Statement>();
            _batchLock = new Lock();

            // decrease the batch timeout if debugging
      #if DEBUG
            BatchTimeout = 10000;
      #endif

            // create the act used to execute batch statements
            Act executeBatch = new Act(() => ExecuteBatch());
            _batchTimer = new Timer(BatchInterval, executeBatch, false);
            _onExecuted = new ArrayRig <IAction>();

            // setup the auto update watch
            if (_autoUpdateTime > 0)
            {
                _autoUpdateWatch = new Watch(_autoUpdateTime, true, OnAutoUpdate);
            }

            try {
                // connect to the cluster
                _session = MetaCluster.Cluster.Connect();
            } catch (Exception ex) {
                // log the error
                Log.Error("Problem getting Keyspace '" + name + "' metadata from the Cluster.", ex);
                return;
            }

            // initialize a Mapper instance for use by this Keyspace
            Mapper = new Mapper(_session);

            // get the metadata associated with this Keyspace
            Metadata = MetaCluster.Cluster.Metadata.GetKeyspace(name);

            // if the Keyspace doesn't exist
            if (Metadata == null)
            {
                // create the Keyspace
                if (MetaCluster.DataCenters == null)
                {
                    _session.CreateKeyspace(name,
                                            ReplicationStrategies.CreateSimpleStrategyReplicationProperty(1));
                }
                else
                {
                    _session.CreateKeyspace(name,
                                            ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(MetaCluster.DataCenters));
                }

                // get the metadata
                Metadata = MetaCluster.Cluster.Metadata.GetKeyspace(name);
            }

            // add the Keyspace to the DataCluster
            MetaCluster.AddKeyspace(this);

            // make the target Keyspace of the Session to this Keyspace
            _session.ChangeKeyspace(Metadata.Name);

            TableMetadata.Take();
            foreach (var tableMetadata in Metadata.GetTablesMetadata())
            {
                TableMetadata.Item.Add(tableMetadata.Name, tableMetadata);
            }
            TableMetadata.Release();
        }
Beispiel #13
0
        public void Ctor_Should_InitializeStrategyClassWithNull_When_NonNullReplicationOptionsArePassedButNullStrategyClassIsPassed()
        {
            var sut = new KeyspaceMetadata(null, "name", true, null, new ConcurrentDictionary <string, string>(), true);

            Assert.IsNull(sut.StrategyClass);
        }
Beispiel #14
0
        public void Ctor_Should_InitializeStrategyClassWithNonNull_When_NullReplicationOptionsArePassedButNonNullStrategyClassIsPassed()
        {
            var sut = new KeyspaceMetadata(null, "name", true, "SimpleStrategy", null, true);

            Assert.IsNotNull(sut.StrategyClass);
        }
Beispiel #15
0
 public void TokenMap_Build_NetworkTopology_Adjacent_Ranges_Test()
 {
     const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
     var hosts = new[]
     {
         //0 and 100 are adjacent
         TestHelper.CreateHost("192.168.0.1", "dc1", "rack1", new HashSet<string> {"0", "100", "1000"}),
         TestHelper.CreateHost("192.168.0.2", "dc1", "rack1", new HashSet<string> {"200",      "2000", "20000"}),
         TestHelper.CreateHost("192.168.0.3", "dc1", "rack1", new HashSet<string> {"300",      "3000", "30000"})
     };
     var ks = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary<string, int> { { "dc1", 2 } });
     var map = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
     var replicas = map.GetReplicas("ks1", new M3PToken(0));
     Assert.AreEqual(2, replicas.Count);
     //It should contain the first host and the second, even though the first host contains adjacent 
     CollectionAssert.AreEqual(new byte[] { 1, 2 }, replicas.Select(TestHelper.GetLastAddressByte));
 }