Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs @ 9257

Last change on this file since 9257 was 9257, checked in by ascheibe, 11 years ago

#2019

  • added missing transactions in the Hive service
  • split scheduling transaction into smaller transactions
  • improved speed of job uploading (AddTask)
  • changed highest isolation level from Serializable to RepeatableRead as phantom reads shouldn't be a problem
File size: 41.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
26using DT = HeuristicLab.Services.Hive.DataTransfer;
27
28namespace HeuristicLab.Services.Hive.DataAccess {
29  public class HiveDao : IHiveDao {
30    public static HiveDataContext CreateContext(bool longRunning = false) {
31      var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
32      if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
33      return context;
34    }
35
36    public HiveDao() { }
37
38    #region Task Methods
39    public DT.Task GetTask(Guid id) {
40      using (var db = CreateContext()) {
41        return DT.Convert.ToDto(db.Tasks.SingleOrDefault(x => x.TaskId == id));
42      }
43    }
44
45    public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) {
46      using (var db = CreateContext()) {
47        return db.Tasks.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
48      }
49    }
50
51    public IEnumerable<DT.LightweightTask> GetLightweightTasks(Expression<Func<Task, bool>> predicate) {
52      List<DT.LightweightTask> tasks = new List<DT.LightweightTask>();
53
54      using (var db = CreateContext()) {
55        var tasksQuery = db.Tasks.Where(predicate).Select(task => new { task.TaskId, task.ExecutionTimeMs, task.ParentTaskId, task.StateLogs, task.State, task.Command });
56        var taskDatasQuery = db.Tasks.Where(predicate).Where(task => task.JobData != null).Select(task => new { task.TaskId, task.JobData.LastUpdate });
57
58        foreach (var task in tasksQuery) {
59          DT.LightweightTask t = new DT.LightweightTask();
60          t.Id = task.TaskId;
61          t.ExecutionTime = TimeSpan.FromMilliseconds(task.ExecutionTimeMs);
62          t.ParentTaskId = task.ParentTaskId;
63          t.StateLog = task.StateLogs == null ? new List<DT.StateLog>() : task.StateLogs.Select(x => DataTransfer.Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList();
64          t.State = DataTransfer.Convert.ToDto(task.State);
65          t.Command = DataTransfer.Convert.ToDto(task.Command);
66          t.LastTaskDataUpdate = taskDatasQuery.Where(x => x.TaskId == task.TaskId).Count() > 0 ? taskDatasQuery.Select(x => x.LastUpdate).First() : DateTime.MinValue;
67          tasks.Add(t);
68        }
69      }
70      return tasks;
71    }
72
73    public IEnumerable<DT.LightweightTask> GetLightweightTasksWithoutStateLog(Expression<Func<Task, bool>> predicate) {
74      List<DT.LightweightTask> tasks = new List<DT.LightweightTask>();
75
76      using (var db = CreateContext()) {
77        var tasksQuery = db.Tasks.Where(predicate).Select(task => new { task.TaskId, task.ExecutionTimeMs, task.ParentTaskId, task.State, task.Command });
78        var taskDatasQuery = db.Tasks.Where(predicate).Where(task => task.JobData != null).Select(task => new { task.TaskId, task.JobData.LastUpdate });
79
80        foreach (var task in tasksQuery) {
81          DT.LightweightTask t = new DT.LightweightTask();
82          t.Id = task.TaskId;
83          t.ExecutionTime = TimeSpan.FromMilliseconds(task.ExecutionTimeMs);
84          t.ParentTaskId = task.ParentTaskId;
85          t.StateLog = new List<DT.StateLog>();
86          t.State = DataTransfer.Convert.ToDto(task.State);
87          t.Command = DataTransfer.Convert.ToDto(task.Command);
88          t.LastTaskDataUpdate = taskDatasQuery.Where(x => x.TaskId == task.TaskId).Count() > 0 ? taskDatasQuery.Select(x => x.LastUpdate).First() : DateTime.MinValue;
89          tasks.Add(t);
90        }
91      }
92      return tasks;
93    }
94
95    public Guid AddTask(DT.Task dto) {
96      using (var db = CreateContext()) {
97        var entity = DT.Convert.ToEntity(dto);
98        db.Tasks.InsertOnSubmit(entity);
99        db.SubmitChanges();
100        foreach (Guid pluginId in dto.PluginsNeededIds) {
101          db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { TaskId = entity.TaskId, PluginId = pluginId });
102        }
103        db.SubmitChanges();
104        return entity.TaskId;
105      }
106    }
107
108    public void UpdateTaskAndPlugins(DT.Task dto) {
109      using (var db = CreateContext()) {
110        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
111        if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto));
112        else DT.Convert.ToEntity(dto, entity);
113        foreach (Guid pluginId in dto.PluginsNeededIds) {
114          if (db.RequiredPlugins.Count(p => p.PluginId == pluginId) == 0) {
115            db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { TaskId = entity.TaskId, PluginId = pluginId });
116          }
117        }
118        db.SubmitChanges();
119      }
120    }
121
122    public void UpdateTask(DT.Task dto) {
123      using (var db = CreateContext()) {
124        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
125        if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto));
126        else DT.Convert.ToEntity(dto, entity);
127        db.SubmitChanges();
128      }
129    }
130
131    public void DeleteTask(Guid id) {
132      using (var db = CreateContext()) {
133        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == id);
134        if (entity != null) db.Tasks.DeleteOnSubmit(entity);
135        db.SubmitChanges(); // taskData and child tasks are deleted by db-trigger
136      }
137    }
138
139    /// <summary>
140    /// returns all parent tasks which are waiting for their child tasks to finish
141    /// </summary>
142    /// <param name="resourceIds">list of resourceids which for which the task should be valid</param>
143    /// <param name="count">maximum number of task to return</param>
144    /// <param name="finished">if true, all parent task which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
145    /// <returns></returns>
146    public IEnumerable<DT.Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
147      using (var db = CreateContext()) {
148        var query = from ar in db.AssignedResources
149                    where resourceIds.Contains(ar.ResourceId)
150                       && ar.Task.State == TaskState.Waiting
151                       && ar.Task.IsParentTask
152                       && (finished ? ar.Task.FinishWhenChildJobsFinished : !ar.Task.FinishWhenChildJobsFinished)
153                       && (from child in db.Tasks
154                           where child.ParentTaskId == ar.Task.TaskId
155                           select child.State == TaskState.Finished
156                               || child.State == TaskState.Aborted
157                               || child.State == TaskState.Failed).All(x => x)
158                       && (from child in db.Tasks // avoid returning WaitForChildTasks task where no child-task exist (yet)
159                           where child.ParentTaskId == ar.Task.TaskId
160                           select child).Count() > 0
161                    orderby ar.Task.Priority descending, db.Random()
162                    select DT.Convert.ToDto(ar.Task);
163        return count == 0 ? query.ToArray() : query.Take(count).ToArray();
164      }
165    }
166
167    public IEnumerable<TaskInfoForScheduler> GetWaitingTasks(DT.Slave slave) {
168      using (var db = CreateContext()) {
169        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
170        //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
171        //Because there is at the moment no case where this makes sense (there don't exist parent tasks which need to be calculated),
172        //we skip this step because it's wasted runtime
173
174        var query = from ar in db.AssignedResources
175                    where resourceIds.Contains(ar.ResourceId)
176                       && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
177                       && ar.Task.State == TaskState.Waiting
178                       && ar.Task.CoresNeeded <= slave.FreeCores
179                       && ar.Task.MemoryNeeded <= slave.FreeMemory
180                    select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority };
181        var waitingTasks = query.ToArray();
182        return waitingTasks;
183      }
184    }
185
186    public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
187      using (var db = CreateContext()) {
188        var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
189        job.State = taskState;
190        db.StateLogs.InsertOnSubmit(new StateLog {
191          TaskId = taskId,
192          State = taskState,
193          SlaveId = slaveId,
194          UserId = userId,
195          Exception = exception,
196          DateTime = DateTime.Now
197        });
198        db.SubmitChanges();
199        job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
200        return DT.Convert.ToDto(job);
201      }
202    }
203    #endregion
204
205    #region TaskData Methods
206    public DT.TaskData GetTaskData(Guid id) {
207      using (var db = CreateContext(true)) {
208        return DT.Convert.ToDto(db.TaskDatas.SingleOrDefault(x => x.TaskId == id));
209      }
210    }
211
212    public IEnumerable<DT.TaskData> GetTaskDatas(Expression<Func<TaskData, bool>> predicate) {
213      using (var db = CreateContext(true)) {
214        return db.TaskDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
215      }
216    }
217
218    public Guid AddTaskData(DT.TaskData dto) {
219      using (var db = CreateContext(true)) {
220        var entity = DT.Convert.ToEntity(dto);
221        db.TaskDatas.InsertOnSubmit(entity);
222        db.SubmitChanges();
223        return entity.TaskId;
224      }
225    }
226
227    public void UpdateTaskData(DT.TaskData dto) {
228      using (var db = CreateContext(true)) {
229        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == dto.TaskId);
230        if (entity == null) db.TaskDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
231        else DT.Convert.ToEntity(dto, entity);
232        db.SubmitChanges();
233      }
234    }
235
236    public void DeleteTaskData(Guid id) {
237      using (var db = CreateContext()) {
238        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
239        if (entity != null) db.TaskDatas.DeleteOnSubmit(entity);
240        db.SubmitChanges();
241      }
242    }
243    #endregion
244
245    #region StateLog Methods
246    public DT.StateLog GetStateLog(Guid id) {
247      using (var db = CreateContext()) {
248        return DT.Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
249      }
250    }
251
252    public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
253      using (var db = CreateContext()) {
254        return db.StateLogs.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
255      }
256    }
257
258    public Guid AddStateLog(DT.StateLog dto) {
259      using (var db = CreateContext()) {
260        var entity = DT.Convert.ToEntity(dto);
261        db.StateLogs.InsertOnSubmit(entity);
262        db.SubmitChanges();
263        return entity.StateLogId;
264      }
265    }
266
267    public void UpdateStateLog(DT.StateLog dto) {
268      using (var db = CreateContext()) {
269        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
270        if (entity == null) db.StateLogs.InsertOnSubmit(DT.Convert.ToEntity(dto));
271        else DT.Convert.ToEntity(dto, entity);
272        db.SubmitChanges();
273      }
274    }
275
276    public void DeleteStateLog(Guid id) {
277      using (var db = CreateContext()) {
278        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
279        if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
280        db.SubmitChanges();
281      }
282    }
283    #endregion
284
285    #region Job Methods
286    public DT.Job GetJob(Guid id) {
287      using (var db = CreateContext()) {
288        return AddStatsToJob(db, DT.Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id)));
289      }
290    }
291
292    private DT.Job AddStatsToJob(HiveDataContext db, DT.Job exp) {
293      if (exp == null)
294        return null;
295
296      var jobs = db.Tasks.Where(j => j.JobId == exp.Id);
297      exp.JobCount = jobs.Count();
298      exp.CalculatingCount = jobs.Count(j => j.State == TaskState.Calculating);
299      exp.FinishedCount = jobs.Count(j => j.State == TaskState.Finished);
300      return exp;
301    }
302
303    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
304      using (var db = CreateContext()) {
305        return db.Jobs.Where(predicate).Select(x => AddStatsToJob(db, DT.Convert.ToDto(x))).ToArray();
306      }
307    }
308
309    public IEnumerable<JobInfoForScheduler> GetJobInfoForScheduler(Expression<Func<Job, bool>> predicate) {
310      using (var db = CreateContext()) {
311        return db.Jobs.Where(predicate).Select(x => new JobInfoForScheduler() { Id = x.JobId, DateCreated = x.DateCreated, OwnerUserId = x.OwnerUserId }).ToArray();
312      }
313    }
314
315    public Guid AddJob(DT.Job dto) {
316      using (var db = CreateContext()) {
317        var entity = DT.Convert.ToEntity(dto);
318        db.Jobs.InsertOnSubmit(entity);
319        if (!db.UserPriorities.Any(x => x.UserId == dto.OwnerUserId))
320          EnqueueUserPriority(new DT.UserPriority { Id = dto.OwnerUserId, DateEnqueued = dto.DateCreated });
321        db.SubmitChanges();
322        return entity.JobId;
323      }
324    }
325
326    public void UpdateJob(DT.Job dto) {
327      using (var db = CreateContext()) {
328        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
329        if (entity == null) db.Jobs.InsertOnSubmit(DT.Convert.ToEntity(dto));
330        else DT.Convert.ToEntity(dto, entity);
331        db.SubmitChanges();
332      }
333    }
334
335    public void DeleteJob(Guid id) {
336      using (var db = CreateContext()) {
337        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
338        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
339        db.SubmitChanges();
340      }
341    }
342    #endregion
343
344    #region JobPermission Methods
345    public DT.JobPermission GetJobPermission(Guid jobId, Guid grantedUserId) {
346      using (var db = CreateContext()) {
347        return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId));
348      }
349    }
350
351    public IEnumerable<DT.JobPermission> GetJobPermissions(Expression<Func<JobPermission, bool>> predicate) {
352      using (var db = CreateContext()) {
353        return db.JobPermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
354      }
355    }
356
357    public void AddJobPermission(DT.JobPermission dto) {
358      using (var db = CreateContext()) {
359        var entity = DT.Convert.ToEntity(dto);
360        db.JobPermissions.InsertOnSubmit(entity);
361        db.SubmitChanges();
362      }
363    }
364
365    public void UpdateJobPermission(DT.JobPermission dto) {
366      using (var db = CreateContext()) {
367        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == dto.JobId && x.GrantedUserId == dto.GrantedUserId);
368        if (entity == null) db.JobPermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
369        else DT.Convert.ToEntity(dto, entity);
370        db.SubmitChanges();
371      }
372    }
373
374    public void DeleteJobPermission(Guid jobId, Guid grantedUserId) {
375      using (var db = CreateContext()) {
376        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
377        if (entity != null) db.JobPermissions.DeleteOnSubmit(entity);
378        db.SubmitChanges();
379      }
380    }
381
382    /// <summary>
383    /// Sets the permissions for a experiment. makes sure that only one permission per user exists.
384    /// </summary>
385    public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
386      using (var db = CreateContext()) {
387        JobPermission jobPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
388        if (jobPermission != null) {
389          if (permission == Permission.NotAllowed) {
390            // not allowed, delete
391            db.JobPermissions.DeleteOnSubmit(jobPermission);
392          } else {
393            // update
394            jobPermission.Permission = permission;
395            jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
396          }
397        } else {
398          // insert
399          if (permission != Permission.NotAllowed) {
400            jobPermission = new JobPermission() { JobId = jobId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };
401            db.JobPermissions.InsertOnSubmit(jobPermission);
402          }
403        }
404        db.SubmitChanges();
405      }
406    }
407    #endregion
408
409    #region Plugin Methods
410    public DT.Plugin GetPlugin(Guid id) {
411      using (var db = CreateContext()) {
412        return DT.Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
413      }
414    }
415
416    public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
417      using (var db = CreateContext()) {
418        return db.Plugins.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
419      }
420    }
421
422    public Guid AddPlugin(DT.Plugin dto) {
423      using (var db = CreateContext()) {
424        var entity = DT.Convert.ToEntity(dto);
425        db.Plugins.InsertOnSubmit(entity);
426        db.SubmitChanges();
427        return entity.PluginId;
428      }
429    }
430
431    public void UpdatePlugin(DT.Plugin dto) {
432      using (var db = CreateContext()) {
433        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
434        if (entity == null) db.Plugins.InsertOnSubmit(DT.Convert.ToEntity(dto));
435        else DT.Convert.ToEntity(dto, entity);
436        db.SubmitChanges();
437      }
438    }
439
440    public void DeletePlugin(Guid id) {
441      using (var db = CreateContext()) {
442        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
443        if (entity != null) db.Plugins.DeleteOnSubmit(entity);
444        db.SubmitChanges();
445      }
446    }
447    #endregion
448
449    #region PluginData Methods
450    public DT.PluginData GetPluginData(Guid id) {
451      using (var db = CreateContext()) {
452        return DT.Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
453      }
454    }
455
456    public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
457      using (var db = CreateContext()) {
458        return db.PluginDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
459      }
460    }
461
462    public Guid AddPluginData(DT.PluginData dto) {
463      using (var db = CreateContext()) {
464        var entity = DT.Convert.ToEntity(dto);
465        db.PluginDatas.InsertOnSubmit(entity);
466        db.SubmitChanges();
467        return entity.PluginDataId;
468      }
469    }
470
471    public void UpdatePluginData(DT.PluginData dto) {
472      using (var db = CreateContext()) {
473        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
474        if (entity == null) db.PluginDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
475        else DT.Convert.ToEntity(dto, entity);
476        db.SubmitChanges();
477      }
478    }
479
480    public void DeletePluginData(Guid id) {
481      using (var db = CreateContext()) {
482        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id);
483        if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
484        db.SubmitChanges();
485      }
486    }
487    #endregion
488
489    #region Slave Methods
490    public DT.Slave GetSlave(Guid id) {
491      using (var db = CreateContext()) {
492        return DT.Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
493      }
494    }
495
496    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
497      using (var db = CreateContext()) {
498        return db.Resources.OfType<Slave>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
499      }
500    }
501
502    public Guid AddSlave(DT.Slave dto) {
503      using (var db = CreateContext()) {
504        var entity = DT.Convert.ToEntity(dto);
505        db.Resources.InsertOnSubmit(entity);
506        db.SubmitChanges();
507        return entity.ResourceId;
508      }
509    }
510
511    public void UpdateSlave(DT.Slave dto) {
512      using (var db = CreateContext()) {
513        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
514        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
515        else DT.Convert.ToEntity(dto, entity);
516        db.SubmitChanges();
517      }
518    }
519
520    public void DeleteSlave(Guid id) {
521      using (var db = CreateContext()) {
522        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
523        if (entity != null) db.Resources.DeleteOnSubmit(entity);
524        db.SubmitChanges();
525      }
526    }
527    #endregion
528
529    #region SlaveGroup Methods
530    public DT.SlaveGroup GetSlaveGroup(Guid id) {
531      using (var db = CreateContext()) {
532        return DT.Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
533      }
534    }
535
536    public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
537      using (var db = CreateContext()) {
538        return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
539      }
540    }
541
542    public Guid AddSlaveGroup(DT.SlaveGroup dto) {
543      using (var db = CreateContext()) {
544        if (dto.Id == Guid.Empty)
545          dto.Id = Guid.NewGuid();
546        var entity = DT.Convert.ToEntity(dto);
547        db.Resources.InsertOnSubmit(entity);
548        db.SubmitChanges();
549        return entity.ResourceId;
550      }
551    }
552
553    public void UpdateSlaveGroup(DT.SlaveGroup dto) {
554      using (var db = CreateContext()) {
555        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
556        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
557        else DT.Convert.ToEntity(dto, entity);
558        db.SubmitChanges();
559      }
560    }
561
562    public void DeleteSlaveGroup(Guid id) {
563      using (var db = CreateContext()) {
564        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
565        if (entity != null) {
566          if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
567            throw new InvalidOperationException("Cannot delete SlaveGroup as long as there are Slaves in the group");
568          }
569          db.Resources.DeleteOnSubmit(entity);
570        }
571        db.SubmitChanges();
572      }
573    }
574    #endregion
575
576    #region Resource Methods
577    public DT.Resource GetResource(Guid id) {
578      using (var db = CreateContext()) {
579        return DT.Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
580      }
581    }
582
583    public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
584      using (var db = CreateContext()) {
585        return db.Resources.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
586      }
587    }
588
589    public Guid AddResource(DT.Resource dto) {
590      using (var db = CreateContext()) {
591        var entity = DT.Convert.ToEntity(dto);
592        db.Resources.InsertOnSubmit(entity);
593        db.SubmitChanges();
594        return entity.ResourceId;
595      }
596    }
597
598    public void UpdateResource(DT.Resource dto) {
599      using (var db = CreateContext()) {
600        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
601        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
602        else DT.Convert.ToEntity(dto, entity);
603        db.SubmitChanges();
604      }
605    }
606
607    public void DeleteResource(Guid id) {
608      using (var db = CreateContext()) {
609        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
610        if (entity != null) db.Resources.DeleteOnSubmit(entity);
611        db.SubmitChanges();
612      }
613    }
614
615    public void AssignJobToResource(Guid taskId, IEnumerable<Guid> resourceIds) {
616      using (var db = CreateContext()) {
617        var task = db.Tasks.Where(x => x.TaskId == taskId).Single();
618        foreach (Guid rId in resourceIds) {
619          task.AssignedResources.Add(new AssignedResource() { TaskId = taskId, ResourceId = rId });
620        }
621        db.SubmitChanges();
622      }
623    }
624
625    public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
626      using (var db = CreateContext()) {
627        var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
628        return job.AssignedResources.Select(x => DT.Convert.ToDto(x.Resource)).ToArray();
629      }
630    }
631
632    /// <summary>
633    /// Returns all parent resources of a resource (the given resource is also added)
634    /// </summary>
635    public IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
636      using (var db = CreateContext()) {
637        var resources = new List<Resource>();
638        CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
639        return resources.Select(r => DT.Convert.ToDto(r)).ToArray();
640      }
641    }
642
643    private void CollectParentResources(List<Resource> resources, Resource resource) {
644      if (resource == null) return;
645      resources.Add(resource);
646      CollectParentResources(resources, resource.ParentResource);
647    }
648
649    /// <summary>
650    /// Returns all child resources of a resource (without the given resource)
651    /// </summary>
652    public IEnumerable<DT.Resource> GetChildResources(Guid resourceId) {
653      using (var db = CreateContext()) {
654        return CollectChildResources(resourceId, db);
655      }
656    }
657
658    public IEnumerable<DT.Resource> CollectChildResources(Guid resourceId, HiveDataContext db) {
659      var childs = new List<DT.Resource>();
660      foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) {
661        childs.Add(DT.Convert.ToDto(child));
662        childs.AddRange(CollectChildResources(child.ResourceId, db));
663      }
664      return childs;
665    }
666
667    public IEnumerable<DT.Task> GetJobsByResourceId(Guid resourceId) {
668      using (var db = CreateContext()) {
669        var resources = GetChildResources(resourceId).Select(x => x.Id).ToList();
670        resources.Add(resourceId);
671
672        var jobs = db.Tasks.Where(j =>
673          j.State == TaskState.Calculating &&
674          j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.HasValue &&
675          resources.Contains(j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.Value));
676        return jobs.Select(j => DT.Convert.ToDto(j)).ToArray();
677      }
678    }
679    #endregion
680
681    #region ResourcePermission Methods
682    public DT.ResourcePermission GetResourcePermission(Guid resourceId, Guid grantedUserId) {
683      using (var db = CreateContext()) {
684        return DT.Convert.ToDto(db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId));
685      }
686    }
687
688    public IEnumerable<DT.ResourcePermission> GetResourcePermissions(Expression<Func<ResourcePermission, bool>> predicate) {
689      using (var db = CreateContext()) {
690        return db.ResourcePermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
691      }
692    }
693
694    public void AddResourcePermission(DT.ResourcePermission dto) {
695      using (var db = CreateContext()) {
696        var entity = db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
697        if (entity == null) { db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto)); db.SubmitChanges(); }
698      }
699    }
700
701    public void UpdateResourcePermission(DT.ResourcePermission dto) {
702      using (var db = CreateContext()) {
703        var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
704        if (entity == null) db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
705        else DT.Convert.ToEntity(dto, entity);
706        db.SubmitChanges();
707      }
708    }
709
710    public void DeleteResourcePermission(Guid resourceId, Guid grantedUserId) {
711      using (var db = CreateContext()) {
712        var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId);
713        if (entity != null) db.ResourcePermissions.DeleteOnSubmit(entity);
714        db.SubmitChanges();
715      }
716    }
717    #endregion
718
719    #region Authorization Methods
720    public Permission GetPermissionForTask(Guid taskId, Guid userId) {
721      using (var db = CreateContext()) {
722        return GetPermissionForJob(GetJobForTask(taskId), userId);
723      }
724    }
725
726    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
727      using (var db = CreateContext()) {
728        Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
729        if (job == null) return Permission.NotAllowed;
730        if (job.OwnerUserId == userId) return Permission.Full;
731        JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId);
732        return permission != null ? permission.Permission : Permission.NotAllowed;
733      }
734    }
735
736    public Guid GetJobForTask(Guid taskId) {
737      using (var db = CreateContext()) {
738        return db.Tasks.Single(j => j.TaskId == taskId).JobId;
739      }
740    }
741    #endregion
742
743    #region Lifecycle Methods
744    public DateTime GetLastCleanup() {
745      using (var db = CreateContext()) {
746        var entity = db.Lifecycles.SingleOrDefault();
747        return entity != null ? entity.LastCleanup : DateTime.MinValue;
748      }
749    }
750
751    public void SetLastCleanup(DateTime datetime) {
752      using (var db = CreateContext()) {
753        var entity = db.Lifecycles.SingleOrDefault();
754        if (entity != null) {
755          entity.LastCleanup = datetime;
756        } else {
757          entity = new Lifecycle();
758          entity.LifecycleId = 0; // always only one entry with ID:0
759          entity.LastCleanup = datetime;
760          db.Lifecycles.InsertOnSubmit(entity);
761        }
762        db.SubmitChanges();
763      }
764    }
765    #endregion
766
767    #region Downtime Methods
768    public DT.Downtime GetDowntime(Guid id) {
769      using (var db = CreateContext()) {
770        return DT.Convert.ToDto(db.Downtimes.SingleOrDefault(x => x.DowntimeId == id));
771      }
772    }
773
774    public IEnumerable<DT.Downtime> GetDowntimes(Expression<Func<Downtime, bool>> predicate) {
775      using (var db = CreateContext()) {
776        return db.Downtimes.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
777      }
778    }
779
780    public Guid AddDowntime(DT.Downtime dto) {
781      using (var db = CreateContext()) {
782        var entity = DT.Convert.ToEntity(dto);
783        db.Downtimes.InsertOnSubmit(entity);
784        db.SubmitChanges();
785        return entity.DowntimeId;
786      }
787    }
788
789    public void UpdateDowntime(DT.Downtime dto) {
790      using (var db = CreateContext()) {
791        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == dto.Id);
792        if (entity == null) db.Downtimes.InsertOnSubmit(DT.Convert.ToEntity(dto));
793        else DT.Convert.ToEntity(dto, entity);
794        db.SubmitChanges();
795      }
796    }
797
798    public void DeleteDowntime(Guid id) {
799      using (var db = CreateContext()) {
800        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == id);
801        if (entity != null) db.Downtimes.DeleteOnSubmit(entity);
802        db.SubmitChanges();
803      }
804    }
805    #endregion
806
807    #region Statistics Methods
808    public DT.Statistics GetStatistic(Guid id) {
809      using (var db = CreateContext()) {
810        return DT.Convert.ToDto(db.Statistics.SingleOrDefault(x => x.StatisticsId == id));
811      }
812    }
813
814    public IEnumerable<DT.Statistics> GetStatistics(Expression<Func<Statistics, bool>> predicate) {
815      using (var db = CreateContext()) {
816        return db.Statistics.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
817      }
818    }
819
820    public Guid AddStatistics(DT.Statistics dto) {
821      using (var db = CreateContext()) {
822        var entity = DT.Convert.ToEntity(dto);
823        db.Statistics.InsertOnSubmit(entity);
824        db.SubmitChanges();
825        foreach (var slaveStat in dto.SlaveStatistics) {
826          slaveStat.Id = entity.StatisticsId;
827          db.SlaveStatistics.InsertOnSubmit(DT.Convert.ToEntity(slaveStat));
828        }
829        if (dto.UserStatistics != null) {
830          foreach (var userStat in dto.UserStatistics) {
831            userStat.Id = entity.StatisticsId;
832            db.UserStatistics.InsertOnSubmit(DT.Convert.ToEntity(userStat));
833          }
834        }
835        db.SubmitChanges();
836        return entity.StatisticsId;
837      }
838    }
839
840    public void DeleteStatistics(Guid id) {
841      using (var db = CreateContext()) {
842        var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
843        if (entity != null) db.Statistics.DeleteOnSubmit(entity);
844        db.SubmitChanges();
845      }
846    }
847
848    public Dictionary<Guid, int> GetWaitingTasksByUser() {
849      using (var db = CreateContext()) {
850        var waitingTasksByUser = from task in db.Tasks
851                                 where task.State == TaskState.Waiting
852                                 group task by task.Job.OwnerUserId into g
853                                 select new { UserId = g.Key, UsedCores = g.Count() };
854        return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
855      }
856    }
857
858    public Dictionary<Guid, int> GetWaitingTasksByUserForResources(List<Guid> resourceIds) {
859      using (var db = CreateContext()) {
860        var waitingTasksByUser = from task in db.Tasks
861                                 where task.State == TaskState.Waiting && task.AssignedResources.Any(x => resourceIds.Contains(x.ResourceId))
862                                 group task by task.Job.OwnerUserId into g
863                                 select new { UserId = g.Key, UsedCores = g.Count() };
864        return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
865      }
866    }
867
868    public Dictionary<Guid, int> GetCalculatingTasksByUser() {
869      using (var db = CreateContext()) {
870        var calculatingTasksByUser = from task in db.Tasks
871                                     where task.State == TaskState.Calculating
872                                     group task by task.Job.OwnerUserId into g
873                                     select new { UserId = g.Key, UsedCores = g.Count() };
874        return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
875      }
876    }
877
878    public Dictionary<Guid, int> GetCalculatingTasksByUserForResources(List<Guid> resourceIds) {
879      using (var db = CreateContext()) {
880        var calculatingTasksByUser = from task in db.Tasks
881                                     where task.State == TaskState.Calculating && task.AssignedResources.Any(x => resourceIds.Contains(x.ResourceId))
882                                     group task by task.Job.OwnerUserId into g
883                                     select new { UserId = g.Key, UsedCores = g.Count() };
884        return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
885      }
886    }
887
888    public List<DT.UserStatistics> GetUserStatistics() {
889      using (var db = CreateContext()) {
890        var userStats = new Dictionary<Guid, DT.UserStatistics>();
891
892        var usedCoresByUser = from job in db.Tasks
893                              where job.State == TaskState.Calculating
894                              group job by job.Job.OwnerUserId into g
895                              select new { UserId = g.Key, UsedCores = g.Count() };
896
897        foreach (var item in usedCoresByUser) {
898          if (!userStats.ContainsKey(item.UserId)) {
899            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
900          }
901          userStats[item.UserId].UsedCores += item.UsedCores;
902        }
903
904        var executionTimesByUser = from task in db.Tasks
905                                   group task by task.Job.OwnerUserId into g
906                                   select new { UserId = g.Key, ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
907        foreach (var item in executionTimesByUser) {
908          if (!userStats.ContainsKey(item.UserId)) {
909            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
910          }
911          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
912        }
913
914        // execution times only of finished task - necessary to compute efficieny
915        var executionTimesFinishedJobs = from job in db.Tasks
916                                         where job.State == TaskState.Finished
917                                         group job by job.Job.OwnerUserId into g
918                                         select new { UserId = g.Key, ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
919
920        foreach (var item in executionTimesFinishedJobs) {
921          if (!userStats.ContainsKey(item.UserId)) {
922            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
923          }
924          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
925        }
926
927        // start to end times only of finished task - necessary to compute efficiency
928        var startToEndTimesFinishedJobs = from job in db.Tasks
929                                          where job.State == TaskState.Finished
930                                          group job by job.Job.OwnerUserId into g
931                                          select new {
932                                            UserId = g.Key,
933                                            StartToEndTime = new TimeSpan(g.Select(x => x.StateLogs.OrderByDescending(sl => sl.DateTime).First().DateTime - x.StateLogs.OrderBy(sl => sl.DateTime).First().DateTime).Sum(ts => ts.Ticks))
934                                          };
935        foreach (var item in startToEndTimesFinishedJobs) {
936          if (!userStats.ContainsKey(item.UserId)) {
937            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
938          }
939          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
940        }
941
942        // also consider executiontimes of DeletedJobStats
943        var deletedJobsExecutionTimesByUsers = from del in db.DeletedJobStatistics
944                                               group del by del.UserId into g
945                                               select new {
946                                                 UserId = g.Key,
947                                                 ExecutionTime = TimeSpan.FromSeconds(g.Select(x => x.ExecutionTimeS).Sum()),
948                                                 ExecutionTimeFinishedJobs = TimeSpan.FromSeconds(g.Select(x => x.ExecutionTimeSFinishedJobs).Sum()),
949                                                 StartToEndTime = TimeSpan.FromSeconds(g.Select(x => x.StartToEndTimeS).Sum())
950                                               };
951        foreach (var item in deletedJobsExecutionTimesByUsers) {
952          if (!userStats.ContainsKey(item.UserId)) {
953            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
954          }
955          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
956          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
957          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
958        }
959
960        return userStats.Values.ToList();
961      }
962    }
963    #endregion
964
965    #region UserPriority Methods
966    public IEnumerable<DT.UserPriority> GetUserPriorities(Expression<Func<UserPriority, bool>> predicate) {
967      using (var db = CreateContext()) {
968        return db.UserPriorities.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
969      }
970    }
971
972    public void EnqueueUserPriority(DT.UserPriority dto) {
973      using (var db = CreateContext()) {
974        var entity = db.UserPriorities.FirstOrDefault(x => x.UserId == dto.Id);
975        if (entity == null) db.UserPriorities.InsertOnSubmit(DT.Convert.ToEntity(dto));
976        else DT.Convert.ToEntity(dto, entity);
977        db.SubmitChanges();
978      }
979    }
980    #endregion
981
982    #region Helpers
983    private void CollectChildTasks(HiveDataContext db, Guid parentTaskId, List<Task> collection) {
984      var tasks = db.Tasks.Where(j => j.ParentTaskId == parentTaskId);
985      foreach (var task in tasks) {
986        collection.Add(task);
987        if (task.IsParentTask)
988          CollectChildTasks(db, task.TaskId, collection);
989      }
990    }
991    #endregion
992  }
993}
Note: See TracBrowser for help on using the repository browser.