Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs @ 16317

Last change on this file since 16317 was 16311, checked in by pfleck, 6 years ago

#2845 Merged trunk changes into branch (15406-15681, 15683-16308)

File size: 5.3 KB
RevLine 
[12468]1#region License Information
2/* HeuristicLab
[16311]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12468]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;
[12691]23using System.Collections.Generic;
[12468]24using System.Data.Linq;
25using System.Linq;
26
27namespace 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) {
[12691]32      return GetByIdQuery(DataContext, id);
[12468]33    }
34
35    public IQueryable<Task> GetAllChildTasks() {
36      return Table.Where(x => !x.IsParentTask);
37    }
[12516]38
[12584]39    public IQueryable<Task> GetByJobId(Guid id) {
[12516]40      return Table.Where(x => x.JobId == id);
41    }
[12691]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
[16311]52      return DataContext.ExecuteQuery<TaskPriorityInfo>(GetWaitingTasksQueryString,
53        slave.ResourceId,
54        Enum.GetName(typeof(TaskState), TaskState.Waiting),
55        slave.FreeCores,
56        slave.FreeMemory).ToList();
[12691]57    }
58
[12789]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>
[16311]64    /// <param name="finished">if true, all parent tasks which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
[12789]65    /// <returns></returns>
66    public IEnumerable<Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
[16311]67    var query = from t in Table
68                where t.State == TaskState.Waiting
69                    && t.IsParentTask
70                    && t.Job.AssignedJobResources.All(x => resourceIds.ToList().Contains(x.ResourceId))
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;
[12789]79      return count == 0 ? query.ToArray() : query.Take(count).ToArray();
80    }
81
[12691]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
[16311]95    private const string GetCalculatingChildTasksByProjectId = @"
96      SELECT t.* FROM [Task] t, [Job] j
97        WHERE t.IsParentTask = 0
98        AND t.TaskState = 'Calculating'
99        AND t.JobId = j.JobId
100        AND j.ProjectId = {0}
101        ORDER BY j.ProjectId
102    ";
[12691]103    private const string GetWaitingTasksQueryString = @"
[16311]104      WITH rbranch AS (
[12691]105        SELECT ResourceId, ParentResourceId
106        FROM [Resource]
107        WHERE ResourceId = {0}
108        UNION ALL
109        SELECT r.ResourceId, r.ParentResourceId
[16311]110        FROM [Resource] r
111        JOIN rbranch rb ON rb.ParentResourceId = r.ResourceId
[12691]112      )
113      SELECT DISTINCT t.TaskId, t.JobId, t.Priority
[16311]114      FROM [Task] t, [Job] j, [AssignedJobResource] ajr, rbranch
[12691]115      WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
[16311]116      AND t.TaskState = {1}
117      AND t.CoresNeeded <= {2}
118      AND t.MemoryNeeded <= {3}
119      AND t.JobId = j.JobId
120      AND j.JobState = 'Online'
121      AND j.JobId = ajr.JobId
122      AND ajr.ResourceId = rbranch.ResourceId
[12691]123    ";
124
125    private const string UpdateExecutionTimeQuery = @"
126      UPDATE [Task]
127         SET ExecutionTimeMs = {0},
128             LastHeartbeat = {1}
129       WHERE TaskId = {2}
130    ";
131    #endregion
[12468]132  }
[12691]133}
Note: See TracBrowser for help on using the repository browser.