コード例 #1
0
        public void Run()
        {
            LoggerManager.Instance.SetThreadContext(new LoggerContext()
            {
                ShardName = _context.LocalShardName != null ? _context.LocalShardName : "", DatabaseName = ""
            });
            _context.StatusLatch.WaitForAny(NodeStatus.Running);
            _startSignal.WaitOne();

            if (_context.ConfigurationSession != null)
            {
                ((OutProcConfigurationSession)_context.ConfigurationSession).RegisterListener(this);
            }

            while (_running)
            {
                try
                {
                    if (_node == null)
                    {
                        _clusterConfigMgr.UpdateClusterConfiguration();
                    }

                    if (_clusterConfigMgr != null)
                    {
                        ShardConfiguration sConfig = _clusterConfigMgr.GetShardConfiguration(_context.LocalShardName);
                        if (sConfig != null && sConfig.Servers != null)
                        {
                            _node = sConfig.Servers.GetServerNode(_context.LocalAddress.IpAddress.ToString());
                        }
                    }
                    if (_node == null && LoggerManager.Instance.ShardLogger != null &&
                        LoggerManager.Instance.ShardLogger.IsWarnEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Warn("NodeToCSHeartbeatTask.Run() ", "Node " + _context.LocalAddress.ToString() +
                                                                " is not part of the configuration.");
                        return;
                    }
                    OperationId lastOpId = null;
                    if (_membershipManager != null && _membershipManager.LatestMembership != null && _membershipManager.LatestMembership.Primary != null && _membershipManager.LatestMembership.Primary.Name.Equals(_context.LocalAddress.IpAddress.ToString()))
                    {
                        lastOpId = _membershipManager.GetLastOperationId;
                    }
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    _heartbeatInterval =
                        _context.ConfigurationSession.ReportHeartbeat(_context.ClusterName, _context.LocalShardName,
                                                                      _node, _membershipManager.LatestMembership, lastOpId) * 1000;
                    watch.Stop();
                    _csStatus = ConnectivityStatus.Connected;
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Debug("NodeToCSHeartbeatTask.Run() ", "Heartbeat sent to the CS at " +
                                                                 DateTime.Now.ToString() + " time taken to report heartbeat :" + watch.Elapsed.TotalSeconds);
                    }
                    if (_heartbeatInterval > 0 && (watch.Elapsed.TotalSeconds > _heartbeatInterval / 2))
                    {
                        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                        {
                            LoggerManager.Instance.ShardLogger.Error("NodeToCSHeartbeatTask.Run() ", "Heartbeat sent to the CS at " +
                                                                     DateTime.Now.ToString() + " time taken to report heartbeat :" + watch.Elapsed.TotalSeconds + " which is greater than half of the hb interval.");
                        }
                    }
                }
                catch (ThreadAbortException e)
                {
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled && _nodeToCSThread != null)
                    {
                        LoggerManager.Instance.ShardLogger.Error(_nodeToCSThread.Name, "Task aborted.");
                    }
                    break;
                }
                //the following should only be done when a connection exception occurs.
                catch (ChannelException e)
                {
                    if (LoggerManager.Instance.ShardLogger != null)
                    {
                        if (LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                        {
                            LoggerManager.Instance.ShardLogger.Error("NodeToCSHeartbeatTask.Run()  ", e.ToString());
                        }
                        if (LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                        {
                            LoggerManager.Instance.ShardLogger.Debug("NodeToCSHeartbeatTask.Run() ",
                                                                     "On CS disconnected process of the membership manager begins execution at " +
                                                                     DateTime.Now.ToString());
                        }
                    }

                    _csStatus = ConnectivityStatus.NotConnected;
                    _membershipManager.OnCSDisconnected();
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Debug("NodeToCSHeartbeatTask.Run() ", "The NodeToCS task paused at " +
                                                                 DateTime.Now.ToString());
                    }

                    //foreach (var cluster in ManagementServer.s_DbmNodeConfiguration.DbmClusters.ClustersConfigurations)
                    //{
                    //    string csIp = cluster.ConfigServers.Nodes[0].Name;
                    //    int csPort = cluster.ConfigServers.Nodes[0].Port;

                    //    BrokenConnectionInfo info = new BrokenConnectionInfo();
                    //    info.BrokenAddress = new Address(csIp, csPort);
                    //    info.SessionType = Common.Communication.SessionTypes.Management;

                    //    _connectionRestoration.RegisterListener(info, this,_context.LocalShardName);
                    //}


                    this.Pause();
                }
                catch (Exception e)
                {
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Error("NodeToCSHbTask.Run() General Exception: ",
                                                                 e.ToString());
                    }

                    if (e.Message.Contains("No configuration server is available to process the request"))
                    {
                        _csStatus = ConnectivityStatus.NotConnected;
                        _membershipManager.OnCSDisconnected();
                        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                        {
                            LoggerManager.Instance.ShardLogger.Debug("NodeToCSHeartbeatTask.Run() ", "The NodeToCS task paused at " +
                                                                     DateTime.Now.ToString());
                        }
                        this.Pause();
                    }
                }
                Stopwatch sleepWatch = new Stopwatch();
                sleepWatch.Start();
                Thread.Sleep(_heartbeatInterval);
                _startSignal.WaitOne();
                sleepWatch.Stop();

                if (sleepWatch.Elapsed.TotalMilliseconds > (_heartbeatInterval + 2000))
                {
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Debug("NodeToCSHeartbeatTask.Run() ", "CS task waited for  " + sleepWatch.Elapsed.TotalSeconds);
                    }
                }
            }
        }