private ISession GetSession(
     ISpeculativeExecutionPolicy speculativeExecutionPolicy = null, bool warmup = true,
     ILoadBalancingPolicy lbp = null, PoolingOptions pooling = null)
 {
     if (_testCluster == null)
     {
         throw new Exception("Test cluster not initialized");
     }
     var builder = Cluster.Builder()
         .AddContactPoint(_testCluster.InitialContactPoint)
         .WithSpeculativeExecutionPolicy(speculativeExecutionPolicy)
         .WithLoadBalancingPolicy(lbp ?? Cassandra.Policies.DefaultLoadBalancingPolicy)
         .WithRetryPolicy(DowngradingConsistencyRetryPolicy.Instance)
         .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(0));
     if (pooling != null)
     {
         builder.WithPoolingOptions(pooling);
     }
     var cluster = builder.Build();
     _clusters.Add(cluster);
     var session = cluster.Connect();
     if (warmup)
     {
         TestHelper.ParallelInvoke(() => session.Execute(QueryLocal), 10);
     }
     _addressNode1 = IPAddress.Parse(_testCluster.ClusterIpPrefix + "1");
     return session;
 }
Exemplo n.º 2
0
 internal Configuration(Policies policies,
                      ProtocolOptions protocolOptions,
                      PoolingOptions poolingOptions,
                      SocketOptions socketOptions,
                      ClientOptions clientOptions,
                      IAuthInfoProvider authProvider)
 {
     this._policies = policies;
     this._protocolOptions = protocolOptions;
     this._poolingOptions = poolingOptions;
     this._socketOptions = socketOptions;
     this._clientOptions = clientOptions;
     this._authProvider = authProvider;
 }
Exemplo n.º 3
0
 public Configuration(Policies policies,
                      ProtocolOptions protocolOptions,
                      PoolingOptions poolingOptions,
                      SocketOptions socketOptions,
                      ClientOptions clientOptions,
                      IAuthInfoProvider authProvider,
                      bool metricsEnabled)
 {
     this._policies = policies;
     this._protocolOptions = protocolOptions;
     this._poolingOptions = poolingOptions;
     this._socketOptions = socketOptions;
     this._clientOptions = clientOptions;
     this._authProvider = authProvider;
     this._metricsEnabled = metricsEnabled;
 }
 private static Configuration GetConfig(int coreConnections = 3, int maxConnections = 8)
 {
     var pooling = new PoolingOptions()
         .SetCoreConnectionsPerHost(HostDistance.Local, coreConnections)
         .SetMaxConnectionsPerHost(HostDistance.Local, maxConnections);
     var config = new Configuration(Policies.DefaultPolicies,
          new ProtocolOptions(),
          pooling,
          new SocketOptions(),
          new ClientOptions(),
          NoneAuthProvider.Instance,
          null,
          new QueryOptions(),
          new DefaultAddressTranslator());
     config.BufferPool = new Microsoft.IO.RecyclableMemoryStreamManager();
     return config;
 }
Exemplo n.º 5
0
 internal Configuration(Policies policies,
                        ProtocolOptions protocolOptions,
                        PoolingOptions poolingOptions,
                        SocketOptions socketOptions,
                        ClientOptions clientOptions,
                        IAuthProvider authProvider,
                        IAuthInfoProvider authInfoProvider,
                        QueryOptions queryOptions)
 {
     _policies = policies;
     _protocolOptions = protocolOptions;
     _poolingOptions = poolingOptions;
     _socketOptions = socketOptions;
     _clientOptions = clientOptions;
     _authProvider = authProvider;
     _authInfoProvider = authInfoProvider;
     _queryOptions = queryOptions;
 }
 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 = new PoolingOptions().SetCoreConnectionsPerHost(HostDistance.Local, 1);
         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))
                 .Continue(t =>
                 {
                     semaphore.Release();
                     return t.Result.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;
     }
 }
 private static Configuration GetConfig(QueryOptions queryOptions = null, Cassandra.Policies policies = null, PoolingOptions poolingOptions = null)
 {
     return(new TestConfigurationBuilder
     {
         Policies = policies ?? new Cassandra.Policies(),
         PoolingOptions = poolingOptions,
         QueryOptions = queryOptions ?? DefaultQueryOptions
     }.Build());
 }
Exemplo n.º 8
0
 /// <summary>
 /// Gets the pooling options. If not specified, gets the default by protocol version
 /// </summary>
 internal PoolingOptions GetPoolingOptions(byte protocolVersion)
 {
     if (this._poolingOptions != null)
     {
         return this._poolingOptions;
     }
     this._poolingOptions = PoolingOptions.GetDefault(protocolVersion);
     return this._poolingOptions;
 }
        public void GetRequest_Batch_With_Empty_Timestamp_Generator()
        {
            var batch = new BatchStatement();

            batch.Add(new SimpleStatement("QUERY"));
            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(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>]
            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);
        }
Exemplo n.º 10
0
        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     = RequestHandlerTests.GetConfig(new QueryOptions(), Cassandra.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>]
            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);
        }
Exemplo n.º 11
0
        private Connection CreateConnection(ProtocolOptions protocolOptions = null, SocketOptions socketOptions = null, PoolingOptions poolingOptions = null)
        {
            if (socketOptions == null)
            {
                socketOptions = new SocketOptions();
            }
            if (protocolOptions == null)
            {
                protocolOptions = new ProtocolOptions();
            }
            var config = new Configuration(
                new Cassandra.Policies(),
                protocolOptions,
                poolingOptions,
                socketOptions,
                new ClientOptions(false, 20000, null),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator(),
                new StartupOptionsFactory(),
                new SessionFactoryBuilder(),
                new Dictionary <string, IExecutionProfile>(),
                new RequestOptionsMapper(),
                null);

            return(CreateConnection(GetProtocolVersion(), config));
        }
Exemplo n.º 12
0
        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     = RequestHandlerTests.GetConfig(new QueryOptions(), Cassandra.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>]
            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)));
        }
Exemplo n.º 13
0
 internal FakeObjectPool(PoolManager poolManager, PoolingOptions <T> options)
     : base(poolManager, new PoolingOptions <T>(options.Instantiator, 0, 0))
 {
 }
        private Connection CreateConnection(ProtocolOptions protocolOptions = null, SocketOptions socketOptions = null, PoolingOptions poolingOptions = null)
        {
            if (socketOptions == null)
            {
                socketOptions = new SocketOptions();
            }
            if (protocolOptions == null)
            {
                protocolOptions = new ProtocolOptions();
            }
            var config = new Configuration(
                new Dse.Policies(),
                protocolOptions,
                poolingOptions,
                socketOptions,
                new ClientOptions(false, 20000, null),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator());

            return(CreateConnection(GetLatestProtocolVersion(), config));
        }
Exemplo n.º 15
0
 internal FakeSimpleObjectPool(PoolManager poolManager, PoolingOptions <T> options)
     : base(poolManager, 0, options.Instantiator)
 {
 }
        private Connection CreateConnection(ProtocolOptions protocolOptions = null, SocketOptions socketOptions = null, PoolingOptions poolingOptions = null)
        {
            if (socketOptions == null)
            {
                socketOptions = new SocketOptions();
            }
            if (protocolOptions == null)
            {
                protocolOptions = new ProtocolOptions();
            }
            var config = new Configuration(
                new Cassandra.Policies(),
                protocolOptions,
                poolingOptions,
                socketOptions,
                new ClientOptions(false, 20000, null),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator());

            config.BufferPool = new RecyclableMemoryStreamManager();
            config.Timer      = new HashedWheelTimer();
            return(CreateConnection(GetLatestProtocolVersion(), config));
        }
Exemplo n.º 17
0
        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().ConfigureAwait(false);

                    var hosts = cluster.AllHosts().ToArray();

                    await TestHelper.TimesLimit(() =>
                                                session.ExecuteAsync(new SimpleStatement("SELECT key FROM system.local")), 100, 16).ConfigureAwait(false);

                    // 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));
                    }).ConfigureAwait(false);

                    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).ConfigureAwait(false);

                    // 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)"));
                        }
                    }
                }
        }
Exemplo n.º 18
0
        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(), Cassandra.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));
        }
Exemplo n.º 19
0
        private Connection CreateConnection(ProtocolOptions protocolOptions = null, SocketOptions socketOptions = null, PoolingOptions poolingOptions = null)
        {
            if (socketOptions == null)
            {
                socketOptions = new SocketOptions();
            }
            if (protocolOptions == null)
            {
                protocolOptions = new ProtocolOptions();
            }

            var config = new TestConfigurationBuilder
            {
                ProtocolOptions = protocolOptions,
                PoolingOptions  = poolingOptions,
                SocketOptions   = socketOptions,
                ClientOptions   = new ClientOptions(false, 20000, null)
            }.Build();

            return(CreateConnection(GetProtocolVersion(), config));
        }