예제 #1
0
        public void TestStrategy()
        {
            IStrategy s       = new RoundRobinStrategy(3);
            int       matched = 0;

            // try 100 times
            for (int i = 1; i <= 100; i++)
            {
                if (s.NextPartition() == i % 3)
                {
                    matched++;
                }
            }
            Assert.Equal(100, matched);

            s       = new RandomStrategy(3);
            matched = 0;
            // try 100 times
            for (int i = 1; i <= 100; i++)
            {
                if (s.NextPartition() == i % 3)
                {
                    matched++;
                }
            }
            Assert.NotEqual(100, matched);
        }
예제 #2
0
        private static DefaultLoadBalancer BuildLoadBalancer() // TODO: include this in library? IOptions / IConfig object?
        {
            var registry         = new ProviderRegistry();
            var strategy         = new RoundRobinStrategy();
            var scheduler        = new Scheduler(interval: 3000); // healthcheck scheduled every 3 seconds
            var heartbeatChecker = new HeartbeatChecker(registry, scheduler);
            var lb = new DefaultLoadBalancer(registry, strategy, heartbeatChecker);

            return(lb);
        }
예제 #3
0
        public RiakCluster(IRiakClusterConfiguration clusterConfiguration, IRiakConnectionFactory connectionFactory)
        {
            _nodePollTime = clusterConfiguration.NodePollTime;
            _nodes        = clusterConfiguration.RiakNodes.Select(rn => new RiakNode(rn, connectionFactory)).Cast <IRiakNode>().ToList();
            _loadBalancer = new RoundRobinStrategy();
            _loadBalancer.Initialise(_nodes);
            _offlineNodes      = new ConcurrentQueue <IRiakNode>();
            _defaultRetryCount = clusterConfiguration.DefaultRetryCount;
            RetryWaitTime      = clusterConfiguration.DefaultRetryWaitTime;

            Task.Factory.StartNew(NodeMonitor);
        }
예제 #4
0
        public RiakCluster(IRiakClusterConfiguration clusterConfiguration, IRiakConnectionFactory connectionFactory)
        {
            _nodePollTime = clusterConfiguration.NodePollTime;
            _nodes        = clusterConfiguration.RiakNodes.Select(rn => new RiakNode(rn, connectionFactory)).Cast <IRiakNode>().ToList();
            _loadBalancer = new RoundRobinStrategy();
            _loadBalancer.Initialise(_nodes);
            _offlineNodes      = new ConcurrentQueue <IRiakNode>();
            _defaultRetryCount = clusterConfiguration.DefaultRetryCount;
            RetryWaitTime      = clusterConfiguration.DefaultRetryWaitTime;

            // node monitor is now asynchronous, just triggered by timer!
            _nodePollTimer = new Timer(state => NodeMonitorCycle(), null, _nodePollTime, Timeout.Infinite);
        }
예제 #5
0
        public RiakCluster(IRiakClusterConfiguration clusterConfiguration)
        {
            _riakConnection = new RiakConnection();
            _nodePollTime   = clusterConfiguration.NodePollTime;

            _nodes =
                clusterConfiguration.RiakNodes.Select(riakNodeConfiguration => new RiakNode(riakNodeConfiguration))
                .Cast <IRiakNode>()
                .ToList();

            _loadBalancer = new RoundRobinStrategy();
            _loadBalancer.Initialise(_nodes);
            _offlineNodes = new ConcurrentQueue <IRiakNode>();

            Task.Factory.StartNew(NodeMonitor);
        }
예제 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RiakCluster"/> class.
        /// </summary>
        /// <param name="clusterConfig">The <see cref="IRiakClusterConfiguration"/> to use for this RiakCluster.</param>
        /// <param name="connectionFactory">The <see cref="IRiakConnectionFactory"/> instance to use for this RiakCluster.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="clusterConfig" /> contains no node information.</exception>
        public RiakCluster(IRiakClusterConfiguration clusterConfig, IRiakConnectionFactory connectionFactory)
        {
            nodePollTime      = clusterConfig.NodePollTime;
            defaultRetryCount = clusterConfig.DefaultRetryCount;
            RetryWaitTime     = clusterConfig.DefaultRetryWaitTime;

            nodes = clusterConfig.RiakNodes.Select(rn =>
                                                   new RiakNode(rn, clusterConfig.Authentication, connectionFactory)).Cast <IRiakNode>().ToList();

            loadBalancer = new RoundRobinStrategy();
            loadBalancer.Initialise(nodes);

            offlineNodes = new ConcurrentQueue <IRiakNode>();

            ct          = cts.Token;
            monitorTask = Task.Factory.StartNew(NodeMonitor, ct);
        }
        public void RoundRobinStrategy_ReturnsProvidersInOrder()
        {
            // Arrange
            var strategy = new RoundRobinStrategy();

            var providers = new List <IProvider>();

            providers.Add(new Provider("0"));
            providers.Add(new Provider("1"));
            providers.Add(new Provider("2"));

            // Act & Assert
            Assert.AreEqual("0", strategy.SelectProvider(providers).Get());
            Assert.AreEqual("1", strategy.SelectProvider(providers).Get());
            Assert.AreEqual("2", strategy.SelectProvider(providers).Get());
            Assert.AreEqual("0", strategy.SelectProvider(providers).Get());
            Assert.AreEqual("1", strategy.SelectProvider(providers).Get());
            Assert.AreEqual("2", strategy.SelectProvider(providers).Get());
        }
예제 #8
0
        public void NoExceptionsShouldBeThrown()
        {
            // three sets for three different threads
            var nodes      = new[] { CreateMockNodes(), CreateMockNodes(), CreateMockNodes() };
            var roundRobin = new RoundRobinStrategy();

            roundRobin.Initialise(nodes.SelectMany(n => n));

            var results = new Exception[3];

            Parallel.For(0, 3, i =>
            {
                results[i] = DoStuffWithNodes(roundRobin, CreateMockNodes());
            });

            foreach (var result in results)
            {
                Assert.IsNull(result);
            }
        }