public void Should_Schedule_Reconnections_In_The_Background()
 {
     var lbp = new RoundRobinPolicy();
     var config = new Configuration(
         new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
         new ProtocolOptions(),
         null,
         new SocketOptions(),
         new ClientOptions(),
         NoneAuthProvider.Instance,
         null,
         new QueryOptions(),
         new DefaultAddressTranslator());
     var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
     var metadata = new Metadata(config);
     metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
     var clusterMock = new Mock<ICluster>();
     clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
     lbp.Initialize(clusterMock.Object);
     using (var cc = NewInstance(testCluster, config, metadata))
     {
         cc.Init();
         var host = metadata.Hosts.First();
         testCluster.Stop(1);
         host.SetDown();
         Thread.Sleep(2000);
         Assert.False(host.IsUp);
         testCluster.Start(1);
         host.BringUpIfDown();
         //Should reconnect using timer
         Thread.Sleep(5000);
         Assert.DoesNotThrow(() => cc.Query("SELECT key FROM system.local", false));
     }
     testCluster.ShutDown();
 }
コード例 #2
0
        public void Should_GetAllSettingsFromBaseProfile_When_DerivedProfileHasNoSettings()
        {
            var go  = new GraphOptions();
            var lbp = new RoundRobinPolicy();
            var sep = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var rp  = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var baseProfileBuilder = new ExecutionProfileBuilder();

            baseProfileBuilder
            .WithLoadBalancingPolicy(lbp)
            .WithSpeculativeExecutionPolicy(sep)
            .WithSerialConsistencyLevel(ConsistencyLevel.LocalSerial)
            .WithConsistencyLevel(ConsistencyLevel.Quorum)
            .WithReadTimeoutMillis(3000)
            .WithGraphOptions(go)
            .WithRetryPolicy(rp);

            var baseProfile = baseProfileBuilder.Build();

            var profile = new ExecutionProfile(baseProfile, new ExecutionProfileBuilder().Build());

            Assert.AreSame(lbp, profile.LoadBalancingPolicy);
            Assert.AreSame(sep, profile.SpeculativeExecutionPolicy);
            Assert.AreSame(rp, profile.RetryPolicy);
            Assert.AreEqual(3000, profile.ReadTimeoutMillis);
            Assert.AreEqual(ConsistencyLevel.LocalSerial, profile.SerialConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.Quorum, profile.ConsistencyLevel);
            Assert.AreEqual(go, profile.GraphOptions);
        }
コード例 #3
0
        public void Should_MapOptionsToProfileWithAllSettingsFromCluster_When_NoProfileIsChangedOrAdded()
        {
            var lbp     = new RoundRobinPolicy();
            var sep     = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var rp      = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var tg      = new AtomicMonotonicTimestampGenerator();
            var cluster = Cluster
                          .Builder()
                          .AddContactPoint("127.0.0.1")
                          .WithQueryOptions(
                new QueryOptions()
                .SetConsistencyLevel(ConsistencyLevel.EachQuorum)
                .SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial)
                .SetDefaultIdempotence(true)
                .SetPageSize(5)
                .SetPrepareOnAllHosts(false)
                .SetReprepareOnUp(false))
                          .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(9999))
                          .WithLoadBalancingPolicy(lbp)
                          .WithSpeculativeExecutionPolicy(sep)
                          .WithRetryPolicy(rp)
                          .WithQueryTimeout(30)
                          .WithTimestampGenerator(tg)
                          .Build();

            Assert.AreEqual(1, cluster.Configuration.RequestOptions.Count);
            var profile = cluster.Configuration.ExecutionProfiles["default"];

            Assert.AreEqual(ConsistencyLevel.EachQuorum, profile.ConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.LocalSerial, profile.SerialConsistencyLevel);
            Assert.AreEqual(9999, profile.ReadTimeoutMillis);
            Assert.AreSame(lbp, profile.LoadBalancingPolicy);
            Assert.AreSame(sep, profile.SpeculativeExecutionPolicy);
            Assert.AreSame(rp, profile.RetryPolicy);
        }
コード例 #4
0
        public void Should_MapOptionsToProfileCorrectly_When_AllSettingsAreProvided()
        {
            var lbp     = new RoundRobinPolicy();
            var sep     = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var rp      = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var cluster = Cluster.Builder().AddContactPoint("127.0.0.1").WithExecutionProfiles(opts =>
            {
                opts.WithProfile("test1", profile => profile
                                 .WithConsistencyLevel(ConsistencyLevel.EachQuorum)
                                 .WithSerialConsistencyLevel(ConsistencyLevel.LocalSerial)
                                 .WithReadTimeoutMillis(9999)
                                 .WithLoadBalancingPolicy(lbp)
                                 .WithSpeculativeExecutionPolicy(sep)
                                 .WithRetryPolicy(rp));
            }).Build();

            var execProfile = cluster.Configuration.ExecutionProfiles["test1"];

            Assert.AreEqual(ConsistencyLevel.EachQuorum, execProfile.ConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.LocalSerial, execProfile.SerialConsistencyLevel);
            Assert.AreEqual(9999, execProfile.ReadTimeoutMillis);
            Assert.AreSame(lbp, execProfile.LoadBalancingPolicy);
            Assert.AreSame(sep, execProfile.SpeculativeExecutionPolicy);
            Assert.AreSame(rp, execProfile.RetryPolicy);
        }
コード例 #5
0
        public void Should_Reconnect_After_Several_Failed_Attempts()
        {
            var lbp    = new RoundRobinPolicy();
            var config = new Configuration(
                new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
                new ProtocolOptions(),
                null,
                new SocketOptions(),
                new ClientOptions(),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator());
            var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
            var metadata    = new Metadata(config);

            metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
            var clusterMock = new Mock <ICluster>();

            clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
            lbp.Initialize(clusterMock.Object);
            using (var cc = NewInstance(testCluster, config))
            {
                cc.Init().Wait(InitTimeout);
                testCluster.Stop(1);
                Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
                Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
                Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
                Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
                testCluster.Start(1);
                Assert.DoesNotThrow(() => TaskHelper.WaitToComplete(cc.Reconnect()));
            }
            testCluster.ShutDown();
        }
コード例 #6
0
        public void Should_MapDefaultProfileToDefaultOptionsCorrectly_When_AllSettingsAreProvided()
        {
            var go      = new GraphOptions();
            var lbp     = new RoundRobinPolicy();
            var sep     = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var rp      = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var cluster =
                Cluster
                .Builder()
                .AddContactPoint("127.0.0.1")
                .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(3000))
                .WithExecutionProfiles(opts =>
            {
                opts.WithProfile("default", profile => profile
                                 .WithConsistencyLevel(ConsistencyLevel.EachQuorum)
                                 .WithSerialConsistencyLevel(ConsistencyLevel.LocalSerial)
                                 .WithReadTimeoutMillis(9999)
                                 .WithLoadBalancingPolicy(lbp)
                                 .WithSpeculativeExecutionPolicy(sep)
                                 .WithRetryPolicy(rp)
                                 .WithGraphOptions(go));
            })
                .Build();

            Assert.AreEqual(1, cluster.Configuration.RequestOptions.Count);
            var options = cluster.Configuration.RequestOptions["default"];

            Assert.AreEqual(ConsistencyLevel.EachQuorum, options.ConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.LocalSerial, options.SerialConsistencyLevel);
            Assert.AreEqual(9999, options.ReadTimeoutMillis);
            Assert.AreSame(lbp, options.LoadBalancingPolicy);
            Assert.AreSame(sep, options.SpeculativeExecutionPolicy);
            Assert.AreSame(rp, options.RetryPolicy);
            Assert.AreSame(go, options.GraphOptions);
        }
コード例 #7
0
        public void Should_MapProfileToOptionsWithSomeSettingsFromBaseProfile_When_ADerivedProfileIsProvided()
        {
            var lbp        = new RoundRobinPolicy();
            var sep        = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var sepProfile = new ConstantSpeculativeExecutionPolicy(200, 50);
            var rp         = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var rpProfile  = new LoggingRetryPolicy(new IdempotenceAwareRetryPolicy(new DefaultRetryPolicy()));
            var tg         = new AtomicMonotonicTimestampGenerator();
            var cluster    = Cluster
                             .Builder()
                             .AddContactPoint("127.0.0.1")
                             .WithQueryOptions(
                new QueryOptions()
                .SetConsistencyLevel(ConsistencyLevel.EachQuorum)
                .SetSerialConsistencyLevel(ConsistencyLevel.Serial)
                .SetDefaultIdempotence(true)
                .SetPageSize(5)
                .SetPrepareOnAllHosts(false)
                .SetReprepareOnUp(false))
                             .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(300))
                             .WithLoadBalancingPolicy(lbp)
                             .WithSpeculativeExecutionPolicy(sep)
                             .WithRetryPolicy(rp)
                             .WithExecutionProfiles(opts => opts
                                                    .WithProfile("baseProfile", baseProfile => baseProfile
                                                                 .WithConsistencyLevel(ConsistencyLevel.Quorum)
                                                                 .WithSpeculativeExecutionPolicy(sepProfile)
                                                                 .WithRetryPolicy(rpProfile))
                                                    .WithDerivedProfile("test1", "baseProfile", profileBuilder => profileBuilder
                                                                        .WithConsistencyLevel(ConsistencyLevel.All)
                                                                        .WithSerialConsistencyLevel(ConsistencyLevel.LocalSerial)))
                             .WithQueryTimeout(30)
                             .WithTimestampGenerator(tg)
                             .Build();

            Assert.AreEqual(3, cluster.Configuration.RequestOptions.Count);
            var options = cluster.Configuration.RequestOptions["test1"];

            Assert.AreEqual(ConsistencyLevel.All, options.ConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.LocalSerial, options.SerialConsistencyLevel);
            Assert.AreEqual(300, options.ReadTimeoutMillis);
            Assert.AreSame(lbp, options.LoadBalancingPolicy);
            Assert.AreSame(sepProfile, options.SpeculativeExecutionPolicy);
            Assert.AreSame(rpProfile, options.RetryPolicy);
            Assert.AreEqual(true, options.DefaultIdempotence);
            Assert.AreEqual(5, options.PageSize);
            Assert.AreEqual(30, options.QueryAbortTimeout);
            Assert.AreSame(tg, options.TimestampGenerator);
        }
コード例 #8
0
        public async Task Should_Issue_Reconnect_Once()
        {
            var lbp    = new RoundRobinPolicy();
            var config = new Configuration(
                new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
                new ProtocolOptions(),
                null,
                new SocketOptions(),
                new ClientOptions(),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator());
            var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
            var metadata    = new Metadata(config);

            metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
            var clusterMock = new Mock <ICluster>();

            clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
            lbp.Initialize(clusterMock.Object);
            using (var cc = NewInstance(testCluster, config))
            {
                await cc.Init();

                testCluster.Stop(1);
                const int lengthPerProcessor = 20;
                var       arr = new Task[Environment.ProcessorCount * lengthPerProcessor];
                Parallel.For(0, Environment.ProcessorCount, j =>
                {
                    for (var i = 0; i < lengthPerProcessor; i++)
                    {
                        arr[j * lengthPerProcessor + i] = cc.Reconnect();
                    }
                });
                // When any of the tasks completes, all the task should have completed
                await Task.WhenAny(arr);

                await Task.Delay(20);

                var notFaultedTaskStatus = arr.Where(t => !t.IsFaulted)
                                           .Select(t => (TaskStatus?)t.Status).FirstOrDefault();
                Assert.Null(notFaultedTaskStatus, "Expected only faulted tasks");
                var ex = Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(arr[0]));
                Assert.AreEqual(1, ex.Errors.Count);
                Assert.IsInstanceOf <SocketException>(ex.Errors.Values.First());
            }
            testCluster.ShutDown();
        }
コード例 #9
0
        public void RoundRobinIsCyclicTest()
        {
            byte hostLength = 4;
            var  hostList   = GetHostList(hostLength);

            var clusterMock = new Mock <ICluster>();

            clusterMock
            .Setup(c => c.AllHosts())
            .Returns(hostList)
            .Verifiable();

            //Initialize the balancing policy
            var policy = new RoundRobinPolicy();

            policy.Initialize(clusterMock.Object);
            var balancedHosts = policy.NewQueryPlan(null, new SimpleStatement());

            //Take a list of hosts of 4, it should get 1 of every one in a cyclic order.
            var firstRound = balancedHosts.ToList();

            Assert.AreEqual(hostLength, firstRound.Count);
            foreach (var host in hostList)
            {
                //Check that each host appears only once
                Assert.AreEqual(1, firstRound.Where(h => h.Equals(host)).Count());
            }

            //test the same but in the following times
            var followingRounds = new List <Host>();

            for (var i = 0; i < 10; i++)
            {
                followingRounds.AddRange(policy.NewQueryPlan(null, new SimpleStatement()));
            }
            Assert.AreEqual(10 * hostLength, followingRounds.Count);

            //Check that the cyclic order is correct
            for (var i = 1; i < followingRounds.Count - 2; i++)
            {
                Assert.AreNotSame(followingRounds[i - 1], followingRounds[i]);
                Assert.AreNotSame(followingRounds[i + 1], followingRounds[i]);
                Assert.AreNotSame(followingRounds[i + 2], followingRounds[i]);
            }

            clusterMock.Verify();
        }
コード例 #10
0
        public void Should_GetNoSettingFromBaseProfile_When_DerivedProfileHasAllSettings()
        {
            var go                 = new GraphOptions().SetName("ee");
            var goProfile          = new GraphOptions().SetName("tt");
            var lbp                = new RoundRobinPolicy();
            var sep                = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var rp                 = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var sepProfile         = new ConstantSpeculativeExecutionPolicy(200, 50);
            var lbpProfile         = new TokenAwarePolicy(new DCAwareRoundRobinPolicy());
            var rpProfile          = new LoggingRetryPolicy(new IdempotenceAwareRetryPolicy(new DefaultRetryPolicy()));
            var baseProfileBuilder = new ExecutionProfileBuilder();

            baseProfileBuilder
            .WithLoadBalancingPolicy(lbp)
            .WithSpeculativeExecutionPolicy(sep)
            .WithSerialConsistencyLevel(ConsistencyLevel.LocalSerial)
            .WithConsistencyLevel(ConsistencyLevel.Quorum)
            .WithReadTimeoutMillis(3000)
            .WithGraphOptions(go)
            .WithRetryPolicy(rp);

            var baseProfile = baseProfileBuilder.Build();

            var derivedProfileBuilder = new ExecutionProfileBuilder();

            derivedProfileBuilder
            .WithLoadBalancingPolicy(lbpProfile)
            .WithSpeculativeExecutionPolicy(sepProfile)
            .WithSerialConsistencyLevel(ConsistencyLevel.Serial)
            .WithConsistencyLevel(ConsistencyLevel.LocalQuorum)
            .WithReadTimeoutMillis(5000)
            .WithGraphOptions(goProfile)
            .WithRetryPolicy(rpProfile);

            var derivedProfile = derivedProfileBuilder.Build();

            var profile = new ExecutionProfile(baseProfile, derivedProfile);

            Assert.AreSame(lbpProfile, profile.LoadBalancingPolicy);
            Assert.AreSame(sepProfile, profile.SpeculativeExecutionPolicy);
            Assert.AreSame(rpProfile, profile.RetryPolicy);
            Assert.AreEqual(5000, profile.ReadTimeoutMillis);
            Assert.AreEqual(ConsistencyLevel.Serial, profile.SerialConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.LocalQuorum, profile.ConsistencyLevel);
            Assert.AreSame(goProfile, profile.GraphOptions);
        }
コード例 #11
0
        public void RoundRobinIsCyclicTestInParallel()
        {
            byte hostLength = 4;
            var  hostList   = GetHostList(hostLength);

            var clusterMock = new Mock <ICluster>();

            clusterMock
            .Setup(c => c.AllHosts())
            .Returns(hostList)
            .Verifiable();

            //Initialize the balancing policy
            var policy = new RoundRobinPolicy();

            policy.Initialize(clusterMock.Object);

            Action action = () =>
            {
                var resultingHosts = new List <Host>();
                var hostEnumerator = policy.NewQueryPlan(null, new SimpleStatement());
                foreach (var h in hostEnumerator)
                {
                    //Slow down to try to execute it at the same time
                    Thread.Sleep(50);
                    resultingHosts.Add(h);
                }
                Assert.AreEqual(hostLength, resultingHosts.Count);
                Assert.AreEqual(hostLength, resultingHosts.Distinct().Count());
            };

            var actions = new List <Action>();

            for (var i = 0; i < 100; i++)
            {
                actions.Add(action);
            }

            var parallelOptions = new ParallelOptions();

            parallelOptions.TaskScheduler          = new ThreadPerTaskScheduler();
            parallelOptions.MaxDegreeOfParallelism = 1000;

            Parallel.Invoke(parallelOptions, actions.ToArray());
            clusterMock.Verify();
        }
コード例 #12
0
        public void Should_MapDefaultProfileToOptionsWithAllSettingsFromCluster_When_NoSettingIsProvided()
        {
            var go      = new GraphOptions();
            var lbp     = new RoundRobinPolicy();
            var sep     = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var rp      = new LoggingRetryPolicy(new DefaultRetryPolicy());
            var tg      = new AtomicMonotonicTimestampGenerator();
            var cluster = Cluster
                          .Builder()
                          .AddContactPoint("127.0.0.1")
                          .WithQueryOptions(
                new QueryOptions()
                .SetConsistencyLevel(ConsistencyLevel.EachQuorum)
                .SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial)
                .SetDefaultIdempotence(true)
                .SetPageSize(5)
                .SetPrepareOnAllHosts(false)
                .SetReprepareOnUp(false))
                          .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(9999))
                          .WithLoadBalancingPolicy(lbp)
                          .WithSpeculativeExecutionPolicy(sep)
                          .WithRetryPolicy(rp)
                          .WithExecutionProfiles(opts => { opts.WithProfile("default", profile => { }); })
                          .WithQueryTimeout(30)
                          .WithTimestampGenerator(tg)
                          .WithGraphOptions(go)
                          .Build();

            Assert.AreEqual(1, cluster.Configuration.RequestOptions.Count);
            var options = cluster.Configuration.RequestOptions["default"];

            Assert.AreEqual(ConsistencyLevel.EachQuorum, options.ConsistencyLevel);
            Assert.AreEqual(ConsistencyLevel.LocalSerial, options.SerialConsistencyLevel);
            Assert.AreEqual(9999, options.ReadTimeoutMillis);
            Assert.AreSame(lbp, options.LoadBalancingPolicy);
            Assert.AreSame(sep, options.SpeculativeExecutionPolicy);
            Assert.AreSame(rp, options.RetryPolicy);
            Assert.AreEqual(true, options.DefaultIdempotence);
            Assert.AreEqual(5, options.PageSize);
            Assert.AreEqual(30, options.QueryAbortTimeout);
            Assert.AreSame(tg, options.TimestampGenerator);
            Assert.AreSame(go, options.GraphOptions);
        }
コード例 #13
0
        public void RoundRobinIsCyclicTest()
        {
            byte hostLength = 4;
            var hostList = GetHostList(hostLength);

            var clusterMock = new Mock<ICluster>();
            clusterMock
                .Setup(c => c.AllHosts())
                .Returns(hostList)
                .Verifiable();

            //Initialize the balancing policy
            var policy = new RoundRobinPolicy();
            policy.Initialize(clusterMock.Object);
            var balancedHosts = policy.NewQueryPlan(null, new SimpleStatement());

            //Take a list of hosts of 4, it should get 1 of every one in a cyclic order.
            var firstRound = balancedHosts.ToList();
            Assert.AreEqual(hostLength, firstRound.Count);
            foreach (var host in hostList)
            {
                //Check that each host appears only once
                Assert.AreEqual(1, firstRound.Where(h => h.Equals(host)).Count());
            }

            //test the same but in the following times
            var followingRounds = new List<Host>();
            for (var i = 0; i < 10; i++)
            {
                followingRounds.AddRange(policy.NewQueryPlan(null, new SimpleStatement()));
            }
            Assert.AreEqual(10 * hostLength, followingRounds.Count);

            //Check that the cyclic order is correct
            for (var i = 1; i < followingRounds.Count - 2; i++)
            {
                Assert.AreNotSame(followingRounds[i - 1], followingRounds[i]);
                Assert.AreNotSame(followingRounds[i + 1], followingRounds[i]);
                Assert.AreNotSame(followingRounds[i + 2], followingRounds[i]);
            }

            clusterMock.Verify();
        }
コード例 #14
0
        public void Should_Reconnect_Once_If_Called_Serially()
        {
            var lbp    = new RoundRobinPolicy();
            var config = new Configuration(
                new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
                new ProtocolOptions(),
                null,
                new SocketOptions(),
                new ClientOptions(),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator());

            config.BufferPool = new Microsoft.IO.RecyclableMemoryStreamManager();
            var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
            var metadata    = new Metadata(config);

            metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
            var clusterMock = new Mock <ICluster>();

            clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
            lbp.Initialize(clusterMock.Object);
            using (var cc = NewInstance(testCluster, config))
            {
                cc.Init();
                testCluster.Stop(1);
                var t1 = cc.Reconnect();
                var t2 = cc.Reconnect();
                var t3 = cc.Reconnect();
                var t4 = cc.Reconnect();
                Assert.AreEqual(t1, t2);
                Assert.AreEqual(t1, t3);
                Assert.AreEqual(t1, t4);
                var ex = Assert.Throws <NoHostAvailableException>(() => TaskHelper.WaitToComplete(t1));
                Assert.AreEqual(1, ex.Errors.Count);
                Assert.IsInstanceOf <SocketException>(ex.Errors.Values.First());
            }
            testCluster.ShutDown();
        }
コード例 #15
0
        public void Should_Schedule_Reconnections_In_The_Background()
        {
            var lbp    = new RoundRobinPolicy();
            var config = new Configuration(
                new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
                new ProtocolOptions(),
                null,
                new SocketOptions(),
                new ClientOptions(),
                NoneAuthProvider.Instance,
                null,
                new QueryOptions(),
                new DefaultAddressTranslator());

            config.BufferPool = new Microsoft.IO.RecyclableMemoryStreamManager();
            var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
            var metadata    = new Metadata(config);

            metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
            var clusterMock = new Mock <ICluster>();

            clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
            lbp.Initialize(clusterMock.Object);
            using (var cc = NewInstance(testCluster, config, metadata))
            {
                cc.Init();
                var host = metadata.Hosts.First();
                testCluster.Stop(1);
                host.SetDown();
                Thread.Sleep(2000);
                Assert.False(host.IsUp);
                testCluster.Start(1);
                host.BringUpIfDown();
                //Should reconnect using timer
                Thread.Sleep(5000);
                Assert.DoesNotThrow(() => cc.Query("SELECT key FROM system.local", false));
            }
            testCluster.ShutDown();
        }
コード例 #16
0
        public void Should_SetLegacyProperties_When_PoliciesAreProvidedByDefaultProfile()
        {
            var lbp1         = new RoundRobinPolicy();
            var sep1         = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var lbp2         = new RoundRobinPolicy();
            var sep2         = new ConstantSpeculativeExecutionPolicy(1000, 1);
            var retryPolicy  = new DefaultRetryPolicy();
            var retryPolicy2 = new DefaultRetryPolicy();
            var cluster      =
                Cluster.Builder()
                .AddContactPoint("127.0.0.1")
                .WithLoadBalancingPolicy(lbp1)
                .WithSpeculativeExecutionPolicy(sep1)
                .WithRetryPolicy(retryPolicy)
                .WithSocketOptions(new SocketOptions().SetReadTimeoutMillis(123))
                .WithQueryOptions(
                    new QueryOptions()
                    .SetConsistencyLevel(ConsistencyLevel.All)
                    .SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial))
                .WithExecutionProfiles(opt => opt
                                       .WithProfile("default", profile =>
                                                    profile
                                                    .WithLoadBalancingPolicy(lbp2)
                                                    .WithSpeculativeExecutionPolicy(sep2)
                                                    .WithRetryPolicy(retryPolicy2)
                                                    .WithConsistencyLevel(ConsistencyLevel.Quorum)
                                                    .WithSerialConsistencyLevel(ConsistencyLevel.Serial)
                                                    .WithReadTimeoutMillis(4412)))
                .Build();

            Assert.AreSame(retryPolicy2, cluster.Configuration.Policies.ExtendedRetryPolicy);
            Assert.AreSame(retryPolicy2, cluster.Configuration.Policies.RetryPolicy);
            Assert.AreSame(sep2, cluster.Configuration.Policies.SpeculativeExecutionPolicy);
            Assert.AreSame(lbp2, cluster.Configuration.Policies.LoadBalancingPolicy);
            Assert.AreEqual(4412, cluster.Configuration.SocketOptions.ReadTimeoutMillis);
            Assert.AreEqual(ConsistencyLevel.Quorum, cluster.Configuration.QueryOptions.GetConsistencyLevel());
            Assert.AreEqual(ConsistencyLevel.Serial, cluster.Configuration.QueryOptions.GetSerialConsistencyLevel());
        }
コード例 #17
0
 public void Should_Reconnect_Once_If_Called_Serially()
 {
     var lbp = new RoundRobinPolicy();
     var config = new Configuration(
         new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
         new ProtocolOptions(),
         null,
         new SocketOptions(),
         new ClientOptions(),
         NoneAuthProvider.Instance,
         null,
         new QueryOptions(),
         new DefaultAddressTranslator());
     config.BufferPool = new Microsoft.IO.RecyclableMemoryStreamManager();
     var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
     var metadata = new Metadata(config);
     metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
     var clusterMock = new Mock<ICluster>();
     clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
     lbp.Initialize(clusterMock.Object);
     using (var cc = NewInstance(testCluster, config))
     {
         cc.Init();
         testCluster.Stop(1);
         var t1 = cc.Reconnect();
         var t2 = cc.Reconnect();
         var t3 = cc.Reconnect();
         var t4 = cc.Reconnect();
         Assert.AreEqual(t1, t2);
         Assert.AreEqual(t1, t3);
         Assert.AreEqual(t1, t4);
         var ex = Assert.Throws<NoHostAvailableException>(() => TaskHelper.WaitToComplete(t1));
         Assert.AreEqual(1, ex.Errors.Count);
         Assert.IsInstanceOf<SocketException>(ex.Errors.Values.First());
     }
     testCluster.ShutDown();
 }
コード例 #18
0
 public DistanceChangingLbp()
 {
     _childPolicy = new RoundRobinPolicy();
 }
コード例 #19
0
		public void TestInitialize()
		{
			contract = new XmlQualifiedName("Service", "urn:org:services");
			roundRobin = new RoundRobinPolicy(target => target.ContractTypeNames.Contains(contract));
		}
コード例 #20
0
        public void RoundRobinIsCyclicTestInParallel()
        {
            byte hostLength = 4;
            var hostList = GetHostList(hostLength);

            var clusterMock = new Mock<ICluster>();
            clusterMock
                .Setup(c => c.AllHosts())
                .Returns(hostList)
                .Verifiable();

            //Initialize the balancing policy
            var policy = new RoundRobinPolicy();
            policy.Initialize(clusterMock.Object);

            Action action = () =>
            {
                var resultingHosts = new List<Host>();
                var hostEnumerator = policy.NewQueryPlan(null, new SimpleStatement());
                foreach (var h in hostEnumerator)
                {
                    //Slow down to try to execute it at the same time
                    Thread.Sleep(50);
                    resultingHosts.Add(h);
                }
                Assert.AreEqual(hostLength, resultingHosts.Count);
                Assert.AreEqual(hostLength, resultingHosts.Distinct().Count());
            };

            var actions = new List<Action>();
            for (var i = 0; i < 100; i++)
            {
                actions.Add(action);
            }
            
            var parallelOptions = new ParallelOptions();
            parallelOptions.TaskScheduler = new ThreadPerTaskScheduler();
            parallelOptions.MaxDegreeOfParallelism = 1000;

            Parallel.Invoke(parallelOptions, actions.ToArray());
            clusterMock.Verify();
        }
コード例 #21
0
 public void TestInitialize()
 {
     contract   = new XmlQualifiedName("Service", "urn:org:services");
     roundRobin = new RoundRobinPolicy(target => target.ContractTypeNames.Contains(contract));
 }
コード例 #22
0
 public void Should_Reconnect_After_Several_Failed_Attempts()
 {
     var lbp = new RoundRobinPolicy();
     var config = new Configuration(
         new Cassandra.Policies(lbp, new ConstantReconnectionPolicy(1000), FallthroughRetryPolicy.Instance),
         new ProtocolOptions(),
         null,
         new SocketOptions(),
         new ClientOptions(),
         NoneAuthProvider.Instance,
         null,
         new QueryOptions(),
         new DefaultAddressTranslator());
     var testCluster = TestClusterManager.GetNonShareableTestCluster(1, DefaultMaxClusterCreateRetries, true, false);
     var metadata = new Metadata(config);
     metadata.AddHost(new IPEndPoint(IPAddress.Parse(testCluster.InitialContactPoint), ProtocolOptions.DefaultPort));
     var clusterMock = new Mock<ICluster>();
     clusterMock.Setup(c => c.AllHosts()).Returns(() => metadata.Hosts.ToCollection());
     lbp.Initialize(clusterMock.Object);
     using (var cc = NewInstance(testCluster, config))
     {
         cc.Init();
         testCluster.Stop(1);
         Assert.Throws<NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
         Assert.Throws<NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
         Assert.Throws<NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
         Assert.Throws<NoHostAvailableException>(() => TaskHelper.WaitToComplete(cc.Reconnect()));
         testCluster.Start(1);
         Assert.DoesNotThrow(() => TaskHelper.WaitToComplete(cc.Reconnect()));
     }
     testCluster.ShutDown();
 }