Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Services.Hive/3.3/Scheduler/RoundRobinTaskScheduler.cs @ 17514

Last change on this file since 17514 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Services.Hive.DataAccess.Interfaces;
26using DA = HeuristicLab.Services.Hive.DataAccess;
27
28namespace HeuristicLab.Services.Hive {
29  public class RoundRobinTaskScheduler : ITaskScheduler {
30    private IPersistenceManager PersistenceManager {
31      get { return ServiceLocator.Instance.PersistenceManager; }
32    }
33
34    public IEnumerable<TaskInfoForScheduler> Schedule(IEnumerable<TaskInfoForScheduler> tasks, int count = 1) {
35      if (!tasks.Any()) return Enumerable.Empty<TaskInfoForScheduler>();
36
37      var pm = PersistenceManager;
38      var userPriorityDao = pm.UserPriorityDao;
39      var jobDao = pm.JobDao;
40
41      var userPriorities = pm.UseTransaction(() => userPriorityDao.GetAll()
42        .OrderBy(x => x.DateEnqueued)
43        .ToArray()
44      );
45
46      var userIds = userPriorities.Select(x => x.UserId).ToList();
47      var jobs = pm.UseTransaction(() => {
48        return jobDao.GetAll()
49          .Where(x => userIds.Contains(x.OwnerUserId))
50          .Select(x => new {
51            Id = x.JobId,
52            DateCreated = x.DateCreated,
53            OwnerUserId = x.OwnerUserId
54          })
55          .ToList();
56      });
57
58      var taskJobRelations = tasks.Join(jobs,
59        task => task.JobId,
60        job => job.Id,
61        (task, job) => new { Task = task, JobInfo = job })
62        .OrderByDescending(x => x.Task.Priority)
63        .ToList();
64
65      var scheduledTasks = new List<TaskInfoForScheduler>();
66      int priorityIndex = 0;
67
68      if (count == 0 || count > taskJobRelations.Count) count = taskJobRelations.Count;
69
70      for (int i = 0; i < count; i++) {
71        var defaultEntry = taskJobRelations.First(); // search first task which is not included yet
72        var priorityEntries = taskJobRelations.Where(x => x.JobInfo.OwnerUserId == userPriorities[priorityIndex].UserId).ToArray(); // search for tasks with desired user priority
73        while (!priorityEntries.Any() && priorityIndex < userPriorities.Length - 1) {
74          priorityIndex++;
75          priorityEntries = taskJobRelations.Where(x => x.JobInfo.OwnerUserId == userPriorities[priorityIndex].UserId).ToArray();
76        }
77        if (priorityEntries.Any()) { // tasks with desired user priority found
78          var priorityEntry = priorityEntries.OrderByDescending(x => x.Task.Priority).ThenBy(x => x.JobInfo.DateCreated).First();
79          if (defaultEntry.Task.Priority <= priorityEntry.Task.Priority) {
80            taskJobRelations.Remove(priorityEntry);
81            scheduledTasks.Add(priorityEntry.Task);
82            UpdateUserPriority(pm, userPriorities[priorityIndex]);
83            priorityIndex++;
84          } else { // there are other tasks with higher priorities
85            taskJobRelations.Remove(defaultEntry);
86            scheduledTasks.Add(defaultEntry.Task);
87          }
88        } else {
89          taskJobRelations.Remove(defaultEntry);
90          scheduledTasks.Add(defaultEntry.Task);
91        }
92        if (priorityIndex >= (userPriorities.Length - 1)) priorityIndex = 0;
93      }
94      return scheduledTasks;
95
96    }
97
98    private void UpdateUserPriority(IPersistenceManager pm, DA.UserPriority up) {
99      pm.UseTransaction(() => {
100        up.DateEnqueued = DateTime.Now;
101        pm.SubmitChanges();
102      });
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.