Ejemplo n.º 1
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                    log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role));
                    var         newMasters   = new List <RedisEndpoint>();
                    var         newSlaves    = new List <RedisEndpoint>();
                    RedisClient masterClient = null;
                    foreach (var hostConfig in allHosts)
                    {
                        try
                        {
                            var testClient = ClientFactory(hostConfig);
                            testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                            var testRole = testClient.GetServerRole();
                            switch (testRole)
                            {
                            case RedisServerRole.Master:
                                newMasters.Add(hostConfig);
                                if (masterClient == null)
                                {
                                    masterClient = testClient;
                                }
                                break;

                            case RedisServerRole.Slave:
                                newSlaves.Add(hostConfig);
                                break;
                            }
                        }
                        catch { /* skip */ }
                    }

                    if (masterClient == null)
                    {
                        Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                        var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                        log.Error(errorMsg);
                        throw new Exception(errorMsg);
                    }

                    ResetMasters(newMasters);
                    ResetSlaves(newSlaves);
                    return(masterClient);
                }
            }

            return(client);
        }
Ejemplo n.º 2
0
 protected bool Equals(RedisEndpoint other)
 {
     return(string.Equals(Host, other.Host) &&
            Port == other.Port &&
            Ssl.Equals(other.Ssl) &&
            ConnectTimeout == other.ConnectTimeout &&
            SendTimeout == other.SendTimeout &&
            ReceiveTimeout == other.ReceiveTimeout &&
            RetryTimeout == other.RetryTimeout &&
            IdleTimeOutSecs == other.IdleTimeOutSecs &&
            Db == other.Db &&
            string.Equals(Client, other.Client) &&
            string.Equals(Password, other.Password) &&
            string.Equals(NamespacePrefix, other.NamespacePrefix));
 }
Ejemplo n.º 3
0
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel       = sentinel;
            this.sentinelClient = new RedisClient(sentinelEndpoint)
            {
                Db             = 0, //Sentinel Servers doesn't support DB, reset to 0
                ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs,
                ReceiveTimeout = sentinel.SentinelWorkerReceiveTimeoutMs,
                SendTimeout    = sentinel.SentinelWorkerSendTimeoutMs,
            };

            if (Log.IsDebugEnabled)
            {
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
            }
        }
Ejemplo n.º 4
0
 public RedisClient(RedisEndpoint config)
     : base(config)
 {
     Init();
 }
Ejemplo n.º 5
0
 public static RedisClient CreateRedisClient(this IRedisResolver resolver, RedisEndpoint config, bool master)
 {
     return(((IRedisResolverExtended)resolver).CreateRedisClient(config, master));
 }
Ejemplo n.º 6
0
        public static RedisEndpoint ToRedisEndpoint(this string connectionString, int?defaultPort = null)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }
            if (connectionString.StartsWith("redis://"))
            {
                connectionString = connectionString.Substring("redis://".Length);
            }

            var domainParts    = connectionString.SplitOnLast('@');
            var qsParts        = domainParts.Last().SplitOnFirst('?');
            var hostParts      = qsParts[0].SplitOnLast(':');
            var useDefaultPort = true;
            var port           = defaultPort.GetValueOrDefault(RedisConfig.DefaultPort);

            if (hostParts.Length > 1)
            {
                port           = int.Parse(hostParts[1]);
                useDefaultPort = false;
            }
            var endpoint = new RedisEndpoint(hostParts[0], port);

            if (domainParts.Length > 1)
            {
                var authParts = domainParts[0].SplitOnFirst(':');
                if (authParts.Length > 1)
                {
                    endpoint.Client = authParts[0];
                }

                endpoint.Password = authParts.Last();
            }

            if (qsParts.Length > 1)
            {
                var qsParams = qsParts[1].Split('&');
                foreach (var param in qsParams)
                {
                    var entry = param.Split('=');
                    var value = entry.Length > 1 ? entry[1].UrlDecode() : null;
                    if (value == null)
                    {
                        continue;
                    }

                    var name = entry[0].ToLower();
                    switch (name)
                    {
                    case "db":
                        endpoint.Db = int.Parse(value);
                        break;

                    case "ssl":
                        endpoint.Ssl = bool.Parse(value);
                        if (useDefaultPort)
                        {
                            endpoint.Port = RedisConfig.DefaultPortSsl;
                        }
                        break;

                    case "client":
                        endpoint.Client = value;
                        break;

                    case "password":
                        endpoint.Password = value;
                        break;

                    case "namespaceprefix":
                        endpoint.NamespacePrefix = value;
                        break;

                    case "connecttimeout":
                        endpoint.ConnectTimeout = int.Parse(value);
                        break;

                    case "sendtimeout":
                        endpoint.SendTimeout = int.Parse(value);
                        break;

                    case "receivetimeout":
                        endpoint.ReceiveTimeout = int.Parse(value);
                        break;

                    case "retrytimeout":
                        endpoint.RetryTimeout = int.Parse(value);
                        break;

                    case "idletimeout":
                    case "idletimeoutsecs":
                        endpoint.IdleTimeOutSecs = int.Parse(value);
                        break;
                    }
                }
            }

            return(endpoint);
        }
Ejemplo n.º 7
0
 public RedisClient CreateRedisClient(RedisEndpoint config, bool master)
 {
     return(ClientFactory(config));
 }
Ejemplo n.º 8
0
 public static string GetHostString(this RedisEndpoint config)
 {
     return("{0}:{1}".Fmt(config.Host, config.Port));
 }
Ejemplo n.º 9
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master)
            {
                var role = RedisServerRole.Unknown;
                try
                {
                    role = client.GetServerRole();
                    if (role == RedisServerRole.Master)
                    {
                        lastValidMasterFromSentinelAt = DateTime.UtcNow;
                        return(client);
                    }
                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);

                    if (client.GetHostString() == lastInvalidMasterHost)
                    {
                        lock (oLock)
                        {
                            if (DateTime.UtcNow - lastValidMasterFromSentinelAt > sentinel.WaitBeforeForcingMasterFailover)
                            {
                                lastInvalidMasterHost         = null;
                                lastValidMasterFromSentinelAt = DateTime.UtcNow;

                                log.Error("Valid master was not found at '{0}' within '{1}'. Sending SENTINEL failover...".Fmt(
                                              client.GetHostString(), sentinel.WaitBeforeForcingMasterFailover), ex);

                                Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);

                                sentinel.ForceMasterFailover();
                                Thread.Sleep(sentinel.WaitBetweenFailedHosts);
                                role = client.GetServerRole();
                            }
                        }
                    }
                    else
                    {
                        lastInvalidMasterHost = client.GetHostString();
                    }
                }

                if (role != RedisServerRole.Master && RedisConfig.VerifyMasterConnections)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            try
                            {
                                var masterConfig = sentinel.GetMaster();
                                var masterClient = ClientFactory(masterConfig);
                                masterClient.ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs;

                                var masterRole = masterClient.GetServerRole();
                                if (masterRole == RedisServerRole.Master)
                                {
                                    lastValidMasterFromSentinelAt = DateTime.UtcNow;
                                    return(masterClient);
                                }
                                else
                                {
                                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                                }
                            }
                            catch { /* Ignore errors until MaxWait */ }

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenFailedHosts)
                            {
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                                           .Fmt(sentinel.MaxWaitBetweenFailedHosts.ToString()));
                            }

                            Thread.Sleep(sentinel.WaitBetweenFailedHosts);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);

                        var         newMasters   = new List <RedisEndpoint>();
                        var         newSlaves    = new List <RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = ClientFactory(hostConfig);
                                testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                case RedisServerRole.Master:
                                    newMasters.Add(hostConfig);
                                    if (masterClient == null)
                                    {
                                        masterClient = testClient;
                                    }
                                    break;

                                case RedisServerRole.Slave:
                                    newSlaves.Add(hostConfig);
                                    break;
                                }
                            }
                            catch { /* skip past invalid master connections */ }
                        }

                        if (masterClient == null)
                        {
                            Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return(masterClient);
                    }
                }
            }

            return(client);
        }