[375] | 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;
|
---|
[377] | 30 | using HeuristicLab.CEDMA.Core;
|
---|
[375] | 31 | using HeuristicLab.Grid;
|
---|
[390] | 32 | using System.Diagnostics;
|
---|
[375] | 33 |
|
---|
| 34 | namespace HeuristicLab.CEDMA.Server {
|
---|
| 35 | public class RunScheduler {
|
---|
[390] | 36 | private class Job {
|
---|
| 37 | public long AgentId;
|
---|
| 38 | public WaitHandle WaitHandle;
|
---|
| 39 | public AtomicOperation Operation;
|
---|
| 40 | }
|
---|
[375] | 41 | private Database database;
|
---|
| 42 | private JobManager jobManager;
|
---|
| 43 | private const int RELEASE_INTERVAL = 5;
|
---|
[378] | 44 | private object remoteCommLock = new object();
|
---|
[390] | 45 | private object queueLock = new object();
|
---|
| 46 | private Queue<Job> jobQueue;
|
---|
[378] | 47 |
|
---|
[375] | 48 | public RunScheduler(Database database, JobManager jobManager) {
|
---|
| 49 | this.database = database;
|
---|
| 50 | this.jobManager = jobManager;
|
---|
[390] | 51 | jobQueue = new Queue<Job>();
|
---|
[380] | 52 | Thread resultsGatheringThread = new Thread(GatherResults);
|
---|
| 53 | resultsGatheringThread.Start();
|
---|
[375] | 54 | }
|
---|
| 55 | public void Run() {
|
---|
| 56 | while(true) {
|
---|
| 57 | ReleaseWaitingRuns();
|
---|
| 58 | Thread.Sleep(TimeSpan.FromSeconds(RELEASE_INTERVAL));
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | private void ReleaseWaitingRuns() {
|
---|
[390] | 62 | IEnumerable<AgentEntry> agents;
|
---|
[378] | 63 | lock(remoteCommLock) {
|
---|
[390] | 64 | agents = database.GetAgents(ProcessStatus.Waiting).Where(a=>!a.ControllerAgent);
|
---|
[378] | 65 | }
|
---|
[390] | 66 | foreach(AgentEntry entry in agents) {
|
---|
[375] | 67 | IOperatorGraph opGraph = (IOperatorGraph)DbPersistenceManager.Restore(entry.RawData);
|
---|
[390] | 68 | AtomicOperation op = new AtomicOperation(opGraph.InitialOperator, new Scope());
|
---|
[378] | 69 | WaitHandle wHandle;
|
---|
| 70 | lock(remoteCommLock) {
|
---|
[390] | 71 | wHandle = jobManager.BeginExecuteOperation(op.Scope, op);
|
---|
| 72 | database.UpdateAgent(entry.Id, ProcessStatus.Active);
|
---|
[378] | 73 | }
|
---|
[375] | 74 |
|
---|
[390] | 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);
|
---|
[380] | 82 | }
|
---|
[378] | 83 | }
|
---|
| 84 | }
|
---|
[375] | 85 |
|
---|
[380] | 86 | private void GatherResults() {
|
---|
[390] | 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 | }
|
---|
[380] | 103 | }
|
---|
| 104 | }
|
---|
[390] | 105 | } finally {
|
---|
| 106 | Debug.Assert(false); // make sure we are notified when this thread is killed while debugging
|
---|
[375] | 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|