Exemple #1
0
        /// <summary>
        /// Builds a request options object without any null settings.
        /// </summary>
        /// <param name="profile">Execution profile that was mapped into this instance. Can be null if it's the default profile.</param>
        /// <param name="defaultProfile">Default execution profile. Can be null.</param>
        /// <param name="policies">Must not be null and the inner policy settings must not be null either.</param>
        /// <param name="socketOptions">Must not be null.</param>
        /// <param name="queryOptions">Must not be null.</param>
        /// <param name="clientOptions">Must not be null.</param>
        public RequestOptions(
            IExecutionProfile profile,
            IExecutionProfile defaultProfile,
            Policies policies,
            SocketOptions socketOptions,
            QueryOptions queryOptions,
            ClientOptions clientOptions)
        {
            _profile        = profile;
            _defaultProfile = defaultProfile;
            _policies       = policies ?? throw new ArgumentNullException(nameof(policies));
            _socketOptions  = socketOptions ?? throw new ArgumentNullException(nameof(socketOptions));
            _queryOptions   = queryOptions ?? throw new ArgumentNullException(nameof(queryOptions));
            _clientOptions  = clientOptions ?? throw new ArgumentNullException(nameof(clientOptions));

            if (profile?.LoadBalancingPolicy == null && policies.LoadBalancingPolicy == null)
            {
                throw new ArgumentNullException(nameof(policies.LoadBalancingPolicy));
            }

            if (profile?.SpeculativeExecutionPolicy == null && policies.SpeculativeExecutionPolicy == null)
            {
                throw new ArgumentNullException(nameof(policies.SpeculativeExecutionPolicy));
            }

            if (profile?.RetryPolicy == null && policies.RetryPolicy == null)
            {
                throw new ArgumentNullException(nameof(policies.ExtendedRetryPolicy));
            }
        }
        private Dictionary <string, object> GetGraphOptions(IExecutionProfile profile)
        {
            if (profile.GraphOptions == null)
            {
                return(null);
            }

            var    consistencyConverter = new ConsistencyInsightsConverter();
            string graphReadConsistency = null;

            if (profile.GraphOptions.ReadConsistencyLevel.HasValue)
            {
                consistencyConverter.TryConvert(profile.GraphOptions.ReadConsistencyLevel.Value, out graphReadConsistency);
            }

            string graphWriteConsistency = null;

            if (profile.GraphOptions.WriteConsistencyLevel.HasValue)
            {
                consistencyConverter.TryConvert(profile.GraphOptions.WriteConsistencyLevel.Value, out graphWriteConsistency);
            }

            return(new Dictionary <string, object>
            {
                { "language", profile.GraphOptions.Language },
                { "source", profile.GraphOptions.Source },
                { "name", profile.GraphOptions.Name },
                { "readConsistency", graphReadConsistency },
                { "writeConsistency", graphWriteConsistency },
                { "readTimeout", profile.GraphOptions.ReadTimeoutMillis }
            });
        }
Exemple #3
0
 internal Policies CloneWithExecutionProfilePolicies(IExecutionProfile defaultProfile)
 {
     return(new Policies(
                defaultProfile.LoadBalancingPolicy ?? _loadBalancingPolicy,
                _reconnectionPolicy,
                _retryPolicy,
                defaultProfile.SpeculativeExecutionPolicy ?? _speculativeExecutionPolicy,
                _timestampGenerator,
                _extendedRetryPolicy));
 }
Exemple #4
0
 /// <summary>
 /// Builds a request options object without any null settings.
 /// </summary>
 /// <param name="profile">Execution profile that was mapped into this instance. Can be null if it's the default profile.</param>
 /// <param name="defaultProfile">Default execution profile. Can be null.</param>
 /// <param name="policies">Must not be null and the inner policy settings must not be null either.</param>
 /// <param name="socketOptions">Must not be null.</param>
 /// <param name="queryOptions">Must not be null.</param>
 /// <param name="clientOptions">Must not be null.</param>
 /// <param name="graphOptions">Must not be null.</param>
 public RequestOptions(
     IExecutionProfile profile,
     IExecutionProfile defaultProfile,
     Policies policies,
     SocketOptions socketOptions,
     QueryOptions queryOptions,
     ClientOptions clientOptions,
     GraphOptions graphOptions) : this(profile, defaultProfile, policies, socketOptions, queryOptions, clientOptions)
 {
     _graphOptions = graphOptions ?? throw new ArgumentNullException(nameof(graphOptions));
 }
        private IExecutionProfileOptions WithProfile(string name, IExecutionProfile profile)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (_profiles.ContainsKey(name))
            {
                throw new ArgumentException("There is already an execution profile with that name.");
            }

            _profiles[name] = profile ?? throw new ArgumentNullException(nameof(profile));
            return(this);
        }
 private ExecutionProfileInfo GetExecutionProfileInfo(IExecutionProfile profile)
 {
     return(new ExecutionProfileInfo
     {
         GraphOptions = GetGraphOptions(profile),
         SerialConsistency = profile.SerialConsistencyLevel,
         Retry = profile.RetryPolicy == null ? null : _retryPolicyInfoProvider.GetPolicyInformation(profile.RetryPolicy),
         SpeculativeExecution = profile.SpeculativeExecutionPolicy == null
             ? null
             : _speculativeExecutionPolicyInfoProvider.GetPolicyInformation(profile.SpeculativeExecutionPolicy),
         LoadBalancing = profile.LoadBalancingPolicy == null
             ? null
             : _loadBalancingPolicyInfoProvider.GetPolicyInformation(profile.LoadBalancingPolicy),
         ReadTimeout = profile.ReadTimeoutMillis,
         Consistency = profile.ConsistencyLevel
     });
 }
        internal ExecutionProfile(IExecutionProfile baseProfile, IExecutionProfile profile)
        {
            if (baseProfile == null)
            {
                throw new ArgumentNullException(nameof(baseProfile));
            }

            if (profile == null)
            {
                throw new ArgumentNullException(nameof(profile));
            }

            Initialize(
                profile.ConsistencyLevel ?? baseProfile.ConsistencyLevel,
                profile.SerialConsistencyLevel ?? baseProfile.SerialConsistencyLevel,
                profile.ReadTimeoutMillis ?? baseProfile.ReadTimeoutMillis,
                profile.LoadBalancingPolicy ?? baseProfile.LoadBalancingPolicy,
                profile.SpeculativeExecutionPolicy ?? baseProfile.SpeculativeExecutionPolicy,
                profile.RetryPolicy ?? baseProfile.RetryPolicy);
        }
        private RequestHandlerMockResult BuildRequestHandler(
            IStatement statement,
            Action <TestConfigurationBuilder> configBuilderAct,
            IExecutionProfile profile)
        {
            var cts        = new CancellationTokenSource();
            var connection = Mock.Of <IConnection>();

            // create config
            var configBuilder = new TestConfigurationBuilder
            {
                ControlConnectionFactory = new FakeControlConnectionFactory(),
                ConnectionFactory        = new FakeConnectionFactory(() => connection),
                Policies = new Cassandra.Policies(new RoundRobinPolicy(), new ConstantReconnectionPolicy(100), new DefaultRetryPolicy())
            };

            configBuilderAct(configBuilder);
            var config          = configBuilder.Build();
            var initializerMock = Mock.Of <IInitializer>();

            Mock.Get(initializerMock).Setup(i => i.ContactPoints).Returns(new List <IPEndPoint>
            {
                new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042),
                new IPEndPoint(IPAddress.Parse("127.0.0.2"), 9042)
            });
            Mock.Get(initializerMock).Setup(i => i.GetConfiguration()).Returns(config);

            // create cluster
            var cluster = Cluster.BuildFrom(initializerMock, new List <string>());

            cluster.Connect();

            // create session
            var session = new Session(cluster, config, null, SerializerManager.Default, null);

            // create request handler
            var options = profile != null
                ? new RequestOptions(profile, null, config.Policies, config.SocketOptions, config.QueryOptions, config.ClientOptions)
                : config.DefaultRequestOptions;
            var requestHandler = new RequestHandler(
                session,
                new SerializerManager(ProtocolVersion.V3).GetCurrentSerializer(),
                statement,
                options);

            // create mock result object
            var mockResult = new RequestHandlerMockResult(requestHandler);

            // mock connection send
            Mock.Get(connection)
            .Setup(c => c.Send(It.IsAny <IRequest>(), It.IsAny <Action <IRequestError, Response> >(), It.IsAny <int>()))
            .Returns <IRequest, Action <IRequestError, Response>, int>((req, act, timeout) =>
            {
                mockResult.SendResults.Enqueue(new ConnectionSendResult {
                    Request = req, TimeoutMillis = timeout
                });
                Task.Run(async() =>
                {
                    var rp  = (FakeRetryPolicy)(statement.RetryPolicy ?? options.RetryPolicy);
                    var sep = (FakeSpeculativeExecutionPolicy)options.SpeculativeExecutionPolicy;
                    if (Interlocked.Read(ref rp.Count) > 0 && Interlocked.Read(ref sep.Count) > 0)
                    {
                        await Task.Delay(1, cts.Token).ConfigureAwait(false);
                        act(null, new ProxyResultResponse(ResultResponse.ResultResponseKind.Void));
                        cts.Cancel();
                    }
                    else
                    {
                        try
                        {
                            await Task.Delay(10, cts.Token).ConfigureAwait(false);
                        }
                        finally
                        {
                            act(RequestError.CreateServerError(new OverloadedException(string.Empty)), null);
                        }
                    }
                });
                return(new OperationState(act, req, timeout, NullOperationObserver.Instance));
            });
            Mock.Get(connection)
            .SetupGet(c => c.EndPoint)
            .Returns(new ConnectionEndPoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042), config.ServerNameResolver, null));

            return(mockResult);
        }
        private IExecutionProfileOptions WithDerivedProfile(string name, string baseProfile, IExecutionProfile profile)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (_profiles.ContainsKey(name))
            {
                throw new ArgumentException("There is already an execution profile with that name.");
            }

            if (profile == null)
            {
                throw new ArgumentNullException(nameof(profile));
            }

            if (baseProfile == null)
            {
                throw new ArgumentNullException(nameof(baseProfile));
            }

            if (!_profiles.TryGetValue(baseProfile, out var baseProfileInstance))
            {
                throw new ArgumentException("The base Execution Profile must be added before the derived Execution Profile.");
            }

            _profiles[name] = new ExecutionProfile(baseProfileInstance, profile);
            return(this);
        }