Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/JobDao.cs @ 5093

Last change on this file since 5093 was 5093, checked in by cneumuel, 14 years ago

#1260

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