Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveTaskScheduler/HeuristicLab.Services.Hive/3.3/Scheduler/RoundRobinTaskScheduler.cs @ 8707

Last change on this file since 8707 was 8707, checked in by jkarder, 12 years ago

#1712: added user queue used to schedule tasks

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
25using HeuristicLab.Services.Hive.DataTransfer;
26
27namespace HeuristicLab.Services.Hive {
28  public class RoundRobinTaskScheduler : ITaskScheduler {
29    private IHiveDao dao {
30      get { return ServiceLocator.Instance.HiveDao; }
31    }
32
33    public IEnumerable<Task> Schedule(IEnumerable<Task> tasks, int count = 1) {
34      if (!tasks.Any()) return Enumerable.Empty<Task>();
35
36      var userPriorities = dao.GetUserPriorities(x => true).OrderBy(x => x.DateEnqueued).ToArray();
37
38      var jobs = new List<Job>();
39      foreach (var userPriority in userPriorities) {
40        var closureUserPriority = userPriority;
41        jobs.AddRange(dao.GetJobs(x => x.OwnerUserId == closureUserPriority.Id));
42      }
43
44      var taskJobRelations = tasks.Join(jobs,
45        task => task.JobId,
46        job => job.Id,
47        (task, job) => new { Task = task, Job = job })
48        .OrderByDescending(x => x.Task.Priority)
49        .ToList();
50
51      var scheduledTasks = new List<Task>();
52      int priorityIndex = 0;
53
54      if (count == 0 || count > taskJobRelations.Count) count = taskJobRelations.Count;
55
56      for (int i = 0; i < count; i++) {
57        var defaultEntry = taskJobRelations.First(); // search first task which is not included yet
58        var priorityEntries = taskJobRelations.Where(x => x.Job.OwnerUserId == userPriorities[priorityIndex].Id).ToArray(); // search for tasks with desired user priority
59        while (!priorityEntries.Any() && ++priorityIndex < userPriorities.Length)
60          priorityEntries = taskJobRelations.Where(x => x.Job.OwnerUserId == userPriorities[priorityIndex].Id).ToArray();
61        if (priorityEntries.Any()) { // tasks with desired user priority found
62          var priorityEntry = priorityEntries.OrderByDescending(x => x.Task.Priority).ThenBy(x => x.Job.DateCreated).First();
63          if (defaultEntry.Task.Priority <= priorityEntry.Task.Priority) {
64            taskJobRelations.Remove(priorityEntry);
65            scheduledTasks.Add(priorityEntry.Task);
66            priorityIndex++;
67          } else { // there are other tasks with higher priorities
68            taskJobRelations.Remove(defaultEntry);
69            scheduledTasks.Add(defaultEntry.Task);
70          }
71        } else {
72          taskJobRelations.Remove(defaultEntry);
73          scheduledTasks.Add(defaultEntry.Task);
74        }
75      }
76
77      // requeue user priorities
78      if (priorityIndex < userPriorities.Length)
79        for (int i = 0; i < priorityIndex; i++) {
80          userPriorities[i].DateEnqueued = DateTime.Now;
81          dao.EnqueueUserPriority(userPriorities[i]);
82        }
83
84      return scheduledTasks;
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.