[4424] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
[3011] | 23 | using System.Collections.Generic;
|
---|
[4424] | 24 | using System.Data.SqlClient;
|
---|
| 25 | using System.IO;
|
---|
[3011] | 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 28 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Hive.Server.LINQDataAccess {
|
---|
[4267] | 31 | public class JobDao : BaseDao<JobDto, Job>, IJobDao {
|
---|
| 32 |
|
---|
[3011] | 33 | #region IGenericDao<JobDto,Job> Members
|
---|
| 34 |
|
---|
| 35 | public JobDto FindById(Guid id) {
|
---|
| 36 | return (from job in Context.Jobs
|
---|
| 37 | where job.JobId.Equals(id)
|
---|
| 38 | select EntityToDto(job, null)).SingleOrDefault();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public IEnumerable<JobDto> FindAll() {
|
---|
| 42 | return (from job in Context.Jobs
|
---|
| 43 | select EntityToDto(job, null)).ToList();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[4264] | 46 | public IEnumerable<JobDto> FindWithLimitations(JobState jobState, int offset, int count) {
|
---|
[3578] | 47 | IQueryable<JobDto> query = null;
|
---|
[4264] | 48 | if (jobState == JobState.Finished) {
|
---|
[4267] | 49 | query = from job in Context.Jobs
|
---|
| 50 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
| 51 | orderby job.DateFinished
|
---|
| 52 | select EntityToDto(job, null);
|
---|
[4264] | 53 | } else if (jobState == JobState.Calculating || jobState == JobState.SnapshotRequested || jobState == JobState.SnapshotSent) {
|
---|
[3578] | 54 | query = from job in Context.Jobs
|
---|
[4267] | 55 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
| 56 | orderby job.DateCalculated
|
---|
| 57 | select EntityToDto(job, null);
|
---|
[3578] | 58 | } else {
|
---|
| 59 | query = from job in Context.Jobs
|
---|
[4267] | 60 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
| 61 | orderby job.DateCreated
|
---|
| 62 | select EntityToDto(job, null);
|
---|
[3578] | 63 | }
|
---|
| 64 |
|
---|
| 65 | return query.Skip(offset).Take(count).ToList();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[3011] | 69 | public byte[] GetBinaryJobFile(Guid jobId) {
|
---|
| 70 | return (from job in Context.Jobs
|
---|
| 71 | where job.JobId.Equals(jobId)
|
---|
| 72 | select job.SerializedJob).SingleOrDefault().ToArray();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public JobDto Insert(JobDto bObj) {
|
---|
| 76 | Job j = DtoToEntity(bObj, null);
|
---|
| 77 | Context.Jobs.InsertOnSubmit(j);
|
---|
[3578] | 78 | CommitChanges();
|
---|
[3011] | 79 | bObj.Id = j.JobId;
|
---|
| 80 | return bObj;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[3931] | 83 | public void SetBinaryJobFile(Guid jobId, byte[] data) {
|
---|
| 84 | Job j = (from job in Context.Jobs
|
---|
| 85 | where job.JobId.Equals(jobId)
|
---|
| 86 | select job).SingleOrDefault();
|
---|
| 87 | j.SerializedJob = data;
|
---|
| 88 | CommitChanges();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[3011] | 91 | public SerializedJob InsertWithAttachedJob(SerializedJob job) {
|
---|
| 92 | Job j = DtoToEntity(job.JobInfo, null);
|
---|
| 93 | j.SerializedJob = job.SerializedJobData;
|
---|
[4368] | 94 | //foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
|
---|
| 95 | // j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
|
---|
[3011] | 96 | Context.Jobs.InsertOnSubmit(j);
|
---|
[3578] | 97 | CommitChanges();
|
---|
[3011] | 98 | job.JobInfo.Id = j.JobId;
|
---|
| 99 | return job;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[4423] | 102 | public void Delete(Guid id) {
|
---|
| 103 | Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(id));
|
---|
[3011] | 104 | Context.Jobs.DeleteOnSubmit(job);
|
---|
[3578] | 105 | CommitChanges();
|
---|
[3011] | 106 | }
|
---|
| 107 |
|
---|
| 108 | public void Update(JobDto bObj) {
|
---|
| 109 | Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
|
---|
[4267] | 110 | DtoToEntity(bObj, job);
|
---|
| 111 | CommitChanges();
|
---|
[3011] | 112 | }
|
---|
| 113 |
|
---|
[4267] | 114 | public IEnumerable<JobDto> FindActiveJobsOfSlave(SlaveDto slave) {
|
---|
[3011] | 115 | return (from j in Context.Jobs
|
---|
[4267] | 116 | where (j.JobState == Enum.GetName(typeof(JobState), JobState.Calculating) ||
|
---|
| 117 | j.JobState == Enum.GetName(typeof(JobState), JobState.Aborted) ||
|
---|
| 118 | j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotRequested) ||
|
---|
[4368] | 119 | j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotSent) ||
|
---|
| 120 | j.JobState == Enum.GetName(typeof(JobState), JobState.WaitForChildJobs)) &&
|
---|
[4267] | 121 | (j.ResourceId.Equals(slave.Id))
|
---|
[3011] | 122 | select EntityToDto(j, null)).ToList();
|
---|
| 123 | }
|
---|
[4119] | 124 |
|
---|
[4368] | 125 | public IEnumerable<JobDto> FindFittingJobs(JobState state, int freeCores, int freeMemory, Guid slaveId) {
|
---|
[4267] | 126 | SlaveGroupDao cgd = new SlaveGroupDao();
|
---|
| 127 |
|
---|
| 128 | List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
|
---|
[3018] | 129 | //Add myself too - enables jobs for one specific host!
|
---|
[4267] | 130 | idList.Add(slaveId);
|
---|
| 131 |
|
---|
| 132 | var q = (from ar in Context.AssignedResources
|
---|
[4368] | 133 | where ar.Job.JobState == Enum.GetName(typeof(JobState), state) &&
|
---|
[3018] | 134 | ar.Job.CoresNeeded <= freeCores &&
|
---|
| 135 | ar.Job.MemoryNeeded <= freeMemory &&
|
---|
| 136 | idList.Contains(ar.ResourceId)
|
---|
[4267] | 137 | orderby ar.Job.Priority descending
|
---|
[3018] | 138 | select EntityToDto(ar.Job, null));
|
---|
| 139 | return q.ToList();
|
---|
[3011] | 140 | }
|
---|
| 141 |
|
---|
[4368] | 142 | public IEnumerable<JobDto> FindJobsWithFinishedChilds(Guid slaveId) {
|
---|
| 143 | SlaveGroupDao cgd = new SlaveGroupDao();
|
---|
| 144 |
|
---|
| 145 | List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
|
---|
| 146 | //Add myself too - enables jobs for one specific host!
|
---|
| 147 | idList.Add(slaveId);
|
---|
| 148 |
|
---|
| 149 | var query = from ar in Context.AssignedResources
|
---|
| 150 | where ar.Job.JobState == Enum.GetName(typeof(JobState), JobState.WaitForChildJobs) &&
|
---|
| 151 | (from child in Context.Jobs
|
---|
| 152 | where child.ParentJobId == ar.Job.JobId
|
---|
[4423] | 153 | select child.JobState == Enum.GetName(typeof(JobState), JobState.Finished)).All(x => x) &&
|
---|
| 154 | (from child in Context.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
|
---|
| 155 | where child.ParentJobId == ar.Job.JobId
|
---|
| 156 | select child).Count() > 0
|
---|
[4368] | 157 | orderby ar.Job.Priority descending
|
---|
| 158 | select EntityToDto(ar.Job, null);
|
---|
| 159 | var list = query.ToList();
|
---|
| 160 | return list;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[4264] | 163 | public IEnumerable<JobDto> GetJobsByState(JobState state) {
|
---|
[3011] | 164 | return (from j in Context.Jobs
|
---|
[4267] | 165 | where (j.JobState == Enum.GetName(typeof(JobState), state))
|
---|
[3011] | 166 | select EntityToDto(j, null)).ToList();
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[4267] | 169 | public void AssignSlaveToJob(Guid slaveId, Guid jobId) {
|
---|
[4368] | 170 | Slave s = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
|
---|
[3011] | 171 | Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
|
---|
[4368] | 172 | s.Jobs.Add(j);
|
---|
| 173 | j.Slave = s;
|
---|
[4267] | 174 | CommitChanges();
|
---|
[3011] | 175 | }
|
---|
| 176 |
|
---|
[4368] | 177 | public void UnAssignSlaveToJob(Guid jobId) {
|
---|
| 178 | Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
|
---|
| 179 | j.Slave = null;
|
---|
| 180 | CommitChanges();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[3011] | 183 | public void SetJobOffline(JobDto job) {
|
---|
| 184 | Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
|
---|
[4267] | 185 | j.Slave = null;
|
---|
| 186 | j.JobState = Enum.GetName(typeof(JobState), JobState.Offline);
|
---|
[3578] | 187 | CommitChanges();
|
---|
[3011] | 188 | }
|
---|
| 189 |
|
---|
| 190 | public Stream GetSerializedJobStream(Guid jobId) {
|
---|
[4267] | 191 | VarBinarySource source = new VarBinarySource((SqlConnection)Context.Connection, null, "Job", "SerializedJob", "JobId", jobId);
|
---|
[3011] | 192 | return new VarBinaryStream(source);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[4170] | 195 | public IEnumerable<JobDto> FindJobsById(IEnumerable<Guid> jobIds) {
|
---|
| 196 | IQueryable<JobDto> jobs = from job in Context.Jobs
|
---|
[4267] | 197 | where jobIds.Contains(job.JobId)
|
---|
| 198 | select EntityToDto(job, null);
|
---|
[4170] | 199 | return jobs.ToList();
|
---|
| 200 | }
|
---|
[4333] | 201 |
|
---|
[4368] | 202 | public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
|
---|
[4333] | 203 | var jobs = from job in Context.Jobs
|
---|
| 204 | where jobIds.Contains(job.JobId)
|
---|
| 205 | select job;
|
---|
| 206 | return jobs.All(job => job.UserId == userId);
|
---|
| 207 | }
|
---|
[4368] | 208 |
|
---|
| 209 | public IEnumerable<JobDto> FindJobsByParentId(Guid? parentJobId, bool recursive) {
|
---|
| 210 | IQueryable<JobDto> query = from job in Context.Jobs
|
---|
| 211 | where parentJobId == null ? !job.ParentJobId.HasValue : job.ParentJobId.Value == parentJobId
|
---|
| 212 | select EntityToDto(job, null);
|
---|
| 213 | List<JobDto> jobs = query.ToList();
|
---|
| 214 | if (recursive) {
|
---|
| 215 | List<JobDto> childs = new List<JobDto>();
|
---|
| 216 | foreach (JobDto job in jobs) {
|
---|
| 217 | childs.AddRange(FindJobsByParentId(job.Id, recursive));
|
---|
| 218 | }
|
---|
| 219 | jobs.AddRange(childs);
|
---|
| 220 | }
|
---|
| 221 | return jobs;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[3011] | 224 | #endregion
|
---|
| 225 |
|
---|
| 226 | public override Job DtoToEntity(JobDto source, Job target) {
|
---|
| 227 | if (source == null)
|
---|
| 228 | return null;
|
---|
| 229 | if (target == null)
|
---|
| 230 | target = new Job();
|
---|
| 231 |
|
---|
| 232 | target.CoresNeeded = source.CoresNeeded;
|
---|
| 233 | target.MemoryNeeded = source.MemoryNeeded;
|
---|
| 234 |
|
---|
| 235 | target.DateCalculated = source.DateCalculated;
|
---|
| 236 | target.DateCreated = source.DateCreated;
|
---|
[3578] | 237 | target.DateFinished = source.DateFinished;
|
---|
[3011] | 238 | target.JobId = source.Id;
|
---|
| 239 |
|
---|
[4423] | 240 | target.ExecutionTime = source.ExecutionTime;
|
---|
[4141] | 241 | target.Exception = source.Exception;
|
---|
[3011] | 242 |
|
---|
| 243 | target.Priority = source.Priority;
|
---|
[4264] | 244 | target.JobState = Enum.GetName(typeof(JobState), source.State);
|
---|
[4333] | 245 | target.UserId = source.UserId;
|
---|
[4423] | 246 | target.ParentJobId = source.ParentJobId;
|
---|
| 247 |
|
---|
[4368] | 248 | foreach (Guid assignRessourceId in source.AssignedResourceIds) {
|
---|
| 249 | if (!target.AssignedResources.Select(x => x.ResourceId).Contains(assignRessourceId)) {
|
---|
| 250 | target.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[3011] | 254 | return target;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[4267] | 257 | //Slave is not used ATM - not sure when we stopped using those...
|
---|
[3011] | 258 | public override JobDto EntityToDto(Job source, JobDto target) {
|
---|
| 259 | if (source == null)
|
---|
| 260 | return null;
|
---|
[4267] | 261 | if (target == null)
|
---|
[3011] | 262 | target = new JobDto();
|
---|
[4267] | 263 |
|
---|
[3011] | 264 | target.CoresNeeded = source.CoresNeeded;
|
---|
| 265 | target.MemoryNeeded = source.MemoryNeeded;
|
---|
| 266 |
|
---|
| 267 | target.DateCalculated = source.DateCalculated;
|
---|
| 268 | target.DateCreated = source.DateCreated;
|
---|
[3578] | 269 | target.DateFinished = source.DateFinished;
|
---|
[3011] | 270 | target.Id = source.JobId;
|
---|
[4368] | 271 |
|
---|
[4141] | 272 | target.Exception = source.Exception;
|
---|
[4423] | 273 | target.ExecutionTime = source.ExecutionTime.HasValue ? source.ExecutionTime.Value : TimeSpan.Zero;
|
---|
[4267] | 274 |
|
---|
[3011] | 275 | target.Priority = source.Priority;
|
---|
[4267] | 276 | target.State = (JobState)Enum.Parse(typeof(JobState), source.JobState, true);
|
---|
[4333] | 277 | target.UserId = source.UserId;
|
---|
[4423] | 278 | target.ParentJobId = source.ParentJobId;
|
---|
| 279 |
|
---|
[4368] | 280 | target.AssignedResourceIds = source.AssignedResources.Select(x => x.ResourceId).ToList();
|
---|
| 281 |
|
---|
[3011] | 282 | return target;
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | }
|
---|