public void TokenAware_TargetPartition_NoHops()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            ITestCluster testCluster = TestClusterManager.CreateNew(3);
            testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
            testCluster.InitClient();

            // Test
            policyTestTools.TableName = TestUtils.GetUniqueTableName();
            policyTestTools.CreateSchema(testCluster.Session, 1);
            var traces = new List<QueryTrace>();
            for (var i = -10; i < 10; i++)
            {
                var partitionKey = BitConverter.GetBytes(i).Reverse().ToArray();
                var statement = new SimpleStatement(String.Format("INSERT INTO " + policyTestTools.TableName + " (k, i) VALUES ({0}, {0})", i))
                    .SetRoutingKey(new RoutingKey() { RawRoutingKey = partitionKey })
                    .EnableTracing();
                var rs = testCluster.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);
            }
        }
예제 #2
0
        public void BatchMixedStatements()
        {
            foreach (var protocolVersion in ProtocolVersionSupported)
            {
                //Use all possible protocol versions
                Cluster.MaxProtocolVersion = protocolVersion;
                //Use a local cluster
                var localCluster = Cluster.Builder().AddContactPoint(IpPrefix + "1").Build();
                var localSession = localCluster.Connect("tester");
                var tableName = "table" + Guid.NewGuid().ToString("N").ToLower();
                CreateTable(tableName);

                var simpleStatement =
                    new SimpleStatement(String.Format("INSERT INTO {0} (id, label, number) VALUES ({1}, {2}, {3})", tableName, 1, "label", 2));
                var ps = localSession.Prepare(string.Format(@"INSERT INTO {0} (id, label, number) VALUES (?, ?, ?)", tableName));
                var batchStatement = new BatchStatement();
                var expectedValues = new List<object[]> {new object[] {1, "label", 2}, new object[] {1, "test", 2}};

                batchStatement.Add(ps.Bind(new object[] {1, "test", 2}));
                batchStatement.Add(simpleStatement);

                var rs = localSession.Execute("SELECT * FROM " + tableName);
                VerifyData(rs, expectedValues);
            }
        }
예제 #3
0
        public void RequestHandlerRetryDecisionTest()
        {
            var statement = new SimpleStatement("SELECT WILL FAIL");
            var request = Session.GetRequest(statement);
            var requestHandler = new RequestHandler<RowSet>(Session, request, statement);

            //Using default retry policy the decision will always be to rethrow on read/write timeout
            var expected = RetryDecision.RetryDecisionType.Rethrow;
            var decision = requestHandler.GetRetryDecision(new ReadTimeoutException(ConsistencyLevel.Quorum, 1, 2, true));
            Assert.AreEqual(expected, decision.DecisionType);
            
            decision = requestHandler.GetRetryDecision(new WriteTimeoutException(ConsistencyLevel.Quorum, 1, 2, "SIMPLE"));
            Assert.AreEqual(expected, decision.DecisionType);

            decision = requestHandler.GetRetryDecision(new UnavailableException(ConsistencyLevel.Quorum, 2, 1));
            Assert.AreEqual(expected, decision.DecisionType);

            decision = requestHandler.GetRetryDecision(new Exception());
            Assert.AreEqual(expected, decision.DecisionType);

            //Expecting to retry when a Cassandra node is Bootstrapping/overloaded
            expected = RetryDecision.RetryDecisionType.Retry;
            decision = requestHandler.GetRetryDecision(new OverloadedException(null));
            Assert.AreEqual(expected, decision.DecisionType);
            decision = requestHandler.GetRetryDecision(new IsBootstrappingException(null));
            Assert.AreEqual(expected, decision.DecisionType);
            decision = requestHandler.GetRetryDecision(new TruncateException(null));
            Assert.AreEqual(expected, decision.DecisionType);
        }
        public void SimpleStatementNamedValuesNotSpecifiedTest()
        {
            var insertQuery = String.Format("INSERT INTO {0} (float_sample, text_sample, bigint_sample, id) VALUES (:MY_float, :my_TexT, :my_BIGint, :id)", AllTypesTableName);
            var statement = new SimpleStatement(insertQuery);

            Assert.Throws<InvalidQueryException>(() => Session.Execute(
                statement.Bind(
                    new {id = Guid.NewGuid(), my_bigint = 1L })));
        }
예제 #5
0
 public void RequestHandler_GetRequest_SimpleStatement_Default_QueryOptions_Are_Used()
 {
     var stmt = new SimpleStatement("DUMMY QUERY");
     Assert.AreEqual(0, stmt.PageSize);
     Assert.Null(stmt.ConsistencyLevel);
     var request = (QueryRequest)RequestHandler<RowSet>.GetRequest(stmt, 2, GetConfig());
     Assert.AreEqual(DefaultQueryOptions.GetPageSize(), request.PageSize);
     Assert.AreEqual(DefaultQueryOptions.GetConsistencyLevel(), request.Consistency);
 }
예제 #6
0
        public void Parallel_Insert_And_Select_Sync()
        {
            var originalTraceLevel = Diagnostics.CassandraTraceSwitch.Level;
                        ITestCluster testCluster = TestClusterManager.GetTestCluster(3);
            Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Warning;
            testCluster.Builder = Cluster.Builder().WithRetryPolicy(DowngradingConsistencyRetryPolicy.Instance);
            testCluster.InitClient();

            ISession session = testCluster.Session;
            string uniqueKsName = "keyspace_" + Randomm.RandomAlphaNum(10);
            session.Execute(@"CREATE KEYSPACE " + uniqueKsName +
                            " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};");
            TestUtils.WaitForSchemaAgreement(testCluster.Cluster);
            session.ChangeKeyspace(uniqueKsName);

            string tableName = "table_" + Guid.NewGuid().ToString("N").ToLower();
            session.Execute(String.Format(TestUtils.CREATE_TABLE_TIME_SERIES, tableName));
            TestUtils.WaitForSchemaAgreement(testCluster.Cluster);

            var insertQuery = String.Format("INSERT INTO {0} (id, event_time, text_sample) VALUES (?, ?, ?)", tableName);
            var insertQueryPrepared = session.Prepare(insertQuery);
            var selectQuery = String.Format("SELECT * FROM {0} LIMIT 10000", tableName);

            const int rowsPerId = 1000;
            object insertQueryStatement = new SimpleStatement(insertQuery);
            if (CassandraVersion.Major < 2)
            {
                //Use prepared statements all the way as it is not possible to bind on a simple statement with C* 1.2
                insertQueryStatement = session.Prepare(insertQuery);
            }
            var actionInsert = GetInsertAction(session, insertQueryStatement, ConsistencyLevel.Quorum, rowsPerId);
            var actionInsertPrepared = GetInsertAction(session, insertQueryPrepared, ConsistencyLevel.Quorum, rowsPerId);
            var actionSelect = GetSelectAction(session, selectQuery, ConsistencyLevel.Quorum, 10);

            //Execute insert sync to have some records
            actionInsert();
            //Execute select sync to assert that everything is going OK
            actionSelect();


            var actions = new List<Action>();
            for (var i = 0; i < 10; i++)
            {
                //Add 10 actions to execute
                actions.AddRange(new[] {actionInsert, actionSelect, actionInsertPrepared});
                actions.AddRange(new[] {actionSelect, actionInsert, actionInsertPrepared, actionInsert});
                actions.AddRange(new[] {actionInsertPrepared, actionInsertPrepared, actionSelect});
            }
            //Execute in parallel the 100 actions
            var parallelOptions = new ParallelOptions();
            parallelOptions.TaskScheduler = new ThreadPerTaskScheduler();
            parallelOptions.MaxDegreeOfParallelism = 300;
            Parallel.Invoke(parallelOptions, actions.ToArray());
            Parallel.Invoke(actions.ToArray());
            Diagnostics.CassandraTraceSwitch.Level = originalTraceLevel;
        }
 public void SimpleStatementSetTimestamp()
 {
     var timestamp = new DateTimeOffset(1999, 12, 31, 1, 2, 3, TimeSpan.Zero);
     var id = Guid.NewGuid();
     var insertStatement = new SimpleStatement(String.Format("INSERT INTO {0} (id, text_sample) VALUES (?, ?)", AllTypesTableName), id, "sample text");
     Session.Execute(insertStatement.SetTimestamp(timestamp));
     var row = Session.Execute(new SimpleStatement(String.Format("SELECT id, text_sample, writetime(text_sample) FROM {0} WHERE id = ?", AllTypesTableName), id)).First();
     Assert.NotNull(row.GetValue<string>("text_sample"));
     Assert.AreEqual(TypeCodec.ToUnixTime(timestamp).Ticks / 10, row.GetValue<object>("writetime(text_sample)"));
 }
예제 #8
0
 public void SimpleStatement_Default_QueryOptions_Are_Used()
 {
     var stmt = new SimpleStatement("DUMMY QUERY");
     Assert.AreEqual(0, stmt.PageSize);
     Assert.Null(stmt.ConsistencyLevel);
     var session = GetInstance();
     var request = (QueryRequest)session.GetRequest(stmt);
     Assert.AreEqual(DefaultQueryOptions.GetPageSize(), request.PageSize);
     Assert.AreEqual(DefaultQueryOptions.GetConsistencyLevel(), request.Consistency);
 }
예제 #9
0
 protected override Task<RowSet> InternalExecuteAsync()
 {
     if (_batchScript.Length == 0)
     {
         return TaskHelper.FromException<RowSet>(new RequestInvalidException("The Batch must contain queries to execute"));
     }
     string cqlQuery = GetCql();
     var stmt = new SimpleStatement(cqlQuery);
     this.CopyQueryPropertiesTo(stmt);
     return _session.ExecuteAsync(stmt);
 }
예제 #10
0
 public void Query_Payload_Test()
 {
     var outgoing = new Dictionary<string, byte[]> { { "k1", Encoding.UTF8.GetBytes("value1") }, { "k2", Encoding.UTF8.GetBytes("value2") } };
     var stmt = new SimpleStatement("SELECT * FROM system.local");
     stmt.SetOutgoingPayload(outgoing);
     var rs = Session.Execute(stmt);
     Assert.NotNull(rs.Info.IncomingPayload);
     Assert.AreEqual(outgoing.Count, rs.Info.IncomingPayload.Count);
     CollectionAssert.AreEqual(outgoing["k1"], rs.Info.IncomingPayload["k1"]);
     CollectionAssert.AreEqual(outgoing["k2"], rs.Info.IncomingPayload["k2"]);
 }
예제 #11
0
 public void RequestHandler_GetRequest_SimpleStatement_QueryOptions_Are_Used()
 {
     var stmt = new SimpleStatement("DUMMY QUERY");
     Assert.AreEqual(0, stmt.PageSize);
     Assert.Null(stmt.ConsistencyLevel);
     var queryOptions = new QueryOptions().SetConsistencyLevel(ConsistencyLevel.LocalQuorum).SetPageSize(100);
     var request = (QueryRequest)RequestHandler<RowSet>.GetRequest(stmt, 2, GetConfig(queryOptions));
     Assert.AreEqual(100, request.PageSize);
     Assert.AreEqual(queryOptions.GetPageSize(), request.PageSize);
     Assert.AreEqual(queryOptions.GetConsistencyLevel(), request.Consistency);
     Assert.AreEqual(ConsistencyLevel.Any, request.SerialConsistency);
 }
예제 #12
0
 public void Warnings_With_Tracing_Test()
 {
     const string query = "BEGIN UNLOGGED BATCH INSERT INTO {0} (k, t) VALUES ('{1}', '{2}') APPLY BATCH";
     SimpleStatement insert = new SimpleStatement(String.Format(query, Table, "warn1", String.Join("", Enumerable.Repeat("a", 5 * 1025))));
     var rs = Session.Execute(insert.EnableTracing());
     
     Assert.NotNull(rs.Info.Warnings);
     Assert.NotNull(rs.Info.QueryTrace);
     Assert.AreEqual(1, rs.Info.Warnings.Length);
     StringAssert.Contains("batch", rs.Info.Warnings[0].ToLowerInvariant());
     StringAssert.Contains("exceeding", rs.Info.Warnings[0].ToLowerInvariant());
 }
예제 #13
0
 public void SimpleStatement_QueryOptions_Are_Used()
 {
     var stmt = new SimpleStatement("DUMMY QUERY");
     Assert.AreEqual(0, stmt.PageSize);
     Assert.Null(stmt.ConsistencyLevel);
     var queryOptions = new QueryOptions().SetConsistencyLevel(ConsistencyLevel.LocalQuorum).SetPageSize(100);
     var session = GetInstance(queryOptions);
     var request = (QueryRequest)session.GetRequest(stmt);
     Assert.AreEqual(100, request.PageSize);
     Assert.AreEqual(queryOptions.GetPageSize(), request.PageSize);
     Assert.AreEqual(queryOptions.GetConsistencyLevel(), request.Consistency);
     Assert.AreEqual(ConsistencyLevel.Any, request.SerialConsistency);
 }
        public void SimpleStatementNamedValuesCaseInsensitivityTest()
        {
            var insertQuery = String.Format("INSERT INTO {0} (id, \"text_sample\", int_sample) VALUES (:my_ID, :my_TEXT, :MY_INT)", AllTypesTableName);
            var statement = new SimpleStatement(insertQuery);

            var id = Guid.NewGuid();
            Session.Execute(
                statement.Bind(
                    new { my_INt = 1, my_TEXT = "WAT1", my_id = id}));

            var row = Session.Execute(String.Format("SELECT * FROM {0} WHERE id = {1:D}", AllTypesTableName, id)).First();
            Assert.AreEqual(1, row.GetValue<int>("int_sample"));
            Assert.AreEqual("WAT1", row.GetValue<string>("text_sample"));
        }
예제 #15
0
 public void RequestHandler_GetRequest_SimpleStatement_Statement_Level_Settings_Are_Used()
 {
     var stmt = new SimpleStatement("DUMMY QUERY");
     stmt.SetConsistencyLevel(ConsistencyLevel.EachQuorum);
     stmt.SetPageSize(350);
     stmt.SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial);
     Assert.AreEqual(350, stmt.PageSize);
     Assert.AreEqual(ConsistencyLevel.EachQuorum, stmt.ConsistencyLevel);
     Assert.AreEqual(ConsistencyLevel.LocalSerial, stmt.SerialConsistencyLevel);
     var request = (QueryRequest)RequestHandler<RowSet>.GetRequest(stmt, 2, GetConfig());
     Assert.AreEqual(350, request.PageSize);
     Assert.AreEqual(ConsistencyLevel.EachQuorum, request.Consistency);
     Assert.AreEqual(ConsistencyLevel.LocalSerial, request.SerialConsistency);
 }
        public void CollectionParamsTests()
        {
            var id = Guid.NewGuid();
            var map = new SortedDictionary<string, string> { { "fruit", "apple" }, { "band", "Beatles" } };
            var list = new List<string> { "one", "two" };
            var set = new List<string> { "set_1one", "set_2two" };

            var insertStatement = new SimpleStatement(String.Format("INSERT INTO {0} (id, map_sample, list_sample, set_sample) VALUES (?, ?, ?, ?)", AllTypesTableName));
            Session.Execute(insertStatement.Bind(id, map, list, set));
            var row = Session.Execute(new SimpleStatement(String.Format("SELECT * FROM {0} WHERE id = ?", AllTypesTableName)).Bind(id)).First();
            CollectionAssert.AreEquivalent(map, row.GetValue<IDictionary<string, string>>("map_sample"));
            CollectionAssert.AreEquivalent(list, row.GetValue<List<string>>("list_sample"));
            CollectionAssert.AreEquivalent(set, row.GetValue<List<string>>("set_sample"));
        }
예제 #17
0
        public void EncodeDecodeTupleAsNestedTest()
        {
            var achievements = new List<Tuple<string, int>>
            {
                new Tuple<string, int>("What", 1),
                new Tuple<string, int>(null, 100),
                new Tuple<string, int>(@"¯\_(ツ)_/¯", 150)
            };
            var insert = new SimpleStatement("INSERT INTO users_tuples (id, achievements) values (?, ?)");
            Session.Execute(insert.Bind(31, achievements));
            var row = Session.Execute("SELECT * FROM users_tuples WHERE id = 31").First();

            Assert.AreEqual(achievements, row.GetValue<List<Tuple<string, int>>>("achievements"));
        }
예제 #18
0
        public void Parallel_Insert_And_Select_Sync()
        {
            var testCluster = TestClusterManager.GetNonShareableTestCluster(3, 1, true, false);
            using (var cluster = Cluster.Builder()
                                        .WithRetryPolicy(AlwaysRetryRetryPolicy.Instance)
                                        .AddContactPoint(testCluster.InitialContactPoint)
                                        .Build())
            {
                var session = cluster.Connect();
                var uniqueKsName = "keyspace_" + Randomm.RandomAlphaNum(10);
                session.Execute(@"CREATE KEYSPACE " + uniqueKsName +
                                " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};");
                session.ChangeKeyspace(uniqueKsName);

                var tableName = "table_" + Guid.NewGuid().ToString("N").ToLower();
                session.Execute(String.Format(TestUtils.CREATE_TABLE_TIME_SERIES, tableName));

                var insertQuery = String.Format("INSERT INTO {0} (id, event_time, text_sample) VALUES (?, ?, ?)", tableName);
                var insertQueryPrepared = session.Prepare(insertQuery);
                var selectQuery = String.Format("SELECT * FROM {0} LIMIT 10000", tableName);

                const int rowsPerId = 1000;
                object insertQueryStatement = new SimpleStatement(insertQuery);
                if (CassandraVersion.Major < 2)
                {
                    //Use prepared statements all the way as it is not possible to bind on a simple statement with C* 1.2
                    insertQueryStatement = session.Prepare(insertQuery);
                }
                var actionInsert = GetInsertAction(session, insertQueryStatement, ConsistencyLevel.Quorum, rowsPerId);
                var actionInsertPrepared = GetInsertAction(session, insertQueryPrepared, ConsistencyLevel.Quorum, rowsPerId);
                var actionSelect = GetSelectAction(session, selectQuery, ConsistencyLevel.Quorum, 10);

                //Execute insert sync to have some records
                actionInsert();
                //Execute select sync to assert that everything is going OK
                actionSelect();


                var actions = new List<Action>();
                for (var i = 0; i < 10; i++)
                {
                    //Add 10 actions to execute
                    actions.AddRange(new[] {actionInsert, actionSelect, actionInsertPrepared});
                    actions.AddRange(new[] {actionSelect, actionInsert, actionInsertPrepared, actionInsert});
                    actions.AddRange(new[] {actionInsertPrepared, actionInsertPrepared, actionSelect});
                }
                //Execute in parallel the 100 actions
                TestHelper.ParallelInvoke(actions.ToArray());
            }
        }
예제 #19
0
 public void Warnings_With_Custom_Payload_Test()
 {
     const string query = "BEGIN UNLOGGED BATCH INSERT INTO {0} (k, t) VALUES ('{1}', '{2}') APPLY BATCH";
     SimpleStatement insert = new SimpleStatement(String.Format(query, Table, "warn1", String.Join("", Enumerable.Repeat("a", 5 * 1025))));
     var outgoing = new Dictionary<string, byte[]> { { "k1", Encoding.UTF8.GetBytes("value1") }, { "k2", Encoding.UTF8.GetBytes("value2") } };
     insert.SetOutgoingPayload(outgoing);
     var rs = Session.Execute(insert);
     
     Assert.NotNull(rs.Info.Warnings);
     Assert.AreEqual(1, rs.Info.Warnings.Length);
     StringAssert.Contains("batch", rs.Info.Warnings[0].ToLowerInvariant());
     StringAssert.Contains("exceeding", rs.Info.Warnings[0].ToLowerInvariant());
     CollectionAssert.AreEqual(outgoing["k1"], rs.Info.IncomingPayload["k1"]);
     CollectionAssert.AreEqual(outgoing["k2"], rs.Info.IncomingPayload["k2"]);
 }
예제 #20
0
        public void RequestHandlerRetriesTest()
        {
            //This statement will fail, then we will fake the syntax error as a ReadTimeout
            var statement = new SimpleStatement("SELECT WILL FAIL").SetRetryPolicy(DowngradingConsistencyRetryPolicy.Instance);
            var request = Session.GetRequest(statement);
            //We will need a mock to fake the responses of Cassandra
            var mock = new Moq.Mock<RequestHandler<RowSet>>(Session, request, statement);
            var requestHandler = mock.Object;
            //Expect Retry method to be called with a lower consistency level
            mock.Setup(r => r.Retry(It.Is<ConsistencyLevel?>(c => c == ConsistencyLevel.Two))).Verifiable();
            //Fake a Error Result
            requestHandler.ResponseHandler(new ReadTimeoutException(ConsistencyLevel.Three, 2, 3, false), null);

            mock.Verify();
        }
예제 #21
0
 public void SimpleStatement_Statement_Level_Settings_Are_Used()
 {
     var stmt = new SimpleStatement("DUMMY QUERY");
     stmt.SetConsistencyLevel(ConsistencyLevel.EachQuorum);
     stmt.SetPageSize(350);
     stmt.SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial);
     Assert.AreEqual(350, stmt.PageSize);
     Assert.AreEqual(ConsistencyLevel.EachQuorum, stmt.ConsistencyLevel);
     Assert.AreEqual(ConsistencyLevel.LocalSerial, stmt.SerialConsistencyLevel);
     var session = GetInstance();
     var request = (QueryRequest)session.GetRequest(stmt);
     Assert.AreEqual(350, request.PageSize);
     Assert.AreEqual(ConsistencyLevel.EachQuorum, request.Consistency);
     Assert.AreEqual(ConsistencyLevel.LocalSerial, request.SerialConsistency);
 }
예제 #22
0
        public void Batch_SimpleStatementSingle()
        {
            var tableName = "table" + Guid.NewGuid().ToString("N").ToLower();
            CreateTable(tableName);

            var batch = new BatchStatement();
            var simpleStatement = new SimpleStatement(String.Format("INSERT INTO {0} (id, label, number) VALUES ({1}, '{2}', {3})", _tableName, 20, "label 20", 20));
            batch.Add(simpleStatement);
            Session.Execute(batch);

            //Verify Results
            var rs = Session.Execute(String.Format("SELECT * FROM {0} WHERE id IN ({1})", _tableName, 20));
            var row = rs.First();
            CollectionAssert.AreEqual(row, new object[] { 20, "label 20", 20});
        }
        public void SimpleStatementNamedValuesTest()
        {
            var insertQuery = String.Format("INSERT INTO {0} (text_sample, int_sample, bigint_sample, id) VALUES (:my_text, :my_int, :my_bigint, :my_id)", AllTypesTableName);
            var statement = new SimpleStatement(insertQuery);

            var id = Guid.NewGuid();
            Session.Execute(
                statement.Bind(
                    new { my_int = 100, my_bigint = -500L, my_id = id, my_text = "named params ftw again!" }));

            var row = Session.Execute(String.Format("SELECT int_sample, bigint_sample, text_sample FROM {0} WHERE id = {1:D}", AllTypesTableName, id)).First();

            Assert.AreEqual(100, row.GetValue<int>("int_sample"));
            Assert.AreEqual(-500L, row.GetValue<long>("bigint_sample"));
            Assert.AreEqual("named params ftw again!", row.GetValue<string>("text_sample"));
        }
예제 #24
0
 public void SimpleStatement_Bind_Named_Values()
 {
     var values = new { Name = "Futurama", Description = "In Stereo where available", Time = DateTimeOffset.Parse("1963-08-28") };
     var stmt = new SimpleStatement(Query).Bind(values);
     var actualValues = new Dictionary<string, object>();
     Assert.AreEqual(3, stmt.QueryValueNames.Count);
     Assert.AreEqual(3, stmt.QueryValues.Length);
     //Order is not guaranteed
     for (var i = 0; i < stmt.QueryValueNames.Count; i++)
     {
         actualValues[stmt.QueryValueNames[i]] = stmt.QueryValues[i];
     }
     //Lowercased
     Assert.AreEqual(values.Name, actualValues["name"]);
     Assert.AreEqual(values.Description, actualValues["description"]);
     Assert.AreEqual(values.Time, actualValues["time"]);
 }
예제 #25
0
        public void Jira_CSHARP_40()
        {
            var clusterInfo = TestUtils.CcmSetup(2);
            try
            {
                var Session = clusterInfo.Session;
                string Keyspace = "excelsior";
                Session.CreateKeyspaceIfNotExists(Keyspace);
                Thread.Sleep(1000);
                Session.ChangeKeyspace(Keyspace);
                const string cqlKeyspaces = "SELECT * from system.schema_keyspaces";
                var query = new SimpleStatement(cqlKeyspaces).EnableTracing();
                {
                    var result = Session.Execute(query);
                    Assert.True(result.Count() > 0, "It should return rows");
                }

                TestUtils.CcmStopForce(clusterInfo, 1);
                TestUtils.CcmStopForce(clusterInfo, 2);
                TestUtils.waitForDown("127.0.0.1", clusterInfo.Cluster, 40);
                TestUtils.waitForDown("127.0.0.2", clusterInfo.Cluster, 40);

                try
                {
                    var result = Session.Execute(query);
                    Assert.True(result.Count() > 0, "It should return rows");
                }
                catch (Exception)
                {
                }

                TestUtils.CcmStart(clusterInfo, 1);
                Thread.Sleep(35000);
                TestUtils.waitFor("127.0.0.1", clusterInfo.Cluster, 60);

                {
                    RowSet result = Session.Execute(query);
                    Assert.True(result.Count() > 0, "It should return rows");
                }
            }
            finally
            {
                TestUtils.CcmRemove(clusterInfo);
            }
        }
예제 #26
0
        public void Should_PagingOnSimpleStatement_When_ReceivedNumberOfRowsIsHigherThanPageSize()
        {
            var pageSize               = 10;
            var totalRowLength         = 1003;
            var table                  = CreateSimpleTableAndInsert(totalRowLength);
            var statementWithPaging    = new SimpleStatement($"SELECT * FROM {table}");
            var statementWithoutPaging = new SimpleStatement($"SELECT * FROM {table}");

            statementWithoutPaging.SetPageSize(int.MaxValue);
            statementWithPaging.SetPageSize(pageSize);

            var rsWithPaging    = Session.Execute(statementWithPaging);
            var rsWithoutPaging = Session.Execute(statementWithoutPaging);

            //Check that the internal list of items count is pageSize
            Assert.True(rsWithPaging.InnerQueueCount == pageSize);
            Assert.True(rsWithoutPaging.InnerQueueCount == totalRowLength);

            var allTheRowsPaged = rsWithPaging.ToList();

            Assert.True(allTheRowsPaged.Count == totalRowLength);
        }
예제 #27
0
        public void TokenAwareTargetsPartitionGuidNoHopsQueryTest()
        {
            var builder     = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
            var clusterInfo = TestUtils.CcmSetup(4, builder);

            try
            {
                var session = clusterInfo.Session;
                session.Execute(String.Format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, TestUtils.SIMPLE_KEYSPACE, 1));
                Thread.Sleep(3000);
                session.ChangeKeyspace(TestUtils.SIMPLE_KEYSPACE);
                Thread.Sleep(3000);
                session.Execute(String.Format("CREATE TABLE {0} (k uuid PRIMARY KEY, i int)", TABLE));
                var traces = new List <QueryTrace>();
                for (var i = 0; i < 10; i++)
                {
                    var key       = Guid.NewGuid();
                    var statement = new SimpleStatement(String.Format("INSERT INTO test (k, i) VALUES ({0}, {1})", key, i))
                                    .SetRoutingKey(
                        new RoutingKey()
                    {
                        RawRoutingKey = TypeCodec.GuidShuffle(key.ToByteArray())
                    })
                                    .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);
                }
            }
            finally
            {
                TestUtils.CcmRemove(clusterInfo);
            }
        }
예제 #28
0
        public void MappingEncodingNestedTest()
        {
            foreach (var protocolVersion in UdtProtocolVersionSupported)
            {
                //Use all possible protocol versions
                Cluster.MaxProtocolVersion = protocolVersion;
                //Use a local cluster
                var localCluster = Cluster.Builder().AddContactPoint(IpPrefix + "1").Build();
                var localSession = localCluster.Connect("tester");
                localSession.UserDefinedTypes.Define(
                    UdtMap.For<Phone>(),
                    UdtMap.For<Contact>()
                          .Map(c => c.FirstName, "first_name")
                          .Map(c => c.LastName, "last_name")
                          .Map(c => c.Birth, "birth_date")
                    );

                var insert = new SimpleStatement("INSERT INTO users_contacts (id, contacts) values (?, ?)");

                //All of the fields null
                var id = 301;
                var contacts = new List<Contact>
                {
                    new Contact
                    {
                        FirstName = "Vincent",
                        LastName = "Vega",
                        Phones = new List<Phone>
                        {
                            new Phone {Alias = "Wat", Number = "0000000000121220000"},
                            new Phone {Alias = "Office", Number = "123"}
                        }
                    }
                };
                localSession.Execute(insert.Bind(id, contacts));
                var rs = localSession.Execute(new SimpleStatement("SELECT * FROM users_contacts WHERE id = ?").Bind(id));
                Assert.AreEqual(contacts, rs.First().GetValue<List<Contact>>("contacts"));
            }
        }
예제 #29
0
        public void Should_Use_Statement_ReadTimeout()
        {
            const int generalReadTimeout   = 1500;
            const int statementReadTimeout = 12000;
            var       testCluster          = TestClusterManager.CreateNew();
            var       socketOptions        = new SocketOptions().SetReadTimeoutMillis(generalReadTimeout);
            var       queryOptions         = new QueryOptions().SetRetryOnTimeout(false);
            var       builder = Cluster.Builder().AddContactPoint(testCluster.InitialContactPoint)
                                .WithSocketOptions(socketOptions)
                                .WithPoolingOptions(PoolingOptions.Create().SetHeartBeatInterval(0))
                                .WithQueryTimeout(Timeout.Infinite)
                                .WithQueryOptions(queryOptions);

            using (var cluster = builder.Build())
            {
                var session = cluster.Connect();
                //warmup
                TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
                testCluster.PauseNode(1);
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                Assert.Throws <OperationTimedOutException>(() => session.Execute("SELECT key FROM system.local"));
                stopWatch.Stop();
                //precision of the timer is not guaranteed
                Assert.Greater(stopWatch.ElapsedMilliseconds, generalReadTimeout - 1000);
                Assert.Less(stopWatch.ElapsedMilliseconds, generalReadTimeout + 1000);

                //Try with an specified timeout at Statement level
                var stmt = new SimpleStatement("SELECT key FROM system.local")
                           .SetReadTimeoutMillis(statementReadTimeout);
                stopWatch.Restart();
                Assert.Throws <OperationTimedOutException>(() => session.Execute(stmt));
                stopWatch.Stop();
                //precision of the timer is not guaranteed
                Assert.Greater(stopWatch.ElapsedMilliseconds, statementReadTimeout - 3000);
                Assert.Less(stopWatch.ElapsedMilliseconds, statementReadTimeout + 3000);
                testCluster.ResumeNode(1);
            }
        }
예제 #30
0
        public void GetRequest_With_Timestamp_Generator_Empty_Value()
        {
            var statement = new SimpleStatement("QUERY");
            var policies  = new Policies(
                Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy,
                Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator());
            var config = new Configuration(
                policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(),
                NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator());
            var request = RequestHandler.GetRequest(statement, Serializer.Default, config);
            var stream  = new MemoryStream();

            request.WriteFrame(1, stream, Serializer);
            var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported);
            var bodyBuffer = new byte[stream.Length - headerSize];

            stream.Position = headerSize;
            stream.Read(bodyBuffer, 0, bodyBuffer.Length);
            // The query request is composed by:
            // <query><consistency><flags><result_page_size>
            var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length)
                              .Concat(Encoding.UTF8.GetBytes(statement.QueryString))
                              .ToArray();

            CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length));
            // Skip the query and consistency (2)
            var offset = queryBuffer.Length + 2;

            // The remaining length should be 13 = flags (1) + result_page_size (4)
            Assert.AreEqual(5, bodyBuffer.Length - offset);
            var flags = (QueryFlags)bodyBuffer[offset];

            Assert.False(flags.HasFlag(QueryFlags.WithDefaultTimestamp));
            Assert.True(flags.HasFlag(QueryFlags.PageSize));
            Assert.False(flags.HasFlag(QueryFlags.Values));
            Assert.False(flags.HasFlag(QueryFlags.WithPagingState));
            Assert.False(flags.HasFlag(QueryFlags.SkipMetadata));
            Assert.False(flags.HasFlag(QueryFlags.WithSerialConsistency));
        }
예제 #31
0
 public Task <Statement> GetStatementAsync(ISession session, Cql cql)
 {
     if (cql.QueryOptions.NoPrepare)
     {
         // Use a SimpleStatement if we're not supposed to prepare
         Statement statement = new SimpleStatement(cql.Statement, cql.Arguments);
         SetStatementProperties(statement, cql);
         return(TaskHelper.ToTask(statement));
     }
     return(_statementCache
            .GetOrAdd(cql.Statement, session.PrepareAsync)
            .Continue(t =>
     {
         if (_statementCache.Count > MaxPreparedStatementsThreshold)
         {
             Logger.Warning(String.Format("The prepared statement cache contains {0} queries. Use parameter markers for queries. You can configure this warning threshold using MappingConfiguration.SetMaxStatementPreparedThreshold() method.", _statementCache.Count));
         }
         Statement boundStatement = t.Result.Bind(cql.Arguments);
         SetStatementProperties(boundStatement, cql);
         return boundStatement;
     }));
 }
예제 #32
0
        public void DowngradingConsistencyRetryTest()
        {
            var dummyStatement = new SimpleStatement().SetRetryPolicy(DowngradingConsistencyRetryPolicy.Instance);

            var handler = new RequestHandler <RowSet>(null, null, dummyStatement);
            //Retry if 1 of 2 replicas are alive
            var decision = handler.GetRetryDecision(new UnavailableException(ConsistencyLevel.Two, 2, 1));

            Assert.True(decision != null && decision.DecisionType == RetryDecision.RetryDecisionType.Retry);

            //Retry if 2 of 3 replicas are alive
            decision = handler.GetRetryDecision(new UnavailableException(ConsistencyLevel.Three, 3, 2));
            Assert.True(decision != null && decision.DecisionType == RetryDecision.RetryDecisionType.Retry);

            //Throw if 0 replicas are alive
            decision = handler.GetRetryDecision(new UnavailableException(ConsistencyLevel.Three, 3, 0));
            Assert.True(decision != null && decision.DecisionType == RetryDecision.RetryDecisionType.Rethrow);

            //Retry if 1 of 3 replicas is alive
            decision = handler.GetRetryDecision(new ReadTimeoutException(ConsistencyLevel.All, 3, 1, false));
            Assert.True(decision != null && decision.DecisionType == RetryDecision.RetryDecisionType.Retry);
        }
        public void SerialConsistencyTest()
        {
            //You can not specify local serial consistency as a valid read one.
            Assert.Throws <RequestInvalidException>(() =>
            {
                Session.Execute("SELECT * FROM " + AllTypesTableName, ConsistencyLevel.LocalSerial);
            });

            //It should work
            var statement = new SimpleStatement("SELECT * FROM " + AllTypesTableName)
                            .SetConsistencyLevel(ConsistencyLevel.Quorum)
                            .SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial);

            //Read consistency specified and write consistency specified
            Session.Execute(statement);

            //You can not specify serial consistency as a valid read one.
            Assert.Throws <RequestInvalidException>(() =>
            {
                Session.Execute("SELECT * FROM " + AllTypesTableName, ConsistencyLevel.Serial);
            });
        }
예제 #34
0
        public void PagingOnSimpleStatementTest()
        {
            var pageSize               = 10;
            var totalRowLength         = 1003;
            var table                  = CreateSimpleTableAndInsert(totalRowLength);
            var statementWithPaging    = new SimpleStatement("SELECT * FROM " + table);
            var statementWithoutPaging = new SimpleStatement("SELECT * FROM " + table);

            statementWithoutPaging.SetPageSize(int.MaxValue);
            statementWithPaging.SetPageSize(pageSize);

            var rsWithPaging    = Session.Execute(statementWithPaging);
            var rsWithoutPaging = Session.Execute(statementWithoutPaging);

            //Check that the internal list of items count is pageSize
            Assert.True(rsWithPaging.InnerQueueCount == pageSize);
            Assert.True(rsWithoutPaging.InnerQueueCount == totalRowLength);

            var allTheRowsPaged = rsWithPaging.ToList();

            Assert.True(allTheRowsPaged.Count == totalRowLength);
        }
예제 #35
0
 public void SimpleStatement_Constructor_Dictionary_Named_Test()
 {
     var valuesDictionary = new Dictionary<string, object>
     {
         {"Name", "Futurama"}, 
         {"Description", "In Stereo where available"}, 
         {"Time", DateTimeOffset.Parse("1963-08-28")}
     };
     var stmt = new SimpleStatement(valuesDictionary, Query);
     var actualValues = new Dictionary<string, object>();
     Assert.AreEqual(3, stmt.QueryValueNames.Count);
     Assert.AreEqual(3, stmt.QueryValues.Length);
     //Order is not guaranteed
     for (var i = 0; i < stmt.QueryValueNames.Count; i++)
     {
         actualValues[stmt.QueryValueNames[i]] = stmt.QueryValues[i];
     }
     //Lowercased
     Assert.AreEqual(valuesDictionary["Name"], actualValues["name"]);
     Assert.AreEqual(valuesDictionary["Description"], actualValues["description"]);
     Assert.AreEqual(valuesDictionary["Time"], actualValues["time"]);
 }
예제 #36
0
        public void Should_UseSerialConsistencyLevel_From_QueryOptions(
            ConsistencyLevel consistencyLevel, ConsistencyLevel serialConsistency)
        {
            using (var simulacronCluster = SimulacronCluster.CreateNew(new SimulacronOptions {
                Nodes = "3,3"
            }))
                using (var cluster = Cluster.Builder()
                                     .AddContactPoint(simulacronCluster.InitialContactPoint)
                                     .WithQueryOptions(new QueryOptions()
                                                       .SetConsistencyLevel(consistencyLevel)
                                                       .SetSerialConsistencyLevel(serialConsistency))
                                     .Build())
                {
                    const string conditionalQuery = "update tbl_serial set value=3 where id=2 if exists";
                    var          session          = cluster.Connect();
                    var          simpleStatement  = new SimpleStatement(conditionalQuery);

                    var result = session.Execute(simpleStatement);
                    Assert.AreEqual(consistencyLevel, result.Info.AchievedConsistency);
                    VerifyConsistency(simulacronCluster, conditionalQuery, consistencyLevel, serialConsistency);
                }
        }
예제 #37
0
 public Task<Statement> GetStatementAsync(ISession session, Cql cql)
 {
     if (cql.QueryOptions.NoPrepare)
     {
         // Use a SimpleStatement if we're not supposed to prepare
         Statement statement = new SimpleStatement(cql.Statement, cql.Arguments);
         SetStatementProperties(statement, cql);
         return TaskHelper.ToTask(statement);
     }
     return _statementCache
         .GetOrAdd(cql.Statement, session.PrepareAsync)
         .Continue(t =>
         {
             if (_statementCache.Count > MaxPreparedStatementsThreshold)
             {
                 Logger.Warning(String.Format("The prepared statement cache contains {0} queries. Use parameter markers for queries. You can configure this warning threshold using MappingConfiguration.SetMaxStatementPreparedThreshold() method.", _statementCache.Count));
             }
             Statement boundStatement = t.Result.Bind(cql.Arguments);
             SetStatementProperties(boundStatement, cql);
             return boundStatement;
         });
 }
예제 #38
0
        public void BatchSimpleStatementMultiple()
        {
            SimpleStatement simpleStatement = null;
            var             tableName       = "table" + Guid.NewGuid().ToString("N").ToLower();
            var             expectedValues  = new List <object[]>();

            CreateTable(tableName);

            BatchStatement batch = new BatchStatement();

            for (var x = 1; x <= 5; x++)
            {
                simpleStatement = new SimpleStatement(String.Format("INSERT INTO {0} (id, label, number) VALUES ({1}, '{2}', {3})", tableName, x, "label" + x, x * x));
                expectedValues.Add(new object[] { x, "label" + x, x * x });
                batch.Add(simpleStatement);
            }
            Session.Execute(batch);

            var rs = Session.Execute("SELECT * FROM " + tableName);

            VerifyData(rs.OrderBy(r => r.GetValue <int>("id")), expectedValues);
        }
예제 #39
0
        public void MappingEncodingNestedTest()
        {
            var localSession = GetNewSession(KeyspaceName);

            localSession.UserDefinedTypes.Define(
                UdtMap.For <Phone>(),
                UdtMap.For <Contact>()
                .Map(c => c.FirstName, "first_name")
                .Map(c => c.LastName, "last_name")
                .Map(c => c.Birth, "birth_date")
                );


            //All of the fields null
            var id       = 301;
            var contacts = new List <Contact>
            {
                new Contact
                {
                    FirstName = "Vincent",
                    LastName  = "Vega",
                    Phones    = new List <Phone>
                    {
                        new Phone {
                            Alias = "Wat", Number = "0000000000121220000"
                        },
                        new Phone {
                            Alias = "Office", Number = "123"
                        }
                    }
                }
            };
            var insert = new SimpleStatement("INSERT INTO users_contacts (id, contacts) values (?, ?)", id, contacts);

            localSession.Execute(insert);
            var rs = localSession.Execute(new SimpleStatement("SELECT * FROM users_contacts WHERE id = ?", id));

            Assert.AreEqual(contacts, rs.First().GetValue <List <Contact> >("contacts"));
        }
        public void CollectionParamsBindTests()
        {
            var id  = Guid.NewGuid();
            var map = new SortedDictionary <string, string> {
                { "fruit", "apple" }, { "band", "Beatles" }
            };
            var list = new List <string> {
                "one", "two"
            };
            var set = new List <string> {
                "set_1one", "set_2two"
            };

            var insertStatement = new SimpleStatement(String.Format("INSERT INTO {0} (id, map_sample, list_sample, set_sample) VALUES (?, ?, ?, ?)", AllTypesTableName), id, map, list, set);

            Session.Execute(insertStatement);
            var row = Session.Execute(new SimpleStatement(String.Format("SELECT * FROM {0} WHERE id = ?", AllTypesTableName), id)).First();

            CollectionAssert.AreEquivalent(map, row.GetValue <IDictionary <string, string> >("map_sample"));
            CollectionAssert.AreEquivalent(list, row.GetValue <List <string> >("list_sample"));
            CollectionAssert.AreEquivalent(set, row.GetValue <List <string> >("set_sample"));
        }
예제 #41
0
        public void TokenAware_NoKey_HopsOccurAndAllNodesAreChosenAsCoordinators()
        {
            // Setup
            var policyTestTools = new PolicyTestTools { TableName = TestUtils.GetUniqueTableName() };

            policyTestTools.CreateSchema(Session, 1, forceSchemaAgreement: true);
            var traces = new List<QueryTrace>();
            for (var i = 1; i < 10; i++)
            {
                //The partition key is wrongly calculated
                var statement = new SimpleStatement(string.Format("INSERT INTO " + policyTestTools.TableName + " (k, i) VALUES ({0}, {0})", i))
                                .EnableTracing();
                var rs = Session.Execute(statement);
                traces.Add(rs.Info.QueryTrace);
            }
            //Check that there were hops
            var hopsPerQuery = traces.Select(t => t.Events.Any(e => e.Source.ToString() == t.Coordinator.ToString()));
            Assert.True(hopsPerQuery.Any(v => v));
            var tracesPerCoordinator = traces.GroupBy(t => t.Coordinator).ToDictionary(t => t.Key, t => t.Count());
            Assert.AreEqual(3, tracesPerCoordinator.Count);
            Assert.IsTrue(tracesPerCoordinator.All(kvp => kvp.Value == 3));
        }
예제 #42
0
        public void Should_IteratePaging_When_ParallelClientsReadRowSet()
        {
            var pageSize       = 25;
            var totalRowLength = 300;
            var table          = CreateSimpleTableAndInsert(totalRowLength);
            var query          = new SimpleStatement($"SELECT * FROM {table} LIMIT 10000").SetPageSize(pageSize);
            var rs             = Session.Execute(query);

            Assert.AreEqual(pageSize, rs.GetAvailableWithoutFetching());
            var    counterList = new ConcurrentBag <int>();
            Action iterate     = () =>
            {
                var counter = rs.Count();
                counterList.Add(counter);
            };

            //Iterate in parallel the RowSet
            Parallel.Invoke(iterate, iterate, iterate, iterate);

            //Check that the sum of all rows in different threads is the same as total rows
            Assert.AreEqual(totalRowLength, counterList.Sum());
        }
예제 #43
0
        public void TokenAware_BindString_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} (k text PRIMARY KEY, i int)", policyTestTools.TableName));
            var    traces = new List <QueryTrace>();
            string key    = "value";

            for (var i = 100; i < 140; i++)
            {
                key += (char)i;
                var partitionKey = Encoding.UTF8.GetBytes(key);
                var statement    = new SimpleStatement("INSERT INTO " + policyTestTools.TableName + " (k, i) VALUES (?, ?)", key, i)
                                   .SetRoutingKey(new RoutingKey()
                {
                    RawRoutingKey = partitionKey
                })
                                   .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 GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp()
        {
            var statement         = new SimpleStatement("STATEMENT WITH TIMESTAMP");
            var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20);

            statement.SetTimestamp(expectedTimestamp);
            var policies = new Policies(
                Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy,
                Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator());
            var config = new Configuration(
                policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(),
                NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator());

            var request    = RequestHandler.GetRequest(statement, Serializer, config);
            var bodyBuffer = GetBodyBuffer(request);

            // The query request is composed by:
            // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp>
            var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length)
                              .Concat(Encoding.UTF8.GetBytes(statement.QueryString))
                              .ToArray();

            CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length));
            // Skip the query and consistency (2)
            var offset = queryBuffer.Length + 2;

            // The remaining length should be = flags (1) + result_page_size (4) + serial_consistency (2) + timestamp (8)
            Assert.AreEqual(15, bodyBuffer.Length - offset);
            var flags = GetQueryFlags(bodyBuffer, ref offset);

            Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp));
            Assert.True(flags.HasFlag(QueryFlags.PageSize));
            // Skip result_page_size (4) + serial_consistency (2)
            offset += 6;
            var timestamp = BeConverter.ToInt64(bodyBuffer, offset);

            Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp);
        }
예제 #45
0
        public void TokenAware_Composite_NoHops()
        {
            // Setup
            PolicyTestTools policyTestTools = new PolicyTestTools();
            var             cluster         = GetNewTemporaryCluster(b => b.WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())));

            // Test
            var session = cluster.Connect();
            var ks      = TestUtils.GetUniqueKeyspaceName().ToLowerInvariant();

            policyTestTools.CreateSchema(session, 1, ks);
            policyTestTools.TableName = TestUtils.GetUniqueTableName();
            session.Execute($"CREATE TABLE {policyTestTools.TableName} (k1 text, k2 int, i int, PRIMARY KEY ((k1, k2)))");
            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);
            }
        }
예제 #46
0
        public void Batch_Timestamp()
        {
            var query = new SimpleStatement($"INSERT INTO {_tableName} (id) values (-99999)");
            var dt    = DateTime.Now;

            Assert.DoesNotThrow(() =>
            {
                //It should work
                var statement = new BatchStatement()
                                .Add(query)
                                .SetConsistencyLevel(ConsistencyLevel.Quorum)
                                .SetTimestamp(dt);

                //Read consistency specified and write consistency specified
                Session.Execute(statement);
            });

            VerifyBatchStatement(
                1,
                new[] { $"INSERT INTO {_tableName} (id) values (-99999)" },
                msg => msg.ConsistencyLevel == ConsistencyLevel.Quorum &&
                msg.DefaultTimestamp == SimulacronAPI.DataType.GetMicroSecondsTimestamp(dt));
        }
        public void SimpleStatement_Constructor_Dictionary_Named_Test()
        {
            var valuesDictionary = new Dictionary <string, object>
            {
                { "Name", "Futurama" },
                { "Description", "In Stereo where available" },
                { "Time", DateTimeOffset.Parse("1963-08-28") }
            };
            var stmt         = new SimpleStatement(valuesDictionary, Query);
            var actualValues = new Dictionary <string, object>();

            Assert.AreEqual(3, stmt.QueryValueNames.Count);
            Assert.AreEqual(3, stmt.QueryValues.Length);
            //Order is not guaranteed
            for (var i = 0; i < stmt.QueryValueNames.Count; i++)
            {
                actualValues[stmt.QueryValueNames[i]] = stmt.QueryValues[i];
            }
            //Lowercased
            Assert.AreEqual(valuesDictionary["Name"], actualValues["name"]);
            Assert.AreEqual(valuesDictionary["Description"], actualValues["description"]);
            Assert.AreEqual(valuesDictionary["Time"], actualValues["time"]);
        }
        public void ExecuteGraph_Should_Build_Payload_With_GraphOptions()
        {
            SimpleStatement coreStatement   = null;
            var             coreSessionMock = GetCoreSessionMock(stmt => coreStatement = stmt);
            var             session         = NewInstance(coreSessionMock.Object,
                                                          new GraphOptions()
                                                          .SetName("name1")
                                                          .SetSource("My source!")
                                                          .SetReadTimeoutMillis(22222)
                                                          .SetReadConsistencyLevel(ConsistencyLevel.LocalQuorum)
                                                          .SetWriteConsistencyLevel(ConsistencyLevel.EachQuorum));

            session.ExecuteGraph(new SimpleGraphStatement("g.V()"));
            Assert.NotNull(coreStatement);
            Assert.NotNull(coreStatement.OutgoingPayload);
            Assert.That(Encoding.UTF8.GetString(coreStatement.OutgoingPayload["graph-source"]), Is.EqualTo("My source!"));
            Assert.AreEqual(Encoding.UTF8.GetString(coreStatement.OutgoingPayload["graph-name"]), "name1");
            Assert.AreEqual(Encoding.UTF8.GetString(coreStatement.OutgoingPayload["graph-read-consistency"]), "LOCAL_QUORUM");
            Assert.AreEqual(Encoding.UTF8.GetString(coreStatement.OutgoingPayload["graph-write-consistency"]), "EACH_QUORUM");
            //default
            Assert.AreEqual(Encoding.UTF8.GetString(coreStatement.OutgoingPayload["graph-language"]), "gremlin-groovy");
            Assert.That(coreStatement.OutgoingPayload["request-timeout"], Is.EqualTo(ToBuffer(22222)));
        }
        private SimpleStatement CreateClusterStatement <T>(T entity)
        {
            try
            {
                var tableName = typeof(T).ExtractTableName <T>();

                // We are interested only in the properties we are not ignoring
                var properties      = entity.GetType().GetCassandraRelevantProperties();
                var propertiesNames = properties.Select(p => p.GetColumnNameAndPrimaryKeyMapping()).ToArray();

                var createCql = $"CREATE TABLE IF NOT EXISTS {_keySpaceName}.{tableName} ({string.Join(",", propertiesNames)})";


                var insertStatment = new SimpleStatement(createCql);

                return(insertStatment);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #50
0
        public async Task <double> MaxAsync <T, TNumericModel>(Expression <Func <T, bool> > predicate, Expression <Func <T, TNumericModel> > propertyExpression)
        {
            try
            {
                var columnName = QueryBuilder.EvaluatePropertyName(propertyExpression);

                var queryStatement = QueryBuilder.EvaluateQuery(predicate);
                var tableName      = typeof(T).ExtractTableName <T>();
                var selectQuery    = $"select max({columnName}) from {tableName} where {queryStatement.Statement}";

                var statement = new SimpleStatement(selectQuery, queryStatement.Values);
                var rows      = await _session.ExecuteAsync(statement);

                var sum = Convert.ToDouble(rows.First()[0]);

                return(sum);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #51
0
        public async Task <Statement> GetStatementAsync(ISession session, Cql cql)
        {
            if (cql.QueryOptions.NoPrepare)
            {
                // Use a SimpleStatement if we're not supposed to prepare
                var statement = new SimpleStatement(cql.Statement, cql.Arguments);
                SetStatementProperties(statement, cql);
                return(statement);
            }
            var ps = await _statementCache.GetOrAdd(cql.Statement, session.PrepareAsync).ConfigureAwait(false);

            if (_statementCache.Count > MaxPreparedStatementsThreshold)
            {
                Logger.Warning(string.Format("The prepared statement cache contains {0} queries. Use parameter" +
                                             "markers for queries. You can configure this warning threshold using" +
                                             " MappingConfiguration.SetMaxStatementPreparedThreshold() method.",
                                             _statementCache.Count));
            }
            var boundStatement = ps.Bind(cql.Arguments);

            SetStatementProperties(boundStatement, cql);
            return(boundStatement);
        }
예제 #52
0
        public void Should_IteratePaging_When_ParallelClientsReadRowSet()
        {
            const int    pageSize       = 25;
            const int    totalRowLength = 300;
            const string table          = "tbl_parallel_paging_read";
            var          query          = new SimpleStatement($"SELECT * FROM {table} LIMIT 10000").SetPageSize(pageSize);
            var          rs             = Session.Execute(query);

            Assert.AreEqual(pageSize, rs.GetAvailableWithoutFetching());

            var counter = 0;

            void Iterate()
            {
                Interlocked.Add(ref counter, rs.Count());
            }

            //Iterate in parallel the RowSet
            Parallel.Invoke(Iterate, Iterate, Iterate, Iterate);

            //Check that the sum of all rows in different threads is the same as total rows
            Assert.AreEqual(totalRowLength, Volatile.Read(ref counter));
        }
예제 #53
0
        public void Batch_SerialConsistency()
        {
            var query = new SimpleStatement($"INSERT INTO {_tableName} (id) values (-99999)");

            Assert.Throws <ArgumentException>(() =>
            {
                //You can not specify local serial consistency as a valid read one.
                var batch = new BatchStatement()
                            .Add(query)
                            .SetBatchType(BatchType.Logged)
                            .SetSerialConsistencyLevel(ConsistencyLevel.Quorum);
                Session.Execute(batch);
            });

            //It should work
            var statement = new BatchStatement()
                            .Add(query)
                            .SetConsistencyLevel(ConsistencyLevel.Quorum)
                            .SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial);

            //Read consistency specified and write consistency specified
            Session.Execute(statement);
        }
예제 #54
0
        public void ExecuteGraph_Should_Call_ExecuteAsync_With_ReadTimeout_Set_To_Default()
        {
            SimpleStatement coreStatement   = null;
            var             coreSessionMock = new Mock <ISession>(MockBehavior.Strict);

            coreSessionMock.Setup(s => s.ExecuteAsync(It.IsAny <IStatement>()))
            .Returns(TaskOf(new RowSet()))
            .Callback <SimpleStatement>(stmt => coreStatement = stmt)
            .Verifiable();
            const int readTimeout = 5000;
            var       session     = NewInstance(coreSessionMock.Object, new GraphOptions().SetReadTimeoutMillis(readTimeout));

            session.ExecuteGraph(new SimpleGraphStatement("g.V()"));
            Assert.NotNull(coreStatement);
            Assert.AreEqual(readTimeout, coreStatement.ReadTimeoutMillis);
            //Another one with the statement level timeout set to zero
            session.ExecuteGraph(new SimpleGraphStatement("g.V()").SetReadTimeoutMillis(0));
            Assert.NotNull(coreStatement);
            Assert.AreEqual(readTimeout, coreStatement.ReadTimeoutMillis);
            Assert.True(coreStatement.OutgoingPayload.ContainsKey("request-timeout"));
            Assert.That(coreStatement.OutgoingPayload["request-timeout"], Is.EqualTo(ToBuffer(readTimeout)));
            coreSessionMock.Verify();
        }
예제 #55
0
        public void Batch_GeneratedTimestamp()
        {
            var query     = new SimpleStatement($"INSERT INTO {_tableName} (id) values (-99999)");
            var generator = new MockTimestampGenerator();

            using (var simulacronCluster = SimulacronCluster.CreateNew(new SimulacronOptions()))
                using (var cluster = Cluster.Builder()
                                     .AddContactPoint(simulacronCluster.InitialContactPoint)
                                     .WithTimestampGenerator(generator).Build())
                {
                    var session        = cluster.Connect();
                    var batchStatement = new BatchStatement().Add(query);
                    session.Execute(batchStatement);
                    var timestamp = generator.Next();
                    var executed  = simulacronCluster.GetQueries(null, "BATCH");
                    Assert.IsNotEmpty(executed);
                    var executedArray = executed.ToArray();
                    Assert.AreEqual(1, executedArray.Length);
                    var log          = executedArray[0];
                    var logtimestamp = (long)log.client_timestamp;
                    Assert.AreEqual(timestamp, logtimestamp);
                }
        }
예제 #56
0
        public async Task <IEnumerable <DeviceEvent> > GetDeviceEventsAsync(double swLon, double swLat, double neLon, double neLat)
        {
            logger.LogInformation($"In GetDeviceEventsAsync. params = ({swLon},{swLat},{neLon},{neLat})");

            var results = new List <DeviceEvent>();

            var statementQuery = new SimpleStatement("select * from " + tableName + " where solr_query='{\"q\": \"*:*\", \"fq\":\"location:[\\\"" + swLon + " " + swLat + "\\\" TO \\\"" + neLon + " " + neLat + "\\\"]\"}' ORDER BY event_time DESC LIMIT " + MaxDeviceEvents);
            var rowSet         = await session.ExecuteAsync(statementQuery);

            foreach (Row row in rowSet)
            {
                var location  = (Dse.Geometry.Point)row["location"];
                var timestamp = (DateTimeOffset)row["event_time"];
                results.Add(new DeviceEvent
                {
                    id        = row["device_id"].ToString(),
                    Location  = GeometryPoint.Create(location.X, location.Y),
                    Timestamp = timestamp.DateTime
                });
            }

            return(results);
        }
예제 #57
0
        public async Task Import(string filePath, string id = null)
        {
            List <Task> listOfTasks = new List <Task>();

            var ret       = _session.Execute($"SELECT max(part) FROM files where id = '{id}'");
            int partCount = ret.First().GetValue <int>(0) + 1;

            SemaphoreSlim fileLock = new SemaphoreSlim(1, 1);
            var           fs       = new FileStream(filePath, FileMode.Create);

            for (int i = 0; i < partCount; i++)
            {
                var statement = new SimpleStatement($"SELECT part, chunk FROM files where id = '{id}' AND part = {i}");
                //convert to await
                var task = _session.ExecuteAsync(statement).ContinueWith(
                    async rows =>
                {
                    var row     = rows.Result.First();
                    int part    = row.GetValue <int>("part");
                    byte[] bits = row.GetValue <byte[]>("chunk");

                    await fileLock.WaitAsync();
                    try
                    {
                        fs.Position = part * _cqlConfig.ChunkSize;
                        await fs.WriteAsync(bits, 0, bits.Length);
                    }
                    finally
                    {
                        fileLock.Release();
                    }
                });
                listOfTasks.Add(task);
            }

            await Task.WhenAll(listOfTasks).ContinueWith(_ => fs.DisposeAsync());
        }
예제 #58
0
        public void TokenAware_Guid_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;
            string uniqueTableName = TestUtils.GetUniqueTableName();

            policyTestTools.CreateSchema(session);
            session.Execute(String.Format("CREATE TABLE {0} (k uuid PRIMARY KEY, i int)", uniqueTableName));
            var traces = new List <QueryTrace>();

            for (var i = 0; i < 10; i++)
            {
                var key       = Guid.NewGuid();
                var statement = new SimpleStatement(String.Format("INSERT INTO " + uniqueTableName + " (k, i) VALUES ({0}, {1})", key, i))
                                .SetRoutingKey(
                    new RoutingKey()
                {
                    RawRoutingKey = TypeCodec.GuidShuffle(key.ToByteArray())
                })
                                .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);
            }
        }
예제 #59
0
 public void Statement_SetPagingState_Null_Does_Not_Disable_AutoPage()
 {
     var statement = new SimpleStatement();
     Assert.True(statement.AutoPage);
     statement.SetPagingState(null);
     Assert.True(statement.AutoPage);
     Assert.Null(statement.PagingState);
 }
예제 #60
0
 public void Statement_SetPagingState_Disables_AutoPage()
 {
     var statement = new SimpleStatement();
     Assert.True(statement.AutoPage);
     statement.SetPagingState(new byte[] { 1, 2, 3, 4, 5, 6 });
     Assert.False(statement.AutoPage);
     Assert.NotNull(statement.PagingState);
 }