public void TestFixtureSetup() { if (CassandraVersion < Version.Parse("2.2")) { return; } _testCluster = TestClusterManager.GetTestCluster(1, 0, false, DefaultMaxClusterCreateRetries, false, false); _testCluster.UpdateConfig("enable_user_defined_functions: true"); _testCluster.Start(1); using (var cluster = Cluster.Builder().AddContactPoint(_testCluster.InitialContactPoint).Build()) { var session = cluster.Connect(); var queries = new[] { "CREATE KEYSPACE ks_udf WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}", "CREATE FUNCTION ks_udf.return_one() RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return 1;'", "CREATE FUNCTION ks_udf.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return s+v;'", "CREATE FUNCTION ks_udf.plus(s bigint, v bigint) RETURNS NULL ON NULL INPUT RETURNS bigint LANGUAGE java AS 'return s+v;'", "CREATE AGGREGATE ks_udf.sum(int) SFUNC plus STYPE int INITCOND 1", "CREATE AGGREGATE ks_udf.sum(bigint) SFUNC plus STYPE bigint INITCOND 2" }; foreach (var q in queries) { session.Execute(q); } } }
private void ReprepareTest(bool useKeyspace) { string keyspace = DefaultKeyspaceName; ITestCluster testCluster = TestClusterManager.GetNonShareableTestCluster(1); testCluster.InitClient(); // make sure client session was just created var nonShareableSession = testCluster.Session; string fqKeyspaceName = ""; if (useKeyspace) { nonShareableSession.ChangeKeyspace(keyspace); } else { fqKeyspaceName = keyspace + "."; } try { nonShareableSession.Execute("CREATE TABLE " + fqKeyspaceName + "test(k text PRIMARY KEY, i int)"); } catch (AlreadyExistsException) { } nonShareableSession.Execute("INSERT INTO " + fqKeyspaceName + "test (k, i) VALUES ('123', 17)"); nonShareableSession.Execute("INSERT INTO " + fqKeyspaceName + "test (k, i) VALUES ('124', 18)"); PreparedStatement ps = nonShareableSession.Prepare("SELECT * FROM " + fqKeyspaceName + "test WHERE k = ?"); var rs = nonShareableSession.Execute(ps.Bind("123")); Assert.AreEqual(rs.First().GetValue <int>("i"), 17); testCluster.Stop(1); TestUtils.WaitForDown(testCluster.ClusterIpPrefix + "1", testCluster.Cluster, 40); testCluster.Start(1); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + "1", DefaultCassandraPort, 60); Assert.True(nonShareableSession.Cluster.AllHosts().Select(h => h.IsUp).Any(), "There should be one node up"); for (var i = 0; i < 10; i++) { var rowset = nonShareableSession.Execute(ps.Bind("124")); Assert.AreEqual(rowset.First().GetValue <int>("i"), 18); } }
private void RetryPolicyTest(ITestCluster testCluster) { PolicyTestTools policyTestTools = new PolicyTestTools(); policyTestTools.TableName = TestUtils.GetUniqueTableName(); policyTestTools.CreateSchema(testCluster.Session, 2); // Test before state policyTestTools.InitPreparedStatement(testCluster, 12); policyTestTools.Query(testCluster, 12); int clusterPosQueried = 1; int clusterPosNotQueried = 2; if (!policyTestTools.Coordinators.ContainsKey(testCluster.ClusterIpPrefix + clusterPosQueried)) { clusterPosQueried = 2; clusterPosNotQueried = 1; } policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1); // Stop one of the nodes and test policyTestTools.ResetCoordinators(); testCluster.Stop(clusterPosQueried); TestUtils.WaitForDown(testCluster.ClusterIpPrefix + clusterPosQueried, testCluster.Cluster, 30); policyTestTools.Query(testCluster, 120); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 120); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 0); // Start the node that was just down, then down the node that was just up policyTestTools.ResetCoordinators(); testCluster.Start(clusterPosQueried); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + clusterPosQueried, DefaultCassandraPort, 30); // Test successful reads DateTime futureDateTime = DateTime.Now.AddSeconds(120); while (policyTestTools.Coordinators.Count < 2 && DateTime.Now < futureDateTime) { policyTestTools.Query(testCluster, 120); Thread.Sleep(75); } // Ensure that the nodes were contacted policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1); policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 1); }
public void TestFixtureSetup() { if (TestClusterManager.CheckCassandraVersion(false, Version.Parse("2.2"), Comparison.LessThan)) { return; } _testCluster = TestClusterManager.GetTestCluster(1, 0, false, DefaultMaxClusterCreateRetries, false, false); _testCluster.UpdateConfig("enable_user_defined_functions: true"); _testCluster.Start(1); using (var cluster = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint).Build()) { var session = cluster.Connect(); var queries = new List <string> { "CREATE KEYSPACE ks_udf WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}", "CREATE FUNCTION ks_udf.return_one() RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return 1;'", "CREATE FUNCTION ks_udf.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return s+v;'", "CREATE FUNCTION ks_udf.plus(s bigint, v bigint) RETURNS NULL ON NULL INPUT RETURNS bigint LANGUAGE java AS 'return s+v;'", "CREATE AGGREGATE ks_udf.sum(int) SFUNC plus STYPE int INITCOND 1", "CREATE AGGREGATE ks_udf.sum(bigint) SFUNC plus STYPE bigint INITCOND 2" }; if (TestClusterManager.CheckDseVersion(new Version(6, 0), Comparison.GreaterThanOrEqualsTo)) { queries.Add("CREATE FUNCTION ks_udf.deterministic(dividend int, divisor int) " + "CALLED ON NULL INPUT RETURNS int DETERMINISTIC LANGUAGE java AS " + "'return dividend / divisor;'"); queries.Add("CREATE FUNCTION ks_udf.monotonic(dividend int, divisor int) " + "CALLED ON NULL INPUT RETURNS int MONOTONIC LANGUAGE java AS " + "'return dividend / divisor;'"); queries.Add("CREATE FUNCTION ks_udf.md(dividend int, divisor int) " + "CALLED ON NULL INPUT RETURNS int DETERMINISTIC MONOTONIC LANGUAGE java AS " + "'return dividend / divisor;'"); queries.Add("CREATE FUNCTION ks_udf.monotonic_on(dividend int, divisor int) " + "CALLED ON NULL INPUT RETURNS int MONOTONIC ON dividend LANGUAGE java AS " + "'return dividend / divisor;'"); queries.Add("CREATE AGGREGATE ks_udf.deta(int) SFUNC plus STYPE int INITCOND 0 DETERMINISTIC;"); } foreach (var q in queries) { session.Execute(q); } } }
private void PrepareForSparkTest(ITestCluster testCluster) { const string replicationConfigStr = "{'class' : 'SimpleStrategy', 'replication_factor' : 2}"; using (var cluster = Cluster.Builder().AddContactPoint(TestClusterManager.InitialContactPoint).Build()) { WaitForWorkers(1); var session = cluster.Connect(); Trace.TraceInformation("GraphMultiNodeTests: Altering keyspace for dse_leases"); session.Execute( "ALTER KEYSPACE dse_leases WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'GraphAnalytics': '2'}"); Trace.TraceInformation("GraphMultiNodeTests: Bootstrapping node 2"); testCluster.BootstrapNode(2, false); Trace.TraceInformation("GraphMultiNodeTests: Setting workload"); testCluster.SetNodeWorkloads(2, new[] { "graph", "spark" }); Trace.TraceInformation("GraphMultiNodeTests: Starting node 2"); testCluster.Start(2); Trace.TraceInformation("Waiting additional time for new node to be ready"); Thread.Sleep(15000); WaitForWorkers(2); Trace.TraceInformation("GraphMultiNodeTests: Creating graph"); session.ExecuteGraph(new SimpleGraphStatement( "system.graph(name)" + ".option('graph.replication_config').set(replicationConfig)" + ".option('graph.system_replication_config').set(replicationConfig)" + ".ifNotExists()" + (!TestClusterManager.SupportsNextGenGraph() ? string.Empty : ".engine(Classic)") + ".create()", new { name = GraphMultiNodeTests.GraphName, replicationConfig = replicationConfigStr })); Trace.TraceInformation("GraphMultiNodeTests: Created graph"); var graphStatements = new StringBuilder(); graphStatements.Append(BaseIntegrationTest.MakeStrict + "\n"); graphStatements.Append(BaseIntegrationTest.AllowScans + "\n"); graphStatements.Append(BaseIntegrationTest.ClassicSchemaGremlinQuery + "\n"); graphStatements.Append(BaseIntegrationTest.ClassicLoadGremlinQuery); session.ExecuteGraph(new SimpleGraphStatement(graphStatements.ToString()).SetGraphName(GraphMultiNodeTests.GraphName)); } }
public void ReconnectionRecyclesPool() { var policy = new ConstantReconnectionPolicy(5000); ITestCluster nonShareableTestCluster = TestClusterManager.GetNonShareableTestCluster(2); 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.schema_columnfamilies"); 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(connections.First().ProtocolVersion) .GetCoreConnectionsPerHost(HostDistance.Local); Assert.AreEqual(expectedCoreConnections, connections.Length); Assert.True(connections.All(c => !c.IsClosed)); }
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; } }
public void FailoverThenReconnect() { var parallelOptions = new ParallelOptions { TaskScheduler = new ThreadPerTaskScheduler(), MaxDegreeOfParallelism = 100 }; var policy = new ConstantReconnectionPolicy(500); ITestCluster nonShareableTestCluster = TestClusterManager.GetNonShareableTestCluster(4); nonShareableTestCluster.Builder = new Builder().WithReconnectionPolicy(policy); nonShareableTestCluster.InitClient(); // this will replace the existing session using the newly assigned Builder instance var session = nonShareableTestCluster.Session; // 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.schema_columnfamilies"); 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.schema_columnfamilies"); 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.BindAddress.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 i = 0; i < 100; i++) { 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.schema_columnfamilies"); 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.BindAddress.ToString()); }
private void RetryPolicyTest(ITestCluster testCluster) { PolicyTestTools policyTestTools = new PolicyTestTools(); policyTestTools.TableName = TestUtils.GetUniqueTableName(); policyTestTools.CreateSchema(testCluster.Session, 2); // Test before state policyTestTools.InitPreparedStatement(testCluster, 12); policyTestTools.Query(testCluster, 12); int clusterPosQueried = 1; int clusterPosNotQueried = 2; if (!policyTestTools.Coordinators.ContainsKey(testCluster.ClusterIpPrefix + clusterPosQueried)) { clusterPosQueried = 2; clusterPosNotQueried = 1; } policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1); // Stop one of the nodes and test policyTestTools.ResetCoordinators(); testCluster.Stop(clusterPosQueried); TestUtils.WaitForDown(testCluster.ClusterIpPrefix + clusterPosQueried, testCluster.Cluster, 30); policyTestTools.Query(testCluster, 120); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 120); policyTestTools.AssertQueried(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 0); // Start the node that was just down, then down the node that was just up policyTestTools.ResetCoordinators(); testCluster.Start(clusterPosQueried); TestUtils.WaitForUp(testCluster.ClusterIpPrefix + clusterPosQueried, DefaultCassandraPort, 30); // Test successful reads DateTime futureDateTime = DateTime.Now.AddSeconds(120); while (policyTestTools.Coordinators.Count < 2 && DateTime.Now < futureDateTime) { policyTestTools.Query(testCluster, 120); Thread.Sleep(75); } // Ensure that the nodes were contacted policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosQueried + ":" + DefaultCassandraPort, 1); policyTestTools.AssertQueriedAtLeast(testCluster.ClusterIpPrefix + clusterPosNotQueried + ":" + DefaultCassandraPort, 1); }