Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Grid/EngineStore.cs @ 254

Last change on this file since 254 was 254, checked in by gkronber, 16 years ago

removed caching of results in engine-store. kept deletion mechanism for expired results to delete results that are never retrieved from the grid-server (ticket #149)

File size: 7.1 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      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          byte[] result = results[guid];
155          results.Remove(guid);
156          return result;
157        } else {
158          // 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
159          if(!waitHandles.ContainsKey(guid)) return null;
160          // otherwise we have a wait-handle and can wait for the result
161          ManualResetEvent waitHandle = waitHandles[guid];
162          // wait
163          if(waitHandle.WaitOne(timeout, true)) {
164            // ok got the result in within the wait time => close and remove the wait-hande and return the result
165            waitHandle.Close();
166            waitHandles.Remove(guid);
167            byte[] result = results[guid];
168            return result;
169          } else {
170            // no result yet, check for which jobs we waited too long and requeue those jobs
171            List<Guid> expiredJobs = FindExpiredJobs(DateTime.Now.AddMinutes(-RUNNING_JOB_EXPIRY_TIME_MIN));
172            foreach(Guid expiredGuid in expiredJobs) {
173              engineList.Insert(0, expiredGuid);
174              waitingEngines[expiredGuid] = runningEngines[expiredGuid];
175              runningEngines.Remove(expiredGuid);
176              runningEngineDate.Remove(expiredGuid);
177            }
178            return null;
179          }
180        }
181      }
182    }
183
184    internal void AbortEngine(Guid guid) {
185      throw new NotImplementedException();
186    }
187
188    internal JobState JobState(Guid guid) {
189      lock(bigLock) {
190        if(waitingEngines.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Waiting;
191        else if(waitHandles.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Busy;
192        else if(results.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Finished;
193        else return HeuristicLab.Grid.JobState.Unkown;
194      }
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.