예제 #1
0
 public void TestFixtureSetup()
 {
     if (CassandraVersion < Version.Parse("2.2"))
     {
         return;
     }
     _testCluster = TestClusterManager.GetTestCluster(1, 0, false, DefaultMaxClusterCreateRetries, false, false);
     _testCluster.UpdateConfig("enable_user_defined_functions: true");
     _testCluster.Start(1);
     using (var cluster = Cluster.Builder().AddContactPoint(_testCluster.InitialContactPoint).Build())
     {
         var session = cluster.Connect();
         var queries = new[]
         {
             "CREATE KEYSPACE  ks_udf WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}",
             "CREATE FUNCTION  ks_udf.return_one() RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return 1;'",
             "CREATE FUNCTION  ks_udf.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return s+v;'",
             "CREATE FUNCTION  ks_udf.plus(s bigint, v bigint) RETURNS NULL ON NULL INPUT RETURNS bigint LANGUAGE java AS 'return s+v;'",
             "CREATE AGGREGATE ks_udf.sum(int) SFUNC plus STYPE int INITCOND 1",
             "CREATE AGGREGATE ks_udf.sum(bigint) SFUNC plus STYPE bigint INITCOND 2"
         };
         foreach (var q in queries)
         {
             session.Execute(q);   
         }
     }
 }
 public void TestFixtureSetUp()
 {
     Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Info;
     _testClusterForAuthTesting = GetTestCcmClusterForAuthTests();
     //Wait 10 seconds as auth table needs to be created
     Thread.Sleep(10000);
 }
예제 #3
0
 public void SetupTest()
 {
     _testCluster = TestClusterManager.GetTestCluster(1);
     _session = _testCluster.Session;
     _uniqueKsName = TestUtils.GetUniqueKeyspaceName();
     _session.CreateKeyspace(_uniqueKsName);
     _session.ChangeKeyspace(_uniqueKsName);
 }
예제 #4
0
        public void Setup()
        {
            _testCluster = TestClusterManager.GetTestCluster(1);
            _session = _testCluster.Session;

            _session.Execute(String.Format(TestUtils.CreateKeyspaceSimpleFormat, _uniqueKeyspaceName, 1));
            _session.ChangeKeyspace(_uniqueKeyspaceName);
            _session.Execute("CREATE TYPE song (id uuid, title text, artist text)");
            _session.Execute("CREATE TABLE albums (id uuid primary key, name text, songs list<frozen<song>>, publishingdate timestamp)");
            _session.Execute(
                new SimpleStatement(
                    "INSERT INTO albums (id, name, songs) VALUES (?, 'Legend', [{id: uuid(), title: 'Africa Unite', artist: 'Bob Marley'}])", 
                    _sampleId));
            _session.UserDefinedTypes.Define(UdtMap.For<Song>());
        }
예제 #5
0
 public void Cluster_Connect_Should_Initialize_Loadbalancing_With_ControlConnection_Address_Set()
 {
     _testCluster = TestClusterManager.CreateNew(2);
     var lbp = new TestLoadBalancingPolicy();
     var builder = Cluster.Builder()
         .AddContactPoint(_testCluster.InitialContactPoint)
         .WithLoadBalancingPolicy(lbp);
     using (var cluster = builder.Build())
     {
         cluster.Connect();
         Assert.NotNull(lbp.ControlConnectionHost);
         Assert.AreEqual(IPAddress.Parse(_testCluster.InitialContactPoint), 
             lbp.ControlConnectionHost.Address.Address);
     }
 }
예제 #6
0
        public void RoundRobin_DcAware_BuildClusterWithNonExistentDc()
        {
            ITestCluster testCluster = TestClusterManager.GetTestCluster(1);

            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2"));
            try
            {
                testCluster.InitClient();
                Assert.Fail("Expected exception was not thrown!");
            }
            catch (ArgumentException e)
            {
                string expectedErrMsg = "Datacenter dc2 does not match any of the nodes, available datacenters: datacenter1.";
                Assert.IsTrue(e.Message.Contains(expectedErrMsg));
            }
        }
        public Task Cluster_Connect_Should_Initialize_Loadbalancing_With_ControlConnection_Address_Set(bool asyncConnect)
        {
            _testCluster = TestClusterManager.CreateNew(2);
            var lbp     = new TestLoadBalancingPolicy();
            var cluster = Cluster.Builder()
                          .AddContactPoint(_testCluster.InitialContactPoint)
                          .WithLoadBalancingPolicy(lbp)
                          .Build();

            return(Run(cluster, asyncConnect, session =>
            {
                Assert.NotNull(lbp.ControlConnectionHost);
                Assert.AreEqual(IPAddress.Parse(_testCluster.InitialContactPoint),
                                lbp.ControlConnectionHost.Address.Address);
            }));
        }
예제 #8
0
        public void RoundRobin_TwoDCs_DcAware()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            ITestCluster    testCluster     = TestClusterManager.GetNonShareableTestCluster(1, 1, DefaultMaxClusterCreateRetries, true);

            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2"));
            testCluster.InitClient();

            policyTestTools.CreateMultiDcSchema(testCluster.Session);
            policyTestTools.InitPreparedStatement(testCluster, 12);
            policyTestTools.Query(testCluster, 12);

            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 0);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 12);
        }
 private ControlConnection NewInstance(ITestCluster testCluster, Configuration config = null, Metadata metadata = null)
 {
     var version = GetProtocolVersion();
     if (config == null)
     {
         config = new Configuration();
     }
     if (metadata == null)
     {
         metadata = new Metadata(config);
         metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
     }
     var cc = new ControlConnection(version, config, metadata);
     metadata.ControlConnection = cc;
     return cc;
 }
예제 #10
0
        public async Task Cluster_Should_Honor_MaxProtocolVersion_Set(bool asyncConnect)
        {
            _testCluster = TestClusterManager.CreateNew(2);

            // Default MaxProtocolVersion
            var clusterDefault = Cluster.Builder()
                                 .AddContactPoint(_testCluster.InitialContactPoint)
                                 .Build();

            Assert.AreEqual(Cluster.MaxProtocolVersion, clusterDefault.Configuration.ProtocolOptions.MaxProtocolVersion);

            // MaxProtocolVersion set
            var clusterMax = Cluster.Builder()
                             .AddContactPoint(_testCluster.InitialContactPoint)
                             .WithMaxProtocolVersion(3)
                             .Build();

            Assert.AreEqual(3, clusterMax.Configuration.ProtocolOptions.MaxProtocolVersion);
            await Connect(clusterMax, asyncConnect, session =>
            {
                if (CassandraVersion < Version.Parse("2.1"))
                {
                    Assert.AreEqual(2, session.BinaryProtocolVersion);
                }
                else
                {
                    Assert.AreEqual(3, session.BinaryProtocolVersion);
                }
            }).ConfigureAwait(false);

            // Arbitary MaxProtocolVersion set, will negotiate down upon connect
            var clusterNegotiate = Cluster.Builder()
                                   .AddContactPoint(_testCluster.InitialContactPoint)
                                   .WithMaxProtocolVersion(10)
                                   .Build();

            Assert.AreEqual(10, clusterNegotiate.Configuration.ProtocolOptions.MaxProtocolVersion);
            await Connect(clusterNegotiate, asyncConnect, session =>
            {
                Assert.LessOrEqual(4, clusterNegotiate.Configuration.ProtocolOptions.MaxProtocolVersion);
            }).ConfigureAwait(false);

            // ProtocolVersion 0 does not exist
            Assert.Throws <ArgumentException>(
                () => Cluster.Builder().AddContactPoint("127.0.0.1").WithMaxProtocolVersion((byte)0));
        }
예제 #11
0
        private void RetryPolicyTest(ITestCluster testCluster)
        {
            PolicyTestTools policyTestTools = new PolicyTestTools();

            policyTestTools.TableName = TestUtils.GetUniqueTableName();
            policyTestTools.CreateSchema(testCluster.Session, 2);

            // Test before state
            policyTestTools.InitPreparedStatement(testCluster, 12);
            policyTestTools.Query(testCluster, 12);
            int clusterPosQueried    = 1;
            int clusterPosNotQueried = 2;

            if (!policyTestTools.Coordinators.ContainsKey(testCluster.ClusterIpPrefix + clusterPosQueried))
            {
                clusterPosQueried    = 2;
                clusterPosNotQueried = 1;
            }
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1);

            // Stop one of the nodes and test
            policyTestTools.ResetCoordinators();
            testCluster.Stop(clusterPosQueried);
            TestUtils.WaitForDown(testCluster.ClusterIpPrefix + clusterPosQueried, testCluster.Cluster, 30);
            policyTestTools.Query(testCluster, 120);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 120);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 0);

            // Start the node that was just down, then down the node that was just up
            policyTestTools.ResetCoordinators();
            testCluster.Start(clusterPosQueried);
            TestUtils.WaitForUp(testCluster.ClusterIpPrefix + clusterPosQueried, DefaultCassandraPort, 30);

            // Test successful reads
            DateTime futureDateTime = DateTime.Now.AddSeconds(120);

            while (policyTestTools.Coordinators.Count < 2 && DateTime.Now < futureDateTime)
            {
                policyTestTools.Query(testCluster, 120);
                Thread.Sleep(75);
            }

            // Ensure that the nodes were contacted
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1);
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 1);
        }
예제 #12
0
        public void TwoSessionsConnectedToSameDcUseSeparatePolicyInstances()
        {
            var          builder     = Cluster.Builder();
            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1, 1, DefaultMaxClusterCreateRetries, true);

            using (var cluster1 = builder.WithConnectionString(String.Format("Contact Points={0}1", testCluster.ClusterIpPrefix)).Build())
                using (var cluster2 = builder.WithConnectionString(String.Format("Contact Points={0}2", testCluster.ClusterIpPrefix)).Build())
                {
                    using (var session1 = (Session)cluster1.Connect())
                        using (var session2 = (Session)cluster2.Connect())
                        {
                            Assert.True(!ReferenceEquals(session1.Policies.LoadBalancingPolicy, session2.Policies.LoadBalancingPolicy), "Load balancing policy instances should be different");
                            Assert.True(!ReferenceEquals(session1.Policies.ReconnectionPolicy, session2.Policies.ReconnectionPolicy), "Reconnection policy instances should be different");
                            Assert.True(!ReferenceEquals(session1.Policies.RetryPolicy, session2.Policies.RetryPolicy), "Retry policy instances should be different");
                        }
                }
        }
예제 #13
0
        public void Cluster_Connect_Should_Initialize_Loadbalancing_With_ControlConnection_Address_Set()
        {
            Diagnostics.CassandraTraceSwitch.Level = System.Diagnostics.TraceLevel.Verbose;
            _testCluster = TestClusterManager.CreateNew(2);
            var lbp     = new TestLoadBalancingPolicy();
            var builder = Cluster.Builder()
                          .AddContactPoint(_testCluster.InitialContactPoint)
                          .WithLoadBalancingPolicy(lbp);

            using (var cluster = builder.Build())
            {
                cluster.Connect();
                Assert.NotNull(lbp.ControlConnectionHost);
                Assert.AreEqual(IPAddress.Parse(_testCluster.InitialContactPoint),
                                lbp.ControlConnectionHost.Address.Address);
            }
        }
        public void RoundRobin_TokenAware_TwoDCsWithOneNodeEach_ReplicationFactorTwo()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            ITestCluster    testCluster     = TestClusterManager.GetNonShareableTestCluster(1, 1, DefaultMaxClusterCreateRetries, true);

            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
            testCluster.InitClient();

            policyTestTools.CreateSchema(testCluster.Session, 2);

            policyTestTools.InitPreparedStatement(testCluster, 12);
            policyTestTools.Query(testCluster, 12);

            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 6);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 6);
        }
예제 #15
0
        public virtual void OneTimeSetUp()
        {
            CultureInfo.DefaultThreadCurrentCulture   = CultureInfo.InvariantCulture;
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;

            if (_reuse && _reusableInstance != null &&
                ReferenceEquals(_reusableInstance, TestClusterManager.LastInstance) &&
                ((Options != null && Options.Equals(TestClusterManager.LastOptions)) || (Options == null && TestClusterManager.LastOptions == null)) &&
                AmountOfNodes == TestClusterManager.LastAmountOfNodes)
            {
                Trace.WriteLine("Reusing ccm instance");
                TestCluster = _reusableInstance;
            }
            else
            {
                TestCluster = TestClusterManager.CreateNew(AmountOfNodes, Options);
                if (_reuse)
                {
                    _reusableInstance = TestCluster;
                }
                else
                {
                    _reusableInstance = null;
                }
            }
            if (CreateSession)
            {
                Cluster = Cluster.Builder().AddContactPoint(TestCluster.InitialContactPoint)
                          .WithQueryTimeout(60000)
                          .WithSocketOptions(new SocketOptions().SetConnectTimeoutMillis(30000))
                          .Build();
                Session = (Session)Cluster.Connect();
                Session.CreateKeyspace(KeyspaceName, null, false);
                Session.ChangeKeyspace(KeyspaceName);
                if (SetupQueries != null)
                {
                    foreach (var query in SetupQueries)
                    {
                        Session.Execute(query);
                    }
                }
            }
        }
예제 #16
0
        public void TestFixtureSetup()
        {
            if (TestClusterManager.CheckCassandraVersion(false, Version.Parse("2.2"), Comparison.LessThan))
            {
                return;
            }
            _testCluster = TestClusterManager.GetTestCluster(1, 0, false, DefaultMaxClusterCreateRetries, false, false);
            _testCluster.UpdateConfig("enable_user_defined_functions: true");
            _testCluster.Start(1);
            using (var cluster = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint).Build())
            {
                var session = cluster.Connect();
                var queries = new List <string>
                {
                    "CREATE KEYSPACE  ks_udf WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}",
                    "CREATE FUNCTION  ks_udf.return_one() RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return 1;'",
                    "CREATE FUNCTION  ks_udf.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return s+v;'",
                    "CREATE FUNCTION  ks_udf.plus(s bigint, v bigint) RETURNS NULL ON NULL INPUT RETURNS bigint LANGUAGE java AS 'return s+v;'",
                    "CREATE AGGREGATE ks_udf.sum(int) SFUNC plus STYPE int INITCOND 1",
                    "CREATE AGGREGATE ks_udf.sum(bigint) SFUNC plus STYPE bigint INITCOND 2"
                };

                if (TestClusterManager.CheckDseVersion(new Version(6, 0), Comparison.GreaterThanOrEqualsTo))
                {
                    queries.Add("CREATE FUNCTION ks_udf.deterministic(dividend int, divisor int) " +
                                "CALLED ON NULL INPUT RETURNS int DETERMINISTIC LANGUAGE java AS " +
                                "'return dividend / divisor;'");
                    queries.Add("CREATE FUNCTION ks_udf.monotonic(dividend int, divisor int) " +
                                "CALLED ON NULL INPUT RETURNS int MONOTONIC LANGUAGE java AS " +
                                "'return dividend / divisor;'");
                    queries.Add("CREATE FUNCTION ks_udf.md(dividend int, divisor int) " +
                                "CALLED ON NULL INPUT RETURNS int DETERMINISTIC MONOTONIC LANGUAGE java AS " +
                                "'return dividend / divisor;'");
                    queries.Add("CREATE FUNCTION ks_udf.monotonic_on(dividend int, divisor int) " +
                                "CALLED ON NULL INPUT RETURNS int MONOTONIC ON dividend LANGUAGE java AS " +
                                "'return dividend / divisor;'");
                    queries.Add("CREATE AGGREGATE ks_udf.deta(int) SFUNC plus STYPE int INITCOND 0 DETERMINISTIC;");
                }

                foreach (var q in queries)
                {
                    session.Execute(q);
                }
            }
        }
예제 #17
0
        public void TupleMetadataTest()
        {
            string keyspaceName = TestUtils.GetUniqueKeyspaceName();
            string tableName    = TestUtils.GetUniqueTableName().ToLower();
            string cqlTable1    = "CREATE TABLE " + tableName + " (id int PRIMARY KEY, phone frozen<tuple<uuid, text, int>>, achievements list<frozen<tuple<text,int>>>)";

            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount);
            var          cluster     = testCluster.Cluster;
            var          session     = testCluster.Session;

            session.CreateKeyspaceIfNotExists(keyspaceName);
            session.ChangeKeyspace(keyspaceName);
            session.Execute(cqlTable1);

            var tableMetadata = cluster.Metadata.GetTable(keyspaceName, tableName);

            Assert.AreEqual(3, tableMetadata.TableColumns.Count());
        }
예제 #18
0
        public void KeyspacesMetadataAvailableAtStartup()
        {
            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount);
            var          cluster     = testCluster.Cluster;

            // Basic status check
            Assert.Greater(cluster.Metadata.GetKeyspaces().Count, 0);
            Assert.NotNull(cluster.Metadata.GetKeyspace("system"));
            Assert.AreEqual("system", cluster.Metadata.GetKeyspace("system").Name);

            //Not existent tables return null
            Assert.Null(cluster.Metadata.GetKeyspace("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12)));
            Assert.Null(cluster.Metadata.GetTable("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12), "nonExistentTable_" + Randomm.RandomAlphaNum(12)));
            Assert.Null(cluster.Metadata.GetTable("system", "nonExistentTable_" + Randomm.RandomAlphaNum(12)));

            //Case sensitive
            Assert.Null(cluster.Metadata.GetKeyspace("SYSTEM"));
        }
        private ControlConnection NewInstance(ITestCluster testCluster, Configuration config = null, Metadata metadata = null)
        {
            var version = GetProtocolVersion();

            if (config == null)
            {
                config = new Configuration();
            }
            if (metadata == null)
            {
                metadata = new Metadata(config);
                metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
            }
            var cc = new ControlConnection(version, config, metadata);

            metadata.ControlConnection = cc;
            return(cc);
        }
예제 #20
0
        /// <summary>
        /// 恢复测试集的环境
        /// </summary>
        /// <param name="testCluster"></param>
        /// <returns></returns>
        private bool RestoreClusterScripts(ITestCluster testCluster)
        {
            bool RestoreClusterScriptsRes = true;

            try
            {
                testCluster.RestoreScripts();
            }
            catch
            {
                RestoreClusterScriptsRes = false;
                throw new Exception("恢复环境失败");
            }
            finally
            {
            }
            return(RestoreClusterScriptsRes);
        }
 public void RetryUntilClusterAuthHealthy(ITestCluster cluster)
 {
     using (var c = ClusterBuilder()
                    .AddContactPoint(cluster.InitialContactPoint)
                    .WithAuthProvider(new PlainTextAuthProvider("wrong_username", "password"))
                    .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(22000).SetConnectTimeoutMillis(60000))
                    .Build())
     {
         TestHelper.RetryAssert(
             () =>
         {
             var ex = Assert.Throws <NoHostAvailableException>(() => c.Connect());
             Assert.IsInstanceOf <AuthenticationException>(ex.Errors.First().Value);
         },
             500,
             300);
     }
 }
예제 #22
0
        public void Cluster_Should_Honor_MaxProtocolVersion_Set()
        {
            _testCluster = TestClusterManager.CreateNew(2);

            // Default MaxProtocolVersion
            var cluster = Cluster.Builder()
                          .AddContactPoint(_testCluster.InitialContactPoint)
                          .Build();

            Assert.AreEqual(Cluster.MaxProtocolVersion, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);

            // MaxProtocolVersion set
            cluster = Cluster.Builder()
                      .AddContactPoint(_testCluster.InitialContactPoint)
                      .WithMaxProtocolVersion(3)
                      .Build();
            Assert.AreEqual(3, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);

            var session = cluster.Connect();

            if (CassandraVersion < Version.Parse("2.1"))
            {
                Assert.AreEqual(2, session.BinaryProtocolVersion);
            }
            else
            {
                Assert.AreEqual(3, session.BinaryProtocolVersion);
            }
            cluster.Shutdown();

            // Arbitary MaxProtocolVersion set, will negotiate down upon connect
            cluster = Cluster.Builder()
                      .AddContactPoint(_testCluster.InitialContactPoint)
                      .WithMaxProtocolVersion(10)
                      .Build();
            Assert.AreEqual(10, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);
            session = cluster.Connect();
            Assert.LessOrEqual(4, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);
            cluster.Shutdown();

            // ProtocolVersion 0 does not exist
            Assert.Throws <ArgumentException>(
                () => Cluster.Builder().AddContactPoint("127.0.0.1").WithMaxProtocolVersion((byte)0));
        }
예제 #23
0
        public void MetadataMethodReconnects()
        {
            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(2);
            var          cluster     = testCluster.Cluster;

            //The control connection is connected to host 1
            Assert.AreEqual(1, TestHelper.GetLastAddressByte(cluster.Metadata.ControlConnection.BindAddress));
            testCluster.StopForce(1);
            Thread.Sleep(10000);

            //The control connection is still connected to host 1
            Assert.AreEqual(1, TestHelper.GetLastAddressByte(cluster.Metadata.ControlConnection.BindAddress));
            var t = cluster.Metadata.GetTable("system", "schema_columnfamilies");

            Assert.NotNull(t);

            //The control connection should be connected to host 2
            Assert.AreEqual(2, TestHelper.GetLastAddressByte(cluster.Metadata.ControlConnection.BindAddress));
        }
예제 #24
0
        private void PrepareForSparkTest(ITestCluster testCluster)
        {
            const string replicationConfigStr = "{'class' : 'SimpleStrategy', 'replication_factor' : 2}";

            using (var cluster = Cluster.Builder().AddContactPoint(TestClusterManager.InitialContactPoint).Build())
            {
                WaitForWorkers(1);

                var session = cluster.Connect();

                Trace.TraceInformation("GraphMultiNodeTests: Altering keyspace for dse_leases");
                session.Execute(
                    "ALTER KEYSPACE dse_leases WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'GraphAnalytics': '2'}");

                Trace.TraceInformation("GraphMultiNodeTests: Bootstrapping node 2");
                testCluster.BootstrapNode(2, false);
                Trace.TraceInformation("GraphMultiNodeTests: Setting workload");
                testCluster.SetNodeWorkloads(2, new[] { "graph", "spark" });
                Trace.TraceInformation("GraphMultiNodeTests: Starting node 2");
                testCluster.Start(2);
                Trace.TraceInformation("Waiting additional time for new node to be ready");
                Thread.Sleep(15000);
                WaitForWorkers(2);

                Trace.TraceInformation("GraphMultiNodeTests: Creating graph");
                session.ExecuteGraph(new SimpleGraphStatement(
                                         "system.graph(name)" +
                                         ".option('graph.replication_config').set(replicationConfig)" +
                                         ".option('graph.system_replication_config').set(replicationConfig)" +
                                         ".ifNotExists()" +
                                         (!TestClusterManager.SupportsNextGenGraph() ? string.Empty : ".engine(Classic)") +
                                         ".create()",
                                         new { name = GraphMultiNodeTests.GraphName, replicationConfig = replicationConfigStr }));
                Trace.TraceInformation("GraphMultiNodeTests: Created graph");

                var graphStatements = new StringBuilder();
                graphStatements.Append(BaseIntegrationTest.MakeStrict + "\n");
                graphStatements.Append(BaseIntegrationTest.AllowScans + "\n");
                graphStatements.Append(BaseIntegrationTest.ClassicSchemaGremlinQuery + "\n");
                graphStatements.Append(BaseIntegrationTest.ClassicLoadGremlinQuery);
                session.ExecuteGraph(new SimpleGraphStatement(graphStatements.ToString()).SetGraphName(GraphMultiNodeTests.GraphName));
            }
        }
예제 #25
0
        public void MetadataMethodReconnects()
        {
            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(2);
            var          cluster     = testCluster.Cluster;

            //The control connection is connected to host 1
            Assert.AreEqual(1, TestHelper.GetLastAddressByte(cluster.Metadata.ControlConnection.EndPoint.GetHostIpEndPointWithFallback()));
            testCluster.StopForce(1);
            Thread.Sleep(10000);

            //The control connection is still connected to host 1
            Assert.AreEqual(1, TestHelper.GetLastAddressByte(cluster.Metadata.ControlConnection.EndPoint.GetHostIpEndPointWithFallback()));
            var t = cluster.Metadata.GetTable("system", "local");

            Assert.NotNull(t);

            //The control connection should be connected to host 2
            Assert.AreEqual(2, TestHelper.GetLastAddressByte(cluster.Metadata.ControlConnection.EndPoint.GetHostIpEndPointWithFallback()));
        }
예제 #26
0
        public async Task Should_Remove_Decommissioned_Node()
        {
            const int numberOfNodes = 2;

            _realCluster = TestClusterManager.CreateNew(numberOfNodes);
            using (var cluster = Cluster.Builder().AddContactPoint(_realCluster.InitialContactPoint).Build())
            {
                await Connect(cluster, false, session =>
                {
                    Assert.AreEqual(numberOfNodes, cluster.AllHosts().Count);
                    if (TestClusterManager.SupportsDecommissionForcefully())
                    {
                        _realCluster.DecommissionNodeForcefully(numberOfNodes);
                    }
                    else
                    {
                        _realCluster.DecommissionNode(numberOfNodes);
                    }
                    _realCluster.Stop(numberOfNodes);
                    Trace.TraceInformation("Node decommissioned");
                    string decommisionedNode = null;
                    TestHelper.RetryAssert(() =>
                    {
                        decommisionedNode = _realCluster.ClusterIpPrefix + 2;
                        Assert.False(TestUtils.IsNodeReachable(IPAddress.Parse(decommisionedNode)));
                        //New node should be part of the metadata
                        Assert.AreEqual(1, cluster.AllHosts().Count);
                    }, 100, 100);
                    var queried = false;
                    for (var i = 0; i < 10; i++)
                    {
                        var rs = session.Execute("SELECT key FROM system.local");
                        if (rs.Info.QueriedHost.Address.ToString() == decommisionedNode)
                        {
                            queried = true;
                            break;
                        }
                    }
                    Assert.False(queried, "Removed node should be queried");
                }).ConfigureAwait(false);
            }
        }
예제 #27
0
        public void TableMetadataCassandra22Types()
        {
            if (TestClusterManager.CheckCassandraVersion(false, new Version(2, 2), Comparison.LessThan))
            {
                Assert.Ignore("Date, Time, SmallInt and TinyInt are supported in 2.2 and above");
            }
            var          keyspaceName = TestUtils.GetUniqueKeyspaceName();
            const string tableName    = "tbl_cass22_types";
            ITestCluster testCluster  = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount);
            var          cluster      = testCluster.Cluster;
            var          session      = testCluster.Session;

            session.CreateKeyspaceIfNotExists(keyspaceName);
            session.ChangeKeyspace(keyspaceName);

            session.Execute(string.Format("CREATE TABLE {0} (" +
                                          "id uuid primary key, " +
                                          "map1 map<smallint, date>," +
                                          "s smallint," +
                                          "b tinyint," +
                                          "d date," +
                                          "t time)", tableName));
            var table = cluster.Metadata
                        .GetKeyspace(keyspaceName)
                        .GetTableMetadata(tableName);

            Assert.AreEqual(6, table.TableColumns.Length);
            CollectionAssert.AreEqual(table.PartitionKeys, new[] { table.TableColumns.First(c => c.Name == "id") });
            var map1 = table.TableColumns.First(c => c.Name == "map1");

            Assert.AreEqual(ColumnTypeCode.Map, map1.TypeCode);
            Assert.IsInstanceOf <MapColumnInfo>(map1.TypeInfo);
            var map1Info = (MapColumnInfo)map1.TypeInfo;

            Assert.AreEqual(ColumnTypeCode.SmallInt, map1Info.KeyTypeCode);
            Assert.AreEqual(ColumnTypeCode.Date, map1Info.ValueTypeCode);

            Assert.AreEqual(ColumnTypeCode.SmallInt, table.TableColumns.First(c => c.Name == "s").TypeCode);
            Assert.AreEqual(ColumnTypeCode.TinyInt, table.TableColumns.First(c => c.Name == "b").TypeCode);
            Assert.AreEqual(ColumnTypeCode.Date, table.TableColumns.First(c => c.Name == "d").TypeCode);
            Assert.AreEqual(ColumnTypeCode.Time, table.TableColumns.First(c => c.Name == "t").TypeCode);
        }
예제 #28
0
        private ITestCluster SetupSessionAndCluster(int nodes, Dictionary <string, string> replication = null)
        {
            ITestCluster testCluster = TestClusterManager.GetTestCluster(nodes);

            _session = testCluster.Session;
            _ksName  = TestUtils.GetUniqueKeyspaceName();
            _session.CreateKeyspace(_ksName, replication);
            TestUtils.WaitForSchemaAgreement(_session.Cluster);
            _session.ChangeKeyspace(_ksName);
            _table = new Table <ManyDataTypesEntity>(_session, new MappingConfiguration());
            _table.Create();
            _defaultPocoList   = ManyDataTypesEntity.GetDefaultAllDataTypesList();
            _preparedStatement = _session.Prepare(_preparedInsertStatementAsString);
            foreach (var manyDataTypesEntity in _defaultPocoList)
            {
                _session.Execute(GetBoundInsertStatementBasedOnEntity(manyDataTypesEntity));
            }

            return(testCluster);
        }
예제 #29
0
        public void ClusterWaitsForSchemaChangesUntilMaxWaitTimeIsReachedMultiple()
        {
            var          index = 0;
            ITestCluster nonShareableTestCluster = TestClusterManager.GetNonShareableTestCluster(2, 0, true, false);

            nonShareableTestCluster.Stop(2);
            using (var cluster = Cluster.Builder()
                                 .AddContactPoint(nonShareableTestCluster.InitialContactPoint)
                                 .Build())
            {
                var session = cluster.Connect();
                //Will wait for all the nodes to have the same schema
                session.Execute("CREATE KEYSPACE ks1 WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3}");
                session.ChangeKeyspace("ks1");
                TestHelper.ParallelInvoke(() =>
                {
                    session.Execute("CREATE TABLE tbl1" + Interlocked.Increment(ref index) + " (id uuid primary key)");
                }, 10);
            }
        }
예제 #30
0
        public void AlreadyExistsException()
        {
            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1);
            ISession     session     = testCluster.Session;

            string keyspace = TestUtils.GetUniqueKeyspaceName();
            string table    = TestUtils.GetUniqueTableName();

            String[] cqlCommands =
            {
                String.Format(TestUtils.CreateKeyspaceSimpleFormat, keyspace,  1),
                String.Format("USE \"{0}\"",                        keyspace),
                String.Format(TestUtils.CreateTableSimpleFormat,    table)
            };

            // Create the schema once
            session.Execute(cqlCommands[0]);
            session.Execute(cqlCommands[1]);
            session.Execute(cqlCommands[2]);

            // Try creating the keyspace again
            var ex = Assert.Throws <AlreadyExistsException>(() => session.Execute(cqlCommands[0]));

            Assert.AreEqual(ex.Keyspace, keyspace);
            Assert.AreEqual(ex.Table, null);
            Assert.AreEqual(ex.WasTableCreation, false);

            session.Execute(cqlCommands[1]);

            // Try creating the table again
            try
            {
                session.Execute(cqlCommands[2]);
            }
            catch (AlreadyExistsException e)
            {
                Assert.AreEqual(e.Keyspace, keyspace);
                Assert.AreEqual(e.Table, table.ToLower());
                Assert.AreEqual(e.WasTableCreation, true);
            }
        }
예제 #31
0
        public Task Cluster_Connect_Should_Use_Node2_Address(bool asyncConnect)
        {
            _testCluster = TestClusterManager.CreateNew(2);
            _testCluster.PauseNode(1);
            var lbp     = new TestLoadBalancingPolicy();
            var cluster = Cluster.Builder()
                          .AddContactPoints(new []
            {
                _testCluster.InitialContactPoint,
                _testCluster.ClusterIpPrefix + "2"
            })
                          .WithLoadBalancingPolicy(lbp)
                          .Build();

            return(Connect(cluster, asyncConnect, session =>
            {
                Assert.NotNull(lbp.ControlConnectionHost);
                Assert.AreEqual(IPAddress.Parse(_testCluster.ClusterIpPrefix + "2"),
                                lbp.ControlConnectionHost.Address.Address);
            }));
        }
예제 #32
0
        /// <summary>
        /// 执行测试集的初始化工作
        /// </summary>
        /// <param name="testCluster"></param>
        /// <param name="lstCurRunLogicData"></param>
        /// <returns></returns>
        private bool SetupTestClusterScripts(ITestCluster testCluster, List <Dictionary <string, string> > lstCurRunLogicData)
        {
            Dictionary <string, string>[] TestCases = lstCurRunLogicData.ToArray();
            bool SetupTestClusterScripsRes          = true;

            try
            {
                if (!testCluster.SetupScripts())
                {
                    SetupTestClusterScripsRes = false;
                    throw new Exception("执行配置脚本返回值为: False!");
                }
            }
            catch
            {
            }
            finally
            {
            }
            return(SetupTestClusterScripsRes);
        }
예제 #33
0
        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"]);
        }
        public void RoundRobin_OneDc_OneNodeAdded_OneNodeDecommissioned()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            ITestCluster    testCluster     = TestClusterManager.GetNonShareableTestCluster(1);

            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new RoundRobinPolicy());
            testCluster.InitClient();

            policyTestTools.CreateSchema(testCluster.Session);
            policyTestTools.InitPreparedStatement(testCluster, 12);
            policyTestTools.Query(testCluster, 12);

            // Validate that all host were queried equally
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 12);

            // Add new node to the end of second cluster, remove node from beginning of first cluster
            policyTestTools.ResetCoordinators();
            // Bootstrap step
            int bootStrapPos = 2;

            testCluster.BootstrapNode(bootStrapPos);
            string newlyBootstrappedIp = testCluster.ClusterIpPrefix + bootStrapPos;

            TestUtils.WaitForUp(newlyBootstrappedIp, DefaultCassandraPort, 30);

            // Validate expected nodes where queried
            policyTestTools.WaitForPolicyToolsQueryToHitBootstrappedIp(testCluster, newlyBootstrappedIp);
            policyTestTools.Query(testCluster, 12);
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 6);
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 6);

            // decommission old node
            policyTestTools.ResetCoordinators();
            testCluster.DecommissionNode(1);
            TestUtils.waitForDecommission(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, testCluster.Cluster, 60);

            policyTestTools.Query(testCluster, 12);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 12);
        }
예제 #35
0
        public void SpeculativeExecution_Pause_Using_All_Stream_Ids()
        {
            var maxProtocolVersion = Cluster.MaxProtocolVersion;

            _testCluster = TestClusterManager.GetNonShareableTestCluster(2, 1, true, false);
            Cluster.MaxProtocolVersion = 2;
            try
            {
                var       pooling        = PoolingOptions.Create();
                var       session        = GetSession(new ConstantSpeculativeExecutionPolicy(50L, 1), true, null, pooling);
                const int pauseThreshold = 140 * 2;
                var       tasks          = new List <Task <IPAddress> >();
                var       semaphore      = new SemaphoreSlim(150 * 2);
                for (var i = 0; i < 512; i++)
                {
                    //Pause after the stream ids are in use for the connections
                    if (i == pauseThreshold)
                    {
                        _testCluster.PauseNode(1);
                    }
                    semaphore.Wait();
                    tasks.Add(session
                              .ExecuteAsync(new SimpleStatement(QueryLocal).SetIdempotence(true))
                              .ContinueSync(rs =>
                    {
                        semaphore.Release();
                        return(rs.Info.QueriedHost.Address);
                    }));
                }
                Task.WaitAll(tasks.Select(t => (Task)t).ToArray());
                _testCluster.ResumeNode(1);
                //There shouldn't be any query using node1 as coordinator passed the threshold.
                Assert.AreEqual(0, tasks.Skip(pauseThreshold).Count(t => t.Result.Equals(_addressNode1)));
                Thread.Sleep(1000);
            }
            finally
            {
                Cluster.MaxProtocolVersion = maxProtocolVersion;
            }
        }
예제 #36
0
        public void TokenAware_Composite_NoHops()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            ITestCluster    testCluster     = TestClusterManager.GetTestCluster(3);

            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
            testCluster.InitClient();

            // Test
            var session = testCluster.Session;

            policyTestTools.CreateSchema(session);
            policyTestTools.TableName = TestUtils.GetUniqueTableName();
            session.Execute(String.Format("CREATE TABLE {0} (k1 text, k2 int, i int, PRIMARY KEY ((k1, k2)))", policyTestTools.TableName));
            var traces = new List <QueryTrace>();

            for (var i = 0; i < 10; i++)
            {
                var statement = new SimpleStatement(String.Format("INSERT INTO " + policyTestTools.TableName + " (k1, k2, i) VALUES ('{0}', {0}, {0})", i))
                                .SetRoutingKey(
                    new RoutingKey()
                {
                    RawRoutingKey = Encoding.UTF8.GetBytes(i.ToString())
                },
                    new RoutingKey()
                {
                    RawRoutingKey = BitConverter.GetBytes(i).Reverse().ToArray()
                })
                                .EnableTracing();
                var rs = session.Execute(statement);
                traces.Add(rs.Info.QueryTrace);
            }
            //Check that there weren't any hops
            foreach (var t in traces)
            {
                //The coordinator must be the only one executing the query
                Assert.True(t.Events.All(e => e.Source.ToString() == t.Coordinator.ToString()), "There were trace events from another host for coordinator " + t.Coordinator);
            }
        }
 public void SpeculativeExecution_Pause_Using_All_Stream_Ids()
 {
     var maxProtocolVersion = Cluster.MaxProtocolVersion;
     _testCluster = TestClusterManager.GetNonShareableTestCluster(2, 1, true, false);
     Cluster.MaxProtocolVersion = 2;
     try
     {
         var pooling = PoolingOptions.DefaultOptions(Version.Parse("2.0")).SetCoreConnectionsPerHost(HostDistance.Local, 1);
         var session = GetSession(new ConstantSpeculativeExecutionPolicy(50L, 1), true, null, pooling);
         const int pauseThreshold = 140 * 2;
         var tasks = new List<Task<IPAddress>>();
         var semaphore = new SemaphoreSlim(150 * 2);
         for (var i = 0; i < 512; i++)
         {
             //Pause after the stream ids are in use for the connections
             if (i == pauseThreshold)
             {
                 _testCluster.PauseNode(1);
             }
             semaphore.Wait();
             tasks.Add(session
                 .ExecuteAsync(new SimpleStatement(QueryLocal).SetIdempotence(true))
                 .Continue(t =>
                 {
                     semaphore.Release();
                     return t.Result.Info.QueriedHost.Address;
                 }));
         }
         Task.WaitAll(tasks.Select(t => (Task)t).ToArray());
         _testCluster.ResumeNode(1);
         //There shouldn't be any query using node1 as coordinator passed the threshold.
         Assert.AreEqual(0, tasks.Skip(pauseThreshold).Count(t => t.Result.Equals(_addressNode1)));
         Thread.Sleep(1000);
     }
     finally
     {
         Cluster.MaxProtocolVersion = maxProtocolVersion;
     }
 }
예제 #38
0
        public void Cluster_Should_Honor_MaxProtocolVersion_Set()
        {
            _testCluster = TestClusterManager.CreateNew(2);

            // Default MaxProtocolVersion
            var cluster = Cluster.Builder()
                .AddContactPoint(_testCluster.InitialContactPoint)
                .Build();
            Assert.AreEqual(Cluster.MaxProtocolVersion, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);

            // MaxProtocolVersion set
            cluster = Cluster.Builder()
                .AddContactPoint(_testCluster.InitialContactPoint)
                .WithMaxProtocolVersion(3)
                .Build();
            Assert.AreEqual(3, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);

            var session = cluster.Connect();
            if (CassandraVersion < Version.Parse("2.1"))
                Assert.AreEqual(2, session.BinaryProtocolVersion);
            else
                Assert.AreEqual(3, session.BinaryProtocolVersion);
            cluster.Shutdown();

            // Arbitary MaxProtocolVersion set, will negotiate down upon connect
            cluster = Cluster.Builder()
                .AddContactPoint(_testCluster.InitialContactPoint)
                .WithMaxProtocolVersion(10)
                .Build();
            Assert.AreEqual(10, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);
            session = cluster.Connect();
            Assert.LessOrEqual(4, cluster.Configuration.ProtocolOptions.MaxProtocolVersion);
            cluster.Shutdown();

            // ProtocolVersion 0 does not exist
            Assert.Throws<ArgumentException>(() => Cluster.Builder().AddContactPoint(_testCluster.InitialContactPoint).WithMaxProtocolVersion(0));
        }
예제 #39
0
 protected virtual void TestFixtureSetUp()
 {
     if (_reuse && _reusableInstance != null && ReferenceEquals(_reusableInstance, TestClusterManager.LastInstance))
     {
         Trace.WriteLine("Reusing single node ccm instance");
         TestCluster = _reusableInstance;
     }
     else
     {
         TestCluster = TestClusterManager.CreateNew(AmountOfNodes);
         if (_reuse)
         {
             _reusableInstance = TestCluster;
         }
         else
         {
             _reusableInstance = null;
         }
     }
     if (CreateSession)
     {
         Cluster = Cluster.Builder().AddContactPoint(TestCluster.InitialContactPoint)
             .WithQueryTimeout(60000)
             .WithSocketOptions(new SocketOptions().SetConnectTimeoutMillis(30000))
             .Build();
         Session = (Session) Cluster.Connect();
         Session.CreateKeyspace(KeyspaceName, null, false);
         Session.ChangeKeyspace(KeyspaceName);
         if (SetupQueries != null)
         {
             foreach (var query in SetupQueries)
             {
                 Session.Execute(query);
             }
         }
     }
 }
예제 #40
0
 public void Should_Add_And_Query_Newly_Bootstrapped_Node()
 {
     _testCluster = TestClusterManager.CreateNew();
     using (var cluster = Cluster.Builder().AddContactPoint(_testCluster.InitialContactPoint).Build())
     {
         var session = cluster.Connect();
         Assert.AreEqual(1, cluster.AllHosts().Count);
         _testCluster.BootstrapNode(2);
         var queried = false;
         Trace.TraceInformation("Node bootstrapped");
         Thread.Sleep(10000);
         var newNodeAddress = _testCluster.ClusterIpPrefix + 2;
         Assert.True(TestHelper.TryConnect(newNodeAddress), "New node does not accept connections");
         //New node should be part of the metadata
         Assert.AreEqual(2, cluster.AllHosts().Count);
         for (var i = 0; i < 10; i++)
         {
             var rs = session.Execute("SELECT key FROM system.local");
             if (rs.Info.QueriedHost.Address.ToString() == newNodeAddress)
             {
                 queried = true;
                 break;
             }
         }
         Assert.True(queried, "Newly bootstrapped node should be queried");
     }
 }
예제 #41
0
 public void WaitForPolicyToolsQueryToHitBootstrappedIp(ITestCluster testCluster, string newlyBootstrappedIp)
 {
     int secondsToPoll = 120;
     DateTime futureDateTime = DateTime.Now.AddSeconds(120);
     Trace.TraceInformation("Polling for " + secondsToPoll + " seconds while we wait for bootstrapped IP to join the ring, be found by the client");
     while (!Coordinators.ContainsKey(newlyBootstrappedIp) && DateTime.Now < futureDateTime)
     {
         try
         {
             Query(testCluster, 10);
         }
         catch (Exception e)
         {
             string[] expectedErrMessages =
             {
                 "Keyspace '" + DefaultKeyspace + "' does not exist",
                 "unconfigured columnfamily",
                 "Cassandra timeout during read query at consistency"
             };
             Assert.IsTrue(e.Message.Contains(expectedErrMessages[0]) || e.Message.Contains(expectedErrMessages[1]) || e.Message.Contains(expectedErrMessages[2]),
                 "Error message '" + e.Message + "' did not contain one of these acceptable error messages: " + string.Join(",", expectedErrMessages));
             Trace.TraceInformation("Caught acceptable one of these acceptable error messages: " + string.Join(",", expectedErrMessages));
         }
         Thread.Sleep(250);
     }
 }
 public void TestTearDown()
 {
     _testCluster.Remove();
     _testCluster = null;
 }
예제 #43
0
        private void RetryPolicyTest(ITestCluster testCluster)
        {
            PolicyTestTools policyTestTools = new PolicyTestTools();
            policyTestTools.TableName = TestUtils.GetUniqueTableName();
            policyTestTools.CreateSchema(testCluster.Session, 2);

            // Test before state
            policyTestTools.InitPreparedStatement(testCluster, 12);
            policyTestTools.Query(testCluster, 12);
            int clusterPosQueried = 1;
            int clusterPosNotQueried = 2;
            if (!policyTestTools.Coordinators.ContainsKey(testCluster.ClusterIpPrefix + clusterPosQueried))
            {
                clusterPosQueried = 2;
                clusterPosNotQueried = 1;
            }
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1);

            // Stop one of the nodes and test
            policyTestTools.ResetCoordinators();
            testCluster.Stop(clusterPosQueried);
            TestUtils.WaitForDown(testCluster.ClusterIpPrefix + clusterPosQueried, testCluster.Cluster, 30);
            policyTestTools.Query(testCluster, 120);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 120);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 0);

            // Start the node that was just down, then down the node that was just up
            policyTestTools.ResetCoordinators();
            testCluster.Start(clusterPosQueried);
            TestUtils.WaitForUp(testCluster.ClusterIpPrefix + clusterPosQueried, DefaultCassandraPort, 30);

            // Test successful reads
            DateTime futureDateTime = DateTime.Now.AddSeconds(120);
            while (policyTestTools.Coordinators.Count < 2 && DateTime.Now < futureDateTime)
            {
                policyTestTools.Query(testCluster, 120);
                Thread.Sleep(75);
            }

            // Ensure that the nodes were contacted
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1);
            policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 1);

        }
예제 #44
0
        //////////////////////////////
        /// Test Helpers
        //////////////////////////////

        public void TestReplicationFactorThree(ITestCluster testCluster)
        {
            _policyTestTools.CreateSchema(testCluster.Session, 3);
            _policyTestTools.InitPreparedStatement(testCluster, 12, ConsistencyLevel.Three);
            _policyTestTools.Query(testCluster, 12, ConsistencyLevel.Three);

            _policyTestTools.ResetCoordinators();
            testCluster.StopForce(2);
            TestUtils.WaitForDownWithWait(testCluster.ClusterIpPrefix + "2", testCluster.Cluster, 5);

            var acceptedList = new List<ConsistencyLevel>
            {
                ConsistencyLevel.Any,
                ConsistencyLevel.One,
                ConsistencyLevel.Two,
                ConsistencyLevel.Quorum,
                ConsistencyLevel.Three,
                ConsistencyLevel.All
            };

            var failList = new List<ConsistencyLevel>();

            // Test successful writes
            foreach (ConsistencyLevel consistencyLevel in acceptedList)
            {
                try
                {
                    _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel);
                }
                catch (Exception e)
                {
                    Assert.Fail(String.Format("Test failed at CL.{0} with message: {1}", consistencyLevel, e.Message));
                }
            }

            // Test successful reads
            foreach (ConsistencyLevel consistencyLevel in acceptedList)
            {
                try
                {
                    _policyTestTools.Query(testCluster, 12, consistencyLevel);
                }
                catch (InvalidQueryException e)
                {
                    var acceptableErrorMessages = new List<string>
                    {
                        "ANY ConsistencyLevel is only supported for writes"
                    };
                    Assert.True(acceptableErrorMessages.Contains(e.Message));
                }
            }

            // Test writes which should fail
            foreach (ConsistencyLevel consistencyLevel in failList)
            {
                try
                {
                    _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel);
                    Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel));
                }
                catch (InvalidQueryException e)
                {
                    var acceptableErrorMessages = new List<string>
                    {
                        "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)",
                        "consistency level EACH_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)"
                    };
                    Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message));
                }
                catch (UnavailableException)
                {
                    // expected to fail when the client has already marked the
                    // node as DOWN
                }
                catch (WriteTimeoutException)
                {
                    // expected to fail when the client hasn't marked the'
                    // node as DOWN yet
                }
            }

            // Test reads which should fail
            foreach (ConsistencyLevel consistencyLevel in failList)
            {
                try
                {
                    _policyTestTools.Query(testCluster, 12, consistencyLevel);
                    Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel));
                }
                catch (InvalidQueryException e)
                {
                    var acceptableErrorMessages = new List<string>
                    {
                        "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)",
                        "EACH_QUORUM ConsistencyLevel is only supported for writes"
                    };
                    Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message));
                }
                catch (ReadTimeoutException)
                {
                    // expected to fail when the client hasn't marked the'
                    // node as DOWN yet
                }
                catch (UnavailableException)
                {
                    // expected to fail when the client has already marked the
                    // node as DOWN
                }
            }
        }
 public void TestFixtureSetUp()
 {
     Diagnostics.CassandraTraceSwitch.Level = System.Diagnostics.TraceLevel.Info;
     _testClusterForAuthTesting = GetTestCcmClusterForAuthTests();
     WaitForAuthenticatedClusterToConnect(_testClusterForAuthTesting);
 }
예제 #46
0
 public void InitPreparedStatement(ITestCluster testCluster, int numberOfInsertsToMake, bool batch)
 {
     InitPreparedStatement(testCluster, numberOfInsertsToMake, batch, ConsistencyLevel.One);
 }
예제 #47
0
 /// <summary>
 /// Validates that the bootstrapped node was added to the cluster and was queried.
 /// </summary>
 public static void ValidateBootStrappedNodeIsQueried(ITestCluster testCluster, int expectedTotalNodeCount, string newlyBootstrappedHost)
 {
     var hostsQueried = new List<string>();
     DateTime timeInTheFuture = DateTime.Now.AddSeconds(120);
     while (testCluster.Cluster.Metadata.AllHosts().ToList().Count() < expectedTotalNodeCount && DateTime.Now < timeInTheFuture)
     {
         var rs = testCluster.Session.Execute("SELECT key FROM system.local");
         hostsQueried.Add(rs.Info.QueriedHost.Address.ToString());
         Thread.Sleep(500);
     }
     Assert.That(testCluster.Cluster.Metadata.AllHosts().ToList().Count, Is.EqualTo(expectedTotalNodeCount));
     timeInTheFuture = DateTime.Now.AddSeconds(120);
     while (!hostsQueried.Contains(newlyBootstrappedHost) && DateTime.Now < timeInTheFuture)
     {
         var rs = testCluster.Session.Execute("SELECT key FROM system.local");
         hostsQueried.Add(rs.Info.QueriedHost.Address.ToString());
         Thread.Sleep(500);
     }
     // Validate host was queried
     Assert.True(hostsQueried.Any(ip => ip.ToString() == newlyBootstrappedHost), "Newly bootstrapped node was not queried!");
 }
예제 #48
0
 public void Should_Remove_Decommissioned_Node()
 {
     _testCluster = TestClusterManager.CreateNew(2);
     using (var cluster = Cluster.Builder().AddContactPoint(_testCluster.InitialContactPoint).Build())
     {
         var session = cluster.Connect();
         Assert.AreEqual(2, cluster.AllHosts().Count);
         _testCluster.DecommissionNode(2);
         Trace.TraceInformation("Node decommissioned");
         Thread.Sleep(10000);
         var decommisionedNode = _testCluster.ClusterIpPrefix + 2;
         Assert.False(TestHelper.TryConnect(decommisionedNode), "Removed node should not accept connections");
         //New node should be part of the metadata
         Assert.AreEqual(1, cluster.AllHosts().Count);
         var queried = false;
         for (var i = 0; i < 10; i++)
         {
             var rs = session.Execute("SELECT key FROM system.local");
             if (rs.Info.QueriedHost.Address.ToString() == decommisionedNode)
             {
                 queried = true;
                 break;
             }
         }
         Assert.False(queried, "Removed node should be queried");
     }
 }
 public void SpeculativeExecution_With_Multiple_Nodes_Pausing()
 {
     _testCluster = TestClusterManager.GetNonShareableTestCluster(3, 1, true, false);
     var session = GetSession(new ConstantSpeculativeExecutionPolicy(50L, 1));
     var timer = new HashedWheelTimer(1000, 64);
     timer.NewTimeout(_ => Task.Factory.StartNew(() => _testCluster.PauseNode(2)), null, 2000);
     //2 secs after resume node2
     timer.NewTimeout(_ => Task.Factory.StartNew(() => _testCluster.ResumeNode(2)), null, 4000);
     timer.NewTimeout(_ => Task.Factory.StartNew(() => _testCluster.PauseNode(1)), null, 6000);
     //4 secs after resume node1
     timer.NewTimeout(_ => Task.Factory.StartNew(() => _testCluster.ResumeNode(1)), null, 10000);
     var finished = false;
     timer.NewTimeout(_ => Task.Factory.StartNew(() => finished = true), null, 12000);
     //64 constant concurrent requests
     var semaphore = new SemaphoreSlim(64);
     while (!finished)
     {
         TestHelper.ParallelInvoke(() =>
         {
             semaphore.Wait();
             session.Execute(new SimpleStatement(QueryLocal).SetIdempotence(true));
             semaphore.Release();
         }, 512);
     }
     Thread.Sleep(1000);
     timer.Dispose();
 }
 public void SetupFixture()
 {
     Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Info;
     // Test ccm cluster without client
     _testCluster = TestClusterManager.GetTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
 }
        /////////////////////////////////
        /// Test Helpers
        ////////////////////////////////
        
        private static void WaitForAuthenticatedClusterToConnect(ITestCluster testCluster)
        {
            DateTime timeInTheFuture = DateTime.Now.AddSeconds(60);
            ISession session = null;
            Trace.TraceInformation("Validating that test cluster with name: " + testCluster.Name + " can be connected to ... ");

            while (DateTime.Now < timeInTheFuture && session == null)
            {
                try
                {
                    Cluster cluster = Cluster
                        .Builder()
                        .AddContactPoint(testCluster.InitialContactPoint)
                        .WithAuthProvider(new PlainTextAuthProvider("cassandra", "cassandra"))
                        .Build();
                    session = cluster.Connect();
                }
                catch (Exception e)
                {
                    Trace.TraceInformation("Failed to connect to authenticated cluster, error msg: " + e.Message);
                    Trace.TraceInformation("Waiting 1 second then trying again ... ");
                    Thread.Sleep(1000);
                    session = null;
                }
            }
        }
예제 #52
0
 public void RemoveTestCluster(ITestCluster testCluster)
 {
     Trace.TraceWarning("Removing test cluster with initial contact point: " + testCluster.InitialContactPoint + " and name: " + testCluster.Name);
     // remove Cassandra instance 
     testCluster.Remove();
     // remove from list
     _testClusters.Remove(testCluster);
 }
예제 #53
0
        private bool TryToInititalizeClusterClient(ITestCluster testCluster)
        {
            try
            {
                foreach (string host in testCluster.ExpectedInitialHosts)
                    TestUtils.WaitForUp(host, DefaultCassandraPort, 30);

                // at this point we expect all the nodes to be up
                testCluster.InitClient();
                return true;
            }
            catch (Exception e)
            {
                Trace.TraceError("Unexpected Error occurred when trying to get shared test cluster with InitialContactPoint: " + testCluster.InitialContactPoint + ", name: " + testCluster.Name);
                Trace.TraceError("Error Message: " + e.Message);
                Trace.TraceError("Error Stack Trace: " + e.StackTrace);
                Trace.TraceError("Removing this cluster, and looping back to create a new one ... ");
            }
            return false;
        }
예제 #54
0
 public void Query(ITestCluster testCluster, int numberOfQueriesToExecute, ConsistencyLevel consistencyLevel)
 {
     Query(testCluster, numberOfQueriesToExecute, false, consistencyLevel);
 }
예제 #55
0
 public void InitPreparedStatement(ITestCluster testCluster, int numberOfInsertsToMake, ConsistencyLevel consistencyLevel)
 {
     InitPreparedStatement(testCluster, numberOfInsertsToMake, false, consistencyLevel);
 }
예제 #56
0
 public void Query(ITestCluster testCluster, int numberOfQueriesToExecute, bool usePrepared, ConsistencyLevel consistencyLevel)
 {
     if (usePrepared)
     {
         BoundStatement bs = PreparedStatement.Bind(0);
         for (int i = 0; i < numberOfQueriesToExecute; ++i)
         {
             ConsistencyLevel cac;
             var rs = testCluster.Session.Execute(bs);
             {
                 string queriedHost = rs.Info.QueriedHost.ToString();
                 cac = rs.Info.AchievedConsistency;
                 AddCoordinator(queriedHost, cac);
             }
         }
     }
     else
     {
         var routingKey = new RoutingKey();
         routingKey.RawRoutingKey = Enumerable.Repeat((byte) 0x00, 4).ToArray();
         for (int i = 0; i < numberOfQueriesToExecute; ++i)
         {
             string hostQueried;
             ConsistencyLevel achievedConsistency;
             var rs = testCluster.Session.Execute(
                         new SimpleStatement(String.Format("SELECT * FROM {0} WHERE k = 0", TableName)).SetRoutingKey(routingKey)
                                                                                                   .SetConsistencyLevel(consistencyLevel));
             {
                 hostQueried = rs.Info.QueriedHost.ToString();
                 achievedConsistency = rs.Info.AchievedConsistency;
                 Trace.TraceInformation("Query {0} executed by {1} with consistency {2}", i, hostQueried, achievedConsistency);
             }
             AddCoordinator(hostQueried, achievedConsistency);
         }
     }
 }
예제 #57
0
        public void InitPreparedStatement(ITestCluster testCluster, int numberOfInsertsToMake, bool batch, ConsistencyLevel consistencyLevel)
        {
            // We don't use insert for our test because the resultSet don't ship the queriedHost
            // Also note that we don't use tracing because this would trigger requests that screw up the test
            for (int i = 0; i < numberOfInsertsToMake; ++i)
                if (batch)
                    // BUG: WriteType == SIMPLE                    
                {
                    var bth = new StringBuilder();
                    bth.AppendLine("BEGIN BATCH");
                    bth.AppendLine(String.Format("INSERT INTO {0}(k, i) VALUES (0, 0)", TableName));
                    bth.AppendLine("APPLY BATCH");

                    testCluster.Session.Execute(new SimpleStatement(bth.ToString()).SetConsistencyLevel(consistencyLevel));
                }
                else
                {
                    testCluster.Session.Execute(
                            new SimpleStatement(String.Format("INSERT INTO {0}(k, i) VALUES (0, 0)", TableName)).SetConsistencyLevel(consistencyLevel));
                }

            PreparedStatement = testCluster.Session.Prepare("SELECT * FROM " + TableName + " WHERE k = ?").SetConsistencyLevel(consistencyLevel);
        }
예제 #58
0
 public void Query(ITestCluster testCluster, int numberOfQueriesToExecute, bool usePrepared)
 {
     Query(testCluster, numberOfQueriesToExecute, usePrepared, ConsistencyLevel.One);
 }
예제 #59
0
 public void SetupFixture()
 {
     _testCluster = TestClusterManager.CreateNew();
 }
예제 #60
0
 private void WaitForTestClusterToInitialize(ITestCluster testCluster)
 {
     int millisecondsSlept = 0;
     while (testCluster.IsBeingCreated && millisecondsSlept < ClusterInitSleepMsMax)
     {
         Console.WriteLine(string.Format("Cluster is initializing, sleeping {1} seconds before attempting to use it ...", ClusterInitSleepMsPerIteration));
         Thread.Sleep(ClusterInitSleepMsPerIteration);
         millisecondsSlept += ClusterInitSleepMsPerIteration;
     }
 }