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