[5405] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Services.Hive.Common;
|
---|
| 5 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Services.Hive {
|
---|
| 8 | public class HeartbeatManager {
|
---|
| 9 | private DataAccess.IHiveDao dao {
|
---|
| 10 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
| 11 | }
|
---|
| 12 | private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
|
---|
| 13 | get { return ServiceLocator.Instance.TransactionManager; }
|
---|
| 14 | }
|
---|
| 15 | private IAuthorizationManager auth {
|
---|
| 16 | get { return ServiceLocator.Instance.AuthorizationManager; }
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | /// <summary>
|
---|
| 20 | /// This method will be called every time a slave sends a heartbeat (-> very often; concurrency is important!)
|
---|
| 21 | /// </summary>
|
---|
| 22 | /// <returns>a list of actions the slave should do</returns>
|
---|
| 23 | public List<MessageContainer> ProcessHeartbeat(Heartbeat heartbeat) {
|
---|
| 24 | List<MessageContainer> actions = new List<MessageContainer>();
|
---|
| 25 | Slave slave = dao.GetSlave(heartbeat.SlaveId);
|
---|
| 26 | if (slave == null) {
|
---|
| 27 | actions.Add(new MessageContainer(MessageContainer.MessageType.SayHello));
|
---|
| 28 | } else {
|
---|
| 29 | // update slave data
|
---|
| 30 | slave.FreeCores = heartbeat.FreeCores;
|
---|
| 31 | slave.FreeMemory = heartbeat.FreeMemory;
|
---|
| 32 | slave.IsAllowedToCalculate = true; // Todo: look into calendar
|
---|
| 33 | slave.SlaveState = (heartbeat.JobProgress != null && heartbeat.JobProgress.Count > 0) ? SlaveState.Calculating : SlaveState.Idle;
|
---|
| 34 | slave.LastHeartbeat = DateTime.Now;
|
---|
| 35 | dao.UpdateSlave(slave);
|
---|
| 36 |
|
---|
| 37 | // update job data
|
---|
| 38 | actions.AddRange(UpdateJobs(heartbeat));
|
---|
| 39 |
|
---|
| 40 | // assign new job
|
---|
[5779] | 41 | if (heartbeat.AssignJob && slave.IsAllowedToCalculate && heartbeat.FreeCores > 0) {
|
---|
[5405] | 42 | var availableJobs = dao.GetWaitingJobs(slave, 1);
|
---|
| 43 | if (availableJobs.Count() > 0) {
|
---|
| 44 | var job = availableJobs.First();
|
---|
| 45 | actions.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob, job.Id));
|
---|
| 46 | AssignJob(slave, job);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | return actions;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private void AssignJob(Slave slave, Job job) {
|
---|
[5786] | 54 | job = dao.UpdateJobState(job.Id, JobState.Transferring, slave.Id, null, null);
|
---|
[5405] | 55 | dao.UpdateSlave(slave);
|
---|
[5786] | 56 |
|
---|
| 57 | // from now on the job has some time to send the next heartbeat (ApplicationConstants.TransferringJobHeartbeatTimeout)
|
---|
| 58 | job.LastHeartbeat = DateTime.Now;
|
---|
| 59 | dao.UpdateJob(job);
|
---|
[5405] | 60 | }
|
---|
| 61 |
|
---|
| 62 | /// <summary>
|
---|
| 63 | /// Update the progress of each job
|
---|
| 64 | /// Checks if all the jobs sent by heartbeat are supposed to be calculated by this slave
|
---|
| 65 | /// </summary>
|
---|
| 66 | private IEnumerable<MessageContainer> UpdateJobs(Heartbeat heartbeat) {
|
---|
| 67 | List<MessageContainer> actions = new List<MessageContainer>();
|
---|
| 68 |
|
---|
| 69 | if (heartbeat.JobProgress == null)
|
---|
| 70 | return actions;
|
---|
| 71 |
|
---|
| 72 | // process the jobProgresses
|
---|
| 73 | foreach (var jobProgress in heartbeat.JobProgress) {
|
---|
| 74 | Job curJob = dao.GetJob(jobProgress.Key);
|
---|
| 75 | if (curJob == null) {
|
---|
| 76 | // job does not exist in db
|
---|
| 77 | actions.Add(new MessageContainer(MessageContainer.MessageType.AbortJob, jobProgress.Key));
|
---|
[5718] | 78 | LogFactory.GetLogger(this.GetType().Namespace).Log("Job does not exist in DB: " + jobProgress.Key);
|
---|
[5405] | 79 | } else {
|
---|
[5511] | 80 | if (curJob.CurrentStateLog.SlaveId == Guid.Empty || curJob.CurrentStateLog.SlaveId != heartbeat.SlaveId) {
|
---|
[5405] | 81 | // assigned slave does not match heartbeat
|
---|
| 82 | actions.Add(new MessageContainer(MessageContainer.MessageType.AbortJob, curJob.Id));
|
---|
[5718] | 83 | LogFactory.GetLogger(this.GetType().Namespace).Log("The slave " + heartbeat.SlaveId + " is not supposed to calculate Job: " + curJob);
|
---|
[5405] | 84 | } else {
|
---|
| 85 | // save job execution time
|
---|
| 86 | curJob.ExecutionTime = jobProgress.Value;
|
---|
| 87 | curJob.LastHeartbeat = DateTime.Now;
|
---|
| 88 |
|
---|
[5779] | 89 | switch (curJob.Command) {
|
---|
| 90 | case Command.Stop:
|
---|
| 91 | actions.Add(new MessageContainer(MessageContainer.MessageType.StopJob, curJob.Id));
|
---|
| 92 | break;
|
---|
| 93 | case Command.Pause:
|
---|
| 94 | actions.Add(new MessageContainer(MessageContainer.MessageType.PauseJob, curJob.Id));
|
---|
| 95 | break;
|
---|
| 96 | case Command.Abort:
|
---|
| 97 | actions.Add(new MessageContainer(MessageContainer.MessageType.AbortJob, curJob.Id));
|
---|
| 98 | break;
|
---|
[5405] | 99 | }
|
---|
| 100 | dao.UpdateJob(curJob);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | return actions;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|