Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/HeartbeatManager.cs @ 5404

Last change on this file since 5404 was 5404, checked in by cneumuel, 14 years ago

#1233

  • changed the workflow of aquireing a new job from server.
    • if a job is available for calculation, the slave receives the jobId already with the heartbeats. The job is then exclusively assigned to this slave.
  • extended the metainfo for a slave by OperatingSystem and CpuArchitecture
  • enhanced the way plugin-dependencies are discovered by using the types used by XmlGenerator. Now only mimimum amount of plugins are transferred.
  • selection of waiting jobs now consideres assigned slave-group
  • more unit tests for service
  • added unit tests for experiment manager
File size: 4.8 KB
RevLine 
[5105]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[5137]23using System.Collections.Generic;
[5105]24using System.Diagnostics;
25using System.Threading;
26using HeuristicLab.Common;
[5137]27using HeuristicLab.Services.Hive.Common;
[5105]28using HeuristicLab.Services.Hive.Common.DataTransfer;
29
[5314]30namespace HeuristicLab.Clients.Hive.Slave {
[5105]31  /// <summary>
32  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
33  /// </summary>
34  public class HeartbeatManager {
35    private static object locker = new object();
36    public TimeSpan Interval { get; set; }
37    private Thread heartBeatThread;
38    private AutoResetEvent waitHandle;
39
40    public HeartbeatManager() {
41      Interval = new TimeSpan(0, 0, 10);
42    }
43
44    public HeartbeatManager(TimeSpan interval) {
45      Interval = interval;
46    }
47
48    private WcfService wcfService;
49
[5137]50    private bool threadStopped;
[5105]51
52    ReaderWriterLockSlim heartBeatThreadIsSleepingLock = new ReaderWriterLockSlim();
[5137]53
[5105]54    /// <summary>
55    /// Starts the Heartbeat signal.
56    /// </summary>
57    public void StartHeartbeat() {
58      this.waitHandle = new AutoResetEvent(true);
[5137]59      wcfService = WcfService.Instance;
60      threadStopped = false;
[5105]61      heartBeatThread = new Thread(RunHeartBeatThread);
62      heartBeatThread.Start();
63    }
64
65    /// <summary>
66    /// Stop the heartbeat
67    /// </summary>
68    public void StopHeartBeat() {
[5137]69      threadStopped = true;
[5105]70      waitHandle.Set();
71      heartBeatThread.Join();
72    }
[5137]73
[5105]74    /// <summary>
75    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
76    /// </summary>
77    public void AwakeHeartBeatThread() {
[5137]78      if (!threadStopped)
79        waitHandle.Set();
[5105]80    }
81
82    private void RunHeartBeatThread() {
[5137]83      while (!threadStopped) {
[5105]84        try {
85          lock (locker) {
86            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
87              wcfService.Connect(ConfigManager.Instance.GetClientInfo()); // Login happens automatically upon successfull connection
88            }
[5137]89            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
[5105]90              HeuristicLab.Services.Hive.Common.DataTransfer.Slave info = ConfigManager.Instance.GetClientInfo();
91
[5137]92              Heartbeat heartBeatData = new Heartbeat {
[5375]93                SlaveId = info.Id, /*Settings.Default.Guid*/
[5105]94                FreeCores = info.Cores.HasValue ? info.Cores.Value - ConfigManager.Instance.GetUsedCores() : 0,
[5404]95                FreeMemory = ConfigManager.GetFreeMemory(),
[5137]96                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs()
[5105]97              };
[5137]98
[5156]99              SlaveClientCom.Instance.ClientCom.LogMessage("Sending Heartbeat: " + heartBeatData);
[5137]100              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
101
[5105]102              if (msgs == null) {
[5156]103                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from Heartbeat");
[5105]104                OnExceptionOccured(new Exception("Error getting response from Heartbeat"));
105              }
106
[5156]107              SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat Response received: ");
108              msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
[5137]109              msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
[5105]110            }
[5137]111          }
[5105]112        }
113        catch (Exception e) {
[5156]114          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat Thread failed badly: " + e.Message);
[5105]115          OnExceptionOccured(e);
116        }
117        waitHandle.WaitOne(this.Interval);
[5137]118      }
[5105]119      waitHandle.Close();
[5156]120      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
[5105]121    }
122
[5137]123
[5105]124    #region Eventhandler
125    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
126    private void OnExceptionOccured(Exception e) {
127      var handler = ExceptionOccured;
128      if (handler != null) handler(this, new EventArgs<Exception>(e));
129    }
130    #endregion
131  }
132}
Note: See TracBrowser for help on using the repository browser.