Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Hive/3.3/Scheduler/RoundRobinTaskScheduler.cs @ 10823

Last change on this file since 10823 was 9123, checked in by ascheibe, 12 years ago

#1712 reintegrated Hive Scheduler branch and made further performance improvements

File size: 3.6 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<TaskInfoForScheduler> Schedule(IEnumerable<TaskInfoForScheduler> tasks, int count = 1) {
34      if (!tasks.Any()) return Enumerable.Empty<TaskInfoForScheduler>();
35
36      var userPriorities = dao.GetUserPriorities(x => true).OrderBy(x => x.DateEnqueued).ToArray();
37
38      var jobs = new List<JobInfoForScheduler>();
39      foreach (var userPriority in userPriorities) {
40        jobs.AddRange(dao.GetJobInfoForScheduler(x => x.OwnerUserId == userPriority.Id));
41      }
42
43      var taskJobRelations = tasks.Join(jobs,
44        task => task.JobId,
45        job => job.Id,
46        (task, job) => new { Task = task, JobInfo = job })
47        .OrderByDescending(x => x.Task.Priority)
48        .ToList();
49
50      var scheduledTasks = new List<TaskInfoForScheduler>();
51      int priorityIndex = 0;
52
53      if (count == 0 || count > taskJobRelations.Count) count = taskJobRelations.Count;
54
55      for (int i = 0; i < count; i++) {
56        var defaultEntry = taskJobRelations.First(); // search first task which is not included yet
57        var priorityEntries = taskJobRelations.Where(x => x.JobInfo.OwnerUserId == userPriorities[priorityIndex].Id).ToArray(); // search for tasks with desired user priority
58        while (!priorityEntries.Any() && priorityIndex < userPriorities.Length - 1) {
59          priorityIndex++;
60          priorityEntries = taskJobRelations.Where(x => x.JobInfo.OwnerUserId == userPriorities[priorityIndex].Id).ToArray();
61        }
62        if (priorityEntries.Any()) { // tasks with desired user priority found
63          var priorityEntry = priorityEntries.OrderByDescending(x => x.Task.Priority).ThenBy(x => x.JobInfo.DateCreated).First();
64          if (defaultEntry.Task.Priority <= priorityEntry.Task.Priority) {
65            taskJobRelations.Remove(priorityEntry);
66            scheduledTasks.Add(priorityEntry.Task);
67            UpdateUserPriority(userPriorities[priorityIndex]);
68            priorityIndex++;
69          } else { // there are other tasks with higher priorities
70            taskJobRelations.Remove(defaultEntry);
71            scheduledTasks.Add(defaultEntry.Task);
72          }
73        } else {
74          taskJobRelations.Remove(defaultEntry);
75          scheduledTasks.Add(defaultEntry.Task);
76        }
77
78        if (priorityIndex >= (userPriorities.Length - 1)) priorityIndex = 0;
79      }
80
81      return scheduledTasks;
82    }
83
84    private void UpdateUserPriority(UserPriority up) {
85      up.DateEnqueued = DateTime.Now;
86      dao.EnqueueUserPriority(up);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.