Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Grid/EngineStore.cs @ 8614

Last change on this file since 8614 was 279, checked in by gkronber, 17 years ago

merged changesets r219:r228, r240, r241:258, r263:265, r267,r268, r269 from trunk into the HL3 stable branch

File size: 6.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Threading;
26using System.ServiceModel;
27
28namespace 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 runningEngines.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
70    public bool TryTakeEngine(out Guid guid, out byte[] engine) {
71      lock(bigLock) {
72        if(engineList.Count == 0) {
73          guid = Guid.Empty;
74          engine = null;
75          return false;
76        } else {
77          guid = engineList[0];
78          engineList.RemoveAt(0);
79          engine = waitingEngines[guid];
80          waitingEngines.Remove(guid);
81          runningEngines[guid] = engine;
82          runningEngineDate[guid] = DateTime.Now;
83          return true;
84        }
85      }
86    }
87
88    public void StoreResult(Guid guid, byte[] result) {
89      lock(bigLock) {
90        // clear old results
91        List<Guid> expiredResults = FindExpiredResults(DateTime.Now.AddMinutes(-RESULT_EXPIRY_TIME_MIN));
92        foreach(Guid expiredGuid in expiredResults) {
93          results.Remove(expiredGuid);
94          waitHandles.Remove(expiredGuid);
95          resultDate.Remove(expiredGuid);
96        }
97        // add the new result
98        runningEngines.Remove(guid);
99        runningEngineDate.Remove(guid);
100        results[guid] = result;
101        resultDate[guid] = DateTime.Now;
102        waitHandles[guid].Set();
103      }
104    }
105
106    private List<Guid> FindExpiredResults(DateTime expirationDate) {
107      List<Guid> expiredResults = new List<Guid>();
108      foreach(Guid guid in results.Keys) {
109        if(resultDate[guid] < expirationDate) {
110          expiredResults.Add(guid);
111        }
112      }
113      return expiredResults;
114    }
115    private List<Guid> FindExpiredJobs(DateTime expirationDate) {
116      List<Guid> expiredJobs = new List<Guid>();
117      foreach(Guid guid in runningEngines.Keys) {
118        if(runningEngineDate[guid] < expirationDate) {
119          expiredJobs.Add(guid);
120        }
121      }
122      return expiredJobs;
123    }
124
125    internal void AddEngine(Guid guid, byte[] engine) {
126      lock(bigLock) {
127        engineList.Add(guid);
128        waitingEngines.Add(guid, engine);
129        waitHandles.Add(guid, new ManualResetEvent(false));
130      }
131    }
132
133    internal byte[] GetResult(Guid guid) {
134      return GetResult(guid, System.Threading.Timeout.Infinite);
135    }
136
137    internal byte[] GetResult(Guid guid, int timeout) {
138      lock(bigLock) {
139        // result already available
140        if(results.ContainsKey(guid)) {
141          // if the wait-handle for this result is still alive then close and remove it
142          if(waitHandles.ContainsKey(guid)) {
143            ManualResetEvent waitHandle = waitHandles[guid];
144            waitHandle.Close();
145            waitHandles.Remove(guid);
146          }
147          byte[] result = results[guid];
148          results.Remove(guid);
149          return result;
150        } else {
151          // 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
152          if(!waitHandles.ContainsKey(guid)) return null;
153          // otherwise we have a wait-handle and can wait for the result
154          ManualResetEvent waitHandle = waitHandles[guid];
155          // wait
156          if(waitHandle.WaitOne(timeout, true)) {
157            // ok got the result in within the wait time => close and remove the wait-hande and return the result
158            waitHandle.Close();
159            waitHandles.Remove(guid);
160            byte[] result = results[guid];
161            return result;
162          } else {
163            // no result yet, check for which jobs we waited too long and requeue those jobs
164            List<Guid> expiredJobs = FindExpiredJobs(DateTime.Now.AddMinutes(-RUNNING_JOB_EXPIRY_TIME_MIN));
165            foreach(Guid expiredGuid in expiredJobs) {
166              engineList.Insert(0, expiredGuid);
167              waitingEngines[expiredGuid] = runningEngines[expiredGuid];
168              runningEngines.Remove(expiredGuid);
169              runningEngineDate.Remove(expiredGuid);
170            }
171            return null;
172          }
173        }
174      }
175    }
176
177    internal void AbortEngine(Guid guid) {
178      throw new NotImplementedException();
179    }
180
181    internal JobState JobState(Guid guid) {
182      lock(bigLock) {
183        if(waitingEngines.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Waiting;
184        else if(waitHandles.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Busy;
185        else if(results.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Finished;
186        else return HeuristicLab.Grid.JobState.Unkown;
187      }
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.