Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs @ 15628

Last change on this file since 15628 was 15628, checked in by jzenisek, 6 years ago

#2839

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