Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.3/Manager/EventManager.cs @ 6721

Last change on this file since 6721 was 6721, checked in by ascheibe, 13 years ago

#1233 Review comments: renamed Job to Task

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Services.Hive.DataAccess;
26using DT = HeuristicLab.Services.Hive.DataTransfer;
27
28
29namespace HeuristicLab.Services.Hive {
30  /// <summary>
31  /// This class offers methods for cleaning up offline slaves and jobs
32  /// </summary>
33  public class EventManager : IEventManager {
34    private IHiveDao dao {
35      get { return ServiceLocator.Instance.HiveDao; }
36    }
37    private IAuthorizationManager auth {
38      get { return ServiceLocator.Instance.AuthorizationManager; }
39    }
40    private ILogger log {
41      get { return LogFactory.GetLogger(this.GetType().Namespace); }
42    }
43
44    public void Cleanup() {
45      log.Log("EventManager.Cleanup()");
46      SetTimeoutSlavesOffline();
47      SetTimeoutJobsWaiting();
48      FinishParentJobs();
49      UpdateStatistics();
50    }
51
52    private void UpdateStatistics() {
53      var slaves = dao.GetSlaves(x => x.SlaveState == SlaveState.Calculating || x.SlaveState == SlaveState.Idle);
54
55      var stats = new DataTransfer.Statistics();
56      stats.TimeStamp = DateTime.Now;
57      var slaveStats = new List<DT.SlaveStatistics>();
58      foreach (var slave in slaves) {
59        slaveStats.Add(new DT.SlaveStatistics() {
60          SlaveId = slave.Id,
61          Cores = slave.Cores.HasValue ? slave.Cores.Value : 0,
62          FreeCores = slave.FreeCores.HasValue ? slave.FreeCores.Value : 0,
63          Memory = slave.Memory.HasValue ? slave.Memory.Value : 0,
64          FreeMemory = slave.FreeMemory.HasValue ? slave.FreeMemory.Value : 0,
65          CpuUtilization = slave.CpuUtilization
66        });
67      }
68      stats.SlaveStatistics = slaveStats;
69      stats.UserStatistics = dao.GetUserStatistics();
70      dao.AddStatistics(stats);
71    }
72
73    /// <summary>
74    /// Searches for slaves which are timed out, puts them and their jobs offline
75    /// </summary>
76    private void SetTimeoutSlavesOffline() {
77      var slaves = dao.GetSlaves(x => x.SlaveState != SlaveState.Offline);
78      foreach (DT.Slave slave in slaves) {
79        if (!slave.LastHeartbeat.HasValue || (DateTime.Now - slave.LastHeartbeat.Value) > HeuristicLab.Services.Hive.Properties.Settings.Default.SlaveHeartbeatTimeout) {
80          slave.SlaveState = DT.SlaveState.Offline;
81          SetJobsWaiting(slave.Id);
82          dao.UpdateSlave(slave);
83        }
84      }
85    }
86
87    /// <summary>
88    /// Looks for parent jobs which have FinishWhenChildJobsFinished and set their state to finished
89    /// </summary>
90    private void FinishParentJobs() {
91      var parentJobsToFinish = dao.GetParentJobs(dao.GetResources(x => true).Select(x => x.Id), 0, true);
92      foreach (var job in parentJobsToFinish) {
93        dao.UpdateJobState(job.Id, TaskState.Finished, null, null, string.Empty);
94      }
95    }
96
97    private void SetJobsWaiting(Guid slaveId) {
98      var jobs = dao.GetJobs(x => x.State == TaskState.Calculating).Where(x => x.StateLog.Last().SlaveId == slaveId);
99      foreach (var j in jobs) {
100        DT.Task job = dao.UpdateJobState(j.Id, TaskState.Waiting, slaveId, null, "Slave timed out.");
101        job.Command = null;
102        dao.UpdateJob(job);
103      }
104    }
105
106    /// <summary>
107    /// Looks for jobs which have not sent heartbeats for some time and reschedules them for calculation
108    /// </summary>
109    private void SetTimeoutJobsWaiting() {
110      var jobs = dao.GetJobs(x => (x.State == TaskState.Calculating && (DateTime.Now - x.LastHeartbeat) > HeuristicLab.Services.Hive.Properties.Settings.Default.CalculatingJobHeartbeatTimeout)
111                               || (x.State == TaskState.Transferring && (DateTime.Now - x.LastHeartbeat) > HeuristicLab.Services.Hive.Properties.Settings.Default.TransferringJobHeartbeatTimeout));
112      foreach (var j in jobs) {
113        DT.Task job = dao.UpdateJobState(j.Id, TaskState.Waiting, null, null, "Slave timed out.");
114        job.Command = null;
115        dao.UpdateJob(job);
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.