Changeset 219 for trunk/sources/HeuristicLab.Grid/EngineStore.cs
- Timestamp:
- 05/06/08 12:22:58 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Grid/EngineStore.cs
r35 r219 31 31 private List<Guid> engineList; 32 32 private Dictionary<Guid, byte[]> waitingEngines; 33 private Dictionary<Guid, byte[]> runningEngines;34 33 private Dictionary<Guid, ManualResetEvent> waitHandles; 35 34 private Dictionary<Guid, byte[]> results; 36 private Dictionary<Guid, string> runningClients;35 private Dictionary<Guid, DateTime> resultDate; 37 36 private object bigLock; 38 37 private ChannelFactory<IClient> clientChannelFactory; … … 45 44 public int RunningJobs { 46 45 get { 47 return runningEngines.Count;46 return waitHandles.Count; 48 47 } 49 48 } … … 58 57 engineList = new List<Guid>(); 59 58 waitingEngines = new Dictionary<Guid, byte[]>(); 60 runningEngines = new Dictionary<Guid, byte[]>();61 runningClients = new Dictionary<Guid, string>();62 59 waitHandles = new Dictionary<Guid, ManualResetEvent>(); 63 60 results = new Dictionary<Guid, byte[]>(); 61 resultDate = new Dictionary<Guid, DateTime>(); 64 62 bigLock = new object(); 65 63 … … 73 71 } 74 72 75 public bool TryTakeEngine( string clientUrl,out Guid guid, out byte[] engine) {73 public bool TryTakeEngine(out Guid guid, out byte[] engine) { 76 74 lock(bigLock) { 77 75 if(engineList.Count == 0) { … … 84 82 engine = waitingEngines[guid]; 85 83 waitingEngines.Remove(guid); 86 runningEngines[guid] = engine;87 runningClients[guid] = clientUrl;88 84 return true; 89 85 } … … 93 89 public void StoreResult(Guid guid, byte[] result) { 94 90 lock(bigLock) { 95 if(!runningEngines.ContainsKey(guid)) return; // ignore result when the engine is not known to be running 96 97 runningEngines.Remove(guid); 98 runningClients.Remove(guid); 91 // clear old results 92 List<Guid> expiredResults = FindExpiredResults(DateTime.Now.AddHours(-1.0)); 93 foreach(Guid expiredGuid in expiredResults) { 94 results.Remove(expiredGuid); 95 waitHandles.Remove(expiredGuid); 96 resultDate.Remove(expiredGuid); 97 } 98 // add the new result 99 99 results[guid] = result; 100 resultDate[guid] = DateTime.Now; 100 101 waitHandles[guid].Set(); 101 102 } 103 } 104 105 private List<Guid> FindExpiredResults(DateTime expirationDate) { 106 List<Guid> expiredResults = new List<Guid>(); 107 foreach(Guid guid in results.Keys) { 108 if(resultDate[guid] < expirationDate) { 109 expiredResults.Add(guid); 110 } 111 } 112 return expiredResults; 102 113 } 103 114 … … 113 124 return GetResult(guid, System.Threading.Timeout.Infinite); 114 125 } 126 115 127 internal byte[] GetResult(Guid guid, int timeout) { 116 128 lock(bigLock) { 117 if(waitHandles.ContainsKey(guid)) { 129 // result already available 130 if(results.ContainsKey(guid)) { 131 // if the wait-handle for this result is still alive then close and remove it 132 if(waitHandles.ContainsKey(guid)) { 133 ManualResetEvent waitHandle = waitHandles[guid]; 134 waitHandle.Close(); 135 waitHandles.Remove(guid); 136 } 137 return results[guid]; 138 } else { 139 // 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 140 if(!waitHandles.ContainsKey(guid)) return null; 141 142 // otherwise we have a wait-handle and can wait for the result 118 143 ManualResetEvent waitHandle = waitHandles[guid]; 144 // wait 119 145 if(waitHandle.WaitOne(timeout, true)) { 146 // ok got the result in within the wait time => close and remove the wait-hande and return the result 120 147 waitHandle.Close(); 121 148 waitHandles.Remove(guid); 122 149 byte[] result = results[guid]; 123 results.Remove(guid);124 150 return result; 125 151 } else { 152 // no result yet return without result 126 153 return null; 127 154 } 128 } else {129 return null;130 155 } 131 156 } … … 133 158 134 159 internal void AbortEngine(Guid guid) { 135 string clientUrl = "";136 160 lock(bigLock) { 137 if(runningClients.ContainsKey(guid)) { 138 clientUrl = runningClients[guid]; 139 IClient client = clientChannelFactory.CreateChannel(new EndpointAddress(clientUrl)); 140 client.Abort(guid); 141 } else if(waitingEngines.ContainsKey(guid)) { 161 if(waitingEngines.ContainsKey(guid)) { 142 162 byte[] engine = waitingEngines[guid]; 143 163 waitingEngines.Remove(guid); … … 148 168 } 149 169 } 170 171 internal JobState JobState(Guid guid) { 172 lock(bigLock) { 173 if(waitingEngines.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Waiting; 174 else if(waitHandles.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Busy; 175 else if(results.ContainsKey(guid)) return HeuristicLab.Grid.JobState.Finished; 176 else return HeuristicLab.Grid.JobState.Unkown; 177 } 178 } 150 179 } 151 180 }
Note: See TracChangeset
for help on using the changeset viewer.