public object Clone()
        {
            BrokenConnectionEntry clone = new BrokenConnectionEntry();

            clone.ConnectionInfo = ConnectionInfo;
            clone.CRetryInfo     = CRetryInfo;
            clone.ConnectionRestorationListener = ConnectionRestorationListener;
            clone.Channel = Channel;
            return(clone);
        }
Example #2
0
        public void RegisterListener(BrokenConnectionInfo entryKey, IConnectionRestorationListener listener, string shardName)
        {
            LoggerManager.Instance.SetThreadContext(new LoggerContext()
            {
                ShardName = shardName != null ? shardName : "", DatabaseName = ""
            });
            BrokenConnectionEntry entry = new BrokenConnectionEntry();

            entry.CRetryInfo = new BrokenConnectionEntry.RetryInfo();
            entry.CRetryInfo.LastRetryTimestamp = DateTime.Now;
            entry.CRetryInfo.RetryInterval      = _basicRetryInterval;

            entry.ConnectionRestorationListener = listener;
            if (_context != null)
            {
                entry.Channel = new DualChannel(entryKey.BrokenAddress.IpAddress.ToString(), entryKey.BrokenAddress.Port, _context.LocalAddress.IpAddress.ToString(), entryKey.SessionType, new TraceProvider(), new ShardChannelFormatter());
            }

            entry.ConnectionInfo = entryKey;
            if (_brokenConnections == null)
            {
                _brokenConnections = new List <BrokenConnectionEntry>();
                this.Start();
            }
            lock (_mutex)
            {
                if (!_brokenConnections.Contains(entry))
                {
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Info("ConnRestorationMgr.RegisterListener()", entry.ToString());
                    }
                    _brokenConnections.Add(entry);
                }
                if (_brokenConnections.Count == 1 && IsPaused)
                {
                    this.Resume();
                }
            }
        }
Example #3
0
        private void UnregisterListener(BrokenConnectionEntry entry)
        {
            if (entry != null)
            {
                if (_brokenConnections != null)
                {
                    lock (_mutex)
                    {
                        _brokenConnections.Remove(entry);

                        if (_brokenConnections.Count == 0)
                        {
                            this.Pause();
                        }
                    }
                }
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled && entry.ConnectionInfo != null && entry.ConnectionInfo.BrokenAddress != null)
                {
                    LoggerManager.Instance.ShardLogger.Info("ConnRestorationMgr.UnregisterListener()", entry.ToString());
                }
            }
        }
 public bool Equals(BrokenConnectionEntry entry)
 {
     return(this.ConnectionInfo != null && entry != null && this.ConnectionInfo.Equals(entry.ConnectionInfo));
 }
Example #5
0
        public void Run()
        {
            try
            {
                LoggerManager.Instance.SetThreadContext(
                    new LoggerContext()
                {
                    ShardName    = _context.LocalShardName ?? "",
                    DatabaseName = ""
                });

                _startSignal.WaitOne();
                while (_running)
                {
                    try
                    {
                        if (_brokenConnections != null && _brokenConnections.Count > 0)
                        {
                            IList <BrokenConnectionEntry> brokenChannelsList = _brokenConnections.ToList();
                            for (int i = 0; i < brokenChannelsList.Count; i++)
                            {
                                BrokenConnectionEntry info = brokenChannelsList[i];

                                if (info != null && _brokenConnections.Contains(info))
                                {
                                    if (info.CRetryInfo != null && info.CRetryInfo.LastRetryTimestamp != null &&
                                        info.CRetryInfo.LastRetryTimestamp.AddSeconds(info.CRetryInfo.RetryInterval) <= DateTime.Now)
                                    {
                                        bool isConnected = false;

                                        try
                                        {
                                            if (info.Channel == null)
                                            {
                                                return;
                                            }
                                            isConnected = info.Channel.Connect(false);
                                        }
                                        catch (ChannelException ex)
                                        {
                                            if (info.CRetryInfo != null && info.CRetryInfo.Retries == 1 && LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsWarnEnabled)
                                            {
                                                LoggerManager.Instance.ShardLogger.Warn("ConRestoreMgr.Run()", info + " Error " + ex.ToString());
                                            }
                                        }
                                        if (isConnected && info.ConnectionRestorationListener != null)
                                        {
                                            try
                                            {
                                                info.ConnectionRestorationListener.OnConnectionRestoration(info.Channel);
                                                UnregisterListener(brokenChannelsList[i]);
                                                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled)
                                                {
                                                    LoggerManager.Instance.ShardLogger.Info("ConnRestorationMgr.Run()", "restored connection " + info);
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                                                {
                                                    LoggerManager.Instance.ShardLogger.Error("ConnRestoreMgr.Run()", info + " Error " + e.ToString());
                                                }
                                            }
                                        }
                                        else
                                        {
                                            //BrokenConnectionEntry cInfo = _brokenConnections[i];
                                            info.CRetryInfo.LastRetryTimestamp = DateTime.Now;
                                            info.CRetryInfo.Retries            = info.CRetryInfo.Retries + 1;
                                            double interval = info.CRetryInfo.RetryInterval + _basicRetryInterval;

                                            info.CRetryInfo.RetryInterval = (interval <= _maxRetryIntervalValue) ? interval : _basicRetryInterval;

                                            lock (_mutex)
                                            {
                                                if (_brokenConnections != null && _brokenConnections.Count > 0 &&
                                                    _brokenConnections.Contains(brokenChannelsList[i]))
                                                {
                                                    _brokenConnections[i] = info;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        Thread.Sleep(_poolingThreshold);

                        _startSignal.WaitOne();
                    }
                    catch (ThreadAbortException e)
                    {
                        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled && _connRestThread != null)
                        {
                            LoggerManager.Instance.ShardLogger.Error(_connRestThread.Name, "Task aborted.");
                        }
                        break;
                    }
                    catch (Exception e)
                    {
                        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                        {
                            LoggerManager.Instance.ShardLogger.Error("ConnRestoreMgr.Run()", e.ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("ConnRestoreMgr.Run()", e.ToString());
                }
            }
        }