1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Data.Linq;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.DataAccess.Daos {
|
---|
28 | public class TaskDao : GenericDao<Guid, Task> {
|
---|
29 | public TaskDao(DataContext dataContext) : base(dataContext) { }
|
---|
30 |
|
---|
31 | public override Task GetById(Guid id) {
|
---|
32 | return GetByIdQuery(DataContext, id);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public IQueryable<Task> GetAllChildTasks() {
|
---|
36 | return Table.Where(x => !x.IsParentTask);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public IQueryable<Task> GetByJobId(Guid id) {
|
---|
40 | return Table.Where(x => x.JobId == id);
|
---|
41 | }
|
---|
42 | public class TaskPriorityInfo {
|
---|
43 | public Guid JobId { get; set; }
|
---|
44 | public Guid TaskId { get; set; }
|
---|
45 | public int Priority { get; set; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public IEnumerable<TaskPriorityInfo> GetWaitingTasks(Slave slave) {
|
---|
49 | //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
|
---|
50 | //Because there is at the moment no case where this makes sense (there don't exist parent tasks which need to be calculated),
|
---|
51 | //we skip this step because it's wasted runtime
|
---|
52 | return DataContext.ExecuteQuery<TaskPriorityInfo>(GetWaitingTasksQueryString,
|
---|
53 | slave.ResourceId,
|
---|
54 | Enum.GetName(typeof(TaskState), TaskState.Waiting),
|
---|
55 | slave.FreeCores,
|
---|
56 | slave.FreeMemory).ToList();
|
---|
57 | }
|
---|
58 |
|
---|
59 | /// <summary>
|
---|
60 | /// returns all parent tasks which are waiting for their child tasks to finish
|
---|
61 | /// </summary>
|
---|
62 | /// <param name="resourceIds">list of resourceids which for which the task should be valid</param>
|
---|
63 | /// <param name="count">maximum number of task to return</param>
|
---|
64 | /// <param name="finished">if true, all parent tasks which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
|
---|
65 | /// <returns></returns>
|
---|
66 | public IEnumerable<Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
|
---|
67 | var query = from t in Table
|
---|
68 | where t.State == TaskState.Waiting
|
---|
69 | && t.IsParentTask
|
---|
70 | && !t.Job.AssignedJobResources.Select(x => x.ResourceId).Except(resourceIds).Any()
|
---|
71 | && t.FinishWhenChildJobsFinished == finished
|
---|
72 | && t.ChildJobs.Any()
|
---|
73 | && t.ChildJobs.All(x =>
|
---|
74 | x.State == TaskState.Finished
|
---|
75 | || x.State == TaskState.Aborted
|
---|
76 | || x.State == TaskState.Failed)
|
---|
77 | orderby t.Priority descending
|
---|
78 | select t;
|
---|
79 | return count == 0 ? query.ToArray() : query.Take(count).ToArray();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void UpdateExecutionTime(Guid taskId, double executionTime) {
|
---|
83 | DataContext.ExecuteCommand(UpdateExecutionTimeQuery, executionTime, DateTime.Now, taskId);
|
---|
84 | }
|
---|
85 |
|
---|
86 | #region Compiled queries
|
---|
87 | private static readonly Func<DataContext, Guid, Task> GetByIdQuery =
|
---|
88 | CompiledQuery.Compile((DataContext db, Guid taskId) =>
|
---|
89 | (from task in db.GetTable<Task>()
|
---|
90 | where task.TaskId == taskId
|
---|
91 | select task).SingleOrDefault());
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | #region String queries
|
---|
95 | private const string GetParentTasksQueryString = @"
|
---|
96 | SELECT t.* FROM [Task] t, [Job] j, [AssignedJobResource] ajr
|
---|
97 | WHERE t.IsParentTask = 1
|
---|
98 | AND t.TaskState = 'Waiting'
|
---|
99 | AND t.JobId = j.JobId
|
---|
100 | AND j.JobId = ajr.JobId
|
---|
101 | AND t.FinishWhenChildJobsFinished = 1
|
---|
102 | ... TODO (not necessary)
|
---|
103 | ";
|
---|
104 | private const string GetWaitingTasksQueryString = @"
|
---|
105 | WITH rbranch AS (
|
---|
106 | SELECT ResourceId, ParentResourceId
|
---|
107 | FROM [Resource]
|
---|
108 | WHERE ResourceId = {0}
|
---|
109 | UNION ALL
|
---|
110 | SELECT r.ResourceId, r.ParentResourceId
|
---|
111 | FROM [Resource] r
|
---|
112 | JOIN rbranch rb ON rb.ParentResourceId = r.ResourceId
|
---|
113 | )
|
---|
114 | SELECT DISTINCT t.TaskId, t.JobId, t.Priority
|
---|
115 | FROM [Task] t, [Job] j, [AssignedJobResource] ajr, rbranch
|
---|
116 | WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
|
---|
117 | AND t.TaskState = {1}
|
---|
118 | AND t.CoresNeeded <= {2}
|
---|
119 | AND t.MemoryNeeded <= {3}
|
---|
120 | AND t.JobId = j.JobId
|
---|
121 | AND j.JobState = 'Online'
|
---|
122 | AND j.JobId = ajr.JobId
|
---|
123 | AND ajr.ResourceId = rbranch.ResourceId
|
---|
124 | ";
|
---|
125 |
|
---|
126 | private const string UpdateExecutionTimeQuery = @"
|
---|
127 | UPDATE [Task]
|
---|
128 | SET ExecutionTimeMs = {0},
|
---|
129 | LastHeartbeat = {1}
|
---|
130 | WHERE TaskId = {2}
|
---|
131 | ";
|
---|
132 | #endregion
|
---|
133 | }
|
---|
134 | } |
---|