예제 #1
0
 /// <summary>
 /// General purpose pipeline
 /// </summary>
 /// <param name="redisClient"></param>
 public RedisAllPurposePipeline(RedisClient redisClient)
     : base(redisClient)
 {
     Init();
 }
예제 #2
0
 protected void ClosePipeline()
 {
     RedisClient.EndPipeline();
 }
예제 #3
0
 /// <summary>
 /// Issue exec command (not queued)
 /// </summary>
 private void Exec()
 {
     RedisClient.Exec();
     RedisClient.FlushAndResetSendBuffer();
 }
예제 #4
0
 public RedisClientHash(RedisClient client, string hashId)
 {
     this.client = client;
     this.hashId = hashId;
 }
예제 #5
0
 public RedisTransaction(RedisClient redisClient)
     : base(redisClient)
 {
 }
예제 #6
0
 public RedisCommandQueue(RedisClient redisClient)
 {
     this.RedisClient = redisClient;
 }
예제 #7
0
 public RedisClientSet(RedisClient client, string setId)
 {
     this.client = client;
     this.setId  = setId;
 }
예제 #8
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);
        }
예제 #9
0
 public RedisClientLists(RedisClient client)
 {
     this.client = client;
 }