Beispiel #1
0
        //Only for tests purpose:
        //private class InsertQuery : CqlCommand
        //{
        //    string Query;

        //    internal InsertQuery(string query)
        //    {
        //        this.Query = query;
        //    }

        //    public override string GetCql()
        //    {
        //        return Query;
        //    }
        //}
        protected void init(CCMBridge.CCMCluster c, int n, bool batch, ConsistencyLevel cl)
        {
            CassandraRoutingKey routingKey = new CassandraRoutingKey();

            routingKey.RawRoutingKey = Enumerable.Repeat((byte)0x00, 4).ToArray();


            // 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 < n; ++i)
            {
                if (batch)
                // BUG: WriteType == SIMPLE
                {
                    StringBuilder bth = new StringBuilder();
                    bth.AppendLine("BEGIN BATCH");
                    bth.AppendLine(String.Format("INSERT INTO {0}(k, i) VALUES (0, 0)", TestUtils.SIMPLE_TABLE));
                    bth.AppendLine("APPLY BATCH");

                    var qh = c.Session.Execute(new SimpleStatement(bth.ToString()).SetRoutingKey(routingKey).SetConsistencyLevel(cl)).QueriedHost;
                }
                else
                {
                    var qh = c.Session.Execute(new SimpleStatement(String.Format("INSERT INTO {0}(k, i) VALUES (0, 0)", TestUtils.SIMPLE_TABLE)).SetRoutingKey(routingKey).SetConsistencyLevel(cl)).QueriedHost;
                }
            }

            prepared = c.Session.Prepare("SELECT * FROM " + TestUtils.SIMPLE_TABLE + " WHERE k = ?").SetConsistencyLevel(cl);
        }
Beispiel #2
0
        public void alreadyExistsExceptionCCM()
        {
            var builder = Cluster.Builder();

            CCMBridge.CCMCluster cluster = CCMBridge.CCMCluster.Create(1, builder);
            try
            {
                Session session  = cluster.Session;
                String  keyspace = "TestKeyspace";
                String  table    = "TestTable";

                String[] cqlCommands = new String[] {
                    String.Format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1),
                    "USE " + keyspace,
                    String.Format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table)
                };

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

                // Try creating the keyspace again
                try
                {
                    session.Execute(cqlCommands[0]);
                }
                catch (AlreadyExistsException e)
                {
                    String expected = String.Format("Keyspace {0} already exists", keyspace.ToLower());

                    Assert.Equal(e.Message, expected);
                    Assert.Equal(e.Keyspace, keyspace.ToLower());
                    Assert.Equal(e.Table, null);
                    Assert.Equal(e.WasTableCreation, false);
                }

                session.Execute(cqlCommands[1]);

                // Try creating the table again
                try
                {
                    session.Execute(cqlCommands[2]);
                }
                catch (AlreadyExistsException e)
                {
                    Assert.Equal(e.Keyspace, keyspace.ToLower());
                    Assert.Equal(e.Table, table.ToLower());
                    Assert.Equal(e.WasTableCreation, true);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cluster.Discard();
            }
        }
Beispiel #3
0
        public void DCAwareRoundRobinTestCCM()
        {
            var builder = Cluster.Builder().WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2"));

            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, 2, builder);
            createMultiDCSchema(c.Session);
            try
            {
                init(c, 12);
                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                assertQueried(Options.Default.IP_PREFIX + "3", 6);
                assertQueried(Options.Default.IP_PREFIX + "4", 6);
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #4
0
 protected void query(CCMBridge.CCMCluster c, int n, bool usePrepared, ConsistencyLevel cl)
 {
     if (usePrepared)
     {
         BoundStatement bs = prepared.Bind(0);
         for (int i = 0; i < n; ++i)
         {
             IPAddress        ccord;
             ConsistencyLevel cac;
             using (var rs = c.Session.Execute(bs))
             {
                 ccord = rs.Info.QueriedHost;
                 cac   = rs.Info.AchievedConsistency;
             }
             addCoordinator(ccord, cac);
         }
     }
     else
     {
         RoutingKey routingKey = new RoutingKey();
         routingKey.RawRoutingKey = Enumerable.Repeat((byte)0x00, 4).ToArray();
         for (int i = 0; i < n; ++i)
         {
             IPAddress        ccord;
             ConsistencyLevel cac;
             using (var rs = c.Session.Execute(new SimpleStatement(String.Format("SELECT * FROM {0} WHERE k = 0", TABLE)).SetRoutingKey(routingKey).SetConsistencyLevel(cl)))
             {
                 ccord = rs.Info.QueriedHost;
                 cac   = rs.Info.AchievedConsistency;
                 Console.WriteLine(string.Format("Query {0} executed by {1} with consistency {2}", i.ToString(), ccord.ToString(), cac.ToString()));
             }
             addCoordinator(ccord, cac);
         }
     }
 }
        public void mixedDurationTestCCM()
        {
            if (CCMCluster != null)
                CCMCluster.Discard();
            CCMCluster = CCMBridge.CCMCluster.Create(3, Cluster.Builder());
            cluster = CCMCluster.Cluster;
            session = CCMCluster.Session;

            session.CreateKeyspace("large_data", ReplicationStrategies.CreateSimpleStrategyReplicationProperty(3));
            session.ChangeKeyspace("large_data");
            session.WaitForSchemaAgreement(
                session.Execute(String.Format("CREATE TABLE {0} (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows")));
            session.WaitForSchemaAgreement(
                session.Execute(String.Format("CREATE TABLE {0} (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows")));
            session.WaitForSchemaAgreement(
                session.Execute(String.Format("CREATE TABLE {0} (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows")));
            session.WaitForSchemaAgreement(
                session.Execute(String.Format("CREATE TABLE {0} (k int PRIMARY KEY, i text)", "large_text")));

            // Create the extra wide table definition
            StringBuilder tableDeclaration = new StringBuilder();
            tableDeclaration.Append("CREATE TABLE wide_table (");
            tableDeclaration.Append("k INT PRIMARY KEY");
            for (int i = 0; i < 330; ++i)
            {
                tableDeclaration.Append(String.Format(", {0} INT", createColumnName(i)));
            }
            tableDeclaration.Append(")");
            session.WaitForSchemaAgreement(
                session.Execute(tableDeclaration.ToString())
            );

            Random rndm = new Random(DateTime.Now.Millisecond);
            try
            {
                for (int i = 0; i < 10; ++i)
                {
                    switch ((int)rndm.Next(0,5))
                    {
                        case 0: testWideRows(); break;
                        case 1: testWideBatchRows(); break;
                        case 2: testByteRows(); break;
                        case 3: testLargeText(); break;
                        case 4: testWideTable(); break;
                        default: break;
                    }
                }
            }
            catch (Exception e)
            {
                CCMCluster.ErrorOut();
                throw e;
            }
            finally
            {
                CCMCluster.Discard();
            }
        }
 public void SetFixture()
 {
     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
     CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
     Session    = CCMCluster.Session;
     Cluster    = CCMCluster.Cluster;
     Session.CreateKeyspaceIfNotExists(Keyspace);
     Session.ChangeKeyspace(Keyspace);
 }
Beispiel #7
0
 public void SetFixture()
 {
     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
     CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
     session = CCMCluster.Session;
     Cluster = CCMCluster.Cluster;
     session.CreateKeyspaceIfNotExists(keyspaceName);
     session.ChangeKeyspace(keyspaceName);
     ents = new TweetsContext(session);
 }
Beispiel #8
0
 public void SetFixture()
 {
     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
     CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
     session    = CCMCluster.Session;
     Cluster    = CCMCluster.Cluster;
     session.CreateKeyspaceIfNotExists(keyspaceName);
     session.ChangeKeyspace(keyspaceName);
     ents = new TweetsContext(session);
 }
        private void SetupDefaultCluster()
        {
            if (CCMCluster != null)
            {
                CCMCluster.Discard();
            }

            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            session    = CCMCluster.Session;
            cluster    = CCMCluster.Cluster;
            session.CreateKeyspaceIfNotExists(ksname);
            session.ChangeKeyspace(ksname);
        }
Beispiel #10
0
        public void readTimeoutExceptionCCM()
        {
            var builder = Cluster.Builder();

            CCMBridge.CCMCluster cluster = CCMBridge.CCMCluster.Create(3, builder);
            try
            {
                Session   session = cluster.Session;
                CCMBridge bridge  = cluster.CCMBridge;

                String keyspace          = "TestKeyspace";
                String table             = "TestTable";
                int    replicationFactor = 3;
                String key = "1";

                session.Cluster.WaitForSchemaAgreement(
                    session.Execute(String.Format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor)));
                session.Execute("USE " + keyspace);
                session.Cluster.WaitForSchemaAgreement(
                    session.Execute(String.Format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table)));

                session.Execute(new SimpleStatement(String.Format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).SetConsistencyLevel(ConsistencyLevel.All));
                session.Execute(new SimpleStatement(String.Format(TestUtils.SELECT_ALL_FORMAT, table)).SetConsistencyLevel(ConsistencyLevel.All)).Dispose();

                bridge.ForceStop(2);
                try
                {
                    session.Execute(new SimpleStatement(String.Format(TestUtils.SELECT_ALL_FORMAT, table)).SetConsistencyLevel(ConsistencyLevel.All)).Dispose();
                }
                catch (ReadTimeoutException e)
                {
                    Assert.Equal(e.ConsistencyLevel, ConsistencyLevel.All);
                    Assert.Equal(e.ReceivedAcknowledgements, 2);
                    Assert.Equal(e.RequiredAcknowledgements, 3);
                    Assert.Equal(e.WasDataRetrieved, true);

                    //ReadTimeoutException copy = (ReadTimeoutException)e.copy();
                    //Assert.Equal(copy.Message, e.Message);
                    //Assert.Equal(copy.WasDataRetrieved, e.WasDataRetrieved);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cluster.Discard();
            }
        }
Beispiel #11
0
        public void tokenAwareWithRF2TestCCM()
        {
            var builder = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));

            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, builder);
            createSchema(c.Session, 2);
            try
            {
                init(c, 12);
                query(c, 12);

                // Not the best test ever, we should use OPP and check we do it the
                // right nodes. But since M3P is hard-coded for now, let just check
                // we just hit only one node.
                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 12);
                assertQueried(Options.Default.IP_PREFIX + "3", 0);

                resetCoordinators();
                c.CCMBridge.BootstrapNode(3);
                TestUtils.waitFor(Options.Default.IP_PREFIX + "3", c.Cluster, 60);

                query(c, 12);

                // We should still be hitting only one node
                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 12);
                assertQueried(Options.Default.IP_PREFIX + "3", 0);

                resetCoordinators();
                c.CCMBridge.Stop(2);
                TestUtils.waitForDown(Options.Default.IP_PREFIX + "2", c.Cluster, 60);

                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 6);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                assertQueried(Options.Default.IP_PREFIX + "3", 6);
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #12
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 #13
0
        public void checkMetadata(string TableName = null, string KeyspaceName = null, TableOptions tableOptions = null)
        {
            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            try
            {
                Session = CCMCluster.Session;
                Cluster = CCMCluster.Cluster;
                Session.CreateKeyspaceIfNotExists(Keyspace);
                Session.ChangeKeyspace(Keyspace);

                checkPureMetadata(TableName, KeyspaceName, tableOptions);
            }
            finally
            {
                CCMCluster.Discard();
            }
        }
Beispiel #14
0
        public void rowsetIteratedTwice()
        {
            var builder = Cluster.Builder();

            CCMBridge.CCMCluster cluster = CCMBridge.CCMCluster.Create(1, builder);
            try
            {
                Session   session = cluster.Session;
                CCMBridge bridge  = cluster.CCMBridge;

                String keyspace = "TestKeyspace";
                String table    = "TestTable";
                String key      = "1";

                session.Cluster.WaitForSchemaAgreement(
                    session.Execute(String.Format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1))
                    );
                session.Execute("USE " + keyspace);
                session.Cluster.WaitForSchemaAgreement(
                    session.Execute(String.Format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table))
                    );

                session.Execute(new SimpleStatement(String.Format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)));
                var rowset = session.Execute(new SimpleStatement(String.Format(TestUtils.SELECT_ALL_FORMAT, table))).GetRows();
                var cnt    = rowset.Count();
                try
                {
                    foreach (var r in rowset)
                    {
                        Console.Write(r.GetValue <string>("k"));
                    }
                    Assert.Fail();
                }
                catch (InvalidOperationException)
                {
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cluster.Discard();
            }
        }
Beispiel #15
0
        public void forceStopCCM()
        {
            var builder = Cluster.Builder().WithLoadBalancingPolicy(new RoundRobinPolicy());

            builder.WithQueryTimeout(10000);
            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(4, builder);
            createSchema(c.Session);
            try
            {
                init(c, 12);
                query(c, 12);
                resetCoordinators();
                c.CCMBridge.ForceStop(1);
                c.CCMBridge.ForceStop(2);
                TestUtils.waitForDown(Options.Default.IP_PREFIX + "1", c.Cluster, 40);
                TestUtils.waitForDown(Options.Default.IP_PREFIX + "2", c.Cluster, 40);

                query(c, 12);

                c.CCMBridge.ForceStop(3);
                c.CCMBridge.ForceStop(4);
                TestUtils.waitForDown(Options.Default.IP_PREFIX + "3", c.Cluster, 40);
                TestUtils.waitForDown(Options.Default.IP_PREFIX + "4", c.Cluster, 40);

                try
                {
                    query(c, 12);
                    Assert.Fail();
                }
                catch (NoHostAvailableException e)
                {
                    // No more nodes so ...
                }
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #16
0
        public void PoliciesAreDifferentInstancesWhenDefault()
        {
            var builder = Cluster.Builder();

            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, 2, builder);

            using (var cluster1 = builder.WithConnectionString(String.Format("Contact Points={0}1", Options.Default.IP_PREFIX)).Build())
                using (var cluster2 = builder.WithConnectionString(String.Format("Contact Points={0}2", Options.Default.IP_PREFIX)).Build())
                {
                    using (var session1 = cluster1.Connect())
                        using (var session2 = cluster2.Connect())
                        {
                            Assert.True(!Object.ReferenceEquals(session1.Policies.LoadBalancingPolicy, session2.Policies.LoadBalancingPolicy), "Load balancing policy instances should be different");
                            Assert.True(!Object.ReferenceEquals(session1.Policies.ReconnectionPolicy, session2.Policies.ReconnectionPolicy), "Reconnection policy instances should be different");
                            Assert.True(!Object.ReferenceEquals(session1.Policies.RetryPolicy, session2.Policies.RetryPolicy), "Retry policy instances should be different");
                        }
                }
        }
Beispiel #17
0
        public void roundRobinTestCCM()
        {
            var builder = Cluster.Builder().WithLoadBalancingPolicy(new RoundRobinPolicy());

            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, builder);
            createSchema(c.Session);
            try
            {
                init(c, 12);
                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 6);
                assertQueried(Options.Default.IP_PREFIX + "2", 6);

                resetCoordinators();
                c.CCMBridge.BootstrapNode(3);
                TestUtils.waitFor(Options.Default.IP_PREFIX + "3", c.Cluster, 60);

                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 4);
                assertQueried(Options.Default.IP_PREFIX + "2", 4);
                assertQueried(Options.Default.IP_PREFIX + "3", 4);

                resetCoordinators();
                c.CCMBridge.DecommissionNode(1);
                TestUtils.waitForDecommission(Options.Default.IP_PREFIX + "1", c.Cluster, 60);

                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "2", 6);
                assertQueried(Options.Default.IP_PREFIX + "3", 6);
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #18
0
 protected void query(CCMBridge.CCMCluster c, int n, bool usePrepared, ConsistencyLevel cl)
 {
     if (usePrepared)
     {
         BoundStatement bs = prepared.Bind(0);
         for (int i = 0; i < n; ++i)
         {
             addCoordinator(c.Session.Execute(bs));
         }
     }
     else
     {
         CassandraRoutingKey routingKey = new CassandraRoutingKey();
         routingKey.RawRoutingKey = Enumerable.Repeat((byte)0x00, 4).ToArray();
         for (int i = 0; i < n; ++i)
         {
             addCoordinator(c.Session.Execute(new SimpleStatement(String.Format("SELECT * FROM {0} WHERE k = 0", TABLE)).SetRoutingKey(routingKey).SetConsistencyLevel(cl)));
         }
     }
 }
        public void SetFixture()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            var clusterb = Cluster.Builder();

            clusterb.WithReconnectionPolicy(new ConstantReconnectionPolicy(100));

            var rp = new RoundRobinPolicyWithReconnectionRetries(new ConstantReconnectionPolicy(100));

            rp.ReconnectionEvent += new EventHandler <RoundRobinPolicyWithReconnectionRetriesEventArgs>((s, ev) =>
            {
                Console.Write("o");
                System.Threading.Thread.Sleep((int)ev.DelayMs);
            });
            clusterb.WithLoadBalancingPolicy(rp);
            clusterb.WithQueryTimeout(60 * 1000);

            CCMCluster = CCMBridge.CCMCluster.Create(2, clusterb);
            Session    = CCMCluster.Session;
            Cluster    = CCMCluster.Cluster;
        }
Beispiel #20
0
        public void checkKSMetadata()
        {
            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            try
            {
                Session = CCMCluster.Session;
                Cluster = CCMCluster.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.ToString(), durableWrites.ToString()))
                    );
                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
            {
                CCMCluster.Discard();
            }
        }
Beispiel #21
0
 protected void init(CCMBridge.CCMCluster c, int n, bool batch)
 {
     init(c, n, batch, ConsistencyLevel.One);
 }
Beispiel #22
0
        /// <summary>
        ///  Init methods that handle writes using batch and consistency options.
        /// </summary>

        protected void init(CCMBridge.CCMCluster c, int n)
        {
            init(c, n, false, ConsistencyLevel.One);
        }
Beispiel #23
0
        public void defaultPolicyTest(Builder builder)
        {
            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, builder);
            createSchema(c.Session);

            // FIXME: Race condition where the nodes are not fully up yet and assertQueried reports slightly different numbers with fallthrough*Policy
            Thread.Sleep(5000);
            try
            {
                init(c, 12);
                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 6);
                assertQueried(Options.Default.IP_PREFIX + "2", 6);

                resetCoordinators();

                // Test reads
                bool successfulQuery = false;
                bool readTimeoutOnce = false;
                bool unavailableOnce = false;
                bool restartOnce     = false;
                for (int i = 0; i < 100; ++i)
                {
                    try
                    {
                        // Force a ReadTimeoutException to be performed once
                        if (!readTimeoutOnce)
                        {
                            c.CCMBridge.ForceStop(2);
                        }

                        // Force an UnavailableException to be performed once
                        if (readTimeoutOnce && !unavailableOnce)
                        {
                            TestUtils.waitForDownWithWait(Options.Default.IP_PREFIX + "2", c.Cluster, 5);
                        }

                        // Bring back node to ensure other errors are not thrown on restart
                        if (unavailableOnce && !restartOnce)
                        {
                            c.CCMBridge.Start(2);
                            restartOnce = true;
                        }

                        query(c, 12);

                        if (restartOnce)
                        {
                            successfulQuery = true;
                        }
                    }
                    catch (UnavailableException e)
                    {
                        //                        Assert.Equal("Not enough replica available for query at consistency ONE (1 required but only 0 alive)".ToLower(), e.Message.ToLower());
                        unavailableOnce = true;
                    }
                    catch (ReadTimeoutException e)
                    {
                        //                        Assert.Equal("Cassandra timeout during read query at consistency ONE (1 responses were required but only 0 replica responded)".ToLower(), e.Message.ToLower());
                        readTimeoutOnce = true;
                    }
                }

                // Ensure the full cycle was completed
                Assert.True(successfulQuery, "Hit testing race condition. [Never completed successfully.] (Shouldn't be an issue.):\n");
                Assert.True(readTimeoutOnce, "Hit testing race condition. [Never encountered a ReadTimeoutException.] (Shouldn't be an issue.):\n");
                Assert.True(unavailableOnce, "Hit testing race condition. [Never encountered an UnavailableException.] (Shouldn't be an issue.):\n");

                // A weak test to ensure that the nodes were contacted
                assertQueriedAtLeast(Options.Default.IP_PREFIX + "1", 1);
                assertQueriedAtLeast(Options.Default.IP_PREFIX + "2", 1);

                resetCoordinators();


                // Test writes
                successfulQuery = false;
                bool writeTimeoutOnce = false;
                unavailableOnce = false;
                restartOnce     = false;
                for (int i = 0; i < 100; ++i)
                {
                    try
                    {
                        // Force a WriteTimeoutException to be performed once
                        if (!writeTimeoutOnce)
                        {
                            c.CCMBridge.ForceStop(2);
                        }

                        // Force an UnavailableException to be performed once
                        if (writeTimeoutOnce && !unavailableOnce)
                        {
                            TestUtils.waitForDownWithWait(Options.Default.IP_PREFIX + "2", c.Cluster, 5);
                        }

                        // Bring back node to ensure other errors are not thrown on restart
                        if (unavailableOnce && !restartOnce)
                        {
                            c.CCMBridge.Start(2);
                            restartOnce = true;
                        }

                        init(c, 12);

                        if (restartOnce)
                        {
                            successfulQuery = true;
                        }
                    }
                    catch (UnavailableException e)
                    {
                        //                        Assert.Equal("Not enough replica available for query at consistency ONE (1 required but only 0 alive)".ToLower(), e.Message.ToLower());
                        unavailableOnce = true;
                    }
                    catch (WriteTimeoutException e)
                    {
                        //                        Assert.Equal("Cassandra timeout during write query at consistency ONE (1 replica were required but only 0 acknowledged the write)".ToLower(), e.Message.ToLower());
                        writeTimeoutOnce = true;
                    }
                }
                // Ensure the full cycle was completed
                Assert.True(successfulQuery, "Hit testing race condition. [Never completed successfully.] (Shouldn't be an issue.):\n");
                Assert.True(writeTimeoutOnce, "Hit testing race condition. [Never encountered a ReadTimeoutException.] (Shouldn't be an issue.):\n");
                Assert.True(unavailableOnce, "Hit testing race condition. [Never encountered an UnavailableException.] (Shouldn't be an issue.):\n");

                // TODO: Missing test to see if nodes were written to

                // Test batch writes
                successfulQuery  = false;
                writeTimeoutOnce = false;
                unavailableOnce  = false;
                restartOnce      = false;
                for (int i = 0; i < 100; ++i)
                {
                    try
                    {
                        // Force a WriteTimeoutException to be performed once
                        if (!writeTimeoutOnce)
                        {
                            c.CCMBridge.ForceStop(2);
                        }

                        // Force an UnavailableException to be performed once
                        if (writeTimeoutOnce && !unavailableOnce)
                        {
                            TestUtils.waitForDownWithWait(Options.Default.IP_PREFIX + "2", c.Cluster, 5);
                        }

                        // Bring back node to ensure other errors are not thrown on restart
                        if (unavailableOnce && !restartOnce)
                        {
                            c.CCMBridge.Start(2);
                            restartOnce = true;
                        }

                        init(c, 12, true);

                        if (restartOnce)
                        {
                            successfulQuery = true;
                        }
                    }
                    catch (UnavailableException e)
                    {
                        //                        Assert.Equal("Not enough replica available for query at consistency ONE (1 required but only 0 alive)", e.Message);
                        unavailableOnce = true;
                    }
                    catch (WriteTimeoutException e)
                    {
                        //                        Assert.Equal("Cassandra timeout during write query at consistency ONE (1 replica were required but only 0 acknowledged the write)", e.Message);
                        writeTimeoutOnce = true;
                    }
                }
                // Ensure the full cycle was completed
                Assert.True(successfulQuery, "Hit testing race condition. [Never completed successfully.] (Shouldn't be an issue.):\n");
                Assert.True(writeTimeoutOnce, "Hit testing race condition. [Never encountered a ReadTimeoutException.] (Shouldn't be an issue.):\n");
                Assert.True(unavailableOnce, "Hit testing race condition. [Never encountered an UnavailableException.] (Shouldn't be an issue.):\n");

                // TODO: Missing test to see if nodes were written to
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #24
0
        public void alwaysRetryRetryPolicyTest()
        {
            Console.Write("MainThread is");
            Console.Write("[");
            Console.Write(Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("]");

            var builder = Cluster.Builder().WithRetryPolicy(new LoggingRetryPolicy(AlwaysRetryRetryPolicy.Instance));

            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, builder);
            createSchema(c.Session);

            try
            {
                init(c, 12);
                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 6);
                assertQueried(Options.Default.IP_PREFIX + "2", 6);

                resetCoordinators();

                // Test failed reads
                c.CCMBridge.ForceStop(2);

                Thread t1 = new Thread(() =>
                {
                    Console.Write("Thread started");
                    Console.Write("[");
                    Console.Write(Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine("]");

                    try
                    {
                        query(c, 12);
                    }
                    catch (ThreadInterruptedException)
                    {
                        Console.Write("Thread broke");
                        Console.Write("[");
                        Console.Write(Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine("]");
                    }
                    Console.Write("Thread finished");
                    Console.Write("[");
                    Console.Write(Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine("]");
                });
                t1.Start();
                t1.Join(10000);
                if (t1.IsAlive)
                {
                    t1.Interrupt();
                }

                t1.Join();

                // A weak test to ensure that the nodes were contacted
                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                resetCoordinators();


                c.CCMBridge.Start(2);
                TestUtils.waitFor(Options.Default.IP_PREFIX + "2", c.Cluster, 30);

                Console.Write("MainThread started");
                Console.Write("[");
                Console.Write(Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("]");

                // Test successful reads
                for (int i = 0; i < 10; ++i)
                {
                    try
                    {
                        query(c, 12);
                    }
                    catch (ThreadInterruptedException)
                    {
                        Console.Write("Main Thread broke");
                        Console.Write("[");
                        Console.Write(Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine("]");
                    }
                }

                Console.Write("Main Thread finished");
                Console.Write("[");
                Console.Write(Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("]");

                // A weak test to ensure that the nodes were contacted
                assertQueriedAtLeast(Options.Default.IP_PREFIX + "1", 1);
                assertQueriedAtLeast(Options.Default.IP_PREFIX + "2", 1);
                resetCoordinators();


                // Test writes
                for (int i = 0; i < 100; ++i)
                {
                    init(c, 12);
                }

                // TODO: Missing test to see if nodes were written to


                // Test failed writes
                c.CCMBridge.ForceStop(2);
                Thread t2 = new Thread(() =>
                {
                    Console.WriteLine("2 Thread started");
                    try
                    {
                        init(c, 12);
                        Assert.Fail();
                    }
                    catch (ThreadInterruptedException)
                    {
                        Console.WriteLine("2 Thread async call broke");
                    }
                    catch (NoHostAvailableException)
                    {
                        Console.WriteLine("2 Thread no host");
                    }
                    Console.WriteLine("2 Thread finished");
                });
                t2.Start();
                t2.Join(10000);
                if (t2.IsAlive)
                {
                    t2.Interrupt();
                }

                t2.Join();

                // TODO: Missing test to see if nodes were written to
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #25
0
 protected void query(CCMBridge.CCMCluster c, int n, bool usePrepared)
 {
     query(c, n, usePrepared, ConsistencyLevel.One);
 }
 public void SetFixture()
 {
     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
     CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
     Session = CCMCluster.Session;
     Cluster = CCMCluster.Cluster;
     Session.CreateKeyspaceIfNotExists(Keyspace);
     Session.ChangeKeyspace(Keyspace);
 }
Beispiel #27
0
        public void unavailableExceptionCCM()
        {
            var builder = Cluster.Builder();

            CCMBridge.CCMCluster cluster = CCMBridge.CCMCluster.Create(3, builder);
            try
            {
                Session   session = cluster.Session;
                CCMBridge bridge  = cluster.CCMBridge;

                String keyspace          = "TestKeyspace";
                String table             = "TestTable";
                int    replicationFactor = 3;
                String key = "1";

                session.Cluster.WaitForSchemaAgreement(
                    session.Execute(String.Format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor))
                    );
                session.Execute("USE " + keyspace);
                session.Cluster.WaitForSchemaAgreement(
                    session.Execute(String.Format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table))
                    );

                session.Execute(new SimpleStatement(String.Format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).SetConsistencyLevel(ConsistencyLevel.All));
                session.Execute(new SimpleStatement(String.Format(TestUtils.SELECT_ALL_FORMAT, table)).SetConsistencyLevel(ConsistencyLevel.All)).Dispose();

                bridge.Stop(2);
                // Ensure that gossip has reported the node as down.
                Thread.Sleep(1000);

                try
                {
                    session.Execute(new SimpleStatement(String.Format(TestUtils.SELECT_ALL_FORMAT, table)).SetConsistencyLevel(ConsistencyLevel.All)).Dispose();
                }
                catch (UnavailableException e)
                {
                    Assert.Equal(e.Consistency, ConsistencyLevel.All);
                    Assert.Equal(e.RequiredReplicas, replicationFactor);
                    Assert.Equal(e.AliveReplicas, replicationFactor - 1);
                }

                try
                {
                    session.Execute(new SimpleStatement(String.Format(TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).SetConsistencyLevel(ConsistencyLevel.All));
                }
                catch (UnavailableException e)
                {
                    String expectedError = String.Format("Not enough replica available for query at consistency {0} ({1} required but only {2} alive)", ConsistencyLevel.All, 3, 2);
                    Assert.Equal(e.Message, expectedError);
                    Assert.Equal(e.Consistency, ConsistencyLevel.All);
                    Assert.Equal(e.RequiredReplicas, replicationFactor);
                    Assert.Equal(e.AliveReplicas, replicationFactor - 1);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cluster.Discard();
            }
        }
        public void SetFixture()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
               	     var clusterb = Cluster.Builder();
            clusterb.WithReconnectionPolicy(new ConstantReconnectionPolicy(100));

            var rp = new RoundRobinPolicyWithReconnectionRetries(new ConstantReconnectionPolicy(100));
            rp.ReconnectionEvent += new EventHandler<RoundRobinPolicyWithReconnectionRetriesEventArgs>((s, ev) =>
            {
                Console.Write("o");
                System.Threading.Thread.Sleep((int)ev.DelayMs);
            });
            clusterb.WithLoadBalancingPolicy(rp);
            clusterb.WithQueryTimeout(60 * 1000);

            CCMCluster = CCMBridge.CCMCluster.Create(2, clusterb);
            Session = CCMCluster.Session;
            Cluster = CCMCluster.Cluster;
        }
Beispiel #29
0
        private void SetupDefaultCluster()
        {
            if (CCMCluster != null)
                CCMCluster.Discard();

            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            session = CCMCluster.Session;
            cluster = CCMCluster.Cluster;
            session.CreateKeyspaceIfNotExists(ksname);
            session.ChangeKeyspace(ksname);
        }
Beispiel #30
0
 protected void init(CCMBridge.CCMCluster c, int n, ConsistencyLevel cl)
 {
     init(c, n, false, cl);
 }
Beispiel #31
0
 /// <summary>
 ///  Query methods that handle reads based on PreparedStatements and/or
 ///  ConsistencyLevels.
 /// </summary>
 protected void query(CCMBridge.CCMCluster c, int n)
 {
     query(c, n, false, ConsistencyLevel.One);
 }
Beispiel #32
0
        /// <summary>
        ///  Tests DowngradingConsistencyRetryPolicy
        /// </summary>

        public void downgradingConsistencyRetryPolicy(Builder builder)
        {
            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(3, builder);
            createSchema(c.Session, 3);

            // FIXME: Race condition where the nodes are not fully up yet and assertQueried reports slightly different numbers
            Thread.Sleep(5000);
            try
            {
                init(c, 12, ConsistencyLevel.All);
                query(c, 12, ConsistencyLevel.All);

                assertQueried(Options.Default.IP_PREFIX + "1", 4);
                assertQueried(Options.Default.IP_PREFIX + "2", 4);
                assertQueried(Options.Default.IP_PREFIX + "3", 4);

                resetCoordinators();
                c.CCMBridge.ForceStop(2);
                TestUtils.waitForDownWithWait(Options.Default.IP_PREFIX + "2", c.Cluster, 10);

                query(c, 12, ConsistencyLevel.All);

                assertQueried(Options.Default.IP_PREFIX + "1", 6);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                assertQueried(Options.Default.IP_PREFIX + "3", 6);

                assertAchievedConsistencyLevel(ConsistencyLevel.Two);

                resetCoordinators();
                c.CCMBridge.ForceStop(1);
                TestUtils.waitForDownWithWait(Options.Default.IP_PREFIX + "1", c.Cluster, 5);
                Thread.Sleep(5000);

                try
                {
                    query(c, 12, ConsistencyLevel.All);
                }
                catch (ReadTimeoutException e)
                {
                    //                    assertEquals("Cassandra timeout during read query at consistency TWO (2 responses were required but only 1 replica responded)", e.getMessage());
                }

                query(c, 12, ConsistencyLevel.Quorum);

                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                assertQueried(Options.Default.IP_PREFIX + "3", 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.One);

                resetCoordinators();

                query(c, 12, ConsistencyLevel.Two);

                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                assertQueried(Options.Default.IP_PREFIX + "3", 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.One);

                resetCoordinators();

                query(c, 12, ConsistencyLevel.One);

                assertQueried(Options.Default.IP_PREFIX + "1", 0);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                assertQueried(Options.Default.IP_PREFIX + "3", 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.One);
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #33
0
 protected void query(CCMBridge.CCMCluster c, int n, ConsistencyLevel cl)
 {
     query(c, n, false, cl);
 }
Beispiel #34
0
        /// <summary>
        ///  Tests DowngradingConsistencyRetryPolicy
        /// </summary>

        public void downgradingConsistencyRetryPolicy(Builder builder)
        {
            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(3, builder);
            createSchema(c.Session, 3);

            // FIXME: Race condition where the nodes are not fully up yet and assertQueried reports slightly different numbers
            Thread.Sleep(5000);
            try
            {
                init(c, 12, ConsistencyLevel.All);
                query(c, 12, ConsistencyLevel.All);

                var hosts = new string[] {
                    "unused",
                    Options.Default.IP_PREFIX + "1",
                    Options.Default.IP_PREFIX + "2",
                    Options.Default.IP_PREFIX + "3"
                };

                assertQueried(hosts[1], 4);
                assertQueried(hosts[2], 4);
                assertQueried(hosts[3], 4);

                resetCoordinators();
                c.CCMBridge.ForceStop(2);
                TestUtils.waitForDownWithWait(hosts[2], c.Cluster, 10);

                query(c, 12, ConsistencyLevel.All);

                // counts are allowed to be off by 1, due to roundrobin modulo shift when "node down" update becomes visible.
                assertQueriedAtLeast(hosts[1], 5);
                assertQueried(hosts[2], 0);
                assertQueriedAtLeast(hosts[3], 5);
                assertQueriedSet(new string[] { hosts[1], hosts[3] }, 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.Two);

                resetCoordinators();
                c.CCMBridge.ForceStop(1);
                TestUtils.waitForDownWithWait(hosts[1], c.Cluster, 5);
                Thread.Sleep(5000);

                try
                {
                    query(c, 12, ConsistencyLevel.All);
                }
                catch (ReadTimeoutException e)
                {
                    //                    assertEquals("Cassandra timeout during read query at consistency TWO (2 responses were required but only 1 replica responded)", e.getMessage());
                }

                query(c, 12, ConsistencyLevel.Quorum);

                assertQueried(hosts[1], 0);
                assertQueried(hosts[2], 0);
                assertQueried(hosts[3], 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.One);

                resetCoordinators();

                query(c, 12, ConsistencyLevel.Two);

                assertQueried(hosts[1], 0);
                assertQueried(hosts[2], 0);
                assertQueried(hosts[3], 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.One);

                resetCoordinators();

                query(c, 12, ConsistencyLevel.One);

                assertQueried(hosts[1], 0);
                assertQueried(hosts[2], 0);
                assertQueried(hosts[3], 12);

                assertAchievedConsistencyLevel(ConsistencyLevel.One);
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
Beispiel #35
0
        public void alwaysIgnoreRetryPolicyTest()
        {
            var builder = Cluster.Builder().WithRetryPolicy(new LoggingRetryPolicy(AlwaysIgnoreRetryPolicy.Instance));

            CCMBridge.CCMCluster c = CCMBridge.CCMCluster.Create(2, builder);
            createSchema(c.Session);

            try
            {
                init(c, 12);
                query(c, 12);

                assertQueried(Options.Default.IP_PREFIX + "1", 6);
                assertQueried(Options.Default.IP_PREFIX + "2", 6);

                resetCoordinators();

                // Test failed reads
                c.CCMBridge.ForceStop(2);
                for (int i = 0; i < 10; ++i)
                {
                    query(c, 12);
                }

                // A weak test to ensure that the nodes were contacted
                assertQueried(Options.Default.IP_PREFIX + "1", 120);
                assertQueried(Options.Default.IP_PREFIX + "2", 0);
                resetCoordinators();


                c.CCMBridge.Start(2);
                TestUtils.waitFor(Options.Default.IP_PREFIX + "2", c.Cluster, 30);

                // Test successful reads
                for (int i = 0; i < 10; ++i)
                {
                    query(c, 12);
                }

                // A weak test to ensure that the nodes were contacted
                assertQueriedAtLeast(Options.Default.IP_PREFIX + "1", 1);
                assertQueriedAtLeast(Options.Default.IP_PREFIX + "2", 1);
                resetCoordinators();


                // Test writes
                for (int i = 0; i < 100; ++i)
                {
                    init(c, 12);
                }

                // TODO: Missing test to see if nodes were written to


                // Test failed writes
                c.CCMBridge.ForceStop(2);
                for (int i = 0; i < 100; ++i)
                {
                    init(c, 12);
                }

                // TODO: Missing test to see if nodes were written to
            }
            catch (Exception e)
            {
                c.ErrorOut();
                throw e;
            }
            finally
            {
                resetCoordinators();
                c.Discard();
            }
        }
 public void SetFixture()
 {
     CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
     Session = CCMCluster.Session;
     Cluster = CCMCluster.Cluster;
 }