Example #1
0
        private async Task SendJsonMessageAsync <T>(T message)
        {
            var queryProtocolOptions = new QueryProtocolOptions(
                ConsistencyLevel.One,
                new object[] { JsonConvert.SerializeObject(message, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }) },
                false,
                0,
                null,
                ConsistencyLevel.Any);

            var response = await RunWithTokenAsync(() =>
                                                   _cluster.Metadata.ControlConnection.UnsafeSendQueryRequestAsync(
                                                       InsightsClient.ReportInsightRpc,
                                                       queryProtocolOptions)).ConfigureAwait(false);

            if (response == null)
            {
                throw new DriverInternalError("Received null response.");
            }

            if (!(response is ResultResponse resultResponse))
            {
                throw new DriverInternalError("Expected ResultResponse but received: " + response.GetType());
            }

            if (resultResponse.Kind != ResultResponse.ResultResponseKind.Void)
            {
                throw new DriverInternalError("Expected ResultResponse of Kind \"Void\" but received: " + resultResponse.Kind);
            }
        }
Example #2
0
 public QueryRequest(int protocolVersion, string cqlQuery, bool tracingEnabled, QueryProtocolOptions queryOptions)
 {
     //TODO: Replace constructor parameters with IStatement
     ProtocolVersion = protocolVersion;
     _cqlQuery = cqlQuery;
     _queryOptions = queryOptions;
     if (tracingEnabled)
     {
         _headerFlags = FrameHeader.HeaderFlag.Tracing;
     }
     if (queryOptions == null)
     {
         throw new ArgumentNullException("queryOptions");
     }
     if (Consistency.IsSerialConsistencyLevel())
     {
         throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
     }
     if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
     {
         throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
     }
     if (protocolVersion < 3)
     {
         //Features supported in protocol v3 and above
         if (queryOptions.Timestamp != null)
         {
             throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 and above.");
         }
         if (queryOptions.ValueNames != null && queryOptions.ValueNames.Count > 0)
         {
             throw new NotSupportedException("Query parameter names feature is supported in Cassandra 2.1 and above.");
         }
     }
 }
Example #3
0
        public void Execute_Prepared_With_Param_Test()
        {
            using (var connection = CreateConnection())
            {
                connection.Open().Wait();

                var prepareRequest = new PrepareRequest(GetSerializer(), "SELECT * FROM system.local WHERE key = ?", null, null);
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                var options = new QueryProtocolOptions(ConsistencyLevel.One, new object[] { "local" }, false, 100, null, ConsistencyLevel.Any);

                var executeRequest = new ExecuteRequest(
                    GetSerializer(),
                    prepareOutput.QueryId,
                    null,
                    new ResultMetadata(prepareOutput.ResultMetadataId, prepareOutput.ResultRowsMetadata),
                    options,
                    false,
                    null);

                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);

                var rows = output.RowSet.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.NotNull(rows[0].GetValue <string>("key"), "It should contain a key column value");
            }
        }
Example #4
0
        public ExecuteRequest(int protocolVersion, byte[] id, RowSetMetadata metadata, bool tracingEnabled, QueryProtocolOptions queryOptions)
        {
            ProtocolVersion = protocolVersion;
            if (metadata != null && queryOptions.Values.Length != metadata.Columns.Length)
            {
                throw new ArgumentException("Number of values does not match with number of prepared statement markers(?).", "values");
            }
            _id           = id;
            _metadata     = metadata;
            _queryOptions = queryOptions;
            if (tracingEnabled)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            if (Consistency.IsSerialConsistencyLevel())
            {
                throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
            }
            if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
            {
                throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
            }
            if (queryOptions.Timestamp != null && protocolVersion < 3)
            {
                throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 or above.");
            }
        }
Example #5
0
 public QueryRequest(int protocolVersion, string cqlQuery, bool tracingEnabled, QueryProtocolOptions queryOptions)
 {
     _cqlQuery     = cqlQuery;
     _queryOptions = queryOptions;
     if (tracingEnabled)
     {
         _headerFlags = FrameHeader.HeaderFlag.Tracing;
     }
     if (queryOptions == null)
     {
         throw new ArgumentNullException("queryOptions");
     }
     if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
     {
         throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
     }
     if (protocolVersion < 3)
     {
         //Features supported in protocol v3 and above
         if (queryOptions.Timestamp != null)
         {
             throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 and above.");
         }
         if (queryOptions.ValueNames != null && queryOptions.ValueNames.Count > 0)
         {
             throw new NotSupportedException("Query parameter names feature is supported in Cassandra 2.1 and above.");
         }
     }
 }
Example #6
0
        public ExecuteRequest(int protocolVersion, byte[] id, RowSetMetadata metadata, bool tracingEnabled, QueryProtocolOptions queryOptions)
        {
            ProtocolVersion = protocolVersion;
            if (metadata != null && queryOptions.Values.Length != metadata.Columns.Length)
            {
                throw new ArgumentException("Number of values does not match with number of prepared statement markers(?).", "values");
            }
            _id = id;
            _metadata = metadata;
            _queryOptions = queryOptions;
            if (tracingEnabled)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            if (Consistency.IsSerialConsistencyLevel())
            {
                throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
            }
            if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
            {
                throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
            }
            if (queryOptions.Timestamp != null && protocolVersion < 3)
            {
                throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 or above.");
            }
        }
Example #7
0
 private QueryRequest GetQueryRequest(string query = BasicQuery, QueryProtocolOptions options = null)
 {
     if (options == null)
     {
         options = QueryProtocolOptions.Default;
     }
     return(new QueryRequest(GetSerializer(), query, options, false, null));
 }
 private QueryRequest GetQueryRequest(string query = BasicQuery, QueryProtocolOptions options = null)
 {
     if (options == null)
     {
         options = QueryProtocolOptions.Default;
     }
     return(new QueryRequest(GetProtocolVersion(), query, false, options));
 }
Example #9
0
        private Task <Response> Query(Connection connection, string query, QueryProtocolOptions options = null)
        {
            if (options == null)
            {
                options = QueryProtocolOptions.Default;
            }
            var request = new QueryRequest(GetLatestProtocolVersion(), query, false, options);

            return(connection.Send(request));
        }
Example #10
0
 public QueryRequest(int streamId, string cqlQuery, bool tracingEnabled, QueryProtocolOptions queryPrtclOptions,
                     ConsistencyLevel?consistency = null)
 {
     _streamId             = streamId;
     _cqlQuery             = cqlQuery;
     _consistency          = consistency;
     _queryProtocolOptions = queryPrtclOptions;
     if (tracingEnabled)
     {
         _headerFlags = 0x02;
     }
 }
Example #11
0
        /// <summary>
        /// Gets the Request to send to a cassandra node based on the statement type
        /// </summary>
        internal static IRequest GetRequest(IStatement statement, ISerializer serializer, IRequestOptions requestOptions)
        {
            ICqlRequest request = null;

            if (statement.IsIdempotent == null)
            {
                statement.SetIdempotence(requestOptions.DefaultIdempotence);
            }

            if (statement is RegularStatement s1)
            {
                s1.Serializer = serializer;
                var options = QueryProtocolOptions.CreateFromQuery(serializer.ProtocolVersion, s1, requestOptions, null);
                options.ValueNames = s1.QueryValueNames;
                request            = new QueryRequest(serializer, s1.QueryString, options, s1.IsTracing, s1.OutgoingPayload);
            }

            if (statement is BoundStatement s2)
            {
                // set skip metadata only when result metadata id is supported because of CASSANDRA-10786
                var skipMetadata =
                    serializer.ProtocolVersion.SupportsResultMetadataId() &&
                    s2.PreparedStatement.ResultMetadata.ContainsColumnDefinitions();

                var options = QueryProtocolOptions.CreateFromQuery(serializer.ProtocolVersion, s2, requestOptions, skipMetadata);
                request = new ExecuteRequest(
                    serializer,
                    s2.PreparedStatement.Id,
                    null,
                    s2.PreparedStatement.ResultMetadata,
                    options,
                    s2.IsTracing,
                    s2.OutgoingPayload);
            }

            if (statement is BatchStatement s)
            {
                s.Serializer = serializer;
                var consistency = requestOptions.ConsistencyLevel;
                if (s.ConsistencyLevel.HasValue)
                {
                    consistency = s.ConsistencyLevel.Value;
                }
                request = new BatchRequest(serializer, s.OutgoingPayload, s, consistency, requestOptions);
            }

            if (request == null)
            {
                throw new NotSupportedException("Statement of type " + statement.GetType().FullName + " not supported");
            }
            return(request);
        }
Example #12
0
        public async Task <Response> SendMetadataRequestAsync(
            IConnection connection, ProtocolVersion version, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
        {
            var      request = new QueryRequest(version, cqlQuery, false, queryProtocolOptions);
            Response response;

            try
            {
                response = await connection.Send(request).ConfigureAwait(false);
            }
            catch (SocketException ex)
            {
                ControlConnection.Logger.Error(
                    $"There was an error while executing on the host {cqlQuery} the query '{connection.EndPoint.EndpointFriendlyName}'", ex);
                throw;
            }
            return(response);
        }
        private async Task <Response> Send(
            IConnection connection, ISerializer serializer, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
        {
            Requests.Enqueue(new MetadataRequest
            {
                Serializer           = serializer,
                CqlQuery             = cqlQuery,
                QueryProtocolOptions = queryProtocolOptions
            });

            await Task.Yield();

            ThrowErrorIfNullRows(cqlQuery);

            var response = new FakeResultResponse(ResultResponse.ResultResponseKind.Rows);

            _responsesByCql.AddOrUpdate(response, _ => cqlQuery, (_, __) => cqlQuery);
            return((Response)response);
        }
        /// <summary>
        /// Gets the Request to send to a cassandra node based on the statement type
        /// </summary>
        internal static IRequest GetRequest(IStatement statement, Serializer serializer, Configuration config)
        {
            ICqlRequest request = null;

            if (statement.IsIdempotent == null)
            {
                statement.SetIdempotence(config.QueryOptions.GetDefaultIdempotence());
            }
            if (statement is RegularStatement)
            {
                var s = (RegularStatement)statement;
                s.Serializer = serializer;
                var options = QueryProtocolOptions.CreateFromQuery(serializer.ProtocolVersion, s, config.QueryOptions, config.Policies);
                options.ValueNames = s.QueryValueNames;
                request            = new QueryRequest(serializer.ProtocolVersion, s.QueryString, s.IsTracing, options);
            }
            if (statement is BoundStatement)
            {
                var s       = (BoundStatement)statement;
                var options = QueryProtocolOptions.CreateFromQuery(serializer.ProtocolVersion, s, config.QueryOptions, config.Policies);
                request = new ExecuteRequest(serializer.ProtocolVersion, s.PreparedStatement.Id, null, s.IsTracing, options);
            }
            if (statement is BatchStatement)
            {
                var s = (BatchStatement)statement;
                s.Serializer = serializer;
                var consistency = config.QueryOptions.GetConsistencyLevel();
                if (s.ConsistencyLevel != null)
                {
                    consistency = s.ConsistencyLevel.Value;
                }
                request = new BatchRequest(serializer.ProtocolVersion, s, consistency);
            }
            if (request == null)
            {
                throw new NotSupportedException("Statement of type " + statement.GetType().FullName + " not supported");
            }
            //Set the outgoing payload for the request
            request.Payload = statement.OutgoingPayload;
            return(request);
        }
Example #15
0
        public void ExecutePreparedWithParamTest()
        {
            using (var connection = CreateConnection())
            {
                connection.Init();

                var prepareRequest = new PrepareRequest(connection.ProtocolVersion, "SELECT * FROM system.schema_columnfamilies WHERE keyspace_name = ?");
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                var options = new QueryProtocolOptions(ConsistencyLevel.One, new[] { "system" }, false, 100, null, ConsistencyLevel.Any);

                var executeRequest = new ExecuteRequest(connection.ProtocolVersion, prepareOutput.QueryId, null, false, options);
                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);

                var rows = output.RowSet.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.True(rows[0].GetValue <string>("columnfamily_name") != null, "It should contain a column family name");
            }
        }
Example #16
0
        /// <summary>
        /// Gets the Request to send to a cassandra node based on the statement type
        /// </summary>
        internal static IRequest GetRequest(IStatement statement, ISerializer serializer, IRequestOptions requestOptions)
        {
            ICqlRequest request = null;

            if (statement.IsIdempotent == null)
            {
                statement.SetIdempotence(requestOptions.DefaultIdempotence);
            }
            if (statement is RegularStatement s1)
            {
                s1.Serializer = serializer;
                var options = QueryProtocolOptions.CreateFromQuery(serializer.ProtocolVersion, s1, requestOptions);
                options.ValueNames = s1.QueryValueNames;
                request            = new QueryRequest(serializer.ProtocolVersion, s1.QueryString, s1.IsTracing, options);
            }
            if (statement is BoundStatement s2)
            {
                var options = QueryProtocolOptions.CreateFromQuery(serializer.ProtocolVersion, s2, requestOptions);
                request = new ExecuteRequest(serializer.ProtocolVersion, s2.PreparedStatement.Id, null,
                                             s2.PreparedStatement.ResultMetadataId, s2.IsTracing, options);
            }
            if (statement is BatchStatement s)
            {
                s.Serializer = serializer;
                var consistency = requestOptions.ConsistencyLevel;
                if (s.ConsistencyLevel.HasValue)
                {
                    consistency = s.ConsistencyLevel.Value;
                }
                request = new BatchRequest(serializer.ProtocolVersion, s, consistency, requestOptions);
            }
            if (request == null)
            {
                throw new NotSupportedException("Statement of type " + statement.GetType().FullName + " not supported");
            }
            //Set the outgoing payload for the request
            request.Payload = statement.OutgoingPayload;
            return(request);
        }
        private Task <Response> Query(Connection connection, string query, QueryProtocolOptions options = null)
        {
            var request = GetQueryRequest(query, options);

            return(connection.Send(request));
        }
Example #18
0
        private Task <Response> Send(IConnection connection, ProtocolVersion version, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
        {
            Requests.Enqueue(new MetadataRequest {
                Version = version, CqlQuery = cqlQuery, QueryProtocolOptions = queryProtocolOptions
            });
            var response = new FakeResultResponse(ResultResponse.ResultResponseKind.Rows);

            _responsesByCql.AddOrUpdate(response, _ => cqlQuery, (_, __) => cqlQuery);
            return(Task.FromResult((Response)response));
        }
Example #19
0
 public Task <Response> UnsafeSendQueryRequestAsync(
     IConnection connection, ProtocolVersion version, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
 {
     return(connection.Send(new QueryRequest(version, cqlQuery, false, queryProtocolOptions)));
 }
 override public void Begin(Session owner, int streamId)
 {
     Connection.BeginExecuteQuery(streamId, Id, CqlQuery, Metadata, owner.RequestCallback, this, owner, IsTracing, QueryProtocolOptions.CreateFromQuery(Statement, owner.Cluster.Configuration.QueryOptions.GetConsistencyLevel()), Consistency);
 }
Example #21
0
 public Task <Response> SendMetadataRequestAsync(IConnection connection, ProtocolVersion version, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
 {
     return(Send(connection, version, cqlQuery, queryProtocolOptions));
 }
Example #22
0
 public Task <Response> UnsafeSendQueryRequestAsync(
     IConnection connection, ISerializer serializer, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
 {
     return(connection.Send(new QueryRequest(serializer, cqlQuery, queryProtocolOptions, false, null)));
 }
 public Task <Response> UnsafeSendQueryRequestAsync(
     IConnection connection, ISerializer serializer, string cqlQuery, QueryProtocolOptions queryProtocolOptions)
 {
     return(Send(connection, serializer, cqlQuery, queryProtocolOptions));
 }
Example #24
0
 protected RegularStatement(QueryProtocolOptions queryProtocolOptions) : base(queryProtocolOptions)
 {
 }