Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13100 was 12878, checked in by ascheibe, 9 years ago

#2388 merged hive statistics branch into trunk

File size: 4.1 KB
RevLine 
[12691]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 {
[12861]29  public class RoundRobinTaskScheduler : ITaskScheduler {
[12691]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
[12858]37      var pm = PersistenceManager;
38      var userPriorityDao = pm.UserPriorityDao;
39      var jobDao = pm.JobDao;
[12691]40
[12858]41      var userPriorities = pm.UseTransaction(() => userPriorityDao.GetAll()
42        .OrderBy(x => x.DateEnqueued)
43        .ToArray()
44      );
[12691]45
[12858]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          })
[12691]55          .ToList();
[12858]56      });
[12691]57
[12858]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();
[12691]64
[12858]65      var scheduledTasks = new List<TaskInfoForScheduler>();
66      int priorityIndex = 0;
[12691]67
[12858]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]);
[12691]83            priorityIndex++;
[12858]84          } else { // there are other tasks with higher priorities
[12691]85            taskJobRelations.Remove(defaultEntry);
86            scheduledTasks.Add(defaultEntry.Task);
87          }
[12858]88        } else {
89          taskJobRelations.Remove(defaultEntry);
90          scheduledTasks.Add(defaultEntry.Task);
[12691]91        }
[12858]92        if (priorityIndex >= (userPriorities.Length - 1)) priorityIndex = 0;
[12691]93      }
[12858]94      return scheduledTasks;
95
[12691]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.