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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Data.SqlClient;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
28 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Hive.Server.LINQDataAccess {
|
---|
31 | public class JobDao : BaseDao<JobDto, Job>, IJobDao {
|
---|
32 |
|
---|
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 |
|
---|
46 | public IEnumerable<JobDto> FindWithLimitations(JobState jobState, int offset, int count) {
|
---|
47 | IQueryable<JobDto> query = null;
|
---|
48 | if (jobState == JobState.Finished) {
|
---|
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);
|
---|
53 | } else if (jobState == JobState.Calculating || jobState == JobState.SnapshotRequested || jobState == JobState.SnapshotSent) {
|
---|
54 | query = from job in Context.Jobs
|
---|
55 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
56 | orderby job.DateCalculated
|
---|
57 | select EntityToDto(job, null);
|
---|
58 | } else {
|
---|
59 | query = from job in Context.Jobs
|
---|
60 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
61 | orderby job.DateCreated
|
---|
62 | select EntityToDto(job, null);
|
---|
63 | }
|
---|
64 |
|
---|
65 | return query.Skip(offset).Take(count).ToList();
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
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);
|
---|
78 | CommitChanges();
|
---|
79 | bObj.Id = j.JobId;
|
---|
80 | return bObj;
|
---|
81 | }
|
---|
82 |
|
---|
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 |
|
---|
91 | public SerializedJob InsertWithAttachedJob(SerializedJob job) {
|
---|
92 | Job j = DtoToEntity(job.JobInfo, null);
|
---|
93 | j.SerializedJob = job.SerializedJobData;
|
---|
94 | //foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
|
---|
95 | // j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
|
---|
96 | Context.Jobs.InsertOnSubmit(j);
|
---|
97 | CommitChanges();
|
---|
98 | job.JobInfo.Id = j.JobId;
|
---|
99 | return job;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void Delete(Guid id) {
|
---|
103 | Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(id));
|
---|
104 | Context.Jobs.DeleteOnSubmit(job);
|
---|
105 | CommitChanges();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void Update(JobDto bObj) {
|
---|
109 | Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
|
---|
110 | DtoToEntity(bObj, job);
|
---|
111 | CommitChanges();
|
---|
112 | }
|
---|
113 |
|
---|
114 | public IEnumerable<JobDto> FindActiveJobsOfSlave(SlaveDto slave) {
|
---|
115 | return (from j in Context.Jobs
|
---|
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) ||
|
---|
119 | j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotSent) ||
|
---|
120 | j.JobState == Enum.GetName(typeof(JobState), JobState.WaitForChildJobs)) &&
|
---|
121 | (j.ResourceId.Equals(slave.Id))
|
---|
122 | select EntityToDto(j, null)).ToList();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public IEnumerable<JobDto> FindFittingJobs(JobState state, int freeCores, int freeMemory, Guid slaveId) {
|
---|
126 | SlaveGroupDao cgd = new SlaveGroupDao();
|
---|
127 |
|
---|
128 | List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
|
---|
129 | //Add myself too - enables jobs for one specific host!
|
---|
130 | idList.Add(slaveId);
|
---|
131 |
|
---|
132 | var q = (from ar in Context.AssignedResources
|
---|
133 | where ar.Job.JobState == Enum.GetName(typeof(JobState), state) &&
|
---|
134 | ar.Job.CoresNeeded <= freeCores &&
|
---|
135 | ar.Job.MemoryNeeded <= freeMemory &&
|
---|
136 | idList.Contains(ar.ResourceId)
|
---|
137 | orderby ar.Job.Priority descending
|
---|
138 | select EntityToDto(ar.Job, null));
|
---|
139 | return q.ToList();
|
---|
140 | }
|
---|
141 |
|
---|
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
|
---|
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
|
---|
157 | orderby ar.Job.Priority descending
|
---|
158 | select EntityToDto(ar.Job, null);
|
---|
159 | var list = query.ToList();
|
---|
160 | return list;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public IEnumerable<JobDto> GetJobsByState(JobState state) {
|
---|
164 | return (from j in Context.Jobs
|
---|
165 | where (j.JobState == Enum.GetName(typeof(JobState), state))
|
---|
166 | select EntityToDto(j, null)).ToList();
|
---|
167 | }
|
---|
168 |
|
---|
169 | public void AssignSlaveToJob(Guid slaveId, Guid jobId) {
|
---|
170 | Slave s = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
|
---|
171 | Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
|
---|
172 | s.Jobs.Add(j);
|
---|
173 | j.Slave = s;
|
---|
174 | CommitChanges();
|
---|
175 | }
|
---|
176 |
|
---|
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 |
|
---|
183 | public void SetJobOffline(JobDto job) {
|
---|
184 | Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
|
---|
185 | j.Slave = null;
|
---|
186 |
|
---|
187 | var childJobs = Context.Jobs.Where(child => child.ParentJobId == j.JobId);
|
---|
188 |
|
---|
189 | if (childJobs.Count() > 0) {
|
---|
190 | j.JobState = JobState.WaitForChildJobs.ToString();
|
---|
191 | } else {
|
---|
192 | j.JobState = JobState.Offline.ToString();
|
---|
193 | }
|
---|
194 |
|
---|
195 | CommitChanges();
|
---|
196 | }
|
---|
197 |
|
---|
198 | public Stream GetSerializedJobStream(Guid jobId) {
|
---|
199 | VarBinarySource source = new VarBinarySource((SqlConnection)Context.Connection, null, "Job", "SerializedJob", "JobId", jobId);
|
---|
200 | return new VarBinaryStream(source);
|
---|
201 | }
|
---|
202 |
|
---|
203 | public IEnumerable<JobDto> FindJobsById(IEnumerable<Guid> jobIds) {
|
---|
204 | IQueryable<JobDto> jobs = from job in Context.Jobs
|
---|
205 | where jobIds.Contains(job.JobId)
|
---|
206 | select EntityToDto(job, null);
|
---|
207 | return jobs.ToList();
|
---|
208 | }
|
---|
209 |
|
---|
210 | public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
|
---|
211 | var jobs = from job in Context.Jobs
|
---|
212 | where jobIds.Contains(job.JobId)
|
---|
213 | select job;
|
---|
214 | return jobs.All(job => job.UserId == userId);
|
---|
215 | }
|
---|
216 |
|
---|
217 | public IEnumerable<JobDto> FindJobsByParentId(Guid? parentJobId, bool recursive) {
|
---|
218 | IQueryable<JobDto> query = from job in Context.Jobs
|
---|
219 | where parentJobId == null ? !job.ParentJobId.HasValue : job.ParentJobId.Value == parentJobId
|
---|
220 | select EntityToDto(job, null);
|
---|
221 | List<JobDto> jobs = query.ToList();
|
---|
222 | if (recursive) {
|
---|
223 | List<JobDto> childs = new List<JobDto>();
|
---|
224 | foreach (JobDto job in jobs) {
|
---|
225 | childs.AddRange(FindJobsByParentId(job.Id, recursive));
|
---|
226 | }
|
---|
227 | jobs.AddRange(childs);
|
---|
228 | }
|
---|
229 | return jobs;
|
---|
230 | }
|
---|
231 |
|
---|
232 | #endregion
|
---|
233 |
|
---|
234 | public override Job DtoToEntity(JobDto source, Job target) {
|
---|
235 | if (source == null)
|
---|
236 | return null;
|
---|
237 | if (target == null)
|
---|
238 | target = new Job();
|
---|
239 |
|
---|
240 | target.CoresNeeded = source.CoresNeeded;
|
---|
241 | target.MemoryNeeded = source.MemoryNeeded;
|
---|
242 |
|
---|
243 | target.DateCalculated = source.DateCalculated;
|
---|
244 | target.DateCreated = source.DateCreated;
|
---|
245 | target.DateFinished = source.DateFinished;
|
---|
246 | target.JobId = source.Id;
|
---|
247 |
|
---|
248 | target.ExecutionTime = source.ExecutionTime;
|
---|
249 | target.Exception = source.Exception;
|
---|
250 |
|
---|
251 | target.Priority = source.Priority;
|
---|
252 | target.JobState = Enum.GetName(typeof(JobState), source.State);
|
---|
253 | target.UserId = source.UserId;
|
---|
254 | target.ParentJobId = source.ParentJobId;
|
---|
255 |
|
---|
256 | foreach (Guid assignRessourceId in source.AssignedResourceIds) {
|
---|
257 | if (!target.AssignedResources.Select(x => x.ResourceId).Contains(assignRessourceId)) {
|
---|
258 | target.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | return target;
|
---|
263 | }
|
---|
264 |
|
---|
265 | //Slave is not used ATM - not sure when we stopped using those...
|
---|
266 | public override JobDto EntityToDto(Job source, JobDto target) {
|
---|
267 | if (source == null)
|
---|
268 | return null;
|
---|
269 | if (target == null)
|
---|
270 | target = new JobDto();
|
---|
271 |
|
---|
272 | target.CoresNeeded = source.CoresNeeded;
|
---|
273 | target.MemoryNeeded = source.MemoryNeeded;
|
---|
274 |
|
---|
275 | target.DateCalculated = source.DateCalculated;
|
---|
276 | target.DateCreated = source.DateCreated;
|
---|
277 | target.DateFinished = source.DateFinished;
|
---|
278 | target.Id = source.JobId;
|
---|
279 |
|
---|
280 | target.Exception = source.Exception;
|
---|
281 | target.ExecutionTime = source.ExecutionTime.HasValue ? source.ExecutionTime.Value : TimeSpan.Zero;
|
---|
282 |
|
---|
283 | target.Priority = source.Priority;
|
---|
284 | target.State = (JobState)Enum.Parse(typeof(JobState), source.JobState, true);
|
---|
285 | target.UserId = source.UserId;
|
---|
286 | target.ParentJobId = source.ParentJobId;
|
---|
287 |
|
---|
288 | target.AssignedResourceIds = source.AssignedResources.Select(x => x.ResourceId).ToList();
|
---|
289 |
|
---|
290 | return target;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|