1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.CEDMA.DB;
|
---|
27 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using System.Threading;
|
---|
30 | using HeuristicLab.CEDMA.Core;
|
---|
31 | using HeuristicLab.Grid;
|
---|
32 | using System.Diagnostics;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.CEDMA.Server {
|
---|
35 | public class RunScheduler {
|
---|
36 | private class Job {
|
---|
37 | public long AgentId;
|
---|
38 | public WaitHandle WaitHandle;
|
---|
39 | public AtomicOperation Operation;
|
---|
40 | }
|
---|
41 | private Database database;
|
---|
42 | private JobManager jobManager;
|
---|
43 | private const int RELEASE_INTERVAL = 5;
|
---|
44 | private object remoteCommLock = new object();
|
---|
45 | private object queueLock = new object();
|
---|
46 | private Queue<Job> jobQueue;
|
---|
47 |
|
---|
48 | public RunScheduler(Database database, JobManager jobManager) {
|
---|
49 | this.database = database;
|
---|
50 | this.jobManager = jobManager;
|
---|
51 | jobQueue = new Queue<Job>();
|
---|
52 | Thread resultsGatheringThread = new Thread(GatherResults);
|
---|
53 | resultsGatheringThread.Start();
|
---|
54 | }
|
---|
55 | public void Run() {
|
---|
56 | while(true) {
|
---|
57 | ReleaseWaitingRuns();
|
---|
58 | Thread.Sleep(TimeSpan.FromSeconds(RELEASE_INTERVAL));
|
---|
59 | }
|
---|
60 | }
|
---|
61 | private void ReleaseWaitingRuns() {
|
---|
62 | IEnumerable<AgentEntry> agents;
|
---|
63 | lock(remoteCommLock) {
|
---|
64 | agents = database.GetAgents(ProcessStatus.Waiting).Where(a=>!a.ControllerAgent);
|
---|
65 | }
|
---|
66 | foreach(AgentEntry entry in agents) {
|
---|
67 | IOperatorGraph opGraph = (IOperatorGraph)DbPersistenceManager.Restore(entry.RawData);
|
---|
68 | AtomicOperation op = new AtomicOperation(opGraph.InitialOperator, new Scope());
|
---|
69 | WaitHandle wHandle;
|
---|
70 | lock(remoteCommLock) {
|
---|
71 | wHandle = jobManager.BeginExecuteOperation(op.Scope, op);
|
---|
72 | database.UpdateAgent(entry.Id, ProcessStatus.Active);
|
---|
73 | }
|
---|
74 |
|
---|
75 | Job job = new Job();
|
---|
76 | job.AgentId = entry.Id;
|
---|
77 | job.Operation = op;
|
---|
78 | job.WaitHandle = wHandle;
|
---|
79 |
|
---|
80 | lock(queueLock) {
|
---|
81 | jobQueue.Enqueue(job);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void GatherResults() {
|
---|
87 | try {
|
---|
88 | while(true) {
|
---|
89 | int runningJobs;
|
---|
90 | lock(queueLock) runningJobs = jobQueue.Count;
|
---|
91 | if(runningJobs==0) Thread.Sleep(1000); // TASK: replace with waithandle
|
---|
92 | else {
|
---|
93 | Job job;
|
---|
94 | lock(queueLock) {
|
---|
95 | job = jobQueue.Dequeue();
|
---|
96 | }
|
---|
97 | job.WaitHandle.WaitOne();
|
---|
98 | job.WaitHandle.Close();
|
---|
99 | lock(remoteCommLock) {
|
---|
100 | jobManager.EndExecuteOperation(job.Operation);
|
---|
101 | database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | } finally {
|
---|
106 | Debug.Assert(false); // make sure we are notified when this thread is killed while debugging
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|