1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Linq;
|
---|
25 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive {
|
---|
28 | public class RoundRobinTaskScheduler : TaskScheduler {
|
---|
29 | private class TaskPriorityResult {
|
---|
30 | public Guid TaskId { get; set; }
|
---|
31 | public Guid OwnerUserId { get; set; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected override IReadOnlyList<Guid> ScheduleInternal(DA.Slave slave, int count) {
|
---|
35 | var pm = PersistenceManager;
|
---|
36 |
|
---|
37 | var result = pm.DataContext.ExecuteQuery<TaskPriorityResult>(
|
---|
38 | GetHighestPriorityWaitingTasksQuery, slave.ResourceId, count, slave.FreeCores, slave.FreeMemory).ToList();
|
---|
39 |
|
---|
40 | foreach (var row in result) {
|
---|
41 | pm.DataContext.ExecuteCommand("UPDATE UserPriority SET DateEnqueued = SYSDATETIME() WHERE UserId = {0}", row.OwnerUserId);
|
---|
42 | }
|
---|
43 |
|
---|
44 | return result.Select(x => x.TaskId).ToArray();
|
---|
45 | }
|
---|
46 |
|
---|
47 | #region Query Strings
|
---|
48 | private string GetHighestPriorityWaitingTasksQuery = @"
|
---|
49 | WITH rbranch AS(
|
---|
50 | SELECT ResourceId, ParentResourceId
|
---|
51 | FROM [Resource]
|
---|
52 | WHERE ResourceId = {0}
|
---|
53 | UNION ALL
|
---|
54 | SELECT r.ResourceId, r.ParentResourceId
|
---|
55 | FROM [Resource] r
|
---|
56 | JOIN rbranch rb ON rb.ParentResourceId = r.ResourceId
|
---|
57 | )
|
---|
58 | SELECT TOP ({1}) t.TaskId, j.OwnerUserId
|
---|
59 | FROM Task t
|
---|
60 | JOIN Job j on t.JobId = j.JobId
|
---|
61 | JOIN AssignedJobResource ajr on j.JobId = ajr.JobId
|
---|
62 | JOIN rbranch on ajr.ResourceId = rbranch.ResourceId
|
---|
63 | JOIN UserPriority u on j.OwnerUserId = u.UserId
|
---|
64 | WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
|
---|
65 | AND t.TaskState = 'Waiting'
|
---|
66 | AND t.CoresNeeded <= {2}
|
---|
67 | AND t.MemoryNeeded <= {3}
|
---|
68 | AND j.JobState = 'Online'
|
---|
69 | ORDER BY t.Priority DESC, u.DateEnqueued ASC, j.DateCreated ASC";
|
---|
70 | #endregion
|
---|
71 | }
|
---|
72 | }
|
---|