Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs @ 5404

Last change on this file since 5404 was 5404, checked in by cneumuel, 13 years ago

#1233

  • changed the workflow of aquireing a new job from server.
    • if a job is available for calculation, the slave receives the jobId already with the heartbeats. The job is then exclusively assigned to this slave.
  • extended the metainfo for a slave by OperatingSystem and CpuArchitecture
  • enhanced the way plugin-dependencies are discovered by using the types used by XmlGenerator. Now only mimimum amount of plugins are transferred.
  • selection of waiting jobs now consideres assigned slave-group
  • more unit tests for service
  • added unit tests for experiment manager
File size: 16.6 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.Linq;
25using System.Linq.Expressions;
26
27namespace HeuristicLab.Services.Hive.DataAccess {
28  using HeuristicLab.Services.Hive.Common.DataTransfer;
29  using HeuristicLab.Services.Hive.DataAccess.Properties;
30  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
31
32  public class HiveDao : IHiveDao {
33    public static HiveDataContext CreateContext() {
34      return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
35    }
36
37    public HiveDao() {
38    }
39
40    #region Job Methods
41    public DT.Job GetJob(Guid id) {
42      using (var db = CreateContext()) {
43        return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
44      }
45    }
46
47    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
48      using (var db = CreateContext()) {
49        return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
50      }
51    }
52
53    public Guid AddJob(DT.Job dto) {
54      using (var db = CreateContext()) {
55        var entity = Convert.ToEntity(dto);
56        db.Jobs.InsertOnSubmit(entity);
57        db.SubmitChanges();
58        foreach (Guid pluginId in dto.PluginsNeededIds) {
59          db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
60        }
61        db.SubmitChanges();
62        return entity.JobId;
63      }
64    }
65
66    public void UpdateJob(DT.Job dto) {
67      using (var db = CreateContext()) {
68        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
69        if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
70        else Convert.ToEntity(dto, entity);
71        // todo: update required plugins
72        db.SubmitChanges();
73      }
74    }
75
76    public void DeleteJob(Guid id) {
77      using (var db = CreateContext()) {
78        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
79        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
80        db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
81      }
82    }
83
84    public IEnumerable<DT.Job> GetWaitingParentJobs(IEnumerable<Guid> resourceIds, int count) {
85      using (var db = CreateContext()) {
86        var query = from ar in db.AssignedResources
87                    where resourceIds.Contains(ar.ResourceId)
88                       && ar.Job.JobState == JobState.WaitingForChildJobs
89                       && (from child in db.Jobs
90                           where child.ParentJobId == ar.Job.JobId
91                           select child.JobState == JobState.Finished).All(x => x)
92                       && (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
93                           where child.ParentJobId == ar.Job.JobId
94                           select child).Count() > 0
95                    orderby ar.Job.Priority descending
96                    select Convert.ToDto(ar.Job);
97        return count == 0 ? query.ToArray() : query.Take(count).ToArray();
98      }
99    }
100
101    public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count) {
102      using (var db = CreateContext()) {
103        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
104        var waitingParentJobs = GetWaitingParentJobs(resourceIds, count);
105        if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
106
107        var query = from ar in db.AssignedResources
108                    where resourceIds.Contains(ar.ResourceId)
109                       && ar.Job.JobState == JobState.Waiting
110                       && ar.Job.CoresNeeded <= slave.FreeCores
111                       && ar.Job.MemoryNeeded <= slave.FreeMemory
112                    orderby ar.Job.Priority descending
113                    select Convert.ToDto(ar.Job);
114        var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
115        return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
116      }
117    }
118    #endregion
119
120    #region JobData Methods
121
122    public DT.JobData GetJobData(Guid id) {
123      using (var db = CreateContext()) {
124        return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
125      }
126    }
127
128    public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
129      using (var db = CreateContext()) {
130        return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
131      }
132    }
133
134    public Guid AddJobData(DT.JobData dto) {
135      using (var db = CreateContext()) {
136        var entity = Convert.ToEntity(dto);
137        db.JobDatas.InsertOnSubmit(entity);
138        db.SubmitChanges();
139        return entity.JobId;
140      }
141    }
142
143    public void UpdateJobData(DT.JobData dto) {
144      using (var db = CreateContext()) {
145        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
146        if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
147        else Convert.ToEntity(dto, entity);
148        db.SubmitChanges();
149      }
150    }
151
152    public void DeleteJobData(Guid id) {
153      using (var db = CreateContext()) {
154        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
155        if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
156        db.SubmitChanges();
157      }
158    }
159    #endregion
160
161    #region HiveExperiment Methods
162    public DT.HiveExperiment GetHiveExperiment(Guid id) {
163      using (var db = CreateContext()) {
164        return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
165      }
166    }
167
168    public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
169      using (var db = CreateContext()) {
170        return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
171      }
172    }
173
174    public Guid AddHiveExperiment(DT.HiveExperiment dto) {
175      using (var db = CreateContext()) {
176        var entity = Convert.ToEntity(dto);
177        db.HiveExperiments.InsertOnSubmit(entity);
178        db.SubmitChanges();
179        return entity.HiveExperimentId;
180      }
181    }
182
183    public void UpdateHiveExperiment(DT.HiveExperiment dto) {
184      using (var db = CreateContext()) {
185        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
186        if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
187        else Convert.ToEntity(dto, entity);
188        db.SubmitChanges();
189      }
190    }
191
192    public void DeleteHiveExperiment(Guid id) {
193      using (var db = CreateContext()) {
194        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
195        if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
196        db.SubmitChanges();
197      }
198    }
199    #endregion
200
201    #region Plugin Methods
202    public DT.Plugin GetPlugin(Guid id) {
203      using (var db = CreateContext()) {
204        return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
205      }
206    }
207
208    public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
209      using (var db = CreateContext()) {
210        return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
211      }
212    }
213
214    public Guid AddPlugin(DT.Plugin dto) {
215      using (var db = CreateContext()) {
216        var entity = Convert.ToEntity(dto);
217        db.Plugins.InsertOnSubmit(entity);
218        db.SubmitChanges();
219        return entity.PluginId;
220      }
221    }
222
223    public void UpdatePlugin(DT.Plugin dto) {
224      using (var db = CreateContext()) {
225        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
226        if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
227        else Convert.ToEntity(dto, entity);
228        db.SubmitChanges();
229      }
230    }
231
232    public void DeletePlugin(Guid id) {
233      using (var db = CreateContext()) {
234        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
235        if (entity != null) db.Plugins.DeleteOnSubmit(entity);
236        db.SubmitChanges();
237      }
238    }
239    #endregion
240
241    #region PluginData Methods
242
243    public DT.PluginData GetPluginData(Guid id) {
244      using (var db = CreateContext()) {
245        return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
246      }
247    }
248
249    public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
250      using (var db = CreateContext()) {
251        return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
252      }
253    }
254
255    public Guid AddPluginData(DT.PluginData dto) {
256      using (var db = CreateContext()) {
257        var entity = Convert.ToEntity(dto);
258        db.PluginDatas.InsertOnSubmit(entity);
259        db.SubmitChanges();
260        return entity.PluginDataId;
261      }
262    }
263
264    public void UpdatePluginData(DT.PluginData dto) {
265      using (var db = CreateContext()) {
266        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
267        if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
268        else Convert.ToEntity(dto, entity);
269        db.SubmitChanges();
270      }
271    }
272
273    public void DeletePluginData(Guid id) {
274      using (var db = CreateContext()) {
275        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id); // todo: check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
276        if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
277        db.SubmitChanges();
278      }
279    }
280    #endregion
281
282    #region Slave Methods
283    public DT.Slave GetSlave(Guid id) {
284      using (var db = CreateContext()) {
285        return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
286      }
287    }
288
289    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
290      using (var db = CreateContext()) {
291        return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
292      }
293    }
294
295    public Guid AddSlave(DT.Slave dto) {
296      using (var db = CreateContext()) {
297        var entity = Convert.ToEntity(dto);
298        db.Resources.InsertOnSubmit(entity);
299        db.SubmitChanges();
300        return entity.ResourceId;
301      }
302    }
303
304    public void UpdateSlave(DT.Slave dto) {
305      using (var db = CreateContext()) {
306        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
307        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
308        else Convert.ToEntity(dto, entity);
309        db.SubmitChanges();
310      }
311    }
312
313    public void DeleteSlave(Guid id) {
314      using (var db = CreateContext()) {
315        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
316        if (entity != null) db.Resources.DeleteOnSubmit(entity);
317        db.SubmitChanges();
318      }
319    }
320    #endregion
321
322    #region SlaveGroup Methods
323    public DT.SlaveGroup GetSlaveGroup(Guid id) {
324      using (var db = CreateContext()) {
325        return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
326      }
327    }
328
329    public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
330      using (var db = CreateContext()) {
331        return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
332      }
333    }
334
335    public Guid AddSlaveGroup(DT.SlaveGroup dto) {
336      using (var db = CreateContext()) {
337        if (dto.Id == Guid.Empty)
338          dto.Id = Guid.NewGuid();
339        var entity = Convert.ToEntity(dto);
340        db.Resources.InsertOnSubmit(entity);
341        db.SubmitChanges();
342        return entity.ResourceId;
343      }
344    }
345
346    public void UpdateSlaveGroup(DT.SlaveGroup dto) {
347      using (var db = CreateContext()) {
348        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
349        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
350        else Convert.ToEntity(dto, entity);
351        db.SubmitChanges();
352      }
353    }
354
355    public void DeleteSlaveGroup(Guid id) {
356      using (var db = CreateContext()) {
357        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
358        if (entity != null) {
359          if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
360            throw new DaoException("Cannot delete SlaveGroup as long as there are Slaves in the group");
361          }
362          db.Resources.DeleteOnSubmit(entity);
363        }
364        db.SubmitChanges();
365      }
366    }
367    #endregion
368
369    #region Resource Methods
370    public DT.Resource GetResource(Guid id) {
371      using (var db = CreateContext()) {
372        return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
373      }
374    }
375
376    public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
377      using (var db = CreateContext()) {
378        return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
379      }
380    }
381
382    public Guid AddResource(DT.Resource dto) {
383      using (var db = CreateContext()) {
384        var entity = Convert.ToEntity(dto);
385        db.Resources.InsertOnSubmit(entity);
386        db.SubmitChanges();
387        return entity.ResourceId;
388      }
389    }
390
391    public void UpdateResource(DT.Resource dto) {
392      using (var db = CreateContext()) {
393        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
394        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
395        else Convert.ToEntity(dto, entity);
396        db.SubmitChanges();
397      }
398    }
399
400    public void DeleteResource(Guid id) {
401      using (var db = CreateContext()) {
402        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
403        if (entity != null) db.Resources.DeleteOnSubmit(entity);
404        db.SubmitChanges();
405      }
406    }
407
408    public void AssignJobToResource(Guid jobId, Guid resourceId) {
409      using (var db = CreateContext()) {
410        var job = db.Jobs.Where(x => x.JobId == jobId).Single();
411        job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
412        db.SubmitChanges();
413      }
414    }
415
416    public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
417      using (var db = CreateContext()) {
418        var job = db.Jobs.Where(x => x.JobId == jobId).Single();
419        return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
420      }
421    }
422
423    /// <summary>
424    /// Returns all parent resources of a resource (the given resource is also added)
425    /// </summary>
426    private IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
427      using (var db = CreateContext()) {
428        var resources = new List<Resource>();
429        CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
430        return resources.Select(r => Convert.ToDto(r)).ToArray();
431      }
432    }
433
434    private void CollectParentResources(List<Resource> resources, Resource resource) {
435      if (resource == null) return;
436      resources.Add(resource);
437      CollectParentResources(resources, resource.ParentResource);
438    }
439    #endregion
440
441    #region Authorization Methods
442    public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
443      using (var db = CreateContext()) {
444        var userIds = from job in db.Jobs // this needs to be fast!
445                      where jobIds.Contains(job.JobId)
446                      select job.UserId;
447        return userIds.All(x => x == userId);
448      }
449    }
450    #endregion
451  }
452}
Note: See TracBrowser for help on using the repository browser.