Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs

Last change on this file was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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<AssignedResource> AssignedResourceTable {
30      get { return DataContext.GetTable<AssignedResource>(); }
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(IEnumerable<Guid> resourceIds, int count, bool finished) {
67      var query = from ar in AssignedResourceTable
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    public void UpdateExecutionTime(Guid taskId, double executionTime) {
86      DataContext.ExecuteCommand(UpdateExecutionTimeQuery, executionTime, DateTime.Now, taskId);
87    }
88
89    #region Compiled queries
90    private static readonly Func<DataContext, Guid, Task> GetByIdQuery =
91      CompiledQuery.Compile((DataContext db, Guid taskId) =>
92        (from task in db.GetTable<Task>()
93         where task.TaskId == taskId
94         select task).SingleOrDefault());
95    #endregion
96
97    #region String queries
98    private const string GetWaitingTasksQueryString = @"
99      WITH pr AS (
100        SELECT ResourceId, ParentResourceId
101        FROM [Resource]
102        WHERE ResourceId = {0}
103        UNION ALL
104        SELECT r.ResourceId, r.ParentResourceId
105        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
106      )
107      SELECT DISTINCT t.TaskId, t.JobId, t.Priority
108      FROM pr JOIN AssignedResources ar ON ar.ResourceId = pr.ResourceId
109          JOIN Task t ON t.TaskId = ar.TaskId
110      WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
111          AND t.TaskState = {1}
112          AND t.CoresNeeded <= {2}
113          AND t.MemoryNeeded <= {3}
114    ";
115
116    private const string UpdateExecutionTimeQuery = @"
117      UPDATE [Task]
118         SET ExecutionTimeMs = {0},
119             LastHeartbeat = {1}
120       WHERE TaskId = {2}
121    ";
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.