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