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
RevLine 
[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
22using System;
[3011]23using System.Collections.Generic;
[4424]24using System.Data.SqlClient;
25using System.IO;
[3011]26using System.Linq;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Server.DataAccess;
29
30namespace 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() {
[5093]42      return (from job in Context.Jobs select EntityToDto(job, null)).ToArray();
[3011]43    }
44
[4264]45    public IEnumerable<JobDto> FindWithLimitations(JobState jobState, int offset, int count) {
[3578]46      IQueryable<JobDto> query = null;
[4264]47      if (jobState == JobState.Finished) {
[4267]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);
[4264]52      } else if (jobState == JobState.Calculating || jobState == JobState.SnapshotRequested || jobState == JobState.SnapshotSent) {
[3578]53        query = from job in Context.Jobs
[4267]54                where job.JobState == Enum.GetName(typeof(JobState), jobState)
55                orderby job.DateCalculated
56                select EntityToDto(job, null);
[3578]57      } else {
58        query = from job in Context.Jobs
[4267]59                where job.JobState == Enum.GetName(typeof(JobState), jobState)
60                orderby job.DateCreated
61                select EntityToDto(job, null);
[3578]62      }
63
64      return query.Skip(offset).Take(count).ToList();
65    }
66
67
[3011]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);
[3578]77      CommitChanges();
[3011]78      bObj.Id = j.JobId;
79      return bObj;
80    }
81
[3931]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
[3011]90    public SerializedJob InsertWithAttachedJob(SerializedJob job) {
91      Job j = DtoToEntity(job.JobInfo, null);
92      j.SerializedJob = job.SerializedJobData;
[4368]93      //foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
94      //  j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
[3011]95      Context.Jobs.InsertOnSubmit(j);
[3578]96      CommitChanges();
[3011]97      job.JobInfo.Id = j.JobId;
98      return job;
99    }
100
[4423]101    public void Delete(Guid id) {
102      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(id));
[3011]103      Context.Jobs.DeleteOnSubmit(job);
[3578]104      CommitChanges();
[3011]105    }
106
107    public void Update(JobDto bObj) {
108      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
[4267]109      DtoToEntity(bObj, job);
110      CommitChanges();
[3011]111    }
112
[4267]113    public IEnumerable<JobDto> FindActiveJobsOfSlave(SlaveDto slave) {
[3011]114      return (from j in Context.Jobs
[4267]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) ||
[4368]118                     j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotSent) ||
119                     j.JobState == Enum.GetName(typeof(JobState), JobState.WaitForChildJobs)) &&
[4267]120                    (j.ResourceId.Equals(slave.Id))
[3011]121              select EntityToDto(j, null)).ToList();
122    }
[4119]123
[4368]124    public IEnumerable<JobDto> FindFittingJobs(JobState state, int freeCores, int freeMemory, Guid slaveId) {
[4267]125      SlaveGroupDao cgd = new SlaveGroupDao();
126
127      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
[3018]128      //Add myself too - enables jobs for one specific host!
[4267]129      idList.Add(slaveId);
130
131      var q = (from ar in Context.AssignedResources
[4368]132               where ar.Job.JobState == Enum.GetName(typeof(JobState), state) &&
[3018]133                     ar.Job.CoresNeeded <= freeCores &&
134                     ar.Job.MemoryNeeded <= freeMemory &&
135                     idList.Contains(ar.ResourceId)
[4267]136               orderby ar.Job.Priority descending
[3018]137               select EntityToDto(ar.Job, null));
138      return q.ToList();
[3011]139    }
140
[4368]141    public IEnumerable<JobDto> FindJobsWithFinishedChilds(Guid slaveId) {
142      SlaveGroupDao cgd = new SlaveGroupDao();
[5093]143
[4368]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
[5093]152                     select
[5000]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) &&
[4423]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
[4368]160                  orderby ar.Job.Priority descending
161                  select EntityToDto(ar.Job, null);
162      var list = query.ToList();
163      return list;
164    }
165
[4264]166    public IEnumerable<JobDto> GetJobsByState(JobState state) {
[3011]167      return (from j in Context.Jobs
[4267]168              where (j.JobState == Enum.GetName(typeof(JobState), state))
[3011]169              select EntityToDto(j, null)).ToList();
170    }
171
[4267]172    public void AssignSlaveToJob(Guid slaveId, Guid jobId) {
[4368]173      Slave s = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
[3011]174      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
[4368]175      s.Jobs.Add(j);
176      j.Slave = s;
[4267]177      CommitChanges();
[3011]178    }
179
[4368]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
[3011]186    public void SetJobOffline(JobDto job) {
187      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
[4267]188      j.Slave = null;
[4772]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
[3578]198      CommitChanges();
[3011]199    }
200
201    public Stream GetSerializedJobStream(Guid jobId) {
[4267]202      VarBinarySource source = new VarBinarySource((SqlConnection)Context.Connection, null, "Job", "SerializedJob", "JobId", jobId);
[3011]203      return new VarBinaryStream(source);
204    }
205
[4170]206    public IEnumerable<JobDto> FindJobsById(IEnumerable<Guid> jobIds) {
207      IQueryable<JobDto> jobs = from job in Context.Jobs
[4267]208                                where jobIds.Contains(job.JobId)
209                                select EntityToDto(job, null);
[4170]210      return jobs.ToList();
211    }
[4333]212
[4368]213    public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
[4333]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    }
[4368]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
[3011]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;
[3578]248      target.DateFinished = source.DateFinished;
[3011]249      target.JobId = source.Id;
250
[5037]251      target.ExecutionTime = source.ExecutionTime.ToString();
[4141]252      target.Exception = source.Exception;
[3011]253
254      target.Priority = source.Priority;
[4264]255      target.JobState = Enum.GetName(typeof(JobState), source.State);
[4333]256      target.UserId = source.UserId;
[4423]257      target.ParentJobId = source.ParentJobId;
258     
[5093]259
[4368]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
[3011]266      return target;
267    }
268
[4267]269    //Slave is not used ATM - not sure when we stopped using those...
[3011]270    public override JobDto EntityToDto(Job source, JobDto target) {
271      if (source == null)
272        return null;
[4267]273      if (target == null)
[3011]274        target = new JobDto();
[4267]275
[3011]276      target.CoresNeeded = source.CoresNeeded;
277      target.MemoryNeeded = source.MemoryNeeded;
278
279      target.DateCalculated = source.DateCalculated;
280      target.DateCreated = source.DateCreated;
[3578]281      target.DateFinished = source.DateFinished;
[3011]282      target.Id = source.JobId;
[4368]283
[4141]284      target.Exception = source.Exception;
[5037]285      TimeSpan ts;
286      if (!TimeSpan.TryParse(source.ExecutionTime, out ts)) ts = TimeSpan.Zero;
287      target.ExecutionTime = ts;
[4267]288
[3011]289      target.Priority = source.Priority;
[4267]290      target.State = (JobState)Enum.Parse(typeof(JobState), source.JobState, true);
[4333]291      target.UserId = source.UserId;
[4423]292      target.ParentJobId = source.ParentJobId;
293     
[4368]294      target.AssignedResourceIds = source.AssignedResources.Select(x => x.ResourceId).ToList();
295
[3011]296      return target;
297    }
298  }
299}
Note: See TracBrowser for help on using the repository browser.