Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/LifecycleManager.cs @ 6419

Last change on this file since 6419 was 6367, checked in by cneumuel, 13 years ago

#1233

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