public void MaterializedView_Should_Retrieve_View_Metadata_Quoted_Identifiers() { var queries = new[] { "CREATE KEYSPACE ks_view_meta2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3}", @"CREATE TABLE ks_view_meta2.t1 (""theKey"" INT, ""the;Clustering"" INT, ""the Value"" INT, PRIMARY KEY (""theKey"", ""the;Clustering""))", @"CREATE MATERIALIZED VIEW ks_view_meta2.mv1 AS SELECT ""theKey"", ""the;Clustering"", ""the Value"" FROM t1 WHERE ""theKey"" IS NOT NULL AND ""the;Clustering"" IS NOT NULL AND ""the Value"" IS NOT NULL PRIMARY KEY (""theKey"", ""the;Clustering"")" }; var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false); using (var cluster = Cluster.Builder().AddContactPoint(testCluster.InitialContactPoint).Build()) { var session = cluster.Connect(); foreach (var q in queries) { session.Execute(q); } var ks = cluster.Metadata.GetKeyspace("ks_view_meta2"); Assert.NotNull(ks); var view = ks.GetMaterializedViewMetadata("mv1"); Assert.NotNull(view); Assert.NotNull(view.Options); Assert.AreEqual("mv1", view.Name); Assert.AreEqual(@"""theKey"" IS NOT NULL AND ""the;Clustering"" IS NOT NULL AND ""the Value"" IS NOT NULL", view.WhereClause); Assert.AreEqual(3, view.TableColumns.Length); Assert.AreEqual(new[] { "ks_view_meta2", "ks_view_meta2", "ks_view_meta2" }, view.TableColumns.Select(c => c.Keyspace)); Assert.AreEqual(new[] { "mv1", "mv1", "mv1" }, view.TableColumns.Select(c => c.Table)); Assert.AreEqual(new[] { "the Value", "the;Clustering", "theKey" }, view.TableColumns.Select(c => c.Name)); Assert.AreEqual(new[] { ColumnTypeCode.Int, ColumnTypeCode.Int, ColumnTypeCode.Int }, view.TableColumns.Select(c => c.TypeCode)); Assert.AreEqual(new[] { "theKey" }, view.PartitionKeys.Select(c => c.Name)); Assert.AreEqual(new[] { "the;Clustering" }, view.ClusteringKeys.Select(c => c.Item1.Name)); Assert.AreEqual(new[] { SortOrder.Ascending }, view.ClusteringKeys.Select(c => c.Item2)); } }
public void KeyspacesMetadataUpToDateViaCassandraEvents() { ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(2); var cluster = testCluster.Cluster; var session = testCluster.Session; var initialLength = cluster.Metadata.GetKeyspaces().Count; Assert.Greater(initialLength, 0); //GetReplicas should yield the primary replica when the Keyspace is not found Assert.AreEqual(1, cluster.GetReplicas("ks2", new byte[] { 0, 0, 0, 1 }).Count); const string createKeyspaceQuery = "CREATE KEYSPACE {0} WITH replication = {{ 'class' : '{1}', {2} }}"; session.Execute(String.Format(createKeyspaceQuery, "ks1", "SimpleStrategy", "'replication_factor' : 1")); session.Execute(String.Format(createKeyspaceQuery, "ks2", "SimpleStrategy", "'replication_factor' : 3")); session.Execute(String.Format(createKeyspaceQuery, "ks3", "NetworkTopologyStrategy", "'dc1' : 1")); session.Execute(String.Format(createKeyspaceQuery, "\"KS4\"", "SimpleStrategy", "'replication_factor' : 3")); //Let the magic happen Thread.Sleep(5000); Assert.Greater(cluster.Metadata.GetKeyspaces().Count, initialLength); var ks1 = cluster.Metadata.GetKeyspace("ks1"); Assert.NotNull(ks1); Assert.AreEqual(ks1.Replication["replication_factor"], 1); var ks2 = cluster.Metadata.GetKeyspace("ks2"); Assert.NotNull(ks2); Assert.AreEqual(ks2.Replication["replication_factor"], 3); //GetReplicas should yield the 2 replicas (rf=3 but cluster=2) when the Keyspace is found Assert.AreEqual(2, cluster.GetReplicas("ks2", new byte[] { 0, 0, 0, 1 }).Count); var ks3 = cluster.Metadata.GetKeyspace("ks3"); Assert.NotNull(ks3); Assert.AreEqual(ks3.Replication["dc1"], 1); Assert.Null(cluster.Metadata.GetKeyspace("ks4")); Assert.NotNull(cluster.Metadata.GetKeyspace("KS4")); }
public void Should_Reconnect_Once_If_Called_Serially() { var lbp = new RoundRobinPolicy(); var config = new Configuration( new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance), new ProtocolOptions(), null, new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false); var metadata = new Metadata(config); metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort)); var clusterMock = new Mock <ICluster>(); clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection()); lbp.Initialize(clusterMock.Object); using (var cc = NewInstance(testCluster, config)) { cc.Init(); testCluster.Stop(1); var t1 = cc.Reconnect(); var t2 = cc.Reconnect(); var t3 = cc.Reconnect(); var t4 = cc.Reconnect(); Assert.AreEqual(t1, t2); Assert.AreEqual(t1, t3); Assert.AreEqual(t1, t4); var ex = Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(t1)); Assert.AreEqual(1, ex.Errors.Count); Assert.IsInstanceOf <SocketException>(ex.Errors.Values.First()); } testCluster.ShutDown(); }
public void TableMetadataClusteringOrderTest() { string keyspaceName = TestUtils.GetUniqueKeyspaceName(); string tableName = TestUtils.GetUniqueTableName().ToLower(); ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount); var cluster = testCluster.Cluster; var session = testCluster.Session; var cql = "CREATE TABLE " + tableName + " (" + @"a text, b int, c text, d text, f text, g text, h timestamp, PRIMARY KEY ((a, b), c, d) ) WITH CLUSTERING ORDER BY (c ASC, d DESC); "; session.CreateKeyspace(keyspaceName); session.ChangeKeyspace(keyspaceName); session.Execute(cql); session.Execute("INSERT INTO " + tableName + " (a, b, c, d) VALUES ('1', 2, '3', '4')"); var rs = session.Execute("select * from " + tableName); Assert.True(rs.GetRows().Count() == 1); var table = cluster.Metadata .GetKeyspace(keyspaceName) .GetTableMetadata(tableName); Assert.NotNull(table); Assert.True(table.TableColumns.Count() == 7); Assert.AreEqual("a, b", String.Join(", ", table.PartitionKeys.Select(p => p.Name))); }
public void Should_Schedule_Reconnections_In_The_Background() { var lbp = new RoundRobinPolicy(); var config = new Configuration( new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance), new ProtocolOptions(), null, new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false); var metadata = new Metadata(config); metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort)); var clusterMock = new Mock <ICluster>(); clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection()); lbp.Initialize(clusterMock.Object); using (var cc = NewInstance(testCluster, config, metadata)) { cc.Init().Wait(InitTimeout); var host = metadata.Hosts.First(); testCluster.Stop(1); host.SetDown(); Thread.Sleep(2000); Assert.False(host.IsUp); testCluster.Start(1); host.BringUpIfDown(); //Should reconnect using timer Thread.Sleep(5000); Assert.DoesNotThrow(() => cc.Query("SELECT key FROM system.local", false)); } testCluster.ShutDown(); }
public void HeartbeatShouldDetectNodeDown() { //Execute a couple of time //Kill connections the node silently //Do nothing for a while //Check if the node is considered as down ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1); var cluster = Cluster.Builder() .AddContactPoint(testCluster.InitialContactPoint) .WithPoolingOptions( new PoolingOptions() .SetCoreConnectionsPerHost(HostDistance.Local, 2) .SetHeartBeatInterval(500)) .WithReconnectionPolicy(new ConstantReconnectionPolicy(Int32.MaxValue)) .Build(); var session = (Session)cluster.Connect(); for (var i = 0; i < 6; i++) { session.Execute("SELECT * FROM system.local"); } var host = cluster.AllHosts().First(); var pool = session.GetOrCreateConnectionPool(host, HostDistance.Local); Trace.TraceInformation("Killing connections"); foreach (var c in pool.OpenConnections) { c.Kill(); } Trace.TraceInformation("Waiting"); for (var i = 0; i < 10; i++) { Thread.Sleep(1000); } Assert.False(cluster.AllHosts().ToList()[0].IsUp); }
public void Should_Throw_NoHostAvailableException_When_All_Hosts_Down() { var testCluster = TestClusterManager.GetNonShareableTestCluster(2, 1, true, false); var socketOptions = new SocketOptions().SetReadTimeoutMillis(3000); var builder = Cluster.Builder().AddContactPoint(testCluster.InitialContactPoint) .WithSocketOptions(socketOptions); using (var cluster = builder.Build()) { var session = cluster.Connect(); //warmup TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10); testCluster.PauseNode(1); testCluster.PauseNode(2); var ex = Assert.Throws <NoHostAvailableException>(() => session.Execute("SELECT key FROM system.local")); Assert.AreEqual(2, ex.Errors.Count); foreach (var innerException in ex.Errors.Values) { Assert.IsInstanceOf <OperationTimedOutException>(innerException); } testCluster.ResumeNode(1); testCluster.ResumeNode(2); } }
public void Should_Reconnect_After_Several_Failed_Attempts() { var lbp = new RoundRobinPolicy(); var config = new Configuration( new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance), new ProtocolOptions(), null, new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); config.BufferPool = new Microsoft.IO.RecyclableMemoryStreamManager(); var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false); var metadata = new Metadata(config); metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort)); var clusterMock = new Mock <ICluster>(); clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection()); lbp.Initialize(clusterMock.Object); using (var cc = NewInstance(testCluster, config)) { cc.Init(); testCluster.Stop(1); Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect())); Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect())); Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect())); Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect())); testCluster.Start(1); Assert.DoesNotThrow(() => TaskHelper.WaitToComplete(cc.Reconnect())); } testCluster.ShutDown(); }
public void ReconnectionRecyclesPool() { var policy = new ConstantReconnectionPolicy(5000); var nonShareableTestCluster = TestClusterManager.GetNonShareableTestCluster(2, 1, true, false); nonShareableTestCluster.Builder = new Builder().WithReconnectionPolicy(policy); nonShareableTestCluster.InitClient(); // this will replace the existing session using the newly assigned Builder instance var session = (Session)nonShareableTestCluster.Session; var hosts = new List <IPEndPoint>(); for (var i = 0; i < 50; i++) { var rs = session.Execute("SELECT * FROM system.local"); if (i == 20) { nonShareableTestCluster.StopForce(2); } else if (i == 30) { nonShareableTestCluster.Start(2); Thread.Sleep(5000); } hosts.Add(rs.Info.QueriedHost); } var pool = session.GetOrCreateConnectionPool(TestHelper.CreateHost(nonShareableTestCluster.InitialContactPoint), HostDistance.Local); var connections = pool.OpenConnections.ToArray(); var expectedCoreConnections = nonShareableTestCluster.Cluster.Configuration .GetPoolingOptions((byte)session.BinaryProtocolVersion) .GetCoreConnectionsPerHost(HostDistance.Local); Assert.AreEqual(expectedCoreConnections, connections.Length); Assert.True(connections.All(c => !c.IsClosed)); }
public void TokenAwareTest(bool usePrepared) { // Setup PolicyTestTools policyTestTools = new PolicyTestTools(); ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1, 1, DefaultMaxClusterCreateRetries, true); testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())); testCluster.InitClient(); policyTestTools.CreateSchema(testCluster.Session); //clusterInfo.Cluster.RefreshSchema(); policyTestTools.InitPreparedStatement(testCluster, 12); policyTestTools.Query(testCluster, 12); // Not the best test ever, we should use OPP and check we do it the // right nodes. But since M3P is hard-coded for now, let just check // we just hit only one node. int nodePosToDecommission = 2; int nodePositionToNotDecommission = 1; if (policyTestTools.Coordinators.ContainsKey(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort)) { nodePosToDecommission = 1; nodePositionToNotDecommission = 2; } policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + nodePosToDecommission + ":" + DefaultCassandraPort, 12); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + nodePositionToNotDecommission + ":" + DefaultCassandraPort, 0); // now try again having stopped the node that was just queried policyTestTools.ResetCoordinators(); testCluster.DecommissionNode(nodePosToDecommission); TestUtils.waitForDecommission(testCluster.ClusterIpPrefix + nodePosToDecommission + ":" + DefaultCassandraPort, testCluster.Cluster, 40); policyTestTools.Query(testCluster, 12, usePrepared); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + nodePosToDecommission + ":" + DefaultCassandraPort, 0); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + nodePositionToNotDecommission + ":" + DefaultCassandraPort, 12); }
public void Jira_CSHARP_40() { ITestCluster nonShareableTestCluster = TestClusterManager.GetNonShareableTestCluster(1); var session = nonShareableTestCluster.Session; string keyspaceName = "excelsior"; session.CreateKeyspaceIfNotExists(keyspaceName); session.ChangeKeyspace(keyspaceName); const string cqlQuery = "SELECT * from system.local"; var query = new SimpleStatement(cqlQuery).EnableTracing(); { var result = session.Execute(query); Assert.Greater(result.Count(), 0, "It should return rows"); } nonShareableTestCluster.StopForce(1); // now wait until node is down bool noHostAvailableExceptionWasCaught = false; while (!noHostAvailableExceptionWasCaught) { try { nonShareableTestCluster.Cluster.Connect(); } catch (Exception e) { if (e.GetType() == typeof(NoHostAvailableException)) { noHostAvailableExceptionWasCaught = true; } else { Trace.TraceWarning("Something other than a NoHostAvailableException was thrown: " + e.GetType() + ", waiting another second ..."); Thread.Sleep(1000); } } } // now restart the node nonShareableTestCluster.Start(1); bool hostWasReconnected = false; DateTime timeInTheFuture = DateTime.Now.AddSeconds(20); while (!hostWasReconnected && DateTime.Now < timeInTheFuture) { try { session.Execute(query); hostWasReconnected = true; } catch (Exception e) { if (e.GetType() == typeof(NoHostAvailableException)) { Trace.TraceInformation("Host still not up yet, waiting another one second ... "); Thread.Sleep(1000); } else { throw e; } } } RowSet rowSet = session.Execute(query); Assert.True(rowSet.GetRows().Count() > 0, "It should return rows"); }
public void ReconnectionPolicyTest(Builder builder, long restartTime, long retryTime, long breakTime) { ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1); _policyTestTools.CreateSchema(testCluster.Session, 1); _policyTestTools.InitPreparedStatement(testCluster, 12); _policyTestTools.Query(testCluster, 12); // Ensure a basic test works _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 12); _policyTestTools.ResetCoordinators(); testCluster.StopForce(1); // Start timing and ensure that the node is down //long startTime = 0; var startTime = Stopwatch.StartNew(); // = 0; try { //startTime = System.nanoTime() / 1000000000; _policyTestTools.Query(testCluster, 12); Assert.Fail("Test race condition where node has not shut off quickly enough."); } catch (NoHostAvailableException) {} long elapsedSeconds; bool restarted = false; while (true) { //thisTime = System.nanoTime() / 1000000000; elapsedSeconds = startTime.ElapsedMilliseconds / 1000; // Restart node at restartTime if (!restarted && elapsedSeconds > restartTime) { testCluster.Start(1); restarted = true; } // Continue testing queries each second try { _policyTestTools.Query(testCluster, 12); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 12); _policyTestTools.ResetCoordinators(); // Ensure the time when the query completes successfully is what was expected Assert.True(retryTime - 6 < elapsedSeconds && elapsedSeconds < retryTime + 6, string.Format("Waited {0} seconds instead an expected {1} seconds wait", elapsedSeconds, retryTime)); } catch (NoHostAvailableException) { Thread.Sleep(1000); continue; } Thread.Sleep((int)(breakTime * 1000)); // The same query once more, just to be sure _policyTestTools.Query(testCluster, 12); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 12); _policyTestTools.ResetCoordinators(); // Ensure the reconnection times reset testCluster.StopForce(1); // Start timing and ensure that the node is down //startTime = 0; startTime.Reset(); try { //startTime = System.nanoTime() / 1000000000; startTime.Start(); _policyTestTools.Query(testCluster, 12); Assert.Fail("Test race condition where node has not shut off quickly enough."); } catch (NoHostAvailableException) { } restarted = false; while (true) { //elapsedSeconds = System.nanoTime() / 1000000000; elapsedSeconds = startTime.ElapsedMilliseconds / 1000; // Restart node at restartTime if (!restarted && elapsedSeconds > restartTime) { testCluster.Start(1); restarted = true; } // Continue testing queries each second try { _policyTestTools.Query(testCluster, 12); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 12); _policyTestTools.ResetCoordinators(); // Ensure the time when the query completes successfully is what was expected Assert.True(retryTime - 3 < elapsedSeconds && elapsedSeconds < retryTime + 3, string.Format("Waited {0} seconds instead an expected {1} seconds wait", elapsedSeconds, retryTime)); } catch (NoHostAvailableException) { Thread.Sleep(1000); continue; } break; } break; } }
/// <summary> /// Validate client behavior with replication three, multiple DCs /// with load balancing policy TokenAware, DCAwareRoundRobin, /// with retry policy DowngradingConsistencyRetryPolicy /// after a node is taken down /// /// @test_category consistency /// @test_category connection:outage,retry_policy /// @test_category load_balancing:round_robin,token_aware,dc_aware /// </summary> public void ReplicationFactorThree_TwoDcs_DcAware_DowngradingConsistencyRetryPolicy() { // Seetup ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(3, 3, DefaultMaxClusterCreateRetries, true); testCluster.Builder = Cluster.Builder() .WithLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy("dc2"))) .WithRetryPolicy(DowngradingConsistencyRetryPolicy.Instance); testCluster.InitClient(); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "1", DefaultCassandraPort, 30); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "2", DefaultCassandraPort, 30); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "3", DefaultCassandraPort, 30); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "4", DefaultCassandraPort, 30); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "5", DefaultCassandraPort, 30); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "6", DefaultCassandraPort, 30); // Test _policyTestTools.CreateMultiDcSchema(testCluster.Session, 3, 3); _policyTestTools.InitPreparedStatement(testCluster, 12, ConsistencyLevel.Two); _policyTestTools.Query(testCluster, 12, ConsistencyLevel.Two); // Validate expected number of host / query counts _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 0); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 0); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "3:" + DefaultCassandraPort, 0); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "4:" + DefaultCassandraPort, 4); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "5:" + DefaultCassandraPort, 4); _policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "6:" + DefaultCassandraPort, 4); _policyTestTools.ResetCoordinators(); testCluster.StopForce(2); // FIXME: This sleep is needed to allow the waitFor() to work TestUtils.WaitForDownWithWait(testCluster.ClusterIpPrefix + "2", testCluster.Cluster, 5); var acceptedList = new List <ConsistencyLevel> { ConsistencyLevel.Any, ConsistencyLevel.One, ConsistencyLevel.Two, ConsistencyLevel.Quorum, ConsistencyLevel.Three, ConsistencyLevel.All, ConsistencyLevel.LocalQuorum, ConsistencyLevel.EachQuorum }; var failList = new List <ConsistencyLevel>(); // Test successful writes foreach (ConsistencyLevel consistencyLevel in acceptedList) { try { _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel); } catch (Exception e) { Assert.Fail(String.Format("Test failed at CL.{0} with message: {1}", consistencyLevel, e.Message)); } } // Test successful reads foreach (ConsistencyLevel consistencyLevel in acceptedList) { try { _policyTestTools.Query(testCluster, 12, consistencyLevel); } catch (InvalidQueryException e) { var acceptableErrorMessages = new List <string> { "EACH_QUORUM ConsistencyLevel is only supported for writes", "ANY ConsistencyLevel is only supported for writes" }; Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message)); } } // Test writes which should fail foreach (ConsistencyLevel consistencyLevel in failList) { try { _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel); Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel)); } catch (UnavailableException) { // expected to fail when the client has already marked the // node as DOWN } catch (WriteTimeoutException) { // expected to fail when the client hasn't marked the' // node as DOWN yet } } // Test reads which should fail foreach (ConsistencyLevel consistencyLevel in failList) { try { _policyTestTools.Query(testCluster, 12, consistencyLevel); Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel)); } catch (ReadTimeoutException) { // expected to fail when the client hasn't marked the' // node as DOWN yet } catch (UnavailableException) { // expected to fail when the client has already marked the // node as DOWN } } }
public void ReplicationFactorThree_TwoDCs_DowngradingConsistencyRetryPolicy() { ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(3, 3, DefaultMaxClusterCreateRetries, true); testCluster.Builder = Cluster.Builder() .WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) .WithRetryPolicy(DowngradingConsistencyRetryPolicy.Instance); testCluster.InitClient(); _policyTestTools.CreateMultiDcSchema(testCluster.Session, 3, 3); _policyTestTools.InitPreparedStatement(testCluster, 12, ConsistencyLevel.Two); // a maximum of 4 IPs should have returned values for the query -- two copies per each of the two DCs int queriesPerIteration = 4; int queriesCompleted = 0; int actualTries = 0; int maxTries = 20; while (_policyTestTools.Coordinators.Count() < 4 && actualTries < maxTries) { _policyTestTools.Query(testCluster, queriesPerIteration, ConsistencyLevel.Two); queriesCompleted += queriesPerIteration; } Assert.IsTrue(_policyTestTools.Coordinators.Count() >= 4, "The minimum number of hosts queried was not met!"); int totalQueriesForAllHosts = _policyTestTools.Coordinators.Sum(c => c.Value); Assert.AreEqual(queriesCompleted, totalQueriesForAllHosts, "The sum of queries for all hosts should equal the number of queries recorded by the calling test!"); _policyTestTools.ResetCoordinators(); testCluster.StopForce(2); // FIXME: This sleep is needed to allow the waitFor() to work TestUtils.WaitForDownWithWait(testCluster.ClusterIpPrefix + "2", testCluster.Cluster, 5); var acceptedList = new List <ConsistencyLevel> { ConsistencyLevel.Any, ConsistencyLevel.One, ConsistencyLevel.Two, ConsistencyLevel.Quorum, ConsistencyLevel.Three, ConsistencyLevel.All, ConsistencyLevel.LocalQuorum, ConsistencyLevel.EachQuorum }; var failList = new List <ConsistencyLevel>(); // Test successful writes foreach (ConsistencyLevel consistencyLevel in acceptedList) { try { _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel); } catch (Exception e) { Assert.Fail(String.Format("Test failed at CL.{0} with message: {1}", consistencyLevel, e.Message)); } } // Test successful reads foreach (ConsistencyLevel consistencyLevel in acceptedList) { try { _policyTestTools.Query(testCluster, 12, consistencyLevel); } catch (InvalidQueryException e) { var acceptableErrorMessages = new List <string> { "EACH_QUORUM ConsistencyLevel is only supported for writes", "ANY ConsistencyLevel is only supported for writes" }; Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message)); } } // Test writes which should fail foreach (ConsistencyLevel consistencyLevel in failList) { try { _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel); Assert.Fail("Expected Exception was not thrown for ConsistencyLevel :" + consistencyLevel); } catch (UnavailableException) { // expected to fail when the client has already marked the // node as DOWN } catch (WriteTimeoutException) { // expected to fail when the client hasn't marked the' // node as DOWN yet } } // Test reads which should fail foreach (ConsistencyLevel consistencyLevel in failList) { try { _policyTestTools.Query(testCluster, 12, consistencyLevel); Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel)); } catch (ReadTimeoutException) { // expected to fail when the client hasn't marked the' // node as DOWN yet } catch (UnavailableException) { // expected to fail when the client has already marked the // node as DOWN } } }
public void ReplicationFactorTwo_DowngradingConsistencyRetryPolicy() { ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(3); testCluster.Builder = Cluster.Builder() .WithLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) .WithRetryPolicy(DowngradingConsistencyRetryPolicy.Instance); testCluster.InitClient(); _policyTestTools.CreateSchema(testCluster.Session, 2); _policyTestTools.InitPreparedStatement(testCluster, 12, ConsistencyLevel.Two); _policyTestTools.Query(testCluster, 12, ConsistencyLevel.Two); string coordinatorHostQueried = _policyTestTools.Coordinators.First().Key.Split(':').First();; int awareCoord = int.Parse(coordinatorHostQueried.Split('.').Last()); int coordinatorsWithMoreThanZeroQueries = 0; foreach (var coordinator in _policyTestTools.Coordinators) { coordinatorsWithMoreThanZeroQueries++; _policyTestTools.AssertQueried(coordinator.Key.ToString(), 6); } Assert.AreEqual(2, coordinatorsWithMoreThanZeroQueries); _policyTestTools.ResetCoordinators(); testCluster.StopForce(awareCoord); TestUtils.WaitForDownWithWait(testCluster.ClusterIpPrefix + awareCoord, testCluster.Cluster, 30); var acceptedList = new List <ConsistencyLevel> { ConsistencyLevel.Any, ConsistencyLevel.One, ConsistencyLevel.Two, ConsistencyLevel.Quorum, ConsistencyLevel.Three, ConsistencyLevel.All }; var failList = new List <ConsistencyLevel>(); // Test successful writes foreach (ConsistencyLevel consistencyLevel in acceptedList) { try { _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel); } catch (Exception e) { Assert.Fail(String.Format("Test failed at CL.{0} with message: {1}", consistencyLevel, e.Message)); } } // Test successful reads foreach (ConsistencyLevel consistencyLevel in acceptedList) { try { _policyTestTools.Query(testCluster, 12, consistencyLevel); } catch (InvalidQueryException e) { var acceptableErrorMessages = new List <string> { "ANY ConsistencyLevel is only supported for writes" }; Assert.True(acceptableErrorMessages.Contains(e.Message)); } } // Test writes which should fail foreach (ConsistencyLevel consistencyLevel in failList) { try { _policyTestTools.InitPreparedStatement(testCluster, 12, consistencyLevel); Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel)); } catch (InvalidQueryException e) { var acceptableErrorMessages = new List <string> { "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)", "consistency level EACH_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)" }; Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message)); } catch (UnavailableException) { // expected to fail when the client has already marked the // node as DOWN } catch (WriteTimeoutException) { // expected to fail when the client hasn't marked the' // node as DOWN yet } } // Test reads which should fail foreach (ConsistencyLevel consistencyLevel in failList) { try { _policyTestTools.Query(testCluster, 12, consistencyLevel); Assert.Fail(String.Format("Test passed at CL.{0}.", consistencyLevel)); } catch (InvalidQueryException e) { var acceptableErrorMessages = new List <string> { "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)", "EACH_QUORUM ConsistencyLevel is only supported for writes" }; Assert.True(acceptableErrorMessages.Contains(e.Message), String.Format("Received: {0}", e.Message)); } catch (ReadTimeoutException) { // expected to fail when the client hasn't marked the' // node as DOWN yet } catch (UnavailableException) { // expected to fail when the client has already marked the // node as DOWN } } }
public void RoundRobin_TwoDCs_EachDcHasOneNodeAddedAndDecommissioned() { // Setup PolicyTestTools policyTestTools = new PolicyTestTools(); ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1, 1, DefaultMaxClusterCreateRetries, true); testCluster.Builder = Cluster.Builder().WithLoadBalancingPolicy(new RoundRobinPolicy()); testCluster.InitClient(); policyTestTools.CreateSchema(testCluster.Session); policyTestTools.InitPreparedStatement(testCluster, 12); policyTestTools.Query(testCluster, 12); // Validate that all host were queried equally policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 6); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 6); // Add new node to the end of first cluster, remove node from beginning of first cluster policyTestTools.ResetCoordinators(); // Bootstrap step testCluster.BootstrapNode(3, "dc1"); string newlyBootstrappedIp = testCluster.ClusterIpPrefix + "3"; TestUtils.WaitForUp(newlyBootstrappedIp, DefaultCassandraPort, 30); // Validate expected nodes where queried policyTestTools.WaitForPolicyToolsQueryToHitBootstrappedIp(testCluster, newlyBootstrappedIp); policyTestTools.Query(testCluster, 12); policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 4); policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 4); policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + "3:" + DefaultCassandraPort, 4); // Remove node from beginning of first cluster policyTestTools.ResetCoordinators(); testCluster.DecommissionNode(1); TestUtils.waitForDecommission(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, testCluster.Cluster, 20); // Validate expected nodes where queried policyTestTools.Query(testCluster, 12); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 0); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 6); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "3:" + DefaultCassandraPort, 6); // Add new node to the end of second cluster, remove node from beginning of first cluster policyTestTools.ResetCoordinators(); testCluster.BootstrapNode(4, "dc2"); newlyBootstrappedIp = testCluster.ClusterIpPrefix + "4"; TestUtils.WaitForUp(newlyBootstrappedIp, DefaultCassandraPort, 30); policyTestTools.ResetCoordinators(); policyTestTools.Query(testCluster, 12); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 0); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 4); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "3:" + DefaultCassandraPort, 4); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "4:" + DefaultCassandraPort, 4); // Remove node from beginning of second cluster policyTestTools.ResetCoordinators(); testCluster.DecommissionNode(2); TestUtils.waitForDecommission(testCluster.ClusterIpPrefix + "2", testCluster.Cluster, 20); policyTestTools.Query(testCluster, 12); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, 0); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, 0); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "3:" + DefaultCassandraPort, 6); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + "4:" + DefaultCassandraPort, 6); }
public void FailoverThenReconnect() { var parallelOptions = new ParallelOptions { TaskScheduler = new ThreadPerTaskScheduler(), MaxDegreeOfParallelism = 100 }; var policy = new ConstantReconnectionPolicy(500); var nonShareableTestCluster = TestClusterManager.GetNonShareableTestCluster(4, 1, true, false); using (var cluster = Cluster.Builder().AddContactPoint(nonShareableTestCluster.InitialContactPoint).WithReconnectionPolicy(policy).Build()) { var session = cluster.Connect(); // Check query to host distribution before killing nodes var queriedHosts = new List <string>(); DateTime futureDateTime = DateTime.Now.AddSeconds(120); while ((from singleHost in queriedHosts select singleHost).Distinct().Count() < 4 && DateTime.Now < futureDateTime) { var rs = session.Execute("SELECT * FROM system.local"); queriedHosts.Add(rs.Info.QueriedHost.ToString()); Thread.Sleep(50); } Assert.AreEqual(4, (from singleHost in queriedHosts select singleHost).Distinct().Count(), "All hosts should have been queried!"); // Create list of actions Action selectAction = () => { var rs = session.Execute("SELECT * FROM system.local"); Assert.Greater(rs.Count(), 0); }; var actions = new List <Action>(); for (var i = 0; i < 100; i++) { actions.Add(selectAction); //Check that the control connection is using first host StringAssert.StartsWith(nonShareableTestCluster.ClusterIpPrefix + "1", nonShareableTestCluster.Cluster.Metadata.ControlConnection.Address.ToString()); //Kill some nodes //Including the one used by the control connection actions.Insert(20, () => nonShareableTestCluster.Stop(1)); actions.Insert(20, () => nonShareableTestCluster.Stop(2)); actions.Insert(80, () => nonShareableTestCluster.Stop(3)); //Execute in parallel more than 100 actions Parallel.Invoke(parallelOptions, actions.ToArray()); //Wait for the nodes to be killed TestUtils.WaitForDown(nonShareableTestCluster.ClusterIpPrefix + "1", nonShareableTestCluster.Cluster, 20); TestUtils.WaitForDown(nonShareableTestCluster.ClusterIpPrefix + "2", nonShareableTestCluster.Cluster, 20); TestUtils.WaitForDown(nonShareableTestCluster.ClusterIpPrefix + "3", nonShareableTestCluster.Cluster, 20); actions = new List <Action>(); for (var j = 0; j < 100; j++) { actions.Add(selectAction); } //Check that the control connection is using first host //bring back some nodes actions.Insert(3, () => nonShareableTestCluster.Start(3)); actions.Insert(50, () => nonShareableTestCluster.Start(2)); actions.Insert(50, () => nonShareableTestCluster.Start(1)); //Execute in parallel more than 100 actions Trace.TraceInformation("Start invoking with restart nodes"); Parallel.Invoke(parallelOptions, actions.ToArray()); //Wait for the nodes to be restarted TestUtils.WaitForUp(nonShareableTestCluster.ClusterIpPrefix + "1", DefaultCassandraPort, 30); TestUtils.WaitForUp(nonShareableTestCluster.ClusterIpPrefix + "2", DefaultCassandraPort, 30); TestUtils.WaitForUp(nonShareableTestCluster.ClusterIpPrefix + "3", DefaultCassandraPort, 30); queriedHosts.Clear(); // keep querying hosts until they are all queried, or time runs out futureDateTime = DateTime.Now.AddSeconds(120); while ((from singleHost in queriedHosts select singleHost).Distinct().Count() < 4 && DateTime.Now < futureDateTime) { var rs = session.Execute("SELECT * FROM system.local"); queriedHosts.Add(rs.Info.QueriedHost.ToString()); Thread.Sleep(50); } //Check that one of the restarted nodes were queried Assert.Contains(nonShareableTestCluster.ClusterIpPrefix + "1:" + DefaultCassandraPort, queriedHosts); Assert.Contains(nonShareableTestCluster.ClusterIpPrefix + "2:" + DefaultCassandraPort, queriedHosts); Assert.Contains(nonShareableTestCluster.ClusterIpPrefix + "3:" + DefaultCassandraPort, queriedHosts); Assert.Contains(nonShareableTestCluster.ClusterIpPrefix + "4:" + DefaultCassandraPort, queriedHosts); //Check that the control connection is still using last host StringAssert.StartsWith(nonShareableTestCluster.ClusterIpPrefix + "4", nonShareableTestCluster.Cluster.Metadata.ControlConnection.Address.ToString()); } } }
public void TableMetadataNestedCollectionsTest() { if (TestClusterManager.CheckCassandraVersion(false, Version.Parse("2.1.3"), Comparison.LessThan)) { Assert.Ignore("Nested frozen collections are supported in 2.1.3 and above"); return; } var keyspaceName = TestUtils.GetUniqueKeyspaceName(); const string tableName = "tbl_nested_cols_meta"; ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount); var cluster = testCluster.Cluster; var session = testCluster.Session; session.CreateKeyspaceIfNotExists(keyspaceName); session.ChangeKeyspace(keyspaceName); session.Execute(string.Format("CREATE TABLE {0} (" + "id uuid primary key, " + "map1 map<varchar, frozen<list<timeuuid>>>," + "map2 map<int, frozen<map<uuid, bigint>>>," + "list1 list<frozen<map<uuid, int>>>)", tableName)); var table = cluster.Metadata .GetKeyspace(keyspaceName) .GetTableMetadata(tableName); Assert.AreEqual(4, table.TableColumns.Length); var map1 = table.TableColumns.First(c => c.Name == "map1"); Assert.AreEqual(ColumnTypeCode.Map, map1.TypeCode); Assert.IsInstanceOf <MapColumnInfo>(map1.TypeInfo); var map1Info = (MapColumnInfo)map1.TypeInfo; Assert.True(map1Info.KeyTypeCode == ColumnTypeCode.Varchar || map1Info.KeyTypeCode == ColumnTypeCode.Text, "Expected {0} but was {1}", ColumnTypeCode.Varchar, map1Info.KeyTypeCode); Assert.AreEqual(ColumnTypeCode.List, map1Info.ValueTypeCode); Assert.IsInstanceOf <ListColumnInfo>(map1Info.ValueTypeInfo); var map1ListInfo = (ListColumnInfo)map1Info.ValueTypeInfo; Assert.AreEqual(ColumnTypeCode.Timeuuid, map1ListInfo.ValueTypeCode); var map2 = table.TableColumns.First(c => c.Name == "map2"); Assert.AreEqual(ColumnTypeCode.Map, map2.TypeCode); Assert.IsInstanceOf <MapColumnInfo>(map2.TypeInfo); var map2Info = (MapColumnInfo)map2.TypeInfo; Assert.AreEqual(ColumnTypeCode.Int, map2Info.KeyTypeCode); Assert.AreEqual(ColumnTypeCode.Map, map2Info.ValueTypeCode); Assert.IsInstanceOf <MapColumnInfo>(map2Info.ValueTypeInfo); var map2MapInfo = (MapColumnInfo)map2Info.ValueTypeInfo; Assert.AreEqual(ColumnTypeCode.Uuid, map2MapInfo.KeyTypeCode); Assert.AreEqual(ColumnTypeCode.Bigint, map2MapInfo.ValueTypeCode); var list1 = table.TableColumns.First(c => c.Name == "list1"); Assert.AreEqual(ColumnTypeCode.List, list1.TypeCode); Assert.IsInstanceOf <ListColumnInfo>(list1.TypeInfo); var list1Info = (ListColumnInfo)list1.TypeInfo; Assert.AreEqual(ColumnTypeCode.Map, list1Info.ValueTypeCode); Assert.IsInstanceOf <MapColumnInfo>(list1Info.ValueTypeInfo); var list1MapInfo = (MapColumnInfo)list1Info.ValueTypeInfo; Assert.AreEqual(ColumnTypeCode.Uuid, list1MapInfo.KeyTypeCode); Assert.AreEqual(ColumnTypeCode.Int, list1MapInfo.ValueTypeCode); }
public void TableMetadataCompositePartitionKeyTest() { string keyspaceName = TestUtils.GetUniqueKeyspaceName(); ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(DefaultNodeCount); var cluster = testCluster.Cluster; var session = testCluster.Session; string tableName1 = TestUtils.GetUniqueTableName().ToLower(); string cql = "CREATE TABLE " + tableName1 + " ( " + @"b int, a text, c int, d int, PRIMARY KEY ((a, b), c))"; session.CreateKeyspaceIfNotExists(keyspaceName); session.ChangeKeyspace(keyspaceName); session.Execute(cql); session.Execute("INSERT INTO " + tableName1 + " (a, b, c, d) VALUES ('1', 2, 3, 4)"); var rs = session.Execute("select * from " + tableName1); Assert.True(rs.GetRows().Count() == 1); var table = cluster.Metadata .GetKeyspace(keyspaceName) .GetTableMetadata(tableName1); Assert.True(table.TableColumns.Count() == 4); Assert.AreEqual(2, table.PartitionKeys.Length); Assert.AreEqual("a, b", String.Join(", ", table.PartitionKeys.Select(p => p.Name))); string tableName2 = TestUtils.GetUniqueTableName().ToLower(); cql = "CREATE TABLE " + tableName2 + " ( " + @"a text, b text, c int, d int, PRIMARY KEY ((a, b, c)))"; session.Execute(cql); table = cluster.Metadata .GetKeyspace(keyspaceName) .GetTableMetadata(tableName2); Assert.True(table.TableColumns.Count() == 4); Assert.AreEqual("a, b, c", String.Join(", ", table.PartitionKeys.Select(p => p.Name))); string tableName3 = TestUtils.GetUniqueTableName().ToLower(); cql = "CREATE TABLE " + tableName3 + " ( " + @"a text, b text, c timestamp, d int, PRIMARY KEY (a, b, c))"; session.Execute(cql); table = cluster.Metadata .GetKeyspace(keyspaceName) .GetTableMetadata(tableName3); Assert.True(table.TableColumns.Count() == 4); //Just 1 partition key Assert.AreEqual("a", String.Join(", ", table.PartitionKeys.Select(p => p.Name))); }