public void TestUpdateKeyspaceWithMultiDcConfiguration() { var clusterConnection = cluster.RetrieveClusterConnection(); var keyspaceName = TestSchemaUtils.GetRandomKeyspaceName(); var createdKeyspace = new Keyspace { Name = keyspaceName, ReplicationStrategy = NetworkTopologyReplicationStrategy.Create(new[] { new DataCenterReplicationFactor("dc1", 3), new DataCenterReplicationFactor("dc2", 5) }) }; clusterConnection.AddKeyspace(createdKeyspace); createdKeyspace.ReplicationStrategy = NetworkTopologyReplicationStrategy.Create(new[] { new DataCenterReplicationFactor("dc3", 7) }); clusterConnection.UpdateKeyspace(createdKeyspace); var keyspaces = clusterConnection.RetrieveKeyspaces(); var actualKeyspace = keyspaces.First(x => x.Name == keyspaceName); AssertKeyspacePropertiesEquals(createdKeyspace, actualKeyspace); }
private void AssertKeyspacePropertiesEquals(Keyspace createdKeyspace, Keyspace actualKeyspace) { Assert.That(actualKeyspace.Name, Is.EqualTo(createdKeyspace.Name)); Assert.That(actualKeyspace.DurableWrites, Is.EqualTo(createdKeyspace.DurableWrites)); Assert.AreEqual(createdKeyspace.ReplicationStrategy.Name, actualKeyspace.ReplicationStrategy.Name); Assert.AreEqual(createdKeyspace.ReplicationStrategy.StrategyOptions, actualKeyspace.ReplicationStrategy.StrategyOptions); }
public void AddKeyspace(Keyspace keyspace) { var addKeyspaceCommand = new AddKeyspaceCommand(keyspace); commandExecutor.Execute(addKeyspaceCommand); logger.Info("Keyspace adding result: {0}", addKeyspaceCommand.Output); }
public string DropColumnFamily(string columnFamily) { Keyspace.ClearCachedKeyspaceSchema(); return(ExecuteOperation(new SimpleOperation <string>(ctx => { return ctx.Session.GetClient().system_drop_column_family(columnFamily); }))); }
public string UpdateColumnFamily(CfDef definition) { Keyspace.ClearCachedKeyspaceSchema(); return(ExecuteOperation(new SimpleOperation <string>(ctx => { return ctx.Session.GetClient().system_update_column_family(definition); }))); }
public override int GetHashCode() { unchecked { var result = (IpEndPoint != null ? IpEndPoint.GetHashCode() : 0); result = (result * 397) ^ (Keyspace != null ? Keyspace.GetHashCode() : 0); result = (result * 397) ^ IsFierce.GetHashCode(); return(result); } }
public void TestTryAddKeyspaceWithInvalidName() { var clusterConnection = cluster.RetrieveClusterConnection(); var createdKeyspace = new Keyspace { Name = "Keyspace-123-123", ReplicationStrategy = SimpleReplicationStrategy.Create(1) }; Assert.Throws <CassandraClientInvalidRequestException>(() => clusterConnection.AddKeyspace(createdKeyspace)); }
public void ActualizeKeyspaces(KeyspaceSchema[] keyspaceShemas, bool changeExistingKeyspaceMetadata) { if (keyspaceShemas == null || keyspaceShemas.Length == 0) { logger.Info("Nothing to actualize since keyspaceShemas is empty"); return; } logger.Info("Start schema actualization..."); eventListener.ActualizationStarted(); var clusterConnection = cassandraCluster.RetrieveClusterConnection(); clusterConnection.WaitUntilSchemaAgreementIsReached(schemaAgreementWaitTimeout); logger.Info("Found {0} keyspaces in schema", keyspaceShemas.Length); var keyspaces = clusterConnection.RetrieveKeyspaces().ToDictionary(keyspace => keyspace.Name, StringComparer.OrdinalIgnoreCase); eventListener.SchemaRetrieved(keyspaces.Values.ToArray()); logger.Info("Found {0} keyspaces in database", keyspaces.Count); foreach (var keyspaceSchema in keyspaceShemas) { logger.Info("Start schema actualization for keyspace: {0}", keyspaceSchema.Name); eventListener.KeyspaceActualizationStarted(keyspaceSchema.Name); var keyspace = new Keyspace { Name = keyspaceSchema.Name, DurableWrites = keyspaceSchema.Configuration.DurableWrites, ReplicationStrategy = keyspaceSchema.Configuration.ReplicationStrategy, }; if (keyspaces.ContainsKey(keyspaceSchema.Name)) { if (changeExistingKeyspaceMetadata) { logger.Info("Keyspace {0} already exists in the database, so run update keyspace command", keyspaceSchema.Name); clusterConnection.UpdateKeyspace(keyspace); } else { logger.Info("Keyspace {0} already exists in the database, changeExistingKeyspaceMetadata is set to False, so do not run update keyspace command", keyspaceSchema.Name); } ActualizeColumnFamilies(keyspaceSchema.Name, keyspaceSchema.Configuration.ColumnFamilies); } else { logger.Info("Keyspace {0} is new, so run add keyspace command", keyspaceSchema.Name); keyspace.ColumnFamilies = keyspaceSchema.Configuration.ColumnFamilies.ToDictionary(family => family.Name); clusterConnection.AddKeyspace(keyspace); eventListener.KeyspaceAdded(keyspace); } } clusterConnection.WaitUntilSchemaAgreementIsReached(schemaAgreementWaitTimeout); eventListener.ActualizationCompleted(); }
/// <summary> /// Gets a typed column family. /// </summary> /// <typeparam name="CompareWith"></typeparam> /// <returns></returns> public CassandraColumnFamily GetColumnFamily(string columnFamily) { if (Keyspace != null) { var schema = Keyspace.GetColumnFamilySchema(columnFamily); if (schema != null) { return(new CassandraColumnFamily(this, schema)); } } return(new CassandraColumnFamily(this, columnFamily)); }
public void SetUp() { cluster = new CassandraCluster(SingleCassandraNodeSetUpFixture.Node.CreateSettings(), Logger.Instance); var clusterConnection = cluster.RetrieveClusterConnection(); var keyspaceName = TestSchemaUtils.GetRandomKeyspaceName(); var createdKeyspace = new Keyspace { Name = keyspaceName, ReplicationStrategy = SimpleReplicationStrategy.Create(1) }; clusterConnection.AddKeyspace(createdKeyspace); keyspaceConnection = cluster.RetrieveKeyspaceConnection(keyspaceName); }
protected override void Setup() { base.Setup(); if (!Keyspace.ColumnFamilyExists(Server, FamilyName)) { Keyspace.AddColumnFamily(Server, new CfDef { Name = FamilyName, Keyspace = KeyspaceName, Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Holds the blog posts" }); } _family = Database.GetColumnFamily <AsciiType>(FamilyName); }
private static void AssertKeyspacesEquals(Keyspace expected, Keyspace actual) { Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.DurableWrites, actual.DurableWrites); Assert.AreEqual(expected.ReplicationStrategy.Name, actual.ReplicationStrategy.Name); Assert.AreEqual(expected.ReplicationStrategy.StrategyOptions, actual.ReplicationStrategy.StrategyOptions); if (expected.ColumnFamilies == null) { Assert.IsNull(actual.ColumnFamilies); } else { Assert.NotNull(actual.ColumnFamilies); actual.ColumnFamilies.Keys.OrderByDescending(s => s).ToArray().AssertEqualsTo(expected.ColumnFamilies.Keys.OrderByDescending(s => s).ToArray()); } }
public void TestAddKeyspace(bool durableWrites) { var clusterConnection = cluster.RetrieveClusterConnection(); var keyspaceName = TestSchemaUtils.GetRandomKeyspaceName(); var createdKeyspace = new Keyspace { Name = keyspaceName, DurableWrites = durableWrites, ReplicationStrategy = SimpleReplicationStrategy.Create(1) }; clusterConnection.AddKeyspace(createdKeyspace); var keyspaces = clusterConnection.RetrieveKeyspaces(); var actualKeyspace = keyspaces.First(x => x.Name == keyspaceName); AssertKeyspacePropertiesEquals(createdKeyspace, actualKeyspace); }
public void UpdateKeyspace(Keyspace keyspace) { clusterConnection.UpdateKeyspace(keyspace); }
public void UpdateKeyspace(Keyspace keyspace) { commandExecutor.Execute(new UpdateKeyspaceCommand(keyspace)); }
public void KeyspaceAdded(Keyspace keyspace) { KeyspaceAddedInvokeCount++; }
public void Failover() { var h1client = new Mock <ICassandraClient>(); var h2client = new Mock <ICassandraClient>(); var h3client = new Mock <ICassandraClient>(); var h1endpoint = new Endpoint("h1", 111, "ip1"); var h2endpoint = new Endpoint("h2", 111, "ip2"); var h3endpoint = new Endpoint("h3", 111, "ip3"); var tprotocol = new Mock <TProtocol>(new Mock <TTransport>().Object); var h1cassandra = new Mock <Apache.Cassandra.Cassandra.Client>(tprotocol.Object).As <Apache.Cassandra.Cassandra.Iface>(); var h2cassandra = new Mock <Apache.Cassandra.Cassandra.Client>(tprotocol.Object).As <Apache.Cassandra.Cassandra.Iface>(); var h3cassandra = new Mock <Apache.Cassandra.Cassandra.Client>(tprotocol.Object).As <Apache.Cassandra.Cassandra.Iface>(); var keyspaceName = "Keyspace1"; var description = new Dictionary <string, Dictionary <string, string> >(); var keyspace1desc = new Dictionary <string, string>(); keyspace1desc.Add(HectorSharp.Keyspace.CF_TYPE, HectorSharp.Keyspace.CF_TYPE_STANDARD); description.Add("Standard1", keyspace1desc); var consistencyLevel = HectorSharp.ConsistencyLevel.ONE; var cp = new ColumnPath("Standard1", null, "Failover"); var pool = new Mock <IKeyedObjectPool <Endpoint, ICassandraClient> >(); var monitor = new Mock <ICassandraClientMonitor>(); // list of available servers var tokenMap = new Dictionary <string, string>(); tokenMap.Add("t1", "h1"); tokenMap.Add("t2", "h2"); tokenMap.Add("t3", "h3"); h1client.Setup(c => c.Client).Returns(h1cassandra.Object); h2client.Setup(c => c.Client).Returns(h2cassandra.Object); h3client.Setup(c => c.Client).Returns(h3cassandra.Object); h1client.Setup(c => c.Port).Returns(h1endpoint.Port); h2client.Setup(c => c.Port).Returns(h2endpoint.Port); h3client.Setup(c => c.Port).Returns(h3endpoint.Port); h1client.Setup(c => c.GetTokenMap(AnyBool())).Returns(tokenMap); h2client.Setup(c => c.GetTokenMap(AnyBool())).Returns(tokenMap); h3client.Setup(c => c.GetTokenMap(AnyBool())).Returns(tokenMap); h1client.Setup(c => c.Endpoint).Returns(h1endpoint); h2client.Setup(c => c.Endpoint).Returns(h2endpoint); h3client.Setup(c => c.Endpoint).Returns(h3endpoint); pool.Setup(p => p.Borrow(IsEndpoint(h1endpoint))).Returns(h1client.Object); pool.Setup(p => p.Borrow(IsEndpoint(h2endpoint))).Returns(h2client.Object); pool.Setup(p => p.Borrow(IsEndpoint(h3endpoint))).Returns(h3client.Object); // success without failover var failoverPolicy = new FailoverPolicy(0, FailoverStrategy.FAIL_FAST); var ks = new Keyspace(h1client.Object, keyspaceName, description, consistencyLevel, failoverPolicy, pool.Object, monitor.Object); ks.Insert("key", cp, "value"); // fail fast h1cassandra.Setup( c => c.insert(AnyString(), AnyString(), Any <Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any <Apache.Cassandra.ConsistencyLevel>())) .Throws(new Apache.Cassandra.TimedOutException()); Assert.Throws <TimedOutException>(() => ks.Insert("key", cp, "value")); // on fail try next one, h1 fails, h2 succeeds failoverPolicy = new FailoverPolicy(3, FailoverStrategy.ON_FAIL_TRY_ONE_NEXT_AVAILABLE); ks = new Keyspace(h1client.Object, keyspaceName, description, consistencyLevel, failoverPolicy, pool.Object, monitor.Object); ks.Insert("key", cp, "value"); h2cassandra.Verify( c => c.insert(AnyString(), AnyString(), Any <Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any <Apache.Cassandra.ConsistencyLevel>()) ); pool.Verify(p => p.Borrow(IsEndpoint(h2endpoint))); // make all nodes fail h2cassandra.Setup( c => c.insert(AnyString(), AnyString(), Any <Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any <Apache.Cassandra.ConsistencyLevel>())) .Throws(new Apache.Cassandra.TimedOutException()); h3cassandra.Setup( c => c.insert(AnyString(), AnyString(), Any <Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any <Apache.Cassandra.ConsistencyLevel>())) .Throws(new Apache.Cassandra.TimedOutException()); ks = new Keyspace(h1client.Object, keyspaceName, description, consistencyLevel, failoverPolicy, pool.Object, monitor.Object); Assert.Throws <TimedOutException>(() => ks.Insert("key", cp, "value")); }
public UpdateKeyspaceCommand(Keyspace keyspaceDefinition) { this.keyspaceDefinition = keyspaceDefinition; }
public void AddKeyspace(Keyspace keyspace) { clusterConnection.AddKeyspace(keyspace); }
public bool ColumnFamilyExists(string columnFamily) { return(Keyspace.ColumnFamilyExists(columnFamily)); }
public void KeyspaceAdded(Keyspace keyspace) { }
public AddKeyspaceCommand(Keyspace keyspaceDefinition) { this.keyspaceDefinition = keyspaceDefinition; }
public void Failover() { var h1client = new Mock<ICassandraClient>(); var h2client = new Mock<ICassandraClient>(); var h3client = new Mock<ICassandraClient>(); var h1endpoint = new Endpoint("h1", 111, "ip1"); var h2endpoint = new Endpoint("h2", 111, "ip2"); var h3endpoint = new Endpoint("h3", 111, "ip3"); var tprotocol = new Mock<TProtocol>(new Mock<TTransport>().Object); var h1cassandra = new Mock<Apache.Cassandra.Cassandra.Client>(tprotocol.Object).As<Apache.Cassandra.Cassandra.Iface>(); var h2cassandra = new Mock<Apache.Cassandra.Cassandra.Client>(tprotocol.Object).As<Apache.Cassandra.Cassandra.Iface>(); var h3cassandra = new Mock<Apache.Cassandra.Cassandra.Client>(tprotocol.Object).As<Apache.Cassandra.Cassandra.Iface>(); var keyspaceName = "Keyspace1"; var description = new Dictionary<string, Dictionary<string, string>>(); var keyspace1desc = new Dictionary<string, string>(); keyspace1desc.Add(HectorSharp.Keyspace.CF_TYPE, HectorSharp.Keyspace.CF_TYPE_STANDARD); description.Add("Standard1", keyspace1desc); var consistencyLevel = HectorSharp.ConsistencyLevel.ONE; var cp = new ColumnPath("Standard1", null, "Failover"); var pool = new Mock<IKeyedObjectPool<Endpoint, ICassandraClient>>(); var monitor = new Mock<ICassandraClientMonitor>(); // list of available servers var tokenMap = new Dictionary<string, string>(); tokenMap.Add("t1", "h1"); tokenMap.Add("t2", "h2"); tokenMap.Add("t3", "h3"); h1client.Setup(c => c.Client).Returns(h1cassandra.Object); h2client.Setup(c => c.Client).Returns(h2cassandra.Object); h3client.Setup(c => c.Client).Returns(h3cassandra.Object); h1client.Setup(c => c.Port).Returns(h1endpoint.Port); h2client.Setup(c => c.Port).Returns(h2endpoint.Port); h3client.Setup(c => c.Port).Returns(h3endpoint.Port); h1client.Setup(c => c.GetTokenMap(AnyBool())).Returns(tokenMap); h2client.Setup(c => c.GetTokenMap(AnyBool())).Returns(tokenMap); h3client.Setup(c => c.GetTokenMap(AnyBool())).Returns(tokenMap); h1client.Setup(c => c.Endpoint).Returns(h1endpoint); h2client.Setup(c => c.Endpoint).Returns(h2endpoint); h3client.Setup(c => c.Endpoint).Returns(h3endpoint); pool.Setup(p => p.Borrow(IsEndpoint(h1endpoint))).Returns(h1client.Object); pool.Setup(p => p.Borrow(IsEndpoint(h2endpoint))).Returns(h2client.Object); pool.Setup(p => p.Borrow(IsEndpoint(h3endpoint))).Returns(h3client.Object); // success without failover var failoverPolicy = new FailoverPolicy(0, FailoverStrategy.FAIL_FAST); var ks = new Keyspace(h1client.Object, keyspaceName, description, consistencyLevel, failoverPolicy, pool.Object, monitor.Object); ks.Insert("key", cp, "value"); // fail fast h1cassandra.Setup( c => c.insert(AnyString(), AnyString(), Any<Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any<Apache.Cassandra.ConsistencyLevel>())) .Throws(new Apache.Cassandra.TimedOutException()); Assert.Throws<TimedOutException>(() => ks.Insert("key", cp, "value")); // on fail try next one, h1 fails, h2 succeeds failoverPolicy = new FailoverPolicy(3, FailoverStrategy.ON_FAIL_TRY_ONE_NEXT_AVAILABLE); ks = new Keyspace(h1client.Object, keyspaceName, description, consistencyLevel, failoverPolicy, pool.Object, monitor.Object); ks.Insert("key", cp, "value"); h2cassandra.Verify( c => c.insert(AnyString(), AnyString(), Any<Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any<Apache.Cassandra.ConsistencyLevel>()) ); pool.Verify(p => p.Borrow(IsEndpoint(h2endpoint))); // make all nodes fail h2cassandra.Setup( c => c.insert(AnyString(), AnyString(), Any<Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any<Apache.Cassandra.ConsistencyLevel>())) .Throws(new Apache.Cassandra.TimedOutException()); h3cassandra.Setup( c => c.insert(AnyString(), AnyString(), Any<Apache.Cassandra.ColumnPath>(), AnyBytes(), AnyLong(), Any<Apache.Cassandra.ConsistencyLevel>())) .Throws(new Apache.Cassandra.TimedOutException()); ks = new Keyspace(h1client.Object, keyspaceName, description, consistencyLevel, failoverPolicy, pool.Object, monitor.Object); Assert.Throws<TimedOutException>(() => ks.Insert("key", cp, "value")); }