Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1712: initial commit

File size: 3.4 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Services.Hive.DataTransfer;
25
26namespace HeuristicLab.Services.Hive {
27  public class RoundRobinTaskScheduler : ITaskScheduler {
28    private IHiveDao dao {
29      get { return ServiceLocator.Instance.HiveDao; }
30    }
31
32    public IEnumerable<Task> Schedule(IEnumerable<Task> tasks, int count = 1) {
33      var userPriorities = dao.GetUserPriorities(x => true).ToArray();
34
35      var jobs = new List<Job>();
36      foreach (var userPriority in userPriorities) {
37        var closureUserPriority = userPriority;
38        jobs.AddRange(dao.GetJobs(x => x.OwnerUserId == closureUserPriority.UserId));
39      }
40
41      var taskJobRelations = tasks.Join(jobs,
42        task => task.JobId,
43        job => job.Id,
44        (task, job) => new { Task = task, Job = job })
45        .ToDictionary(k => k, v => false);
46
47      var scheduledTasks = new List<Task>();
48      int priorityIndex = 0;
49      if (count == 0 || count > taskJobRelations.Count) count = taskJobRelations.Count;
50      for (int i = 0; i < count; i++) {
51        var defaultEntry = taskJobRelations.First(x => !x.Value); // search first task which is not included yet
52        var priorityEntries = taskJobRelations.Where(x => !x.Value && x.Key.Job.OwnerUserId == userPriorities[priorityIndex].UserId).ToArray(); // search for tasks with desired user priority
53        while (!priorityEntries.Any() && priorityIndex < priorityEntries.Length)
54          priorityEntries = taskJobRelations.Where(x => !x.Value && x.Key.Job.OwnerUserId == userPriorities[++priorityIndex].UserId).ToArray();
55        if (priorityEntries.Any()) { // tasks with desired user priority found
56          var priorityEntry = priorityEntries.OrderByDescending(x => x.Key.Task.Priority).ThenBy(x => x.Key.Task.DateCreated).First();
57          if (defaultEntry.Key.Task.Priority <= priorityEntry.Key.Task.Priority) {
58            taskJobRelations[priorityEntry.Key] = true;
59            scheduledTasks.Add(priorityEntry.Key.Task);
60            priorityIndex++;
61          } else { // there are other tasks with higher priority
62            taskJobRelations[defaultEntry.Key] = true;
63            scheduledTasks.Add(defaultEntry.Key.Task);
64          }
65        } else {
66          taskJobRelations[defaultEntry.Key] = true;
67          scheduledTasks.Add(defaultEntry.Key.Task);
68        }
69      }
70
71      // dequeue and enqueue priorities [0, priorityIndex), i.e. {4,3,6,2,5,1} and priorityIndex = 3 -> {2,5,1,4,3,6}
72      // i don't know how to solve this yet ... (race conditions, locking?)
73
74      return scheduledTasks;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.