Exemple #1
0
        private Cluster(IEnumerable <IPAddress> contactPoints, Configuration configuration)
        {
            this._contactPoints = contactPoints;
            this._configuration = configuration;
            this._metadata      = new Metadata(configuration.Policies.ReconnectionPolicy);

            var controlpolicies = new Cassandra.Policies(
                //new ControlConnectionLoadBalancingPolicy(_configuration.Policies.LoadBalancingPolicy),
                _configuration.Policies.LoadBalancingPolicy,
                new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000),
                Cassandra.Policies.DefaultRetryPolicy);

            foreach (var ep in _contactPoints)
            {
                Metadata.AddHost(ep);
            }

            var poolingOptions = new PoolingOptions()
                                 .SetCoreConnectionsPerHost(HostDistance.Local, 0)
                                 .SetMaxConnectionsPerHost(HostDistance.Local, 1)
                                 .SetMinSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 0)
                                 .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 127)
            ;

            var controlConnection = new ControlConnection(this, new List <IPAddress>(), controlpolicies,
                                                          new ProtocolOptions(_configuration.ProtocolOptions.Port, configuration.ProtocolOptions.SslOptions),
                                                          poolingOptions, _configuration.SocketOptions,
                                                          new ClientOptions(
                                                              true,
                                                              _configuration.ClientOptions.QueryAbortTimeout, null),
                                                          _configuration.AuthProvider,
                                                          _configuration.AuthInfoProvider);

            _metadata.SetupControllConnection(controlConnection);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public void GetRequest_With_Timestamp_Generator_Empty_Value()
        {
            var statement = 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(
                statement, RequestHandlerTests.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.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));
        }
Exemple #5
0
 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());
 }
        /// <summary>
        ///  Creates a new session on this cluster and sets a keyspace to use.
        /// </summary>
        /// <param name="keyspace"> The name of the keyspace to use for the created <code>Session</code>. </param>
        /// <returns>a new session on this cluster set to keyspace:
        ///  <code>keyspaceName</code>. </returns>
        public Session Connect(string keyspace)
        {
            bool controlConnectionCreated = false;

            lock (_controlConnectionGuard)
            {
                if (_controlConnection == null)
                {
                    controlConnectionCreated = true;
                    var controlpolicies = new Cassandra.Policies(
                        _configuration.Policies.LoadBalancingPolicy,
                        new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000),
                        Cassandra.Policies.DefaultRetryPolicy);

                    _hosts = new Hosts(_configuration.Policies.ReconnectionPolicy);

                    foreach (var ep in _contactPoints)
                    {
                        _hosts.AddIfNotExistsOrBringUpIfDown(ep);
                    }

                    var poolingOptions = new PoolingOptions().SetCoreConnectionsPerHost(HostDistance.Local, 1);

                    _controlConnection = new ControlConnection(this, new List <IPAddress>(), controlpolicies,
                                                               _configuration.ProtocolOptions,
                                                               poolingOptions, _configuration.SocketOptions,
                                                               new ClientOptions(
                                                                   _configuration.ClientOptions.WithoutRowSetBuffering,
                                                                   _configuration.ClientOptions.QueryAbortTimeout, null,
                                                                   _configuration.ClientOptions.AsyncCallAbortTimeout),
                                                               _configuration.AuthInfoProvider,
                                                               _configuration.MetricsEnabled);

                    _metadata = new Metadata(_hosts, _controlConnection);

                    _controlConnection.Init();
                }
            }
            var scs = new Session(this, _contactPoints, _configuration.Policies,
                                  _configuration.ProtocolOptions,
                                  _configuration.PoolingOptions, _configuration.SocketOptions,
                                  _configuration.ClientOptions,
                                  _configuration.AuthInfoProvider, _configuration.MetricsEnabled, keyspace, _hosts);

            scs.Init();
            lock (_connectedSessions)
                _connectedSessions.Add(scs);
            _logger.Info("Session connected!");

            if (controlConnectionCreated)
            {
                RefreshSchema();
            }

            return(scs);
        }
        private static Configuration GetConfig(int coreConnections = 3, int maxConnections = 8, IReconnectionPolicy rp = null)
        {
            var pooling = new PoolingOptions()
                          .SetCoreConnectionsPerHost(HostDistance.Local, coreConnections)
                          .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 1500)
                          .SetMaxConnectionsPerHost(HostDistance.Local, maxConnections);
            var policies = new Cassandra.Policies(null, rp, null, null, null);
            var config   = new TestConfigurationBuilder
            {
                Policies       = policies,
                PoolingOptions = pooling
            }.Build();

            return(config);
        }
Exemple #8
0
        private Cluster(IEnumerable<IPAddress> contactPoints, Configuration configuration)
        {
            this._contactPoints = contactPoints;
            this._configuration = configuration;
            this._metadata = new Metadata(configuration.Policies.ReconnectionPolicy);

            var controlpolicies = new Cassandra.Policies(
                _configuration.Policies.LoadBalancingPolicy,
                new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000),
                Cassandra.Policies.DefaultRetryPolicy);

            foreach (var ep in _contactPoints)
                Metadata.AddHost(ep);

            var poolingOptions = new PoolingOptions().SetCoreConnectionsPerHost(HostDistance.Local, 1);

            var controlConnection = new ControlConnection(this, new List<IPAddress>(), controlpolicies,
                                                        new ProtocolOptions(_configuration.ProtocolOptions.Port),
                                                       poolingOptions, _configuration.SocketOptions,
                                                       new ClientOptions(
                                                           true,
                                                           _configuration.ClientOptions.QueryAbortTimeout, null,
                                                           _configuration.ClientOptions.AsyncCallAbortTimeout),
                                                       _configuration.AuthInfoProvider);

            _metadata.SetupControllConnection(controlConnection);
        }
        /// <summary>
        ///  Creates a new session on this cluster and sets a keyspace to use.
        /// </summary>
        /// <param name="keyspace"> The name of the keyspace to use for the created <code>Session</code>. </param>
        /// <returns>a new session on this cluster set to keyspace: 
        ///  <code>keyspaceName</code>. </returns>
        public Session Connect(string keyspace)
        {
            bool controlConnectionCreated = false;
            lock (_controlConnectionGuard)
            {
                if (_controlConnection == null)
                {
                    controlConnectionCreated = true;
                    var controlpolicies = new Cassandra.Policies(
                        _configuration.Policies.LoadBalancingPolicy,
                        new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000),
                        Cassandra.Policies.DefaultRetryPolicy);

                    _hosts = new Hosts(_configuration.Policies.ReconnectionPolicy);

                    foreach (var ep in _contactPoints)
                        _hosts.AddIfNotExistsOrBringUpIfDown(ep);

                    var poolingOptions = new PoolingOptions().SetCoreConnectionsPerHost(HostDistance.Local, 1);

                    _controlConnection = new ControlConnection(this, new List<IPAddress>(), controlpolicies,
                                                               _configuration.ProtocolOptions,
                                                               poolingOptions, _configuration.SocketOptions,
                                                               new ClientOptions(
                                                                   _configuration.ClientOptions.WithoutRowSetBuffering,
                                                                   _configuration.ClientOptions.QueryAbortTimeout, null,
                                                                   _configuration.ClientOptions.AsyncCallAbortTimeout),
                                                               _configuration.AuthInfoProvider,
                                                               _configuration.MetricsEnabled);

                    _metadata = new Metadata(_hosts, _controlConnection);

                    _controlConnection.Init();
                }
            }
            var scs = new Session(this, _contactPoints, _configuration.Policies,
                                  _configuration.ProtocolOptions,
                                  _configuration.PoolingOptions, _configuration.SocketOptions,
                                  _configuration.ClientOptions,
                                  _configuration.AuthInfoProvider, _configuration.MetricsEnabled, keyspace, _hosts);
            scs.Init();
            lock (_connectedSessions)
                _connectedSessions.Add(scs);
            _logger.Info("Session connected!");

            if (controlConnectionCreated)
                RefreshSchema();

            return scs;
        }
Exemple #10
0
 private static IRequestOptions GetRequestOptions(QueryOptions queryOptions = null, Cassandra.Policies policies = null)
 {
     return(RequestHandlerTests.GetConfig(queryOptions, policies).DefaultRequestOptions);
 }