[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)));
|
---|
[419] | 76 |
|
---|
[503] | 77 | byte[] rawData = database.GetAgentRawData(entry.Id);
|
---|
| 78 | IOperatorGraph opGraph = (IOperatorGraph)PersistenceManager.RestoreFromGZip(rawData);
|
---|
| 79 |
|
---|
[420] | 80 | PatchLinks(opGraph, new Dictionary<long, IOperator>());
|
---|
[419] | 81 |
|
---|
| 82 | AtomicOperation operation = new AtomicOperation(opGraph.InitialOperator, scope);
|
---|
[378] | 83 | WaitHandle wHandle;
|
---|
| 84 | lock(remoteCommLock) {
|
---|
[419] | 85 | wHandle = jobManager.BeginExecuteOperation(operation.Scope, operation);
|
---|
[390] | 86 | database.UpdateAgent(entry.Id, ProcessStatus.Active);
|
---|
[378] | 87 | }
|
---|
[375] | 88 |
|
---|
[390] | 89 | Job job = new Job();
|
---|
| 90 | job.AgentId = entry.Id;
|
---|
[419] | 91 | job.Operation = operation;
|
---|
[390] | 92 | job.WaitHandle = wHandle;
|
---|
| 93 |
|
---|
| 94 | lock(queueLock) {
|
---|
| 95 | jobQueue.Enqueue(job);
|
---|
[392] | 96 | runningJobs.Set();
|
---|
[380] | 97 | }
|
---|
[378] | 98 | }
|
---|
| 99 | }
|
---|
[375] | 100 |
|
---|
[420] | 101 | private void PatchLinks(IOperatorGraph opGraph, Dictionary<long, IOperator> patchedOperators) {
|
---|
| 102 | Dictionary<IOperator, IOperator> patchDictionary = new Dictionary<IOperator, IOperator>();
|
---|
| 103 | foreach(IOperator op in opGraph.Operators) {
|
---|
| 104 | IOperator patched = PatchLinks(op, patchedOperators);
|
---|
| 105 | patchDictionary.Add(op, patched);
|
---|
| 106 | }
|
---|
| 107 | foreach(KeyValuePair<IOperator, IOperator> p in patchDictionary) {
|
---|
| 108 | IOperator original = p.Key;
|
---|
| 109 | IOperator patch = p.Value;
|
---|
| 110 | if(original != patch) {
|
---|
| 111 | foreach(IOperator subOperator in original.SubOperators) {
|
---|
| 112 | patch.AddSubOperator(subOperator);
|
---|
| 113 | }
|
---|
| 114 | if(opGraph.InitialOperator == original)
|
---|
| 115 | opGraph.InitialOperator = patch;
|
---|
| 116 | opGraph.RemoveOperator(original.Guid);
|
---|
| 117 | opGraph.AddOperator(patch);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private IOperator PatchLinks(IOperator op, Dictionary<long, IOperator> patchedOperators) {
|
---|
[419] | 123 | if(op is OperatorLink) {
|
---|
| 124 | OperatorLink link = op as OperatorLink;
|
---|
[420] | 125 | if(patchedOperators.ContainsKey(link.Id)) {
|
---|
| 126 | return patchedOperators[link.Id];
|
---|
| 127 | } else {
|
---|
| 128 | OperatorEntry targetEntry = database.GetOperator(link.Id);
|
---|
| 129 | IOperator target = (IOperator)PersistenceManager.RestoreFromGZip(targetEntry.RawData);
|
---|
| 130 | patchedOperators.Add(link.Id, target);
|
---|
| 131 | PatchLinks(target, patchedOperators);
|
---|
| 132 | return target;
|
---|
| 133 | }
|
---|
[419] | 134 | } else if(op is CombinedOperator) {
|
---|
[420] | 135 | PatchLinks(((CombinedOperator)op).OperatorGraph, patchedOperators);
|
---|
| 136 | return op;
|
---|
[419] | 137 | }
|
---|
[420] | 138 | return op;
|
---|
[419] | 139 | }
|
---|
| 140 |
|
---|
[380] | 141 | private void GatherResults() {
|
---|
[390] | 142 | try {
|
---|
| 143 | while(true) {
|
---|
[392] | 144 | Job job = null;
|
---|
| 145 | lock(queueLock) if(jobQueue.Count > 0) job = jobQueue.Dequeue();
|
---|
| 146 | if(job == null) runningJobs.WaitOne();
|
---|
[390] | 147 | else {
|
---|
| 148 | job.WaitHandle.WaitOne();
|
---|
| 149 | job.WaitHandle.Close();
|
---|
| 150 | lock(remoteCommLock) {
|
---|
[392] | 151 | try {
|
---|
| 152 | jobManager.EndExecuteOperation(job.Operation);
|
---|
| 153 | database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
|
---|
| 154 | } catch(JobExecutionException ex) {
|
---|
| 155 | database.UpdateAgent(job.AgentId, ProcessStatus.Error);
|
---|
| 156 | }
|
---|
[390] | 157 | }
|
---|
[380] | 158 | }
|
---|
| 159 | }
|
---|
[390] | 160 | } finally {
|
---|
| 161 | Debug.Assert(false); // make sure we are notified when this thread is killed while debugging
|
---|
[375] | 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|