public void KeyspacesMetadataAvailableAtStartup(bool metadataSync) { var cluster = GetNewTemporaryCluster(builder => builder.WithMetadataSyncOptions(new MetadataSyncOptions().SetMetadataSyncEnabled(metadataSync))); // Basic status check Assert.Greater(cluster.Metadata.GetKeyspaces().Count, 0); Assert.NotNull(cluster.Metadata.GetKeyspace("system")); Assert.AreEqual("system", cluster.Metadata.GetKeyspace("system").Name); Assert.NotNull(cluster.Metadata.GetKeyspace("system").AsCqlQuery()); //Not existent tables return null Assert.Null(cluster.Metadata.GetKeyspace("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12))); Assert.Null(cluster.Metadata.GetTable("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12), "nonExistentTable_" + Randomm.RandomAlphaNum(12))); Assert.Null(cluster.Metadata.GetTable("system", "nonExistentTable_" + Randomm.RandomAlphaNum(12))); //Case sensitive Assert.Null(cluster.Metadata.GetKeyspace("SYSTEM")); }
public void KeyspacesMetadataAvailableAtStartup() { ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount); var cluster = testCluster.Cluster; // Basic status check Assert.Greater(cluster.Metadata.GetKeyspaces().Count, 0); Assert.NotNull(cluster.Metadata.GetKeyspace("system")); Assert.AreEqual("system", cluster.Metadata.GetKeyspace("system").Name); //Not existent tables return null Assert.Null(cluster.Metadata.GetKeyspace("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12))); Assert.Null(cluster.Metadata.GetTable("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12), "nonExistentTable_" + Randomm.RandomAlphaNum(12))); Assert.Null(cluster.Metadata.GetTable("system", "nonExistentTable_" + Randomm.RandomAlphaNum(12))); //Case sensitive Assert.Null(cluster.Metadata.GetKeyspace("SYSTEM")); }
public void insertingSingleCollectionPrepared(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string mapSyntax = ""; object valueCollection = null; int Cnt = 10; if (CassandraCollectionType == "list" || CassandraCollectionType == "set") { Type openType = CassandraCollectionType == "list" ? typeof(List <>) : typeof(HashSet <>); Type listType = openType.MakeGenericType(TypeOfDataToBeInputed); valueCollection = Activator.CreateInstance(listType); MethodInfo addM = listType.GetMethod("Add"); for (int i = 0; i < Cnt; i++) { object randomValue = Randomm.RandomVal(TypeOfDataToBeInputed); addM.Invoke(valueCollection, new[] { randomValue }); } } else if (CassandraCollectionType == "map") { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; Type openType = typeof(SortedDictionary <,>); Type dicType = openType.MakeGenericType(TypeOfKeyForMap, TypeOfDataToBeInputed); valueCollection = Activator.CreateInstance(dicType); MethodInfo addM = dicType.GetMethod("Add"); for (int i = 0; i < Cnt; i++) { RETRY: try { object randomKey = Randomm.RandomVal(TypeOfKeyForMap); object randomValue = Randomm.RandomVal(TypeOfDataToBeInputed); addM.Invoke(valueCollection, new[] { randomKey, randomValue }); } catch { goto RETRY; } } } string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName)) ); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); PreparedStatement prepInsert = QueryTools.PrepareQuery(Session, string.Format("INSERT INTO {0}(tweet_id,some_collection) VALUES (?, ?);", tableName)); Session.Execute(prepInsert.Bind(tweet_id, valueCollection).SetConsistencyLevel(ConsistencyLevel.Quorum)); QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void checkingOrderOfCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null, string pendingMode = "") { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string openBracket = CassandraCollectionType == "list" ? "[" : "{"; string closeBracket = CassandraCollectionType == "list" ? "]" : "}"; string mapSyntax = ""; string randomKeyValue = string.Empty; if (TypeOfKeyForMap != null) { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; if (TypeOfKeyForMap == typeof(DateTimeOffset)) { randomKeyValue = Randomm.RandomVal(typeof(DateTimeOffset)) .GetType() .GetMethod("ToString", new[] { typeof(string) }) .Invoke(Randomm.RandomVal(typeof(DateTimeOffset)), new object[1] { "yyyy-MM-dd H:mm:sszz00" }) + "' : '"; } else { randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed) + "' : '"; } } string tableName = "table" + Guid.NewGuid().ToString("N"); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName))); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); var longQ = new StringBuilder(); longQ.AppendLine("BEGIN BATCH "); int CollectionElementsNo = 100; var orderedAsInputed = new List <Int32>(CollectionElementsNo); string inputSide = "some_collection + {1}"; if (CassandraCollectionType == "list" && pendingMode == "prepending") { inputSide = "{1} + some_collection"; } for (int i = 0; i < CollectionElementsNo; i++) { int data = i * (i % 2); longQ.AppendFormat(@"UPDATE {0} SET some_collection = " + inputSide + " WHERE tweet_id = {2};" , tableName, openBracket + randomKeyValue + data + closeBracket, tweet_id); orderedAsInputed.Add(data); } longQ.AppendLine("APPLY BATCH;"); QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting..."); if (CassandraCollectionType == "set") { orderedAsInputed.Sort(); orderedAsInputed.RemoveRange(0, orderedAsInputed.LastIndexOf(0)); } else if (CassandraCollectionType == "list" && pendingMode == "prepending") { orderedAsInputed.Reverse(); } var rs = Session.Execute(string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); { int ind = 0; foreach (Row row in rs.GetRows()) { foreach (object value in row[1] as IEnumerable) { Assert.True(orderedAsInputed[ind] == (int)value); ind++; } } } QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void WideTable() { string uniqueTableName = "wide_table" + Randomm.RandomAlphaNum(16); TestWideTable(_session, uniqueTableName); }
public void insertingSingleCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null) { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string openBracket = CassandraCollectionType == "list" ? "[" : "{"; string closeBracket = CassandraCollectionType == "list" ? "]" : "}"; string mapSyntax = ""; object randomValue = Randomm.RandomVal(TypeOfDataToBeInputed); if (TypeOfDataToBeInputed == typeof(string)) { randomValue = "'" + randomValue.ToString().Replace("'", "''") + "'"; } string randomKeyValue = string.Empty; if (TypeOfKeyForMap != null) { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; if (TypeOfKeyForMap == typeof(DateTimeOffset)) { randomKeyValue = "'" + (Randomm.RandomVal(typeof(DateTimeOffset)) .GetType() .GetMethod("ToString", new[] { typeof(string) }) .Invoke(Randomm.RandomVal(typeof(DateTimeOffset)), new object[1] { "yyyy-MM-dd H:mm:sszz00" }) + "'"); } else if (TypeOfKeyForMap == typeof(string)) { randomKeyValue = "'" + Randomm.RandomVal(TypeOfDataToBeInputed).ToString().Replace("'", "''") + "'"; } else { randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed).ToString(); } } string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName)) ); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id,some_collection) VALUES ({1}, {2});", tableName, tweet_id, openBracket + randomKeyValue + (string.IsNullOrEmpty(randomKeyValue) ? "" : " : ") + randomValue + closeBracket)); var longQ = new StringBuilder(); longQ.AppendLine("BEGIN BATCH "); int CollectionElementsNo = 100; object rval = Randomm.RandomVal(TypeOfDataToBeInputed); for (int i = 0; i < CollectionElementsNo; i++) { object val = rval; if (TypeOfDataToBeInputed == typeof(string)) { val = "'" + val.ToString().Replace("'", "''") + "'"; } longQ.AppendFormat(@"UPDATE {0} SET some_collection = some_collection + {1} WHERE tweet_id = {2};" , tableName, openBracket + randomKeyValue + (string.IsNullOrEmpty(randomKeyValue) ? "" : " : ") + val + closeBracket, tweet_id); } longQ.AppendLine("APPLY BATCH;"); QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting..."); QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }
public void WideByteRows() { string uniqueTableName = "wide_byte_rows" + Randomm.RandomAlphaNum(16); TestByteRows(_session, uniqueTableName); }
public void LargeText() { string uniqueTableName = "large_text_" + Randomm.RandomAlphaNum(16); TestLargeText(_session, uniqueTableName); }
private static string GetUniqueTableName() { return("LgDataTsts_" + Randomm.RandomAlphaNum(16)); }
public void WideBatchRows() { string uniqueTableName = "wide_batch_rows" + Randomm.RandomAlphaNum(16); TestWideBatchRows(_session, uniqueTableName); }
public void ParallelInsertTest() { Cluster localCluster = _builder.Build(); ISession localSession = localCluster.Connect(); string keyspaceName = "kp_pi1_" + Randomm.RandomAlphaNum(10); localSession.Execute(string.Format(@"CREATE KEYSPACE {0} WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 2 }};", keyspaceName)); TestUtils.WaitForSchemaAgreement(localCluster); localSession.ChangeKeyspace(keyspaceName); for (int KK = 0; KK < 1; KK++) { string tableName = "table" + Guid.NewGuid().ToString("N").ToLower(); try { localSession.Execute(string.Format(@" CREATE TABLE {0}( tweet_id uuid, author text, body text, isok boolean, PRIMARY KEY(tweet_id))", tableName)); TestUtils.WaitForSchemaAgreement(localCluster); } catch (AlreadyExistsException) { } int RowsNo = 1000; var ar = new IAsyncResult[RowsNo]; var threads = new List <Thread>(); var monit = new object(); int readyCnt = 0; Trace.TraceInformation("Preparing..."); for (int idx = 0; idx < RowsNo; idx++) { int i = idx; threads.Add(new Thread(() => { try { lock (monit) { readyCnt++; Monitor.Wait(monit); } ar[i] = localSession.BeginExecute(string.Format(@" INSERT INTO {0} (tweet_id, author, isok, body) VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid(), i, i % 2 == 0 ? "false" : "true"), ConsistencyLevel.One, null, null); Interlocked.MemoryBarrier(); } catch { } })); } for (int idx = 0; idx < RowsNo; idx++) { threads[idx].Start(); } lock (monit) { while (true) { if (readyCnt < RowsNo) { Monitor.Exit(monit); Thread.Sleep(100); Monitor.Enter(monit); } else { Monitor.PulseAll(monit); break; } } } Trace.TraceInformation("Start!"); var done = new HashSet <int>(); while (done.Count < RowsNo) { for (int i = 0; i < RowsNo; i++) { Interlocked.MemoryBarrier(); if (!done.Contains(i) && ar[i] != null) { if (ar[i].AsyncWaitHandle.WaitOne(10)) { try { localSession.EndExecute(ar[i]); } catch (Exception ex) { Trace.TraceError("There was an exception while trying to end the async Execution: " + Environment.NewLine + ex); } done.Add(i); } } } } Trace.TraceInformation("Inserted... now we are checking the count"); var ret = localSession.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100), ConsistencyLevel.Quorum); Assert.AreEqual(RowsNo, ret.GetRows().ToList().Count); for (int idx = 0; idx < RowsNo; idx++) { threads[idx].Join(); } } }
public void MassiveAsyncTest() { Cluster localCluster = _builder.Build(); ISession localSession = localCluster.Connect(); string keyspaceName = "kp_mat_" + Randomm.RandomAlphaNum(8); localSession.Execute( string.Format(@"CREATE KEYSPACE {0} WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 2 }};" , keyspaceName)); localSession.ChangeKeyspace(keyspaceName); string tableName = "table" + Randomm.RandomAlphaNum(8); localSession.Execute(string.Format(@"CREATE TABLE {0}( tweet_id uuid, author text, body text, isok boolean, PRIMARY KEY(tweet_id))", tableName)); int RowsNo = 100; var ar = new bool[RowsNo]; var thr = new Thread(() => { for (int i = 0; i < RowsNo; i++) { int tmpi = i; localSession.BeginExecute(string.Format(@"INSERT INTO {0} ( tweet_id, author, isok, body) VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid(), i, i % 2 == 0 ? "false" : "true") , ConsistencyLevel.One, _ => { ar[tmpi] = true; Interlocked.MemoryBarrier(); }, null); } }); thr.Start(); var done = new HashSet <int>(); while (done.Count < RowsNo) { for (int i = 0; i < RowsNo; i++) { Interlocked.MemoryBarrier(); if (!done.Contains(i) && ar[i]) { done.Add(i); } } } thr.Join(); Trace.TraceInformation("Inserted... now we are checking the count"); var ret = localSession.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100), ConsistencyLevel.Quorum); Assert.AreEqual(RowsNo, ret.GetRows().ToList().Count); localSession.Dispose(); }
public void massivePreparedStatementTest() { string tableName = "table" + Guid.NewGuid().ToString("N"); try { QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, numb1 double, numb2 int );", tableName)); TestUtils.WaitForSchemaAgreement(Session.Cluster); } catch (AlreadyExistsException) { } var numberOfPrepares = 100; var values = new List <object[]>(numberOfPrepares); var prepares = new List <PreparedStatement>(); Parallel.For(0, numberOfPrepares, i => { PreparedStatement prep = QueryTools.PrepareQuery(Session, string.Format("INSERT INTO {0}(tweet_id, numb1, numb2) VALUES ({1}, ?, ?);", tableName, Guid.NewGuid())); lock (prepares) prepares.Add(prep); }); Parallel.ForEach(prepares, prep => { QueryTools.ExecutePreparedQuery(Session, prep, new object[] { (double)Randomm.RandomVal(typeof(double)), (int)Randomm.RandomVal(typeof(int)) }); }); QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); }
public void KeyspacesMetadataAvailableAtStartup() { var cluster = GetNewCluster(); // Basic status check Assert.Greater(cluster.Metadata.GetKeyspaces().Count, 0); Assert.NotNull(cluster.Metadata.GetKeyspace("system")); Assert.AreEqual("system", cluster.Metadata.GetKeyspace("system").Name); Assert.NotNull(cluster.Metadata.GetKeyspace("system").AsCqlQuery()); //Not existent tables return null Assert.Null(cluster.Metadata.GetKeyspace("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12))); Assert.Null(cluster.Metadata.GetTable("nonExistentKeyspace_" + Randomm.RandomAlphaNum(12), "nonExistentTable_" + Randomm.RandomAlphaNum(12))); Assert.Null(cluster.Metadata.GetTable("system", "nonExistentTable_" + Randomm.RandomAlphaNum(12))); //Case sensitive Assert.Null(cluster.Metadata.GetKeyspace("SYSTEM")); }
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; }