1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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, slave.ResourceId, Enum.GetName(typeof(TaskState), TaskState.Waiting), slave.FreeCores, slave.FreeMemory).ToList();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void UpdateExecutionTime(Guid taskId, double executionTime) {
|
---|
56 | DataContext.ExecuteCommand(UpdateExecutionTimeQuery, executionTime, DateTime.Now, taskId);
|
---|
57 | }
|
---|
58 |
|
---|
59 | #region Compiled queries
|
---|
60 | private static readonly Func<DataContext, Guid, Task> GetByIdQuery =
|
---|
61 | CompiledQuery.Compile((DataContext db, Guid taskId) =>
|
---|
62 | (from task in db.GetTable<Task>()
|
---|
63 | where task.TaskId == taskId
|
---|
64 | select task).SingleOrDefault());
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | #region String queries
|
---|
68 | private const string GetWaitingTasksQueryString = @"
|
---|
69 | WITH pr AS (
|
---|
70 | SELECT ResourceId, ParentResourceId
|
---|
71 | FROM [Resource]
|
---|
72 | WHERE ResourceId = {0}
|
---|
73 | UNION ALL
|
---|
74 | SELECT r.ResourceId, r.ParentResourceId
|
---|
75 | FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
|
---|
76 | )
|
---|
77 | SELECT DISTINCT t.TaskId, t.JobId, t.Priority
|
---|
78 | FROM pr JOIN AssignedResources ar ON ar.ResourceId = pr.ResourceId
|
---|
79 | JOIN Task t ON t.TaskId = ar.TaskId
|
---|
80 | WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
|
---|
81 | AND t.TaskState = {1}
|
---|
82 | AND t.CoresNeeded <= {2}
|
---|
83 | AND t.MemoryNeeded <= {3}
|
---|
84 | ";
|
---|
85 |
|
---|
86 | private const string UpdateExecutionTimeQuery = @"
|
---|
87 | UPDATE [Task]
|
---|
88 | SET ExecutionTimeMs = {0},
|
---|
89 | LastHeartbeat = {1}
|
---|
90 | WHERE TaskId = {2}
|
---|
91 | ";
|
---|
92 | #endregion
|
---|
93 | }
|
---|
94 | } |
---|