Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive/3.3/Scheduler/RoundRobinTaskScheduler.cs @ 12271

Last change on this file since 12271 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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