예제 #1
0
        public void Can_failover_at_runtime()
        {
            var    failoverHost = "redis-failover:6379";
            string key          = "test:failover";

            var localClient = new RedisClient("localhost");

            localClient.Remove(key);
            var failoverClient = new RedisClient(failoverHost);

            failoverClient.Remove(key);

            var clientManager = new PooledRedisClientManager(new[] { "localhost" });

            RunInLoop(clientManager, callback: () =>
            {
                lock (clientManager)
                    Monitor.Pulse(clientManager);
            });

            Thread.Sleep(100);

            clientManager.FailoverTo(failoverHost);

            lock (clientManager)
                Monitor.Wait(clientManager);

            var localIncr    = localClient.Get <int>(key);
            var failoverIncr = failoverClient.Get <int>(key);

            Assert.That(localIncr, Is.GreaterThan(0));
            Assert.That(failoverIncr, Is.GreaterThan(0));
            Assert.That(localIncr + failoverIncr, Is.EqualTo(100));
        }
예제 #2
0
        public void PooledRedisClientManager_can_execute_CustomResolver()
        {
            var resolver = new FixedResolver(MasterHosts[0].ToRedisEndpoint(), SlaveHosts[0].ToRedisEndpoint());

            using (var redisManager = new PooledRedisClientManager("127.0.0.1:8888")
            {
                RedisResolver = resolver
            })
            {
                using (var master = redisManager.GetClient())
                {
                    Assert.That(master.GetHostString(), Is.EqualTo(MasterHosts[0]));
                    master.SetValue("KEY", "1");
                }
                using (var master = redisManager.GetClient())
                {
                    Assert.That(master.GetHostString(), Is.EqualTo(MasterHosts[0]));
                    master.Increment("KEY", 1);
                }
                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(1));

                5.Times(i =>
                {
                    using (var slave = redisManager.GetReadOnlyClient())
                    {
                        Assert.That(slave.GetHostString(), Is.EqualTo(SlaveHosts[0]));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                });
                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(2));

                redisManager.FailoverTo("127.0.0.1:9999", "127.0.0.1:9999");

                5.Times(i =>
                {
                    using (var master = redisManager.GetClient())
                    {
                        Assert.That(master.GetHostString(), Is.EqualTo(MasterHosts[0]));
                        Assert.That(master.GetValue("KEY"), Is.EqualTo("2"));
                    }
                    using (var slave = redisManager.GetReadOnlyClient())
                    {
                        Assert.That(slave.GetHostString(), Is.EqualTo(SlaveHosts[0]));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                });
                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(4));
            }
        }
예제 #3
0
        public void PooledRedisClientManager_can_execute_CustomResolver()
        {
            var resolver = new FixedResolver(RedisEndpoint.Create(Config.Sentinel6380), RedisEndpoint.Create(Config.Sentinel6381));

            using (var redisManager = new PooledRedisClientManager("127.0.0.1:8888")
            {
                RedisResolver = resolver
            }) {
                using (var master = redisManager.GetClient()) {
                    Assert.That(master.GetHostString(), Is.EqualTo(Config.Sentinel6380));
                    master.SetValue("KEY", "1");
                }

                using (var master = redisManager.GetClient()) {
                    Assert.That(master.GetHostString(), Is.EqualTo(Config.Sentinel6380));
                    master.Increment("KEY", 1);
                }

                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(1));

                for (var i = 0; i < 5; i++)
                {
                    using (var slave = redisManager.GetReadOnlyClient()) {
                        Assert.That(slave.GetHostString(), Is.EqualTo(Config.Sentinel6381));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                }

                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(2));

                redisManager.FailoverTo("127.0.0.1:9999", "127.0.0.1:9999");

                for (var i = 0; i < 5; i++)
                {
                    using (var master = redisManager.GetClient()) {
                        Assert.That(master.GetHostString(), Is.EqualTo(Config.Sentinel6380));
                        Assert.That(master.GetValue("KEY"), Is.EqualTo("2"));
                    }

                    using (var slave = redisManager.GetReadOnlyClient()) {
                        Assert.That(slave.GetHostString(), Is.EqualTo(Config.Sentinel6381));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                }

                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(4));
            }
        }
예제 #4
0
        public void Can_failover_MqServer_at_runtime()
        {
            const int iterations   = 100;
            var       failoverHost = "redis-failover:6379";
            var       localClient  = new RedisClient("localhost:6379");

            localClient.FlushDb();
            var failoverClient = new RedisClient(failoverHost);

            failoverClient.FlushDb();

            var clientManager = new PooledRedisClientManager(new[] { "localhost" });
            var mqHost        = new RedisMqServer(clientManager);

            var map      = new Dictionary <string, int>();
            var received = 0;

            mqHost.RegisterHandler <Msg>(c =>
            {
                var dto = c.GetBody();
                received++;
                int count;
                map.TryGetValue(dto.Host, out count);
                map[dto.Host] = count + 1;

                lock (clientManager)
                {
                    "Received #{0} from {1}".Print(received, dto.Host);
                    if (received == iterations)
                    {
                        Monitor.Pulse(clientManager);
                    }
                }

                return(null);
            });

            mqHost.Start();

            RunMqInLoop(mqHost, iterations: iterations, callback: () =>
            {
                lock (clientManager)
                    "{0} msgs were published.".Print(iterations);
            });

            Thread.Sleep(500);

            clientManager.FailoverTo(failoverHost);

            lock (clientManager)
                Monitor.Wait(clientManager);

            map.PrintDump();
            "localclient inq: {0}, outq: {1}".Print(
                localClient.GetListCount("mq:Msg.inq"),
                localClient.GetListCount("mq:Msg.outq"));
            "failoverClient inq: {0}, outq: {1}".Print(
                failoverClient.GetListCount("mq:Msg.inq"),
                failoverClient.GetListCount("mq:Msg.outq"));

            Assert.That(received, Is.EqualTo(100));
            Assert.That(map.Count, Is.EqualTo(2));
            var msgsFromAllHosts = 0;

            foreach (var count in map.Values)
            {
                Assert.That(count, Is.GreaterThan(0));
                msgsFromAllHosts += count;
            }
            Assert.That(msgsFromAllHosts, Is.EqualTo(iterations));
        }
        public void Can_failover_MqServer_at_runtime()
        {
            const int iterations = 100;
            var failoverHost = "redis-failover:6379";
            var localClient = new RedisClient("localhost:6379");

            localClient.FlushDb();
            var failoverClient = new RedisClient(failoverHost);
            failoverClient.FlushDb();

            var clientManager = new PooledRedisClientManager(new[] { "localhost" });
            var mqHost = new RedisMqServer(clientManager);

            var map = new Dictionary<string, int>();
            var received = 0;
            mqHost.RegisterHandler<Msg>(c =>
            {
                var dto = c.GetBody();
                received++;
                int count;
                map.TryGetValue(dto.Host, out count);
                map[dto.Host] = count + 1;

                lock (clientManager)
                {
                    "Received #{0} from {1}".Print(received, dto.Host);
                    if (received == iterations)
                        Monitor.Pulse(clientManager);
                }

                return null;
            });

            mqHost.Start();

            RunMqInLoop(mqHost, iterations: iterations, callback: () =>
            {
                lock (clientManager)
                    "{0} msgs were published.".Print(iterations);
            });

            Thread.Sleep(500);

            clientManager.FailoverTo(failoverHost);

            lock (clientManager)
                Monitor.Wait(clientManager);

            map.PrintDump();
            "localclient inq: {0}, outq: {1}".Print(
                localClient.GetListCount("mq:Msg.inq"),
                localClient.GetListCount("mq:Msg.outq"));
            "failoverClient inq: {0}, outq: {1}".Print(
                failoverClient.GetListCount("mq:Msg.inq"),
                failoverClient.GetListCount("mq:Msg.outq"));

            Assert.That(received, Is.EqualTo(100));
            Assert.That(map.Count, Is.EqualTo(2));
            var msgsFromAllHosts = 0;
            foreach (var count in map.Values)
            {
                Assert.That(count, Is.GreaterThan(0));
                msgsFromAllHosts += count;
            }
            Assert.That(msgsFromAllHosts, Is.EqualTo(iterations));
        }
        public void Can_failover_at_runtime()
        {
            var failoverHost = "redis-failover:6379";
            string key = "test:failover";

            var localClient = new RedisClient("localhost");
            localClient.Remove(key);
            var failoverClient = new RedisClient(failoverHost);
            failoverClient.Remove(key);

            var clientManager = new PooledRedisClientManager(new[] { "localhost" });

            RunInLoop(clientManager, callback:() =>
                {
                    lock (clientManager)
                        Monitor.Pulse(clientManager);
                });

            Thread.Sleep(100);

            clientManager.FailoverTo(failoverHost);

            lock (clientManager)
                Monitor.Wait(clientManager);

            var localIncr = localClient.Get<int>(key);
            var failoverIncr = failoverClient.Get<int>(key);
            Assert.That(localIncr, Is.GreaterThan(0));
            Assert.That(failoverIncr, Is.GreaterThan(0));
            Assert.That(localIncr + failoverIncr, Is.EqualTo(100));
        }
        public void PooledRedisClientManager_can_execute_CustomResolver()
        {
            var resolver = new FixedResolver(MasterHosts[0].ToRedisEndpoint(), SlaveHosts[0].ToRedisEndpoint());
            using (var redisManager = new PooledRedisClientManager("127.0.0.1:8888")
            {
                RedisResolver = resolver
            })
            {
                using (var master = redisManager.GetClient())
                {
                    Assert.That(master.GetHostString(), Is.EqualTo(MasterHosts[0]));
                    master.SetValue("KEY", "1");
                }
                using (var master = redisManager.GetClient())
                {
                    Assert.That(master.GetHostString(), Is.EqualTo(MasterHosts[0]));
                    master.Increment("KEY", 1);
                }
                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(1));

                5.Times(i =>
                {
                    using (var slave = redisManager.GetReadOnlyClient())
                    {
                        Assert.That(slave.GetHostString(), Is.EqualTo(SlaveHosts[0]));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                });
                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(2));

                redisManager.FailoverTo("127.0.0.1:9999", "127.0.0.1:9999");

                5.Times(i =>
                {
                    using (var master = redisManager.GetClient())
                    {
                        Assert.That(master.GetHostString(), Is.EqualTo(MasterHosts[0]));
                        Assert.That(master.GetValue("KEY"), Is.EqualTo("2"));
                    }
                    using (var slave = redisManager.GetReadOnlyClient())
                    {
                        Assert.That(slave.GetHostString(), Is.EqualTo(SlaveHosts[0]));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                });
                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(4));
            }
        }