private void CreateKeyspaceWithPropertiesTest(string strategyClass) { var clusterInfo = TestUtils.CcmSetup(2); try { Session = clusterInfo.Session; Cluster = clusterInfo.Cluster; bool durable_writes = Randomm.Instance.NextBoolean(); int?replication_factor = null; int?data_centers_count = null; Dictionary <string, int> datacenters_replication_factors = null; if (strategyClass == ReplicationStrategies.SimpleStrategy) { replication_factor = Randomm.Instance.Next(1, 21); Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateSimpleStrategyReplicationProperty((int)replication_factor), durable_writes); Session.ChangeKeyspace(Keyspace); } else if (strategyClass == ReplicationStrategies.NetworkTopologyStrategy) { data_centers_count = Randomm.Instance.Next(1, 11); datacenters_replication_factors = new Dictionary <string, int>((int)data_centers_count); for (int i = 0; i < data_centers_count; i++) { datacenters_replication_factors.Add("dc" + i, Randomm.Instance.Next(1, 21)); } Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty( datacenters_replication_factors), durable_writes); } KeyspaceMetadata ksmd = Cluster.Metadata.GetKeyspace(Keyspace); Assert.AreEqual(strategyClass, ksmd.StrategyClass); Assert.AreEqual(durable_writes, ksmd.DurableWrites); if (replication_factor != null) { Assert.AreEqual(replication_factor, ksmd.Replication["replication_factor"]); } if (datacenters_replication_factors != null) { Assert.True(datacenters_replication_factors.SequenceEqual(ksmd.Replication)); } } finally { TestUtils.CcmRemove(clusterInfo); } }
public void CreateKeyspaceWithPropertiesTest(string strategy_class) { CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder()); try { Session = CCMCluster.Session; Cluster = CCMCluster.Cluster; Randomm rndm = new Randomm(DateTime.Now.Millisecond); bool durable_writes = rndm.NextBoolean(); int?replication_factor = null; int?data_centers_count = null; Dictionary <string, int> datacenters_replication_factors = null; if (strategy_class == ReplicationStrategies.SimpleStrategy) { replication_factor = rndm.Next(1, 21); Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateSimpleStrategyReplicationProperty((int)replication_factor), durable_writes); Session.ChangeKeyspace(Keyspace); } else if (strategy_class == ReplicationStrategies.NetworkTopologyStrategy) { data_centers_count = rndm.Next(1, 11); datacenters_replication_factors = new Dictionary <string, int>((int)data_centers_count); for (int i = 0; i < data_centers_count; i++) { datacenters_replication_factors.Add("dc" + i.ToString(), rndm.Next(1, 21)); } Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(datacenters_replication_factors), durable_writes); } KeyspaceMetadata ksmd = Cluster.Metadata.GetKeyspace(Keyspace); Assert.Equal(strategy_class, ksmd.StrategyClass); Assert.Equal(durable_writes, ksmd.DurableWrites); if (replication_factor != null) { Assert.Equal(replication_factor, ksmd.Replication["replication_factor"]); } if (datacenters_replication_factors != null) { Assert.True(datacenters_replication_factors.SequenceEqual(ksmd.Replication)); } } finally { CCMCluster.Discard(); } }
public void CheckSimpleStrategyKeyspace() { ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount); var session = testCluster.Session; bool durableWrites = Randomm.Instance.NextBoolean(); string keyspaceName = TestUtils.GetUniqueKeyspaceName(); string strategyClass = ReplicationStrategies.SimpleStrategy; int replicationFactor = Randomm.Instance.Next(1, 21); session.CreateKeyspace(keyspaceName, ReplicationStrategies.CreateSimpleStrategyReplicationProperty(replicationFactor), durableWrites); session.ChangeKeyspace(keyspaceName); KeyspaceMetadata ksmd = testCluster.Cluster.Metadata.GetKeyspace(keyspaceName); Assert.AreEqual(strategyClass, ksmd.StrategyClass); Assert.AreEqual(durableWrites, ksmd.DurableWrites); Assert.AreEqual(replicationFactor, ksmd.Replication["replication_factor"]); }
public async Task <IActionResult> CreateSchema() { using (var context = await _cassandraCluster.ConnectAsync()) { context.CreateKeyspaceIfNotExists(KEY_SPACE, ReplicationStrategies.CreateSimpleStrategyReplicationProperty(REPLICA_COUNT)); } using (var context = await _cassandraCluster.ConnectAsync(KEY_SPACE)) { await context.ExecuteAsync(new SimpleStatement($@" CREATE TABLE IF NOT EXISTS {nameof(TestEntity)} ( id uuid PRIMARY KEY, name text, age int, description text, address text, phone text, email text);")); } return(Ok()); }
private static async Task <Connection> ConnectAsync(Cluster cluster, CancellationToken cancellationToken = default) { var replication = ReplicationStrategies.CreateSimpleStrategyReplicationProperty(1); var session = cluster.ConnectAndCreateDefaultKeyspaceIfNotExists(replication); try { await CreateTableAsync(); return(new Connection(session, cluster)); } catch { session.Dispose(); throw; } Task CreateTableAsync() { var cql = "CREATE TABLE IF NOT EXISTS migrations (major SMALLINT, minor SMALLINT, time TIMESTAMP, PRIMARY KEY (major, minor))"; return(session.ExecuteAsync(new SimpleStatement(cql))); } }
/// <summary> /// Initialize a Keyspace by name in association with the specified cluster. /// </summary> internal Keyspace(string name, MetaCluster metaCluster) { if (metaCluster == null) { MetaCluster = ManagerCql.DefaultCluster; } else { MetaCluster = metaCluster; } // initialize cache collections Tables = new Shared <Dictionary <string, Table> >(new Dictionary <string, Table>()); Statements = new Shared <Dictionary <int, PreparedStatement> >(new Dictionary <int, PreparedStatement>()); TableMetadata = new Shared <Dictionary <string, TableMetadata> >(new Dictionary <string, TableMetadata>()); // initialize the batch related fields _batch = new ArrayRig <Statement>(); _batchLock = new Lock(); // decrease the batch timeout if debugging #if DEBUG BatchTimeout = 10000; #endif // create the act used to execute batch statements Act executeBatch = new Act(() => ExecuteBatch()); _batchTimer = new Timer(BatchInterval, executeBatch, false); _onExecuted = new ArrayRig <IAction>(); // setup the auto update watch if (_autoUpdateTime > 0) { _autoUpdateWatch = new Watch(_autoUpdateTime, true, OnAutoUpdate); } try { // connect to the cluster _session = MetaCluster.Cluster.Connect(); } catch (Exception ex) { // log the error Log.Error("Problem getting Keyspace '" + name + "' metadata from the Cluster.", ex); return; } // initialize a Mapper instance for use by this Keyspace Mapper = new Mapper(_session); // get the metadata associated with this Keyspace Metadata = MetaCluster.Cluster.Metadata.GetKeyspace(name); // if the Keyspace doesn't exist if (Metadata == null) { // create the Keyspace if (MetaCluster.DataCenters == null) { _session.CreateKeyspace(name, ReplicationStrategies.CreateSimpleStrategyReplicationProperty(1)); } else { _session.CreateKeyspace(name, ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(MetaCluster.DataCenters)); } // get the metadata Metadata = MetaCluster.Cluster.Metadata.GetKeyspace(name); } // add the Keyspace to the DataCluster MetaCluster.AddKeyspace(this); // make the target Keyspace of the Session to this Keyspace _session.ChangeKeyspace(Metadata.Name); TableMetadata.Take(); foreach (var tableMetadata in Metadata.GetTablesMetadata()) { TableMetadata.Item.Add(tableMetadata.Name, tableMetadata); } TableMetadata.Release(); }
public void mixedDurationTestCCM() { if (CCMCluster != null) { CCMCluster.Discard(); } CCMCluster = CCMBridge.CCMCluster.Create(3, Cluster.Builder()); cluster = CCMCluster.Cluster; session = CCMCluster.Session; session.CreateKeyspace("large_data", ReplicationStrategies.CreateSimpleStrategyReplicationProperty(3)); session.ChangeKeyspace("large_data"); session.WaitForSchemaAgreement( session.Execute(String.Format("CREATE TABLE {0} (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows"))); session.WaitForSchemaAgreement( session.Execute(String.Format("CREATE TABLE {0} (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows"))); session.WaitForSchemaAgreement( session.Execute(String.Format("CREATE TABLE {0} (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows"))); session.WaitForSchemaAgreement( session.Execute(String.Format("CREATE TABLE {0} (k int PRIMARY KEY, i text)", "large_text"))); // Create the extra wide table definition StringBuilder tableDeclaration = new StringBuilder(); tableDeclaration.Append("CREATE TABLE wide_table ("); tableDeclaration.Append("k INT PRIMARY KEY"); for (int i = 0; i < 330; ++i) { tableDeclaration.Append(String.Format(", \"{0}\" INT", createColumnName(i))); } tableDeclaration.Append(")"); session.WaitForSchemaAgreement( session.Execute(tableDeclaration.ToString()) ); Random rndm = new Random(DateTime.Now.Millisecond); try { for (int i = 0; i < 10; ++i) { switch ((int)rndm.Next(0, 5)) { case 0: testWideRows(); break; case 1: testWideBatchRows(); break; case 2: testByteRows(); break; case 3: testLargeText(); break; case 4: testWideTable(); break; default: break; } } } catch (Exception e) { CCMCluster.ErrorOut(); throw e; } finally { CCMCluster.Discard(); } }