source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/Manager/EventManager.cs @ 12768

Last change on this file since 12768 was 12768, checked in by dglaser, 6 years ago

#2388:

HeuristicLab.Services.Hive.DataAccess-3.3:

  • Removed old statistics tables
  • Updated SQL Scripts

HeuristicLab.Services.WebApp-3.3:
HeuristicLab.Services.WebApp.Status-3.3:
HeuristicLab.Services.WebApp.Statistics-3.3:

  • Minor changes
File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using HeuristicLab.Services.Hive.DataAccess;
25using HeuristicLab.Services.Hive.DataAccess.Interfaces;
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 task
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    private ITransactionManager trans {
44      get { return ServiceLocator.Instance.TransactionManager; }
45    }
46
47    public void Cleanup() {
48      trans.UseTransaction(() => {
49        SetTimeoutSlavesOffline();
50        SetTimeoutTasksWaiting();
51        DeleteObsoleteSlaves();
52      });
53
54      trans.UseTransaction(() => {
55        FinishParentTasks();
56      });
57    }
58
59    /// <summary>
60    /// Searches for slaves which are timed out, puts them and their task offline
61    /// </summary>
62    private void SetTimeoutSlavesOffline() {
63      var slaves = dao.GetSlaves(x => x.SlaveState != SlaveState.Offline);
64      foreach (DT.Slave slave in slaves) {
65        if (!slave.LastHeartbeat.HasValue || (DateTime.Now - slave.LastHeartbeat.Value) > HeuristicLab.Services.Hive.Properties.Settings.Default.SlaveHeartbeatTimeout) {
66          slave.SlaveState = DT.SlaveState.Offline;
67          dao.UpdateSlave(slave);
68        }
69      }
70    }
71
72    /// <summary>
73    /// Looks for parent tasks which have FinishWhenChildJobsFinished and set their state to finished
74    /// </summary>
75    private void FinishParentTasks() {
76      var parentTasksToFinish = dao.GetParentTasks(dao.GetResources(x => true).Select(x => x.Id), 0, true);
77      foreach (var task in parentTasksToFinish) {
78        dao.UpdateTaskState(task.Id, TaskState.Finished, null, null, string.Empty);
79      }
80    }
81
82    /// <summary>
83    /// Looks for task which have not sent heartbeats for some time and reschedules them for calculation
84    /// </summary>
85    private void SetTimeoutTasksWaiting() {
86      var tasks = dao.GetTasks(x => (x.State == TaskState.Calculating && (DateTime.Now - x.LastHeartbeat) > HeuristicLab.Services.Hive.Properties.Settings.Default.CalculatingJobHeartbeatTimeout)
87                               || (x.State == TaskState.Transferring && (DateTime.Now - x.LastHeartbeat) > HeuristicLab.Services.Hive.Properties.Settings.Default.TransferringJobHeartbeatTimeout));
88      foreach (var j in tasks) {
89        DT.Task task = dao.UpdateTaskState(j.Id, TaskState.Waiting, null, null, "Slave timed out.");
90        task.Command = null;
91        dao.UpdateTask(task);
92      }
93    }
94
95    /// <summary>
96    /// Searches for slaves that are disposable and deletes them if they were offline for too long
97    /// </summary>
98    private void DeleteObsoleteSlaves() {
99      var slaves = dao.GetSlaves(x => x.IsDisposable.GetValueOrDefault() &&
100                                      x.SlaveState == SlaveState.Offline &&
101                                      (DateTime.Now - x.LastHeartbeat) > Hive.Properties.Settings.Default.SweepInterval)
102                                .Select(x => x.Id)
103                                .ToArray();
104
105      foreach (Guid slaveId in slaves) {
106        var downtimesAvailable = dao.GetDowntimes(x => x.ResourceId == slaveId).Any();
107        if (!downtimesAvailable) {
108          dao.DeleteSlave(slaveId);
109        }
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.