Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HeartbeatManager.cs @ 5636

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

#1233

  • updated jobstates documentation
  • enhanced ganttChart
  • fixed setting of jobstates
  • added option to force lifecycle-trigger (mainly for testing purposes)
File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Services.Hive.Common;
5using HeuristicLab.Services.Hive.Common.DataTransfer;
6using HeuristicLab.Tracing;
7
8namespace HeuristicLab.Services.Hive {
9  public class HeartbeatManager {
10    private DataAccess.IHiveDao dao {
11      get { return ServiceLocator.Instance.HiveDao; }
12    }
13    private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
14      get { return ServiceLocator.Instance.TransactionManager; }
15    }
16    private IAuthorizationManager auth {
17      get { return ServiceLocator.Instance.AuthorizationManager; }
18    }
19
20    /// <summary>
21    /// This method will be called every time a slave sends a heartbeat (-> very often; concurrency is important!)
22    /// </summary>
23    /// <returns>a list of actions the slave should do</returns>
24    public List<MessageContainer> ProcessHeartbeat(Heartbeat heartbeat) {
25      List<MessageContainer> actions = new List<MessageContainer>();
26      Slave slave = dao.GetSlave(heartbeat.SlaveId);
27      if (slave == null) {
28        actions.Add(new MessageContainer(MessageContainer.MessageType.SayHello));
29      } else {
30        // update slave data
31        slave.FreeCores = heartbeat.FreeCores;
32        slave.FreeMemory = heartbeat.FreeMemory;
33        slave.IsAllowedToCalculate = true; // Todo: look into calendar
34        slave.SlaveState = (heartbeat.JobProgress != null && heartbeat.JobProgress.Count > 0) ? SlaveState.Calculating : SlaveState.Idle;
35        slave.LastHeartbeat = DateTime.Now;
36        dao.UpdateSlave(slave);
37
38        // update job data
39        actions.AddRange(UpdateJobs(heartbeat));
40
41        // assign new job
42        if (heartbeat.AssignJob && this.IsAllowedToSendJobs() && slave.IsAllowedToCalculate && heartbeat.FreeCores > 0) {
43          var availableJobs = dao.GetWaitingJobs(slave, 1);
44          if (availableJobs.Count() > 0) {
45            var job = availableJobs.First();
46            actions.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob, job.Id));
47            AssignJob(slave, job);
48          }
49        }
50      }
51      return actions;
52    }
53
54    private void AssignJob(Slave slave, Job job) {
55      dao.UpdateJobState(job.Id, JobState.Transferring, slave.Id, null, null);
56      dao.UpdateSlave(slave);
57    }
58
59    /// <summary>
60    /// Update the progress of each job
61    /// Checks if all the jobs sent by heartbeat are supposed to be calculated by this slave
62    /// </summary>
63    private IEnumerable<MessageContainer> UpdateJobs(Heartbeat heartbeat) {
64      List<MessageContainer> actions = new List<MessageContainer>();
65
66      if (heartbeat.JobProgress == null)
67        return actions;
68
69      // process the jobProgresses
70      foreach (var jobProgress in heartbeat.JobProgress) {
71        Job curJob = dao.GetJob(jobProgress.Key);
72        if (curJob == null) {
73          // job does not exist in db
74          actions.Add(new MessageContainer(MessageContainer.MessageType.AbortJob, jobProgress.Key));
75          Logger.Error("Job does not exist in DB: " + jobProgress.Key);
76        } else {
77          if (curJob.CurrentStateLog.SlaveId == Guid.Empty || curJob.CurrentStateLog.SlaveId != heartbeat.SlaveId) {
78            // assigned slave does not match heartbeat
79            actions.Add(new MessageContainer(MessageContainer.MessageType.AbortJob, curJob.Id));
80            Logger.Error("The slave " + heartbeat.SlaveId + " is not supposed to calculate Job: " + curJob);
81          } else {
82            // save job execution time
83            curJob.ExecutionTime = jobProgress.Value;
84            curJob.LastHeartbeat = DateTime.Now;
85
86            if (curJob.State == JobState.Aborted) {
87              // a request to abort the job has been set
88              actions.Add(new MessageContainer(MessageContainer.MessageType.AbortJob, curJob.Id));
89            }
90            dao.UpdateJob(curJob);
91          }
92        }
93      }
94      return actions;
95    }
96
97    /// <summary>
98    /// Returns true if there are enough resources to send a job
99    /// There should not be too many jobs sent simultaniously
100    /// </summary>
101    private bool IsAllowedToSendJobs() {
102      return true; // JobsCurrentlyTransferring < ApplicationConstants.MaxJobTransferCount;
103      // Todo: see if unlimited job transfer count works. if not, look into db and count jobs in state Transferring
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.