Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6347 was 6267, checked in by cneumuel, 14 years ago

#1233

  • extended statistics recording:
    • execution times of users are captured
    • execution times and start-to-finish time of finished jobs is captured (to computer hive overhead)
    • data of deleted jobs is automatically captured in DeletedJobStatistics
  • changed ExecutionTime type in database from string to float (milliseconds are stored instead of TimeSpan.ToString())
  • added IsPrivileged field to job to indicate if it should be executed in a privileged sandbox
  • added CpuUtilization field to slave to be able to report cpu utilization
  • added GetJobsByResourceId to retrieve all jobs which are currently beeing calculated in a slave(-group)
  • TransactionManager now allows to use serializable tranactions (used for lifecycle trigger)
File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Services.Hive.Common;
5using HeuristicLab.Services.Hive.Common.DataTransfer;
6
7namespace HeuristicLab.Services.Hive {
8  /// <summary>
9  /// This class offers methods for cleaning up offline slaves and jobs
10  /// </summary>
11  public class LifecycleManager : ILifecycleManager {
12    private DataAccess.IHiveDao dao {
13      get { return ServiceLocator.Instance.HiveDao; }
14    }
15    private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
16      get { return ServiceLocator.Instance.TransactionManager; }
17    }
18    private IAuthorizationManager auth {
19      get { return ServiceLocator.Instance.AuthorizationManager; }
20    }
21    private ILogger log {
22      get { return LogFactory.GetLogger(this.GetType().Namespace); }
23    }
24
25    public void Cleanup() {
26      log.Log("LifecycleManager.Cleanup()");
27      SetTimeoutSlavesOffline();
28      SetTimeoutJobsWaiting();
29      FinishParentJobs();
30      UpdateStatistics();
31    }
32
33    private void UpdateStatistics() {
34      var slaves = dao.GetSlaves(x => x.SlaveState == SlaveState.Calculating || x.SlaveState == SlaveState.Idle);
35
36      var stats = new Statistics();
37      stats.TimeStamp = DateTime.Now;
38      var slaveStats = new List<SlaveStatistics>();
39      foreach (var slave in slaves) {
40        slaveStats.Add(new SlaveStatistics() {
41          SlaveId = slave.Id,
42          Cores = slave.Cores.HasValue ? slave.Cores.Value : 0,
43          FreeCores = slave.FreeCores.HasValue ? slave.FreeCores.Value : 0,
44          Memory = slave.Memory.HasValue ? slave.Memory.Value : 0,
45          FreeMemory = slave.FreeMemory.HasValue ? slave.FreeMemory.Value : 0,
46          CpuUtilization = slave.CpuUtilization
47        });
48      }
49      stats.SlaveStatistics = slaveStats;
50
51      stats.UserStatistics = dao.GetUserStatistics();
52
53      dao.AddStatistics(stats);
54    }
55
56    /// <summary>
57    /// Searches for slaves which are timed out, puts them and their jobs offline
58    /// </summary>
59    private void SetTimeoutSlavesOffline() {
60      var slaves = dao.GetSlaves(x => x.SlaveState != SlaveState.Offline);
61      foreach (Slave slave in slaves) {
62        if (!slave.LastHeartbeat.HasValue || (DateTime.Now - slave.LastHeartbeat.Value) > ApplicationConstants.SlaveHeartbeatTimeout) {
63          slave.SlaveState = SlaveState.Offline;
64          SetJobsWaiting(slave.Id);
65          dao.UpdateSlave(slave);
66        }
67      }
68    }
69
70    /// <summary>
71    /// Looks for parent jobs which have FinishWhenChildJobsFinished and set their state to finished
72    /// </summary>
73    private void FinishParentJobs() {
74      var parentJobsToFinish = dao.GetParentJobs(dao.GetResources(x => true).Select(x => x.Id), 0, true);
75      foreach (var job in parentJobsToFinish) {
76        dao.UpdateJobState(job.Id, JobState.Finished, null, null, string.Empty);
77      }
78    }
79
80    private void SetJobsWaiting(Guid slaveId) {
81      var jobs = dao.GetJobs(x => x.State == JobState.Calculating).Where(x => x.StateLog.Last().SlaveId == slaveId);
82      foreach (var j in jobs) {
83        Job job = dao.UpdateJobState(j.Id, JobState.Waiting, slaveId, null, "Slave timed out.");
84        job.Command = null;
85        dao.UpdateJob(job);
86      }
87    }
88
89    /// <summary>
90    /// Looks for jobs which have not sent heartbeats for some time and reschedules them for calculation
91    /// </summary>
92    private void SetTimeoutJobsWaiting() {
93      var jobs = dao.GetJobs(x => (x.State == JobState.Calculating && (DateTime.Now - x.LastHeartbeat) > ApplicationConstants.CalculatingJobHeartbeatTimeout)
94                               || (x.State == JobState.Transferring && (DateTime.Now - x.LastHeartbeat) > ApplicationConstants.TransferringJobHeartbeatTimeout));
95      foreach (var j in jobs) {
96        Job job = dao.UpdateJobState(j.Id, JobState.Waiting, null, null, "Slave timed out.");
97        job.Command = null;
98        dao.UpdateJob(job);
99      }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.