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);
        }
        public void TokenMap_Rebuild_With_Decommissioned_DC_Existing_RF()
        {
            // Create a 2dc:1node each cluster
            var clusterOptions = new TestClusterOptions();

            clusterOptions.Dc2NodeLength = 1;
            var testCluster = TestClusterManager.CreateNew(1, clusterOptions);

            testCluster.Builder = Cluster.Builder().AddContactPoint("127.0.0.1").WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc1"));
            testCluster.Cluster = testCluster.Builder.Build();
            testCluster.Session = testCluster.Cluster.Connect();

            PolicyTestTools policyTestTools = new PolicyTestTools();

            // Create a ks with RF = dc1:1, dc2:1
            policyTestTools.CreateMultiDcSchema(testCluster.Session, 1, 1);
            policyTestTools.InitPreparedStatement(testCluster, 12, false, ConsistencyLevel.All);
            // Replicas now in each node in each dc

            policyTestTools.Query(testCluster, 12, false);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 12);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 0);
            testCluster.Cluster.Shutdown();

            testCluster.DecommissionNode(1);
            // dc1 no longer has any hosts

            testCluster.Builder = Cluster.Builder().AddContactPoint("127.0.0.2").WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2"));
            testCluster.Cluster = testCluster.Builder.Build();
            // Should be able to connect and rebuild token map
            testCluster.Session = testCluster.Cluster.Connect(policyTestTools.DefaultKeyspace);

            policyTestTools.ResetCoordinators();
            policyTestTools.Query(testCluster, 12, false);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 0);
            policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 12);
            testCluster.Cluster.Shutdown();
        }
Esempio n. 3
0
        public void ReplicationFactorThree_TwoDCs_DowngradingConsistencyRetryPolicy()
        {
            ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(3, 3, DefaultMaxClusterCreateRetries, true);

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

            _policyTestTools.CreateMultiDcSchema(testCluster.Session, 3, 3);
            _policyTestTools.InitPreparedStatement(testCluster, 12, ConsistencyLevel.Two);

            // a maximum of 4 IPs should have returned values for the query -- two copies per each of the two DCs
            int queriesPerIteration = 4;
            int queriesCompleted    = 0;
            int actualTries         = 0;
            int maxTries            = 20;

            while (_policyTestTools.Coordinators.Count() < 4 && actualTries < maxTries)
            {
                _policyTestTools.Query(testCluster, queriesPerIteration, ConsistencyLevel.Two);
                queriesCompleted += queriesPerIteration;
            }

            Assert.IsTrue(_policyTestTools.Coordinators.Count() >= 4, "The minimum number of hosts queried was not met!");
            int totalQueriesForAllHosts = _policyTestTools.Coordinators.Sum(c => c.Value);

            Assert.AreEqual(queriesCompleted, totalQueriesForAllHosts,
                            "The sum of queries for all hosts should equal the number of queries recorded by the calling test!");

            _policyTestTools.ResetCoordinators();
            testCluster.StopForce(2);
            // FIXME: This sleep is needed to allow the waitFor() to work
            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,
                ConsistencyLevel.LocalQuorum,
                ConsistencyLevel.EachQuorum
            };

            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>
                    {
                        "EACH_QUORUM ConsistencyLevel is only supported for writes",
                        "ANY ConsistencyLevel is only supported for writes"
                    };
                    Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message));
                }
            }

            // Test writes which should fail
            foreach (ConsistencyLevel consistencyLevel in failList)
            {
                try
                {
                    _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel);
                    Assert.Fail("Expected Exception was not thrown for ConsistencyLevel :" + consistencyLevel);
                }
                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 (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
                }
            }
        }