예제 #1
0
        /// <summary>
        /// Timer callback for heartbeat
        /// </summary>
        /// <param name="state">unused state param</param>
        private void RunHeartbeat(object state)
        {
            // If the heartbeat interval needs to be ignored, do so now and return
            if (this.ignoreNextHeartbeatInterval)
            {
                this.ignoreNextHeartbeatInterval = false;
                return;
            }

            // If the timer is closed, return
            if (this.heartbeatTimer == null)
            {
                return;
            }

            bool isBrokerLoaded = false;
            bool pingSucceeded  = false;

            try
            {
                // Connect to the broker launcher
                IBrokerLauncher launcher = this.factory.GetBrokerLauncherClientForHeartbeat();

                if (this.supportNewPing)
                {
                    try
                    {
                        string result = launcher.PingBroker2(this.sessionId);

#if API
                        SessionBase.TraceSource.TraceEvent(TraceEventType.Verbose, 0, "[BrokerHeartbeatHelper] Ping succeeded. Result = {0}, ExpectedResult = {1}", result, this.expectPingResult);
#endif

                        pingSucceeded = true;
                        if (this.expectPingResult == null)
                        {
                            this.expectPingResult = result;
                        }

                        if (result == Constant.PingBroker2Result_BrokerNotExist ||
                            result != this.expectPingResult)
                        {
                            isBrokerLoaded = false;
                        }
                        else
                        {
                            isBrokerLoaded = true;
                        }
                    }
                    catch (ActionNotSupportedException)
                    {
                        if (this.expectPingResult == null)
                        {
                            this.supportNewPing = false;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                if (!this.supportNewPing)
                {
                    // Ping it
                    isBrokerLoaded = launcher.PingBroker(this.sessionId);
                    pingSucceeded  = true;
                }
            }
            catch (Exception e)
            {
#if API
                SessionBase.TraceSource.TraceEvent(TraceEventType.Warning, 0, "[BrokerHeartbeatHelper] Exception occured when pinging broker: {0}", e);
#elif WebAPI
                Microsoft.Hpc.RuntimeTrace.TraceHelper.TraceInfo(this.sessionId, "[BrokerHeartbeatHelper] Exception occured when pinging broker: {0}", e);
#endif

                // If the max missed heartbeats have been hit
                if (++this.missedHeartbeats == this.clientBrokerHeartbeatRetryCount)
                {
                    // Signal the broker is down
                    this.SendBrokerDownSignal(true);

                    // Shutdown the heartbeat
                    this.Stop();
                }
                else
                {
                    // Recreate the broker controller client so it can be used for next ping
                    this.factory.CloseBrokerLauncherClientForHeartbeat();
                }
            }

            // If ping call succeeded
            if (pingSucceeded)
            {
                // If broker isnt loaded
                if (!isBrokerLoaded)
                {
                    // Signal the broker is down
                    this.SendBrokerDownSignal(false);

                    // Shutdown the heartbeat
                    this.Stop();
                }
                else
                {
                    // Suceeded so reset missed heartbeat count
                    this.missedHeartbeats = 0;
                }
            }
        }