Exemplo n.º 1
0
        public List <string> GetActiveSentinelHosts(IEnumerable <string> sentinelHosts)
        {
            var activeSentinelHosts = new List <string>();

            foreach (var sentinelHost in sentinelHosts.ToArray())
            {
                try
                {
                    var endpoint = sentinelHost.ToRedisEndpoint(defaultPort: RedisConfig.DefaultPortSentinel);
                    using (var sentinelWorker = new RedisSentinelWorker(this, endpoint))
                    {
                        var activeHosts = sentinelWorker.GetSentinelHosts(MasterName);

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

                        foreach (var activeHost in activeHosts)
                        {
                            if (!activeSentinelHosts.Contains(activeHost))
                            {
                                activeSentinelHosts.Add(activeHost);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Could not get active Sentinels from: {0}".Fmt(sentinelHost), ex);
                }
            }
            return(activeSentinelHosts);
        }
Exemplo n.º 2
0
        private RedisSentinelWorker GetNextSentinel()
        {
            RedisSentinelWorker disposeWorker = null;

            try
            {
                lock (oLock)
                {
                    if (this.worker != null)
                    {
                        disposeWorker = this.worker;
                        this.worker   = null;
                    }

                    if (++sentinelIndex >= SentinelEndpoints.Length)
                    {
                        sentinelIndex = 0;
                    }

                    var sentinelWorker = new RedisSentinelWorker(this, SentinelEndpoints[sentinelIndex])
                    {
                        OnSentinelError = OnSentinelError
                    };

                    return(sentinelWorker);
                }
            }
            finally
            {
                disposeWorker?.Dispose();
            }
        }
Exemplo n.º 3
0
 public void Dispose()
 {
     if (worker != null)
     {
         worker.Dispose();
         worker = null;
     }
 }
Exemplo n.º 4
0
        private RedisSentinelWorker GetValidSentinelWorker()
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            if (this.worker != null)
            {
                return(this.worker);
            }

            RedisException lastEx = null;

            while (this.worker == null && ShouldRetry())
            {
                var step = 0;
                try
                {
                    this.worker = GetNextSentinel();
                    step        = 1;
                    GetRedisManager();

                    step = 2;
                    this.worker.BeginListeningForConfigurationChanges();
                    this.failures = 0; //reset
                    return(this.worker);
                }
                catch (RedisException ex)
                {
                    if (Log.IsDebugEnabled)
                    {
                        var name = step switch {
                            0 => "GetNextSentinel()",
                            1 => "GetRedisManager()",
                            2 => "BeginListeningForConfigurationChanges()",
                            _ => $"Step {step}",
                        };
                        Log.Debug($"Failed to {name}: {ex.Message}");
                    }

                    if (OnWorkerError != null)
                    {
                        OnWorkerError(ex);
                    }

                    lastEx      = ex;
                    this.worker = null;
                    this.failures++;
                    Interlocked.Increment(ref RedisState.TotalFailedSentinelWorkers);
                }
            }

            this.failures = 0; //reset
            TaskUtils.Sleep(WaitBetweenFailedHosts);
            throw new RedisException("No Redis Sentinels were available", lastEx);
        }
Exemplo n.º 5
0
 public void Dispose()
 {
     if (worker != null)
     {
         worker.SentinelError -= Worker_SentinelError;
         worker.Dispose();
         worker = null;
     }
 }
Exemplo n.º 6
0
        private void Worker_SentinelError(object sender, EventArgs e)
        {
            var worker = sender as RedisSentinelWorker;

            if (worker != null)
            {
                worker.SentinelError -= Worker_SentinelError;
                worker.Dispose();

                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
Exemplo n.º 7
0
        private RedisSentinelWorker GetNextSentinel()
        {
            sentinelIndex++;

            if (sentinelIndex >= sentinels.Count)
            {
                sentinelIndex = 0;
            }

            var sentinelWorker = new RedisSentinelWorker(sentinels[sentinelIndex], this.sentinelName, this.clientManager);

            sentinelWorker.SentinelError += Worker_SentinelError;
            return(sentinelWorker);
        }
Exemplo n.º 8
0
        private RedisSentinelWorker GetNextSentinel()
        {
            sentinelIndex++;

            if (sentinelIndex >= sentinels.Count)
            {
                sentinelIndex = 0;
            }

            var sentinelWorker = new RedisSentinelWorker(sentinels[sentinelIndex], this.sentinelName, this.clientManager);

            sentinelWorker.SentinelError += Worker_SentinelError;
            return sentinelWorker;
        }
        private void OnSentinelError(Exception ex)
        {
            if (this.worker != null)
            {
                Log.Error("Error on existing SentinelWorker, reconnecting...");

                if (OnWorkerError != null)
                {
                    OnWorkerError(ex);
                }

                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Raised if there is an error from a sentinel worker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Worker_SentinelError(object sender, EventArgs e)
        {
            var worker = sender as RedisSentinelWorker;

            if (worker != null)
            {
                // dispose the worker
                worker.SentinelError -= Worker_SentinelError;
                worker.Dispose();

                // get a new worker and start looking for more changes
                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
Exemplo n.º 11
0
        private RedisSentinelWorker GetNextSentinel()
        {
            sentinelIndex++;

            if (sentinelIndex >= sentinels.Count)
            {
                sentinelIndex = 0;
            }

            var sentinelWorker = new RedisSentinelWorker(this, sentinels[sentinelIndex], this.sentinelName)
            {
                OnSentinelError = OnSentinelError
            };

            return(sentinelWorker);
        }
Exemplo n.º 12
0
        public List <string> GetActiveSentinelHosts(IEnumerable <string> sentinelHosts)
        {
            var activeSentinelHosts = new List <string>();

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

                    var endpoint = sentinelHost.ToRedisEndpoint(defaultPort: RedisConfig.DefaultPortSentinel);
                    using (var sentinelWorker = new RedisSentinelWorker(this, endpoint))
                    {
                        if (!activeSentinelHosts.Contains(sentinelHost))
                        {
                            activeSentinelHosts.Add(sentinelHost);
                        }

                        var activeHosts = sentinelWorker.GetSentinelHosts(MasterName);
                        foreach (var activeHost in activeHosts)
                        {
                            if (!activeSentinelHosts.Contains(activeHost))
                            {
                                activeSentinelHosts.Add(SentinelHostFilter != null
                                    ? SentinelHostFilter(activeHost)
                                    : activeHost);
                            }
                        }
                    }

                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("All active Sentinels Found: " + string.Join(", ", activeSentinelHosts));
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Could not get active Sentinels from: {0}".Fmt(sentinelHost), ex);
                }
            }
            return(activeSentinelHosts);
        }
Exemplo n.º 13
0
        private RedisSentinelWorker GetValidSentinelWorker()
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            if (this.worker != null)
            {
                return(this.worker);
            }

            RedisException lastEx = null;

            while (this.worker == null && ShouldRetry())
            {
                try
                {
                    this.worker = GetNextSentinel();
                    GetRedisManager();
                    this.worker.BeginListeningForConfigurationChanges();
                    this.failures = 0; //reset
                    return(this.worker);
                }
                catch (RedisException ex)
                {
                    if (OnWorkerError != null)
                    {
                        OnWorkerError(ex);
                    }

                    lastEx      = ex;
                    this.worker = null;
                    this.failures++;
                    Interlocked.Increment(ref RedisState.TotalFailedSentinelWorkers);
                }
            }

            this.failures = 0; //reset
            Thread.Sleep(WaitBetweenFailedHosts);

            throw new RedisException("No Redis Sentinels were available", lastEx);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Raised if there is an error from a sentinel worker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSentinelError(Exception ex)
        {
            if (this.worker != null)
            {
                Log.Error("Error on existing SentinelWorker, reconnecting...");

                if (OnWorkerError != null)
                {
                    OnWorkerError(ex);
                }

                // dispose the worker
                this.worker.Dispose();

                // get a new worker and start looking for more changes
                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
Exemplo n.º 15
0
        private void GetValidSentinel()
        {
            while (this.clientManager == null && ShouldRetry())
            {
                try
                {
                    this.worker = GetNextSentinel();
                    this.clientManager = worker.GetClientManager();
                    this.worker.BeginListeningForConfigurationChanges();
                }
                catch (RedisException)
                {
                    if (this.worker != null)
                    {
                        this.worker.SentinelError -= Worker_SentinelError;
                        this.worker.Dispose();
                    }

                    this.failures++;
                }
            }
        }
Exemplo n.º 16
0
        private void GetValidSentinel()
        {
            while (this.clientManager == null && ShouldRetry())
            {
                try
                {
                    this.worker        = GetNextSentinel();
                    this.clientManager = worker.GetClientManager();
                    this.worker.BeginListeningForConfigurationChanges();
                }
                catch (RedisException)
                {
                    if (this.worker != null)
                    {
                        this.worker.SentinelError -= Worker_SentinelError;
                        this.worker.Dispose();
                    }

                    this.failures++;
                }
            }
        }
Exemplo n.º 17
0
        private void GetValidSentinel()
        {
            while (this.clientManager == null && failures < RedisSentinel.MaxFailures)
            {
                try
                {
                    worker        = GetNextSentinel();
                    clientManager = worker.GetClientManager();
                    worker.BeginListeningForConfigurationChanges();
                }
                catch (RedisException)
                {
                    if (worker != null)
                    {
                        worker.SentinelError -= Worker_SentinelError;
                        worker.Dispose();
                    }

                    failures++;
                }
            }
        }
Exemplo n.º 18
0
        private void GetValidSentinel()
        {
            while (this.clientManager == null && failures < RedisSentinel.MaxFailures)
            {
                try
                {
                    worker = GetNextSentinel();
                    clientManager = worker.GetClientManager();
                    worker.BeginListeningForConfigurationChanges();
                }
                catch (RedisException)
                {
                    if (worker != null)
                    {
                        worker.SentinelError -= Worker_SentinelError;
                        worker.Dispose();
                    }

                    failures++;
                }
            }
        }
Exemplo n.º 19
0
        private RedisSentinelWorker GetValidSentinel()
        {
            if (this.worker != null)
            {
                return(this.worker);
            }

            RedisException lastEx = null;

            while (this.RedisManager == null && ShouldRetry())
            {
                try
                {
                    this.worker       = GetNextSentinel();
                    this.RedisManager = worker.GetClientManager();
                    this.worker.BeginListeningForConfigurationChanges();
                    return(this.worker);
                }
                catch (RedisException ex)
                {
                    if (OnWorkerError != null)
                    {
                        OnWorkerError(ex);
                    }

                    lastEx = ex;
                    if (this.worker != null)
                    {
                        this.worker.Dispose();
                    }

                    this.failures++;
                }
            }

            throw new RedisException("RedisSentinel is not accessible", lastEx);
        }
Exemplo n.º 20
0
        private RedisSentinelWorker GetNextSentinel()
        {
            RedisSentinelWorker disposeWorker = null;

            try
            {
                lock (oLock)
                {
                    if (this.worker != null)
                    {
                        disposeWorker = this.worker;
                        this.worker   = null;
                    }

                    if (++sentinelIndex >= SentinelEndpoints.Length)
                    {
                        sentinelIndex = 0;
                    }

                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug($"Attempt to connect to next sentinel '{SentinelEndpoints[sentinelIndex]}'...");
                    }

                    var sentinelWorker = new RedisSentinelWorker(this, SentinelEndpoints[sentinelIndex])
                    {
                        OnSentinelError = OnSentinelError
                    };

                    return(sentinelWorker);
                }
            }
            finally
            {
                disposeWorker?.Dispose();
            }
        }
Exemplo n.º 21
0
 public void Dispose()
 {
     if (worker != null)
     {
         worker.SentinelError -= Worker_SentinelError;
         worker.Dispose();
         worker = null;
     }
 }
Exemplo n.º 22
0
 public void Dispose()
 {
     if (worker == null) return;
     
     worker.Dispose();
     worker = null;
 }
Exemplo n.º 23
0
        public List<string> GetActiveSentinelHosts(IEnumerable<string> sentinelHosts)
        {
            var activeSentinelHosts = new List<string>();
            foreach (var sentinelHost in sentinelHosts.ToArray())
            {
                try
                {
                    var endpoint = sentinelHost.ToRedisEndpoint(defaultPort: RedisConfig.DefaultPortSentinel);
                    using (var sentinelWorker = new RedisSentinelWorker(this, endpoint))
                    {
                        var activeHosts = sentinelWorker.GetSentinelHosts(MasterName);

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

                        foreach (var activeHost in activeHosts)
                        {
                            if (!activeSentinelHosts.Contains(activeHost))
                                activeSentinelHosts.Add(activeHost);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Could not get active Sentinels from: {0}".Fmt(sentinelHost), ex);
                }
            }
            return activeSentinelHosts;
        }
        private RedisSentinelWorker GetValidSentinel()
        {
            if (this.worker != null)
                return this.worker;

            RedisException lastEx = null;

            while (this.RedisManager == null && ShouldRetry())
            {
                try
                {
                    this.worker = GetNextSentinel();
                    this.RedisManager = worker.GetClientManager();
                    this.worker.BeginListeningForConfigurationChanges();
                    return this.worker;
                }
                catch (RedisException ex)
                {
                    if (OnWorkerError != null)
                        OnWorkerError(ex);

                    lastEx = ex;
                    if (this.worker != null)
                        this.worker.Dispose();

                    this.failures++;
                }
            }

            throw new RedisException("RedisSentinel is not accessible", lastEx);
        }
Exemplo n.º 25
0
        private void OnSentinelError(Exception ex)
        {
            if (this.worker != null)
            {
                Log.Error("Error on existing SentinelWorker, reconnecting...");

                if (OnWorkerError != null)
                    OnWorkerError(ex);

                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
 public void Dispose()
 {
     if (worker != null)
     {
         worker.Dispose();
         worker = null;
     }
 }
Exemplo n.º 27
0
        private RedisSentinelWorker GetNextSentinel()
        {
            lock (oLock)
            {
                if (this.worker != null)
                {
                    this.worker.Dispose();
                    this.worker = null;
                }

                if (++sentinelIndex >= SentinelEndpoints.Length)
                    sentinelIndex = 0;

                var sentinelWorker = new RedisSentinelWorker(this, SentinelEndpoints[sentinelIndex])
                {
                    OnSentinelError = OnSentinelError
                };

                return sentinelWorker;
            }
        }
Exemplo n.º 28
0
        public List<string> GetActiveSentinelHosts(IEnumerable<string> sentinelHosts)
        {
            var activeSentinelHosts = new List<string>();
            foreach (var sentinelHost in sentinelHosts.ToArray())
            {
                try
                {
                    if (Log.IsDebugEnabled)
                        Log.Debug("Connecting to all available Sentinels to discover Active Sentinel Hosts...");

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

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

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

                    if (Log.IsDebugEnabled)
                        Log.Debug("All active Sentinels Found: " + string.Join(", ", activeSentinelHosts));
                }
                catch (Exception ex)
                {
                    Log.Error("Could not get active Sentinels from: {0}".Fmt(sentinelHost), ex);
                }
            }
            return activeSentinelHosts;
        }
Exemplo n.º 29
0
        private RedisSentinelWorker GetValidSentinel()
        {
            if (this.worker != null)
                return this.worker;

            RedisException lastEx = null;

            while (this.worker == null && ShouldRetry())
            {
                try
                {
                    this.worker = GetNextSentinel();
                    GetRedisManager();
                    this.worker.BeginListeningForConfigurationChanges();
                    return this.worker;
                }
                catch (RedisException ex)
                {
                    if (OnWorkerError != null)
                        OnWorkerError(ex);

                    lastEx = ex;
                    this.failures++;
                    this.totalFailures++;
                }
            }

            this.failures = 0; //reset
            Thread.Sleep(WaitBetweenSentinelLookups);

            throw new RedisException("No Redis Sentinels were available", lastEx);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Raised if there is an error from a sentinel worker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Worker_SentinelError(object sender, EventArgs e)
        {
            var worker = sender as RedisSentinelWorker;

            if (worker != null)
            {
                // dispose the worker
                worker.SentinelError -= Worker_SentinelError;
                worker.Dispose();

                // get a new worker and start looking for more changes
                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
Exemplo n.º 31
0
        private void Worker_SentinelError(object sender, EventArgs e)
        {
            var worker = sender as RedisSentinelWorker;

            if (worker != null)
            {
                worker.SentinelError -= Worker_SentinelError;
                worker.Dispose();

                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
        private RedisSentinelWorker GetNextSentinel()
        {
            sentinelIndex++;

            if (sentinelIndex >= sentinels.Count)
                sentinelIndex = 0;

            var sentinelWorker = new RedisSentinelWorker(this, sentinels[sentinelIndex], this.masterName)
            {
                OnSentinelError = OnSentinelError
            };

            return sentinelWorker;
        }
        /// <summary>
        /// Raised if there is an error from a sentinel worker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSentinelError(Exception ex)
        {
            if (this.worker != null)
            {
                Log.Error("Error on existing SentinelWorker, reconnecting...");

                if (OnWorkerError != null)
                    OnWorkerError(ex);

                // dispose the worker
                this.worker.Dispose();

                // get a new worker and start looking for more changes
                this.worker = GetNextSentinel();
                this.worker.BeginListeningForConfigurationChanges();
            }
        }
Exemplo n.º 34
0
        private RedisSentinelWorker GetValidSentinelWorker()
        {
            if (isDisposed)
                throw new ObjectDisposedException(GetType().Name);

            if (this.worker != null)
                return this.worker;

            RedisException lastEx = null;

            while (this.worker == null && ShouldRetry())
            {
                try
                {
                    this.worker = GetNextSentinel();
                    GetRedisManager();
                    this.worker.BeginListeningForConfigurationChanges();
                    this.failures = 0; //reset
                    return this.worker;
                }
                catch (RedisException ex)
                {
                    if (OnWorkerError != null)
                        OnWorkerError(ex);

                    lastEx = ex;
                    this.worker = null;
                    this.failures++;
                    Interlocked.Increment(ref RedisState.TotalFailedSentinelWorkers);
                }
            }

            this.failures = 0; //reset
            Thread.Sleep(WaitBetweenFailedHosts);

            throw new RedisException("No Redis Sentinels were available", lastEx);
        }