[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;
|
---|
| 30 | using HeuristicLab.Grid;
|
---|
[390] | 31 | using System.Diagnostics;
|
---|
[393] | 32 | using HeuristicLab.Data;
|
---|
[419] | 33 | using HeuristicLab.CEDMA.Core;
|
---|
| 34 | using HeuristicLab.Operators;
|
---|
[375] | 35 |
|
---|
| 36 | namespace HeuristicLab.CEDMA.Server {
|
---|
| 37 | public class RunScheduler {
|
---|
[390] | 38 | private class Job {
|
---|
| 39 | public long AgentId;
|
---|
| 40 | public WaitHandle WaitHandle;
|
---|
| 41 | public AtomicOperation Operation;
|
---|
| 42 | }
|
---|
[393] | 43 | private string serverUri;
|
---|
[375] | 44 | private Database database;
|
---|
| 45 | private JobManager jobManager;
|
---|
| 46 | private const int RELEASE_INTERVAL = 5;
|
---|
[378] | 47 | private object remoteCommLock = new object();
|
---|
[390] | 48 | private object queueLock = new object();
|
---|
| 49 | private Queue<Job> jobQueue;
|
---|
[392] | 50 | private AutoResetEvent runningJobs = new AutoResetEvent(false);
|
---|
[378] | 51 |
|
---|
[393] | 52 | public RunScheduler(Database database, JobManager jobManager, string serverUri) {
|
---|
[375] | 53 | this.database = database;
|
---|
| 54 | this.jobManager = jobManager;
|
---|
[393] | 55 | this.serverUri = serverUri;
|
---|
[390] | 56 | jobQueue = new Queue<Job>();
|
---|
[380] | 57 | Thread resultsGatheringThread = new Thread(GatherResults);
|
---|
| 58 | resultsGatheringThread.Start();
|
---|
[375] | 59 | }
|
---|
| 60 | public void Run() {
|
---|
| 61 | while(true) {
|
---|
| 62 | ReleaseWaitingRuns();
|
---|
| 63 | Thread.Sleep(TimeSpan.FromSeconds(RELEASE_INTERVAL));
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | private void ReleaseWaitingRuns() {
|
---|
[398] | 67 | ICollection<AgentEntry> agents;
|
---|
[378] | 68 | lock(remoteCommLock) {
|
---|
[398] | 69 | agents = database.GetAgents(ProcessStatus.Waiting);
|
---|
[378] | 70 | }
|
---|
[390] | 71 | foreach(AgentEntry entry in agents) {
|
---|
[393] | 72 | Scope scope = new Scope();
|
---|
| 73 | // initialize CEDMA variables for the execution of the agent
|
---|
| 74 | scope.AddVariable(new Variable("AgentId", new IntData((int)entry.Id)));
|
---|
| 75 | scope.AddVariable(new Variable("CedmaServerUri", new StringData(serverUri)));
|
---|
[403] | 76 | IOperatorGraph opGraph = (IOperatorGraph)PersistenceManager.RestoreFromGZip(entry.RawData);
|
---|
[419] | 77 |
|
---|
[420] | 78 | PatchLinks(opGraph, new Dictionary<long, IOperator>());
|
---|
[419] | 79 |
|
---|
| 80 | AtomicOperation operation = new AtomicOperation(opGraph.InitialOperator, scope);
|
---|
[378] | 81 | WaitHandle wHandle;
|
---|
| 82 | lock(remoteCommLock) {
|
---|
[419] | 83 | wHandle = jobManager.BeginExecuteOperation(operation.Scope, operation);
|
---|
[390] | 84 | database.UpdateAgent(entry.Id, ProcessStatus.Active);
|
---|
[378] | 85 | }
|
---|
[375] | 86 |
|
---|
[390] | 87 | Job job = new Job();
|
---|
| 88 | job.AgentId = entry.Id;
|
---|
[419] | 89 | job.Operation = operation;
|
---|
[390] | 90 | job.WaitHandle = wHandle;
|
---|
| 91 |
|
---|
| 92 | lock(queueLock) {
|
---|
| 93 | jobQueue.Enqueue(job);
|
---|
[392] | 94 | runningJobs.Set();
|
---|
[380] | 95 | }
|
---|
[378] | 96 | }
|
---|
| 97 | }
|
---|
[375] | 98 |
|
---|
[420] | 99 | private void PatchLinks(IOperatorGraph opGraph, Dictionary<long, IOperator> patchedOperators) {
|
---|
| 100 | Dictionary<IOperator, IOperator> patchDictionary = new Dictionary<IOperator, IOperator>();
|
---|
| 101 | foreach(IOperator op in opGraph.Operators) {
|
---|
| 102 | IOperator patched = PatchLinks(op, patchedOperators);
|
---|
| 103 | patchDictionary.Add(op, patched);
|
---|
| 104 | }
|
---|
| 105 | foreach(KeyValuePair<IOperator, IOperator> p in patchDictionary) {
|
---|
| 106 | IOperator original = p.Key;
|
---|
| 107 | IOperator patch = p.Value;
|
---|
| 108 | if(original != patch) {
|
---|
| 109 | foreach(IOperator subOperator in original.SubOperators) {
|
---|
| 110 | patch.AddSubOperator(subOperator);
|
---|
| 111 | }
|
---|
| 112 | if(opGraph.InitialOperator == original)
|
---|
| 113 | opGraph.InitialOperator = patch;
|
---|
| 114 | opGraph.RemoveOperator(original.Guid);
|
---|
| 115 | opGraph.AddOperator(patch);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private IOperator PatchLinks(IOperator op, Dictionary<long, IOperator> patchedOperators) {
|
---|
[419] | 121 | if(op is OperatorLink) {
|
---|
| 122 | OperatorLink link = op as OperatorLink;
|
---|
[420] | 123 | if(patchedOperators.ContainsKey(link.Id)) {
|
---|
| 124 | return patchedOperators[link.Id];
|
---|
| 125 | } else {
|
---|
| 126 | OperatorEntry targetEntry = database.GetOperator(link.Id);
|
---|
| 127 | IOperator target = (IOperator)PersistenceManager.RestoreFromGZip(targetEntry.RawData);
|
---|
| 128 | patchedOperators.Add(link.Id, target);
|
---|
| 129 | PatchLinks(target, patchedOperators);
|
---|
| 130 | return target;
|
---|
| 131 | }
|
---|
[419] | 132 | } else if(op is CombinedOperator) {
|
---|
[420] | 133 | PatchLinks(((CombinedOperator)op).OperatorGraph, patchedOperators);
|
---|
| 134 | return op;
|
---|
[419] | 135 | }
|
---|
[420] | 136 | return op;
|
---|
[419] | 137 | }
|
---|
| 138 |
|
---|
[380] | 139 | private void GatherResults() {
|
---|
[390] | 140 | try {
|
---|
| 141 | while(true) {
|
---|
[392] | 142 | Job job = null;
|
---|
| 143 | lock(queueLock) if(jobQueue.Count > 0) job = jobQueue.Dequeue();
|
---|
| 144 | if(job == null) runningJobs.WaitOne();
|
---|
[390] | 145 | else {
|
---|
| 146 | job.WaitHandle.WaitOne();
|
---|
| 147 | job.WaitHandle.Close();
|
---|
| 148 | lock(remoteCommLock) {
|
---|
[392] | 149 | try {
|
---|
| 150 | jobManager.EndExecuteOperation(job.Operation);
|
---|
| 151 | database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
|
---|
| 152 | } catch(JobExecutionException ex) {
|
---|
| 153 | database.UpdateAgent(job.AgentId, ProcessStatus.Error);
|
---|
| 154 | }
|
---|
[390] | 155 | }
|
---|
[380] | 156 | }
|
---|
| 157 | }
|
---|
[390] | 158 | } finally {
|
---|
| 159 | Debug.Assert(false); // make sure we are notified when this thread is killed while debugging
|
---|
[375] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|