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.Text;
|
---|
25 | using System.Threading;
|
---|
26 | using System.ServiceModel;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Grid {
|
---|
29 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
|
---|
30 | public class EngineStore : IEngineStore {
|
---|
31 | private List<Guid> engineList;
|
---|
32 | private Dictionary<Guid, byte[]> waitingEngines;
|
---|
33 | private Dictionary<Guid, byte[]> runningEngines;
|
---|
34 | private Dictionary<Guid, ManualResetEvent> waitHandles;
|
---|
35 | private Dictionary<Guid, byte[]> results;
|
---|
36 | private Dictionary<Guid, DateTime> resultDate;
|
---|
37 | private Dictionary<Guid, DateTime> runningEngineDate;
|
---|
38 | private const int RESULT_EXPIRY_TIME_MIN = 10;
|
---|
39 | private const int RUNNING_JOB_EXPIRY_TIME_MIN = 10;
|
---|
40 | private object bigLock;
|
---|
41 | public int WaitingJobs {
|
---|
42 | get {
|
---|
43 | return waitingEngines.Count;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public int RunningJobs {
|
---|
48 | get {
|
---|
49 | return waitHandles.Count;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public int WaitingResults {
|
---|
54 | get {
|
---|
55 | return results.Count;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public EngineStore() {
|
---|
60 | engineList = new List<Guid>();
|
---|
61 | waitingEngines = new Dictionary<Guid, byte[]>();
|
---|
62 | runningEngines = new Dictionary<Guid, byte[]>();
|
---|
63 | waitHandles = new Dictionary<Guid, ManualResetEvent>();
|
---|
64 | results = new Dictionary<Guid, byte[]>();
|
---|
65 | resultDate = new Dictionary<Guid, DateTime>();
|
---|
66 | runningEngineDate = new Dictionary<Guid, DateTime>();
|
---|
67 | bigLock = new object();
|
---|
68 |
|
---|
69 | NetTcpBinding binding = new NetTcpBinding();
|
---|
70 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
71 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
72 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
73 | binding.Security.Mode = SecurityMode.None;
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | public bool TryTakeEngine(out Guid guid, out byte[] engine) {
|
---|
78 | lock(bigLock) {
|
---|
79 | if(engineList.Count == 0) {
|
---|
80 | guid = Guid.Empty;
|
---|
81 | engine = null;
|
---|
82 | return false;
|
---|
83 | } else {
|
---|
84 | guid = engineList[0];
|
---|
85 | engineList.RemoveAt(0);
|
---|
86 | engine = waitingEngines[guid];
|
---|
87 | waitingEngines.Remove(guid);
|
---|
88 | runningEngines[guid] = engine;
|
---|
89 | runningEngineDate[guid] = DateTime.Now;
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void StoreResult(Guid guid, byte[] result) {
|
---|
96 | lock(bigLock) {
|
---|
97 | // clear old results
|
---|
98 | List<Guid> expiredResults = FindExpiredResults(DateTime.Now.AddMinutes(-RESULT_EXPIRY_TIME_MIN));
|
---|
99 | foreach(Guid expiredGuid in expiredResults) {
|
---|
100 | results.Remove(expiredGuid);
|
---|
101 | waitHandles.Remove(expiredGuid);
|
---|
102 | resultDate.Remove(expiredGuid);
|
---|
103 | }
|
---|
104 | // add the new result
|
---|
105 | runningEngines.Remove(guid);
|
---|
106 | runningEngineDate.Remove(guid);
|
---|
107 | results[guid] = result;
|
---|
108 | resultDate[guid] = DateTime.Now;
|
---|
109 | waitHandles[guid].Set();
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private List<Guid> FindExpiredResults(DateTime expirationDate) {
|
---|
114 | List<Guid> expiredResults = new List<Guid>();
|
---|
115 | foreach(Guid guid in results.Keys) {
|
---|
116 | if(resultDate[guid] < expirationDate) {
|
---|
117 | expiredResults.Add(guid);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return expiredResults;
|
---|
121 | }
|
---|
122 | private List<Guid> FindExpiredJobs(DateTime expirationDate) {
|
---|
123 | List<Guid> expiredJobs = new List<Guid>();
|
---|
124 | foreach(Guid guid in runningEngines.Keys) {
|
---|
125 | if(runningEngineDate[guid] < expirationDate) {
|
---|
126 | expiredJobs.Add(guid);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | return expiredJobs;
|
---|
130 | }
|
---|
131 |
|
---|
132 | internal void AddEngine(Guid guid, byte[] engine) {
|
---|
133 | lock(bigLock) {
|
---|
134 | engineList.Add(guid);
|
---|
135 | waitingEngines.Add(guid, engine);
|
---|
136 | waitHandles.Add(guid, new ManualResetEvent(false));
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | internal byte[] GetResult(Guid guid) {
|
---|
141 | return GetResult(guid, System.Threading.Timeout.Infinite);
|
---|
142 | }
|
---|
143 |
|
---|
144 | internal byte[] GetResult(Guid guid, int timeout) {
|
---|
145 | lock(bigLock) {
|
---|
146 | // result already available
|
---|
147 | if(results.ContainsKey(guid)) {
|
---|
148 | // if the wait-handle for this result is still alive then close and remove it
|
---|
149 | if(waitHandles.ContainsKey(guid)) {
|
---|
150 | ManualResetEvent waitHandle = waitHandles[guid];
|
---|
151 | waitHandle.Close();
|
---|
152 | waitHandles.Remove(guid);
|
---|
153 | }
|
---|
154 | return results[guid];
|
---|
155 | } else {
|
---|
156 | // result not yet available, if there is also no wait-handle for that result then we will never have a result and can return null
|
---|
157 | if(!waitHandles.ContainsKey(guid)) return null;
|
---|
158 | // otherwise we have a wait-handle and can wait for the result
|
---|
159 | ManualResetEvent waitHandle = waitHandles[guid];
|
---|
160 | // wait
|
---|
161 | if(waitHandle.WaitOne(timeout, true)) {
|
---|
162 | // ok got the result in within the wait time => close and remove the wait-hande and return the result
|
---|
163 | waitHandle.Close();
|
---|
164 | waitHandles.Remove(guid);
|
---|
165 | byte[] result = results[guid];
|
---|
166 | return result;
|
---|
167 | } else {
|
---|
168 | // no result yet, check for which jobs we waited too long and requeue those jobs
|
---|
169 | List<Guid> expiredJobs = FindExpiredJobs(DateTime.Now.AddMinutes(-RUNNING_JOB_EXPIRY_TIME_MIN));
|
---|
170 | foreach(Guid expiredGuid in expiredJobs) {
|
---|
171 | engineList.Insert(0, expiredGuid);
|
---|
172 | waitingEngines[expiredGuid] = runningEngines[expiredGuid];
|
---|
173 | runningEngines.Remove(expiredGuid);
|
---|
174 | runningEngineDate.Remove(expiredGuid);
|
---|
175 | }
|
---|
176 | return null;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | internal void AbortEngine(Guid guid) {
|
---|
183 | throw new NotImplementedException();
|
---|
184 | }
|
---|
185 |
|
---|
186 | internal JobState JobState(Guid guid) {
|
---|
187 | lock(bigLock) {
|
---|
188 | if(waitingEngines.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Waiting;
|
---|
189 | else if(waitHandles.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Busy;
|
---|
190 | else if(results.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Finished;
|
---|
191 | else return HeuristicLab.Grid.JobState.Unkown;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|