Exemplo n.º 1
0
        public void IdempotenceAwareRetryPolicy_Should_Use_ChildPolicy_OnReadTimeout()
        {
            var testPolicy = new TestRetryPolicy();
            var policy     = new IdempotenceAwareRetryPolicy(testPolicy);
            var decision   = policy.OnReadTimeout(new SimpleStatement("Q"), ConsistencyLevel.All, 0, 0, true, 1);

            Assert.AreEqual(decision.DecisionType, RetryDecision.RetryDecisionType.Ignore);
            Assert.AreEqual(1, testPolicy.ReadTimeoutCounter);
            Assert.AreEqual(0, testPolicy.WriteTimeoutCounter);
            Assert.AreEqual(0, testPolicy.UnavailableCounter);
        }
        public void IdempotenceAwareRetryPolicy_Should_Rethrow_OnWriteTimeout_With_Non_Idempotent_Statements()
        {
            var testPolicy = new TestRetryPolicy();
            var policy     = new IdempotenceAwareRetryPolicy(testPolicy);
            var decision   = policy.OnWriteTimeout(new SimpleStatement("Q").SetIdempotence(false), ConsistencyLevel.All, "BATCH", 0, 0, 1);

            Assert.AreEqual(decision.DecisionType, RetryDecision.RetryDecisionType.Rethrow);
            Assert.AreEqual(0, testPolicy.ReadTimeoutCounter);
            Assert.AreEqual(0, testPolicy.WriteTimeoutCounter);
            Assert.AreEqual(0, testPolicy.UnavailableCounter);
        }
        public void ShouldUseChildRetryPolicy_OnWriteTimeout()
        {
            const string keyspace = "idempotenceAwarepolicytestks";
            var          options  = new TestClusterOptions {
                CassandraYaml = new[] { "phi_convict_threshold: 16" }
            };
            var testCluster = TestClusterManager.CreateNew(2, options);

            using (var cluster = Cluster.Builder().AddContactPoint(testCluster.InitialContactPoint)
                                 .WithQueryTimeout(60000)
                                 .WithSocketOptions(new SocketOptions().SetConnectTimeoutMillis(30000))
                                 .Build())
            {
                var session   = cluster.Connect();
                var tableName = TestUtils.GetUniqueTableName();
                session.DeleteKeyspaceIfExists(keyspace);
                session.Execute(string.Format(TestUtils.CreateKeyspaceSimpleFormat, keyspace, 2), ConsistencyLevel.All);
                session.ChangeKeyspace(keyspace);
                session.Execute(new SimpleStatement(string.Format("CREATE TABLE {0} (k int PRIMARY KEY, i int)", tableName)).SetConsistencyLevel(ConsistencyLevel.All));

                testCluster.PauseNode(2);

                var testPolicy = new TestRetryPolicy();
                var policy     = new IdempotenceAwareRetryPolicy(testPolicy);

                try
                {
                    session.Execute(new SimpleStatement(string.Format("INSERT INTO {0}(k, i) VALUES (0, 0)", tableName))
                                    .SetIdempotence(true)
                                    .SetConsistencyLevel(ConsistencyLevel.All)
                                    .SetRetryPolicy(policy));
                }
                catch (WriteTimeoutException)
                {
                    //throws a WriteTimeoutException, as its set as an idempotent query, it will call the childPolicy
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.ReadTimeoutCounter));
                    Assert.AreEqual(1L, Interlocked.Read(ref testPolicy.WriteTimeoutCounter));
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.UnavailableCounter));
                }
                catch (UnavailableException)
                {
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.ReadTimeoutCounter));
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.WriteTimeoutCounter));
                    Assert.AreEqual(1L, Interlocked.Read(ref testPolicy.UnavailableCounter));
                }
                catch (Exception e)
                {
                    Trace.TraceWarning(e.Message);
                }

                Interlocked.Exchange(ref testPolicy.UnavailableCounter, 0);
                Interlocked.Exchange(ref testPolicy.WriteTimeoutCounter, 0);
                Interlocked.Exchange(ref testPolicy.ReadTimeoutCounter, 0);

                //testing with unidempotent query
                try
                {
                    session.Execute(new SimpleStatement(string.Format("INSERT INTO {0}(k, i) VALUES (0, 0)", tableName))
                                    .SetIdempotence(false)
                                    .SetConsistencyLevel(ConsistencyLevel.All)
                                    .SetRetryPolicy(policy));
                }
                catch (WriteTimeoutException)
                {
                    //throws a WriteTimeoutException, as its set as NOT an idempotent query, it will not call the childPolicy
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.ReadTimeoutCounter));
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.WriteTimeoutCounter));
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.UnavailableCounter));
                }
                catch (UnavailableException)
                {
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.ReadTimeoutCounter));
                    Assert.AreEqual(0L, Interlocked.Read(ref testPolicy.WriteTimeoutCounter));
                    Assert.AreEqual(1L, Interlocked.Read(ref testPolicy.UnavailableCounter));
                }
                catch (Exception e)
                {
                    Trace.TraceWarning(e.Message);
                }
            }

            testCluster.Remove();
        }