Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4423 was 4423, checked in by cneumuel, 14 years ago
  • Refactored HL.Hive.Experiment. JobItems are not called HiveJobs and OptimizerJobs do not contain a hierarchy anymore.
  • Dynamic generation of jobs on a slave are not reflected on the client user interface.
  • Optimizer-Trees are now strictly synchronized with the HiveJob-Trees (also the ComputeInParallel property is taken into account when the Child HiveJobs are created)
  • Improved the way a class can report progress and lock the UI (IProgressReporter, IProgress, Progress, ProgressView)
  • Changes were made to the config-files, so that server and clients work with blade12.hpc.fh-hagenberg.at
  • Lots of small changes and bugfixes
File size: 10.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using System.Data.Linq;
7using HeuristicLab.Hive.Server.DataAccess;
8using System.IO;
9using System.Data.SqlClient;
10
11namespace 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(Guid id) {
84      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(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                    (from child in Context.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
136                     where child.ParentJobId == ar.Job.JobId
137                     select child).Count() > 0
138                  orderby ar.Job.Priority descending
139                  select EntityToDto(ar.Job, null);
140      var list = query.ToList();
141      return list;
142    }
143
144    public IEnumerable<JobDto> GetJobsByState(JobState state) {
145      return (from j in Context.Jobs
146              where (j.JobState == Enum.GetName(typeof(JobState), state))
147              select EntityToDto(j, null)).ToList();
148    }
149
150    public void AssignSlaveToJob(Guid slaveId, Guid jobId) {
151      Slave s = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
152      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
153      s.Jobs.Add(j);
154      j.Slave = s;
155      CommitChanges();
156    }
157
158    public void UnAssignSlaveToJob(Guid jobId) {
159      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
160      j.Slave = null;
161      CommitChanges();
162    }
163
164    public void SetJobOffline(JobDto job) {
165      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
166      j.Slave = null;
167      j.JobState = Enum.GetName(typeof(JobState), JobState.Offline);
168      CommitChanges();
169    }
170
171    public Stream GetSerializedJobStream(Guid jobId) {
172      VarBinarySource source = new VarBinarySource((SqlConnection)Context.Connection, null, "Job", "SerializedJob", "JobId", jobId);
173      return new VarBinaryStream(source);
174    }
175
176    public IEnumerable<JobDto> FindJobsById(IEnumerable<Guid> jobIds) {
177      IQueryable<JobDto> jobs = from job in Context.Jobs
178                                where jobIds.Contains(job.JobId)
179                                select EntityToDto(job, null);
180      return jobs.ToList();
181    }
182
183    public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
184      var jobs = from job in Context.Jobs
185                 where jobIds.Contains(job.JobId)
186                 select job;
187      return jobs.All(job => job.UserId == userId);
188    }
189
190    public IEnumerable<JobDto> FindJobsByParentId(Guid? parentJobId, bool recursive) {
191      IQueryable<JobDto> query = from job in Context.Jobs
192                                 where parentJobId == null ? !job.ParentJobId.HasValue : job.ParentJobId.Value == parentJobId
193                                 select EntityToDto(job, null);
194      List<JobDto> jobs = query.ToList();
195      if (recursive) {
196        List<JobDto> childs = new List<JobDto>();
197        foreach (JobDto job in jobs) {
198          childs.AddRange(FindJobsByParentId(job.Id, recursive));
199        }
200        jobs.AddRange(childs);
201      }
202      return jobs;
203    }
204
205    #endregion
206
207    public override Job DtoToEntity(JobDto source, Job target) {
208      if (source == null)
209        return null;
210      if (target == null)
211        target = new Job();
212
213      target.CoresNeeded = source.CoresNeeded;
214      target.MemoryNeeded = source.MemoryNeeded;
215
216      target.DateCalculated = source.DateCalculated;
217      target.DateCreated = source.DateCreated;
218      target.DateFinished = source.DateFinished;
219      target.JobId = source.Id;
220
221      target.ExecutionTime = source.ExecutionTime;
222      target.Exception = source.Exception;
223
224      target.Priority = source.Priority;
225      target.JobState = Enum.GetName(typeof(JobState), source.State);
226      target.UserId = source.UserId;
227      target.ParentJobId = source.ParentJobId;
228     
229      foreach (Guid assignRessourceId in source.AssignedResourceIds) {
230        if (!target.AssignedResources.Select(x => x.ResourceId).Contains(assignRessourceId)) {
231          target.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
232        }
233      }
234
235      return target;
236    }
237
238    //Slave is not used ATM - not sure when we stopped using those...
239    public override JobDto EntityToDto(Job source, JobDto target) {
240      if (source == null)
241        return null;
242      if (target == null)
243        target = new JobDto();
244
245      target.CoresNeeded = source.CoresNeeded;
246      target.MemoryNeeded = source.MemoryNeeded;
247
248      target.DateCalculated = source.DateCalculated;
249      target.DateCreated = source.DateCreated;
250      target.DateFinished = source.DateFinished;
251      target.Id = source.JobId;
252
253      target.Exception = source.Exception;
254      target.ExecutionTime = source.ExecutionTime.HasValue ? source.ExecutionTime.Value : TimeSpan.Zero;
255
256      target.Priority = source.Priority;
257      target.State = (JobState)Enum.Parse(typeof(JobState), source.JobState, true);
258      target.UserId = source.UserId;
259      target.ParentJobId = source.ParentJobId;
260     
261      target.AssignedResourceIds = source.AssignedResources.Select(x => x.ResourceId).ToList();
262
263      return target;
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.