public List <RedisEndpoint> GetSlaves()
        {
            var sentinelWorker = this.GetValidSentinelWorker();
            var hosts          = sentinelWorker.GetSlaveHosts(this.MasterName);

            return(this.ConfigureHosts(hosts).Select(x => RedisEndpoint.Create(x)).ToList());
        }
        public void RefreshActiveSentinels()
        {
            var activeHosts = this.GetActiveSentinelHosts(this.SentinelHosts);

            if (activeHosts.Count == 0)
            {
                return;
            }

            lock (this.SentinelHosts) {
                this._lastSentinelsRefresh = DateTime.UtcNow;

                foreach (var value in activeHosts)
                {
                    if (!this.SentinelHosts.Contains(value))
                    {
                        this.SentinelHosts.Add(value);
                    }
                }

                this.SentinelEndpoints = this.SentinelHosts
                                         .Select(x => RedisEndpoint.Create(x, RedisConfig.DefaultPortSentinel))
                                         .ToArray();
            }
        }
        /// <summary>
        ///     Initialize Sentinel Subscription and Configure Redis ClientsManager
        /// </summary>
        public IRedisClientManager Start()
        {
            lock (this._oLock) {
                for (var i = 0; i < this.SentinelHosts.Count; i++)
                {
                    var parts = this.SentinelHosts[i].SplitOnLast(':');
                    if (parts.Length == 1)
                    {
                        this.SentinelHosts[i] = parts[0] + ":" + RedisConfig.DefaultPortSentinel;
                    }
                }

                if (this.ScanForOtherSentinels)
                {
                    this.RefreshActiveSentinels();
                }

                this.SentinelEndpoints = this.SentinelHosts
                                         .Select(x => RedisEndpoint.Create(x, RedisConfig.DefaultPortSentinel))
                                         .ToArray();

                var sentinelWorker = this.GetValidSentinelWorker();

                if (this.RedisManager == null || sentinelWorker == null)
                {
                    throw new Exception("Unable to resolve sentinels!");
                }

                return(this.RedisManager);
            }
        }
 /// <summary>
 ///     Hosts can be an IP Address or Hostname in the format: host[:port]
 ///     e.g. 127.0.0.1:6379
 ///     default is: localhost:6379
 /// </summary>
 /// <param name="readWriteHosts" >The write hosts.</param>
 /// <param name="readOnlyHosts" >The read hosts.</param>
 /// <param name="initialDb" >initialDb</param>
 public BasicRedisClientManager(
     IEnumerable <string> readWriteHosts,
     IEnumerable <string> readOnlyHosts,
     long?initialDb = null)
     : this(RedisEndpoint.Create(readWriteHosts), RedisEndpoint.Create(readOnlyHosts), initialDb)
 {
 }
示例#5
0
        public void Can_use_password_with_equals()
        {
            var connString = "127.0.0.1?password="******"p@55w0rd=");

            var config = RedisEndpoint.Create(connString);

            Assert.That(config.Password, Is.EqualTo("p@55w0rd="));
        }
示例#6
0
        public void Can_Parse_Host()
        {
            var hosts     = new[] { "[email protected]:6123" };
            var endPoints = RedisEndpoint.Create(hosts);

            Assert.AreEqual(1, endPoints.Count);
            var ep = endPoints[0];

            Assert.AreEqual("host.com", ep.Host);
            Assert.AreEqual(6123, ep.Port);
            Assert.AreEqual("pass", ep.Password);
        }
示例#7
0
        public void Host_May_Contain_AtChar()
        {
            var hosts     = new[] { "@pa1@ss@localhost:6123" };
            var endPoints = RedisEndpoint.Create(hosts);

            Assert.AreEqual(1, endPoints.Count);
            var ep = endPoints[0];

            Assert.AreEqual("@pa1@ss", ep.Password);
            Assert.AreEqual("localhost", ep.Host);
            Assert.AreEqual(6123, ep.Port);
        }
        public RedisEndpoint GetMaster()
        {
            var sentinelWorker = this.GetValidSentinelWorker();
            var host           = sentinelWorker.GetMasterHost(this.MasterName);

            if (this.ScanForOtherSentinels && DateTime.UtcNow - this._lastSentinelsRefresh > this.RefreshSentinelHostsAfter)
            {
                this.RefreshActiveSentinels();
            }

            return(host != null
                ? RedisEndpoint.Create(this.HostFilter != null?this.HostFilter(host) : host)
                : null);
        }
示例#9
0
        public void RedisManagerPool_can_execute_CustomResolver()
        {
            var resolver = new FixedResolver(RedisEndpoint.Create(Config.Sentinel6380), RedisEndpoint.Create(Config.Sentinel6381));

            using (var redisManager = new RedisManagerPool("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.Sentinel6380));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                }

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

                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.Sentinel6380));
                        Assert.That(slave.GetValue("KEY"), Is.EqualTo("2"));
                    }
                }

                Assert.That(resolver.NewClientsInitialized, Is.EqualTo(2));
            }
        }
示例#10
0
        public void Does_encode_values_when_serializing_to_ConnectionString()
        {
            var config = new RedisEndpoint {
                Host     = "host",
                Port     = 1,
                Password = "******"
            };

            var connString = config.ToString();

            Assert.That(connString, Is.EqualTo("host:1?Password=p%4055W0rd%3D"));

            var fromConfig = RedisEndpoint.Create(connString);

            Assert.That(fromConfig.Host, Is.EqualTo(config.Host));
            Assert.That(fromConfig.Port, Is.EqualTo(config.Port));
            Assert.That(fromConfig.Password, Is.EqualTo(config.Password));
        }
示例#11
0
        public List <string> GetActiveSentinelHosts(IEnumerable <string> sentinelHosts)
        {
            var activeSentinelHosts = new List <string>();

            foreach (var sentinelHost in sentinelHosts.ToArray())
            {
                try {
                    if (_logger.IsDebugEnabled())
                    {
                        _logger.Debug("Connecting to all available Sentinels to discover Active Sentinel Hosts...");
                    }

                    var endpoint = RedisEndpoint.Create(sentinelHost, RedisConfig.DefaultPortSentinel);
                    using (var sentinelWorker = new RedisSentinelWorker(this, endpoint)) {
                        var activeHosts = sentinelWorker.GetSentinelHosts(this.MasterName);

                        if (!activeSentinelHosts.Contains(sentinelHost))
                        {
                            activeSentinelHosts.Add(sentinelHost);
                        }

                        foreach (var activeHost in activeHosts)
                        {
                            if (!activeSentinelHosts.Contains(activeHost))
                            {
                                activeSentinelHosts.Add(activeHost);
                            }
                        }
                    }

                    if (_logger.IsDebugEnabled())
                    {
                        _logger.Debug("All active Sentinels Found: " + string.Join(", ", activeSentinelHosts));
                    }
                } catch (Exception ex) {
                    _logger.Error(string.Format("Could not get active Sentinels from: {0}", sentinelHost), ex);
                }
            }

            return(activeSentinelHosts);
        }
示例#12
0
 /// <inheritdoc />
 public virtual void ResetSlaves(IEnumerable <string> hosts)
 {
     this.ResetSlaves(RedisEndpoint.Create(hosts));
 }
示例#13
0
 /// <inheritdoc />
 public RedisResolver(IEnumerable <string> masters, IEnumerable <string> slaves)
     : this(RedisEndpoint.Create(masters), RedisEndpoint.Create(slaves))
 {
 }
        public void Defaults_to_default_sentinel_port()
        {
            var sentinelEndpoint = RedisEndpoint.Create("127.0.0.1", RedisConfig.DefaultPortSentinel);

            Assert.That(sentinelEndpoint.Port, Is.EqualTo(RedisConfig.DefaultPortSentinel));
        }
示例#15
0
 /// <inheritdoc />
 public RedisSentinelResolver(RedisSentinel sentinel, IEnumerable <string> masters, IEnumerable <string> slaves)
     : this(sentinel, RedisEndpoint.Create(masters), RedisEndpoint.Create(slaves))
 {
 }
示例#16
0
        public void Does_Serialize_RedisEndpoint(string connString, string expectedString)
        {
            var actual = RedisEndpoint.Create(connString);

            Assert.That(actual.ToString(), Is.EqualTo(expectedString));
        }