public void GetRequest_Batch_With_64K_Queries() { var batch = new BatchStatement(); for (var i = 0; i < ushort.MaxValue; i++) { batch.Add(new SimpleStatement("QUERY")); } var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator(), Mock.Of <IStartupOptionsFactory>(), new SessionFactoryBuilder()); var request = RequestHandler.GetRequest(batch, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] CollectionAssert.AreEqual(new byte[] { 0xff, 0xff }, bodyBuffer.Skip(1).Take(2)); }
public void GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp() { var statement = new SimpleStatement("STATEMENT WITH TIMESTAMP"); var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20); statement.SetTimestamp(expectedTimestamp); var policies = new Cassandra.Policies( Cassandra.Policies.DefaultLoadBalancingPolicy, Cassandra.Policies.DefaultReconnectionPolicy, Cassandra.Policies.DefaultRetryPolicy, Cassandra.Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = RequestHandlerTests.GetConfig(new QueryOptions(), policies, PoolingOptions.Create()); var request = RequestHandler.GetRequest(statement, Serializer, config.DefaultRequestOptions); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); // Skip result_page_size (4) + serial_consistency (2) offset += 6; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp); }
public void PoolingOptions_Create_Based_On_Protocol(ProtocolVersion protocolVersion, int coreConnectionLength) { var sCluster = SimulacronCluster.CreateNew(new SimulacronOptions()); var options1 = PoolingOptions.Create(protocolVersion); using (var cluster = Cluster.Builder() .AddContactPoint(sCluster.InitialContactPoint) .WithPoolingOptions(options1) .Build()) { var session = (IInternalSession)cluster.Connect(); var allHosts = cluster.AllHosts(); var host = allHosts.First(); var pool = session.GetOrCreateConnectionPool(host, HostDistance.Local); TestHelper.WaitUntil(() => pool.OpenConnections == coreConnectionLength); var ports = sCluster.GetConnectedPorts(); TestHelper.RetryAssert(() => { ports = sCluster.GetConnectedPorts(); //coreConnectionLength + 1 (the control connection) Assert.AreEqual(coreConnectionLength + 1, ports.Count); }, 100, 200); } }
public void GetRequest_Batch_With_Empty_Timestamp_Generator() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1 + 2 + 1; var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = (QueryFlags)bodyBuffer[offset++]; Assert.False(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); // No more data Assert.AreEqual(bodyBuffer.Length, offset); }
public async Task Connection_Should_Send_Options_Requests_For_Heartbeats(bool executeQuery) { var builder = Cluster.Builder() .WithPoolingOptions(PoolingOptions.Create().SetHeartBeatInterval(1000)) .AddContactPoint(_testCluster.InitialContactPoint); using (var cluster = builder.Build()) { var session = await cluster.ConnectAsync().ConfigureAwait(false); var logs = await _testCluster.GetNodes().First() .GetQueriesAsync(null, OptionsQueryType).ConfigureAwait(false); // Test idle connection after connect and after a successful query if (executeQuery) { await session.ExecuteAsync(new SimpleStatement(Query)).ConfigureAwait(false); } var initialCount = logs.Count; await Task.Delay(1400).ConfigureAwait(false); logs = await _testCluster.GetNodes().First() .GetQueriesAsync(null, OptionsQueryType).ConfigureAwait(false); Assert.That(logs.Count, Is.GreaterThan(initialCount)); } }
public void GetRequest_With_Timestamp_Generator_Empty_Value() { var statement = new SimpleStatement("QUERY"); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(statement, Serializer.Default, config); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.False(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); Assert.False(flags.HasFlag(QueryFlags.Values)); Assert.False(flags.HasFlag(QueryFlags.WithPagingState)); Assert.False(flags.HasFlag(QueryFlags.SkipMetadata)); Assert.True(flags.HasFlag(QueryFlags.WithSerialConsistency)); }
private IHostConnectionPool CreatePool(IEndPointResolver res = null) { _host = new Host(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042)); if (res == null) { _resolver = Mock.Of <IEndPointResolver>(); Mock.Get(_resolver).Setup(resolver => resolver.GetConnectionEndPointAsync(_host, It.IsAny <bool>())) .ReturnsAsync((Host h, bool b) => new ConnectionEndPoint(h.Address, null)); } else { _resolver = res; } var pool = new HostConnectionPool( _host, new TestConfigurationBuilder { EndPointResolver = _resolver, ConnectionFactory = new FakeConnectionFactory(), Policies = new Cassandra.Policies( new RoundRobinPolicy(), new ConstantReconnectionPolicy(1), new DefaultRetryPolicy(), NoSpeculativeExecutionPolicy.Instance, new AtomicMonotonicTimestampGenerator()), PoolingOptions = PoolingOptions.Create(ProtocolVersion.V4).SetCoreConnectionsPerHost(HostDistance.Local, 2) }.Build(), SerializerManager.Default.GetCurrentSerializer(), new MetricsObserverFactory(new MetricsManager(new NullDriverMetricsProvider(), new DriverMetricsOptions(), false, "s1")) ); pool.SetDistance(HostDistance.Local); // set expected connections length return(pool); }
public void GetRequest_Batch_With_Empty_Timestamp_Generator() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1 + 2 + 1; var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.False(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); // Only serial consistency left Assert.AreEqual(bodyBuffer.Length, offset + 2); }
public async Task Should_Create_Core_Connections_To_Hosts_In_Local_Dc_When_Warmup_Is_Enabled() { const int nodeLength = 4; var poolingOptions = PoolingOptions.Create().SetCoreConnectionsPerHost(HostDistance.Local, 5); // Use multiple DCs: 4 nodes in first DC and 3 nodes in second DC using (var testCluster = SimulacronCluster.CreateNew(new SimulacronOptions { Nodes = $"{nodeLength},3" })) using (var cluster = Cluster.Builder() .AddContactPoint(testCluster.InitialContactPoint) .WithPoolingOptions(poolingOptions).Build()) { var session = await cluster.ConnectAsync().ConfigureAwait(false); var state = session.GetState(); var hosts = state.GetConnectedHosts(); Assert.AreEqual(nodeLength, hosts.Count); foreach (var host in hosts) { Assert.AreEqual(poolingOptions.GetCoreConnectionsPerHost(HostDistance.Local), state.GetOpenConnections(host)); } } }
public void GetRequest_Batch_With_Provided_Timestamp() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var providedTimestamp = DateTimeOffset.Now; // To microsecond precision providedTimestamp = providedTimestamp.Subtract(TimeSpan.FromTicks(providedTimestamp.Ticks % 10)); batch.SetTimestamp(providedTimestamp); var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1 + 2 + 1; var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); // Skip serial consistency offset += 2; var timestamp = TypeSerializer.UnixStart.AddTicks(BeConverter.ToInt64(bodyBuffer, offset) * 10); Assert.AreEqual(providedTimestamp, timestamp); }
public async Task Should_Use_Next_Host_When_First_Host_Is_Busy() { const int connectionLength = 2; const int maxRequestsPerConnection = 100; var builder = Cluster.Builder() .WithPoolingOptions( PoolingOptions.Create() .SetCoreConnectionsPerHost(HostDistance.Local, connectionLength) .SetMaxConnectionsPerHost(HostDistance.Local, connectionLength) .SetHeartBeatInterval(0) .SetMaxRequestsPerConnection(maxRequestsPerConnection)) .WithLoadBalancingPolicy(new TestHelper.OrderedLoadBalancingPolicy()); using (var testCluster = SimulacronCluster.CreateNew(new SimulacronOptions { Nodes = "3" })) using (var cluster = builder.AddContactPoint(testCluster.InitialContactPoint).Build()) { const string query = "SELECT * FROM simulated_ks.table1"; testCluster.Prime(new { when = new { query }, then = new { result = "success", delay_in_ms = 3000 } }); var session = await cluster.ConnectAsync(); var hosts = cluster.AllHosts().ToArray(); // Wait until all connections to first host are created await TestHelper.WaitUntilAsync(() => session.GetState().GetInFlightQueries(hosts[0]) == connectionLength); const int overflowToNextHost = 10; var length = maxRequestsPerConnection * connectionLength + Environment.ProcessorCount + overflowToNextHost; var tasks = new List <Task <RowSet> >(length); for (var i = 0; i < length; i++) { tasks.Add(session.ExecuteAsync(new SimpleStatement(query))); } var results = await Task.WhenAll(tasks); // At least the first n (maxRequestsPerConnection * connectionLength) went to the first host Assert.That(results.Count(r => r.Info.QueriedHost.Equals(hosts[0].Address)), Is.GreaterThanOrEqualTo(maxRequestsPerConnection * connectionLength)); // At least the following m (overflowToNextHost) went to the second host Assert.That(results.Count(r => r.Info.QueriedHost.Equals(hosts[1].Address)), Is.GreaterThanOrEqualTo(overflowToNextHost)); } }
public void PoolingOptions_Create_Based_On_Protocol_Version(ProtocolVersion protocolVersion, int coreConnections, int maxConnections) { var options1 = PoolingOptions.Create(protocolVersion); var cluster1 = Cluster.Builder() .AddContactPoint("::1") .WithPoolingOptions(options1) .Build(); Assert.AreEqual(coreConnections, cluster1.Configuration.PoolingOptions.GetCoreConnectionsPerHost(HostDistance.Local)); Assert.AreEqual(maxConnections, cluster1.Configuration.PoolingOptions.GetMaxConnectionPerHost(HostDistance.Local)); }
public void Should_Use_Statement_ReadTimeout() { const int generalReadTimeout = 100; const int statementReadTimeout = 3000; _testCluster = SimulacronCluster.CreateNew(1); var socketOptions = new SocketOptions().SetReadTimeoutMillis(generalReadTimeout); var queryOptions = new QueryOptions().SetRetryOnTimeout(false); var builder = Cluster.Builder().AddContactPoint(_testCluster.InitialContactPoint) .WithSocketOptions(socketOptions) .WithPoolingOptions(PoolingOptions.Create().SetHeartBeatInterval(0)) .WithQueryTimeout(Timeout.Infinite) .WithQueryOptions(queryOptions); using (var cluster = builder.Build()) { var session = cluster.Connect(); //warmup TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10); var nodes = _testCluster.GetNodes().ToList(); var node = nodes[0]; node.Prime(new { when = new { query = "SELECT key FROM system.local" }, then = new { result = "success", delay_in_ms = 10000, rows = new[] { new { key = "123" } }, column_types = new { key = "ascii" }, ignore_on_prepare = false } }); var stopWatch = new Stopwatch(); stopWatch.Start(); Assert.Throws <OperationTimedOutException>(() => session.Execute("SELECT key FROM system.local")); stopWatch.Stop(); //precision of the timer is not guaranteed Assert.Greater(stopWatch.ElapsedMilliseconds, generalReadTimeout - 1000); Assert.Less(stopWatch.ElapsedMilliseconds, generalReadTimeout + 1000); //Try with an specified timeout at Statement level var stmt = new SimpleStatement("SELECT key FROM system.local") .SetReadTimeoutMillis(statementReadTimeout); stopWatch.Restart(); Assert.Throws <OperationTimedOutException>(() => session.Execute(stmt)); stopWatch.Stop(); //precision of the timer is not guaranteed Assert.Greater(stopWatch.ElapsedMilliseconds, statementReadTimeout - 1000); Assert.Less(stopWatch.ElapsedMilliseconds, statementReadTimeout + 1000); } }
public void GetRequest_Batch_With_Timestamp_Generator() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var startDate = DateTimeOffset.Now; // To microsecond precision startDate = startDate.Subtract(TimeSpan.FromTicks(startDate.Ticks % 10)); var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator(), Mock.Of <IStartupOptionsFactory>(), new SessionFactoryBuilder()); var request = RequestHandler.GetRequest(batch, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1; // n = 1 Assert.AreEqual(1, BeConverter.ToInt16(bodyBuffer, offset)); // Query_1 <kind><string><n_params> offset += 2; // kind = 0, not prepared Assert.AreEqual(0, bodyBuffer[offset++]); var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); // Skip serial consistency offset += 2; var timestamp = TypeSerializer.UnixStart.AddTicks(BeConverter.ToInt64(bodyBuffer, offset) * 10); Assert.GreaterOrEqual(timestamp, startDate); Assert.LessOrEqual(timestamp, DateTimeOffset.Now.Add(TimeSpan.FromMilliseconds(100))); }
public void GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp() { var statement = new SimpleStatement("STATEMENT WITH TIMESTAMP"); var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20); statement.SetTimestamp(expectedTimestamp); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator(), Mock.Of <IStartupOptionsFactory>(), new SessionFactoryBuilder()); var request = RequestHandler.GetRequest(statement, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; // The remaining length should be = flags (1) + result_page_size (4) + serial_consistency (2) + timestamp (8) Assert.AreEqual(15, bodyBuffer.Length - offset); var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); // Skip result_page_size (4) + serial_consistency (2) offset += 6; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp); }
public void GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp() { var statement = new SimpleStatement("STATEMENT WITH TIMESTAMP"); var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20); statement.SetTimestamp(expectedTimestamp); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(statement, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The query request is composed by: // <query><consistency><flags><result_page_size><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; // The remaining length should be 13 = flags (1) + result_page_size (4) + timestamp (8) Assert.AreEqual(13, bodyBuffer.Length - offset); var flags = (QueryFlags)bodyBuffer[offset]; Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); // Skip flags (1) + result_page_size (4) offset += 5; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp); }
protected static void BuildTargetClusterAndSession() { if (_targetSession != null) { return; } _logger.LogInformation("Building target cluster and connecting session..."); _targetCluster = Cluster.Builder() .WithPort(Settings.Values.Connections.Target.Port) .WithRetryPolicy(new RetryPolicy()) .WithPoolingOptions(PoolingOptions.Create()) .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(0)) .AddContactPoints(Settings.Values.Connections.Target.Endpoints) .Build(); _targetSession = _targetCluster.Connect(); }
private static Cluster CreateCluster(CassandraSettings settings) { var poolingOptions = PoolingOptions.Create(); poolingOptions.SetMaxConnectionsPerHost(HostDistance.Remote, settings.MaxConnectionsPerHost); var builder = Cluster.Builder().WithPoolingOptions(poolingOptions); var hosts = settings.Hosts; if (settings.UseSsl) { var sslOptions = new SSLOptions(); sslOptions.SetCertificateRevocationCheck(false); sslOptions.SetRemoteCertValidationCallback(ValidateServerCertificate); sslOptions.SetHostNameResolver((Func <IPAddress, string>)(internalIpAddress => { var host = hosts.FirstOrDefault <Host>((Func <Host, bool>)(o => o.IpAddress == internalIpAddress.ToString())); if (host != null && !string.IsNullOrWhiteSpace(host.HostName)) { return(host.HostName); } return(internalIpAddress.ToString()); })); builder = builder.WithSSL(sslOptions); } if (settings.UseClusterCredentials) { builder = builder.WithCredentials(settings.ClusterUser, settings.ClusterPassword); } if (settings.UseQueryOptions) { var queryOptions = new QueryOptions(); queryOptions.SetConsistencyLevel(settings.ConsistencyLevel); builder = builder.WithQueryOptions(queryOptions); } var cluster = builder.AddContactPoints(hosts.Select <Host, string>((Func <Host, string>)(o => o.IpAddress))).Build(); return(cluster); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseCassandra("Contact Points=127.0.0.1;", opt => { opt.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "cv"); }, o => { o.WithQueryOptions(new QueryOptions().SetConsistencyLevel(ConsistencyLevel.LocalOne)) .WithReconnectionPolicy(new ConstantReconnectionPolicy(1000)) .WithRetryPolicy(new DefaultRetryPolicy()) .WithLoadBalancingPolicy(new TokenAwarePolicy(Policies.DefaultPolicies.LoadBalancingPolicy)) .WithDefaultKeyspace(GetType().Name) .WithPoolingOptions( PoolingOptions.Create() .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Remote, 1_000_000) .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 1_000_000) .SetMaxConnectionsPerHost(HostDistance.Local, 1_000_000) .SetMaxConnectionsPerHost(HostDistance.Remote, 1_000_000) .SetMaxRequestsPerConnection(1_000_000) ); });
public async Task Session_GetState_Should_Return_A_Snapshot_Of_The_Pools_State() { var poolingOptions = PoolingOptions.Create().SetCoreConnectionsPerHost(HostDistance.Local, 2); using (var cluster = Cluster.Builder() .AddContactPoint(_testCluster.InitialContactPoint) .WithPoolingOptions(poolingOptions) .Build()) { var session = cluster.Connect(); var counter = 0; ISessionState state = null; // Warmup await TestHelper.TimesLimit(() => session.ExecuteAsync(new SimpleStatement(Query)), 64, 32).ConfigureAwait(false); const int limit = 100; // Perform several queries and get a snapshot somewhere await TestHelper.TimesLimit(async() => { var count = Interlocked.Increment(ref counter); if (count == 180) { // after some requests state = session.GetState(); } return(await session.ExecuteAsync(new SimpleStatement(Query)).ConfigureAwait(false)); }, 280, 100).ConfigureAwait(false); Assert.NotNull(state); var stringState = state.ToString(); CollectionAssert.AreEquivalent(cluster.AllHosts(), state.GetConnectedHosts()); foreach (var host in cluster.AllHosts()) { Assert.AreEqual(2, state.GetOpenConnections(host)); StringAssert.Contains($"\"{host.Address}\": {{", stringState); } var totalInFlight = cluster.AllHosts().Sum(h => state.GetInFlightQueries(h)); Assert.Greater(totalInFlight, 0); Assert.LessOrEqual(totalInFlight, limit); } }
public void SpeculativeExecution_Pause_Using_All_Stream_Ids() { var maxProtocolVersion = Cluster.MaxProtocolVersion; _testCluster = TestClusterManager.GetNonShareableTestCluster(2, 1, true, false); Cluster.MaxProtocolVersion = 2; try { var pooling = PoolingOptions.Create(); var session = GetSession(new ConstantSpeculativeExecutionPolicy(50L, 1), true, null, pooling); const int pauseThreshold = 140 * 2; var tasks = new List <Task <IPAddress> >(); var semaphore = new SemaphoreSlim(150 * 2); for (var i = 0; i < 512; i++) { //Pause after the stream ids are in use for the connections if (i == pauseThreshold) { _testCluster.PauseNode(1); } semaphore.Wait(); tasks.Add(session .ExecuteAsync(new SimpleStatement(QueryLocal).SetIdempotence(true)) .ContinueSync(rs => { semaphore.Release(); return(rs.Info.QueriedHost.Address); })); } Task.WaitAll(tasks.Select(t => (Task)t).ToArray()); _testCluster.ResumeNode(1); //There shouldn't be any query using node1 as coordinator passed the threshold. Assert.AreEqual(0, tasks.Skip(pauseThreshold).Count(t => t.Result.Equals(_addressNode1))); Thread.Sleep(1000); } finally { Cluster.MaxProtocolVersion = maxProtocolVersion; } }
public void Should_Use_Statement_ReadTimeout() { const int generalReadTimeout = 1500; const int statementReadTimeout = 12000; var testCluster = TestClusterManager.CreateNew(); var socketOptions = new SocketOptions().SetReadTimeoutMillis(generalReadTimeout); var queryOptions = new QueryOptions().SetRetryOnTimeout(false); var builder = Cluster.Builder().AddContactPoint(testCluster.InitialContactPoint) .WithSocketOptions(socketOptions) .WithPoolingOptions(PoolingOptions.Create().SetHeartBeatInterval(0)) .WithQueryTimeout(Timeout.Infinite) .WithQueryOptions(queryOptions); using (var cluster = builder.Build()) { var session = cluster.Connect(); //warmup TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10); testCluster.PauseNode(1); var stopWatch = new Stopwatch(); stopWatch.Start(); Assert.Throws <OperationTimedOutException>(() => session.Execute("SELECT key FROM system.local")); stopWatch.Stop(); //precision of the timer is not guaranteed Assert.Greater(stopWatch.ElapsedMilliseconds, generalReadTimeout - 1000); Assert.Less(stopWatch.ElapsedMilliseconds, generalReadTimeout + 1000); //Try with an specified timeout at Statement level var stmt = new SimpleStatement("SELECT key FROM system.local") .SetReadTimeoutMillis(statementReadTimeout); stopWatch.Restart(); Assert.Throws <OperationTimedOutException>(() => session.Execute(stmt)); stopWatch.Stop(); //precision of the timer is not guaranteed Assert.Greater(stopWatch.ElapsedMilliseconds, statementReadTimeout - 3000); Assert.Less(stopWatch.ElapsedMilliseconds, statementReadTimeout + 3000); testCluster.ResumeNode(1); } }
public void GetRequest_With_Timestamp_Generator_Empty_Value() { var statement = new SimpleStatement("QUERY"); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(statement, Serializer.Default, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The query request is composed by: // <query><consistency><flags><result_page_size> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; // The remaining length should be 13 = flags (1) + result_page_size (4) Assert.AreEqual(5, bodyBuffer.Length - offset); var flags = (QueryFlags)bodyBuffer[offset]; Assert.False(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); Assert.False(flags.HasFlag(QueryFlags.Values)); Assert.False(flags.HasFlag(QueryFlags.WithPagingState)); Assert.False(flags.HasFlag(QueryFlags.SkipMetadata)); Assert.False(flags.HasFlag(QueryFlags.WithSerialConsistency)); }
public void GetRequest_Batch_With_Provided_Timestamp() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var providedTimestamp = DateTimeOffset.Now; // To microsecond precision providedTimestamp = providedTimestamp.Subtract(TimeSpan.FromTicks(providedTimestamp.Ticks % 10)); batch.SetTimestamp(providedTimestamp); var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1 + 2 + 1; var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = (QueryFlags)bodyBuffer[offset++]; Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); var timestamp = TypeSerializer.UnixStart.AddTicks(BeConverter.ToInt64(bodyBuffer, offset) * 10); Assert.AreEqual(providedTimestamp, timestamp); }
public void GetRequest_Batch_With_64K_Queries() { var batch = new BatchStatement(); for (var i = 0; i < ushort.MaxValue; i++) { batch.Add(new SimpleStatement("QUERY")); } var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] CollectionAssert.AreEqual(new byte[] { 0xff, 0xff }, bodyBuffer.Skip(1).Take(2)); }
public async Task Should_Throw_NoHostAvailableException_When_All_Host_Are_Busy() { const int connectionLength = 2; const int maxRequestsPerConnection = 50; var lbp = new TestHelper.OrderedLoadBalancingPolicy().UseRoundRobin(); var builder = Cluster.Builder() .WithPoolingOptions( PoolingOptions.Create() .SetCoreConnectionsPerHost(HostDistance.Local, connectionLength) .SetMaxConnectionsPerHost(HostDistance.Local, connectionLength) .SetHeartBeatInterval(0) .SetMaxRequestsPerConnection(maxRequestsPerConnection)) .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(0)) .WithLoadBalancingPolicy(lbp); using (var testCluster = SimulacronCluster.CreateNew(new SimulacronOptions { Nodes = "3" })) using (var cluster = builder.AddContactPoint(testCluster.InitialContactPoint).Build()) { const string query = "SELECT * FROM simulated_ks.table1"; testCluster.Prime(new { when = new { query }, then = new { result = "success", delay_in_ms = 3000 } }); var session = await cluster.ConnectAsync(); var hosts = cluster.AllHosts().ToArray(); await TestHelper.TimesLimit(() => session.ExecuteAsync(new SimpleStatement("SELECT key FROM system.local")), 100, 16); // Wait until all connections to all host are created await TestHelper.WaitUntilAsync(() => { var state = session.GetState(); return(state.GetConnectedHosts().All(h => state.GetInFlightQueries(h) == connectionLength)); }); lbp.UseFixedOrder(); const int busyExceptions = 10; var length = maxRequestsPerConnection * connectionLength * hosts.Length + Environment.ProcessorCount + busyExceptions; var tasks = new List <Task <Exception> >(length); for (var i = 0; i < length; i++) { tasks.Add(TestHelper.EatUpException(session.ExecuteAsync(new SimpleStatement(query)))); } var results = await Task.WhenAll(tasks); // Only successful responses or NoHostAvailableException expected Assert.Null(results.FirstOrDefault(e => e != null && !(e is NoHostAvailableException))); // At least the first n (maxRequestsPerConnection * connectionLength * hosts.length) succeeded Assert.That(results.Count(e => e == null), Is.GreaterThanOrEqualTo(maxRequestsPerConnection * connectionLength * hosts.Length)); // At least the following m (busyExceptions) failed var failed = results.Where(e => e is NoHostAvailableException).Cast <NoHostAvailableException>() .ToArray(); Assert.That(failed, Has.Length.GreaterThanOrEqualTo(busyExceptions)); foreach (var ex in failed) { Assert.That(ex.Errors, Has.Count.EqualTo(hosts.Length)); foreach (var kv in ex.Errors) { Assert.IsInstanceOf <BusyPoolException>(kv.Value); var busyException = (BusyPoolException)kv.Value; Assert.AreEqual(kv.Key, busyException.Address); Assert.That(busyException.ConnectionLength, Is.EqualTo(connectionLength)); Assert.That(busyException.MaxRequestsPerConnection, Is.EqualTo(maxRequestsPerConnection)); Assert.That(busyException.Message, Is.EqualTo( $"All connections to host {busyException.Address} are busy, {maxRequestsPerConnection}" + $" requests are in-flight on each {connectionLength} connection(s)")); } } } }
public void GetRequest_Batch_With_64K_Queries() { var batch = new BatchStatement(); for (var i = 0; i < ushort.MaxValue; i++) { batch.Add(new SimpleStatement("QUERY")); } var config = RequestHandlerTests.GetConfig(new QueryOptions(), Policies.DefaultPolicies, PoolingOptions.Create()); var request = RequestHandler.GetRequest(batch, Serializer, config.DefaultRequestOptions); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] CollectionAssert.AreEqual(new byte[] { 0xff, 0xff }, bodyBuffer.Skip(1).Take(2)); }