[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6983] | 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.DataTransfer;
|
---|
| 26 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Services.Hive {
|
---|
| 29 | public class HeartbeatManager {
|
---|
| 30 | private IHiveDao dao {
|
---|
| 31 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
| 32 | }
|
---|
| 33 | private IAuthorizationManager auth {
|
---|
| 34 | get { return ServiceLocator.Instance.AuthorizationManager; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// This method will be called every time a slave sends a heartbeat (-> very often; concurrency is important!)
|
---|
| 39 | /// </summary>
|
---|
| 40 | /// <returns>a list of actions the slave should do</returns>
|
---|
| 41 | public List<MessageContainer> ProcessHeartbeat(Heartbeat heartbeat) {
|
---|
| 42 | List<MessageContainer> actions = new List<MessageContainer>();
|
---|
| 43 | Slave slave = dao.GetSlave(heartbeat.SlaveId);
|
---|
| 44 | if (slave == null) {
|
---|
| 45 | actions.Add(new MessageContainer(MessageContainer.MessageType.SayHello));
|
---|
[7187] | 46 | } else {
|
---|
| 47 | if (heartbeat.HbInterval != slave.HbInterval) {
|
---|
| 48 | actions.Add(new MessageContainer(MessageContainer.MessageType.NewHBInterval));
|
---|
[6983] | 49 | }
|
---|
[7187] | 50 |
|
---|
[6983] | 51 | // update slave data
|
---|
| 52 | slave.FreeCores = heartbeat.FreeCores;
|
---|
| 53 | slave.FreeMemory = heartbeat.FreeMemory;
|
---|
| 54 | slave.CpuUtilization = heartbeat.CpuUtilization;
|
---|
| 55 | slave.IsAllowedToCalculate = SlaveIsAllowedToCalculate(slave.Id);
|
---|
| 56 | slave.SlaveState = (heartbeat.JobProgress != null && heartbeat.JobProgress.Count > 0) ? SlaveState.Calculating : SlaveState.Idle;
|
---|
| 57 | slave.LastHeartbeat = DateTime.Now;
|
---|
| 58 | dao.UpdateSlave(slave);
|
---|
| 59 |
|
---|
| 60 | // update task data
|
---|
| 61 | actions.AddRange(UpdateTasks(heartbeat, slave.IsAllowedToCalculate));
|
---|
| 62 |
|
---|
| 63 | // assign new task
|
---|
| 64 | if (heartbeat.AssignJob && slave.IsAllowedToCalculate && heartbeat.FreeCores > 0) {
|
---|
| 65 | var availableJobs = dao.GetWaitingTasks(slave, 1);
|
---|
| 66 | if (availableJobs.Count() > 0) {
|
---|
| 67 | var job = availableJobs.First();
|
---|
| 68 | if (AssignJob(slave, job))
|
---|
| 69 | actions.Add(new MessageContainer(MessageContainer.MessageType.CalculateTask, job.Id));
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | return actions;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // returns true if assignment was successful
|
---|
| 77 | private bool AssignJob(Slave slave, Task task) {
|
---|
| 78 | // load task again and check if it is still available (this is an attempt to reduce the race condition which causes multiple heartbeats to get the same task assigned)
|
---|
| 79 | if (dao.GetTask(task.Id).State != TaskState.Waiting) return false;
|
---|
| 80 |
|
---|
| 81 | task = dao.UpdateTaskState(task.Id, DataAccess.TaskState.Transferring, slave.Id, null, null);
|
---|
| 82 |
|
---|
| 83 | // from now on the task has some time to send the next heartbeat (ApplicationConstants.TransferringJobHeartbeatTimeout)
|
---|
| 84 | task.LastHeartbeat = DateTime.Now;
|
---|
| 85 | dao.UpdateTask(task);
|
---|
| 86 | return true;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /// <summary>
|
---|
| 90 | /// Update the progress of each task
|
---|
| 91 | /// Checks if all the task sent by heartbeat are supposed to be calculated by this slave
|
---|
| 92 | /// </summary>
|
---|
| 93 | private IEnumerable<MessageContainer> UpdateTasks(Heartbeat heartbeat, bool IsAllowedToCalculate) {
|
---|
| 94 | List<MessageContainer> actions = new List<MessageContainer>();
|
---|
| 95 |
|
---|
| 96 | if (heartbeat.JobProgress == null)
|
---|
| 97 | return actions;
|
---|
| 98 |
|
---|
| 99 | if (!IsAllowedToCalculate && heartbeat.JobProgress.Count != 0) {
|
---|
| 100 | actions.Add(new MessageContainer(MessageContainer.MessageType.PauseAll));
|
---|
| 101 | } else {
|
---|
| 102 | // process the jobProgresses
|
---|
| 103 | foreach (var jobProgress in heartbeat.JobProgress) {
|
---|
| 104 | Task curJob = dao.GetTask(jobProgress.Key);
|
---|
| 105 | if (curJob == null) {
|
---|
| 106 | // task does not exist in db
|
---|
| 107 | actions.Add(new MessageContainer(MessageContainer.MessageType.AbortTask, jobProgress.Key));
|
---|
| 108 | DA.LogFactory.GetLogger(this.GetType().Namespace).Log("Job does not exist in DB: " + jobProgress.Key);
|
---|
| 109 | } else {
|
---|
| 110 | if (curJob.CurrentStateLog.SlaveId == Guid.Empty || curJob.CurrentStateLog.SlaveId != heartbeat.SlaveId) {
|
---|
| 111 | // assigned slave does not match heartbeat
|
---|
| 112 | actions.Add(new MessageContainer(MessageContainer.MessageType.AbortTask, curJob.Id));
|
---|
| 113 | DA.LogFactory.GetLogger(this.GetType().Namespace).Log("The slave " + heartbeat.SlaveId + " is not supposed to calculate Job: " + curJob);
|
---|
| 114 | } else if (!TaskIsAllowedToBeCalculatedBySlave(heartbeat.SlaveId, curJob)) {
|
---|
| 115 | // assigned resources ids of task do not match with slaveId (and parent resourceGroupIds); this might happen when slave is moved to different group
|
---|
| 116 | actions.Add(new MessageContainer(MessageContainer.MessageType.PauseTask, curJob.Id));
|
---|
| 117 | } else {
|
---|
| 118 | // save task execution time
|
---|
| 119 | curJob.ExecutionTime = jobProgress.Value;
|
---|
| 120 | curJob.LastHeartbeat = DateTime.Now;
|
---|
| 121 |
|
---|
| 122 | switch (curJob.Command) {
|
---|
| 123 | case Command.Stop:
|
---|
| 124 | actions.Add(new MessageContainer(MessageContainer.MessageType.StopTask, curJob.Id));
|
---|
| 125 | break;
|
---|
| 126 | case Command.Pause:
|
---|
| 127 | actions.Add(new MessageContainer(MessageContainer.MessageType.PauseTask, curJob.Id));
|
---|
| 128 | break;
|
---|
| 129 | case Command.Abort:
|
---|
| 130 | actions.Add(new MessageContainer(MessageContainer.MessageType.AbortTask, curJob.Id));
|
---|
| 131 | break;
|
---|
| 132 | }
|
---|
| 133 | dao.UpdateTask(curJob);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | return actions;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private bool TaskIsAllowedToBeCalculatedBySlave(Guid slaveId, Task curTask) {
|
---|
| 142 | var assignedResourceIds = dao.GetAssignedResources(curTask.Id).Select(x => x.Id);
|
---|
| 143 | var slaveResourceIds = dao.GetParentResources(slaveId).Select(x => x.Id);
|
---|
| 144 | return assignedResourceIds.Any(x => slaveResourceIds.Contains(x));
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | private bool SlaveIsAllowedToCalculate(Guid slaveId) {
|
---|
| 148 | // the slave may only calculate if there is no downtime right now. this needs to be checked for every parent resource also
|
---|
| 149 | return dao.GetParentResources(slaveId).All(r => dao.GetDowntimes(x => x.ResourceId == r.Id && (DateTime.Now >= x.StartDate) && (DateTime.Now <= x.EndDate)).Count() == 0);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|