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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
25 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.Manager {
|
---|
28 | public class EventManager : IEventManager {
|
---|
29 | private const string SlaveTimeout = "Slave timed out.";
|
---|
30 | private IPersistenceManager PersistenceManager {
|
---|
31 | get { return ServiceLocator.Instance.PersistenceManager; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void Cleanup() {
|
---|
35 | var pm = PersistenceManager;
|
---|
36 | // same transactions as the old EventManager
|
---|
37 | pm.UseTransaction(() => {
|
---|
38 | SetTimeoutSlavesOffline(pm);
|
---|
39 | SetTimeoutTasksWaiting(pm);
|
---|
40 | DeleteObsoleteSlaves(pm);
|
---|
41 | pm.SubmitChanges();
|
---|
42 | });
|
---|
43 |
|
---|
44 | pm.UseTransaction(() => {
|
---|
45 | FinishParentTasks(pm);
|
---|
46 | pm.SubmitChanges();
|
---|
47 | });
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Searches for slaves which are timed out, puts them and their task offline
|
---|
52 | /// </summary>
|
---|
53 | private void SetTimeoutSlavesOffline(IPersistenceManager pm) {
|
---|
54 | var slaveDao = pm.SlaveDao;
|
---|
55 | var slaves = slaveDao.GetOnlineSlaves();
|
---|
56 | foreach (var slave in slaves) {
|
---|
57 | if (!slave.LastHeartbeat.HasValue ||
|
---|
58 | (DateTime.Now - slave.LastHeartbeat.Value) >
|
---|
59 | HeuristicLab.Services.Hive.Properties.Settings.Default.SlaveHeartbeatTimeout) {
|
---|
60 | slave.SlaveState = SlaveState.Offline;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Looks for parent tasks which have FinishWhenChildJobsFinished and set their state to finished
|
---|
67 | /// </summary>
|
---|
68 | private void FinishParentTasks(IPersistenceManager pm) {
|
---|
69 | var resourceDao = pm.ResourceDao;
|
---|
70 | var taskDao = pm.TaskDao;
|
---|
71 | var resourceIds = resourceDao.GetAll().Select(x => x.ResourceId).ToList();
|
---|
72 | var parentTasksToFinish = taskDao.GetParentTasks(resourceIds, 0, true);
|
---|
73 | foreach (var task in parentTasksToFinish) {
|
---|
74 | task.State = TaskState.Finished;
|
---|
75 | task.StateLogs.Add(new StateLog {
|
---|
76 | State = task.State,
|
---|
77 | SlaveId = null,
|
---|
78 | UserId = null,
|
---|
79 | Exception = string.Empty,
|
---|
80 | DateTime = DateTime.Now
|
---|
81 | });
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /// <summary>
|
---|
86 | /// Looks for task which have not sent heartbeats for some time and reschedules them for calculation
|
---|
87 | /// </summary>
|
---|
88 | private void SetTimeoutTasksWaiting(IPersistenceManager pm) {
|
---|
89 | var taskDao = pm.TaskDao;
|
---|
90 | var tasks = taskDao.GetAll().Where(x => (x.State == TaskState.Calculating && (DateTime.Now - x.LastHeartbeat) > HeuristicLab.Services.Hive.Properties.Settings.Default.CalculatingJobHeartbeatTimeout)
|
---|
91 | || (x.State == TaskState.Transferring && (DateTime.Now - x.LastHeartbeat) > HeuristicLab.Services.Hive.Properties.Settings.Default.TransferringJobHeartbeatTimeout));
|
---|
92 | foreach (var task in tasks) {
|
---|
93 | task.State = TaskState.Waiting;
|
---|
94 | task.StateLogs.Add(new StateLog {
|
---|
95 | State = task.State,
|
---|
96 | SlaveId = null,
|
---|
97 | UserId = null,
|
---|
98 | Exception = SlaveTimeout,
|
---|
99 | DateTime = DateTime.Now
|
---|
100 | });
|
---|
101 | task.Command = null;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /// <summary>
|
---|
106 | /// Searches for slaves that are disposable and deletes them if they were offline for too long
|
---|
107 | /// </summary>
|
---|
108 | private void DeleteObsoleteSlaves(IPersistenceManager pm) {
|
---|
109 | var slaveDao = pm.SlaveDao;
|
---|
110 | var downtimeDao = pm.DowntimeDao;
|
---|
111 | var slaveIds = slaveDao.GetAll()
|
---|
112 | .Where(x => x.IsDisposable.GetValueOrDefault()
|
---|
113 | && x.SlaveState == SlaveState.Offline
|
---|
114 | && (DateTime.Now - x.LastHeartbeat) > Hive.Properties.Settings.Default.SweepInterval)
|
---|
115 | .Select(x => x.ResourceId)
|
---|
116 | .ToList();
|
---|
117 | foreach (var id in slaveIds) {
|
---|
118 | bool downtimesAvailable = downtimeDao.GetByResourceId(id).Any();
|
---|
119 | if (!downtimesAvailable) {
|
---|
120 | slaveDao.Delete(id);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|