[2] | 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 {
|
---|
[32] | 29 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
|
---|
[2] | 30 | public class EngineStore : IEngineStore {
|
---|
[34] | 31 | private List<Guid> engineList;
|
---|
[2] | 32 | private Dictionary<Guid, byte[]> waitingEngines;
|
---|
[33] | 33 | private Dictionary<Guid, ManualResetEvent> waitHandles;
|
---|
[2] | 34 | private Dictionary<Guid, byte[]> results;
|
---|
[219] | 35 | private Dictionary<Guid, DateTime> resultDate;
|
---|
[2] | 36 | private object bigLock;
|
---|
[32] | 37 | private ChannelFactory<IClient> clientChannelFactory;
|
---|
[2] | 38 | public int WaitingJobs {
|
---|
| 39 | get {
|
---|
| 40 | return waitingEngines.Count;
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public int RunningJobs {
|
---|
| 45 | get {
|
---|
[219] | 46 | return waitHandles.Count;
|
---|
[2] | 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public int WaitingResults {
|
---|
| 51 | get {
|
---|
| 52 | return results.Count;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public EngineStore() {
|
---|
[34] | 57 | engineList = new List<Guid>();
|
---|
[2] | 58 | waitingEngines = new Dictionary<Guid, byte[]>();
|
---|
[33] | 59 | waitHandles = new Dictionary<Guid, ManualResetEvent>();
|
---|
[2] | 60 | results = new Dictionary<Guid, byte[]>();
|
---|
[219] | 61 | resultDate = new Dictionary<Guid, DateTime>();
|
---|
[2] | 62 | bigLock = new object();
|
---|
[32] | 63 |
|
---|
| 64 | NetTcpBinding binding = new NetTcpBinding();
|
---|
| 65 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
| 66 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
| 67 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
| 68 | binding.Security.Mode = SecurityMode.None;
|
---|
| 69 |
|
---|
| 70 | clientChannelFactory = new ChannelFactory<IClient>(binding);
|
---|
[2] | 71 | }
|
---|
| 72 |
|
---|
[219] | 73 | public bool TryTakeEngine(out Guid guid, out byte[] engine) {
|
---|
[32] | 74 | lock(bigLock) {
|
---|
[34] | 75 | if(engineList.Count == 0) {
|
---|
[2] | 76 | guid = Guid.Empty;
|
---|
| 77 | engine = null;
|
---|
| 78 | return false;
|
---|
| 79 | } else {
|
---|
[34] | 80 | guid = engineList[0];
|
---|
| 81 | engineList.RemoveAt(0);
|
---|
[2] | 82 | engine = waitingEngines[guid];
|
---|
| 83 | waitingEngines.Remove(guid);
|
---|
| 84 | return true;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public void StoreResult(Guid guid, byte[] result) {
|
---|
[32] | 90 | lock(bigLock) {
|
---|
[219] | 91 | // clear old results
|
---|
[221] | 92 | List<Guid> expiredResults = FindExpiredResults(DateTime.Now.AddMinutes(-10.0));
|
---|
[219] | 93 | foreach(Guid expiredGuid in expiredResults) {
|
---|
| 94 | results.Remove(expiredGuid);
|
---|
| 95 | waitHandles.Remove(expiredGuid);
|
---|
| 96 | resultDate.Remove(expiredGuid);
|
---|
| 97 | }
|
---|
| 98 | // add the new result
|
---|
[2] | 99 | results[guid] = result;
|
---|
[219] | 100 | resultDate[guid] = DateTime.Now;
|
---|
[33] | 101 | waitHandles[guid].Set();
|
---|
[2] | 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[219] | 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;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[2] | 115 | internal void AddEngine(Guid guid, byte[] engine) {
|
---|
[32] | 116 | lock(bigLock) {
|
---|
[34] | 117 | engineList.Add(guid);
|
---|
[2] | 118 | waitingEngines.Add(guid, engine);
|
---|
[33] | 119 | waitHandles.Add(guid, new ManualResetEvent(false));
|
---|
[2] | 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | internal byte[] GetResult(Guid guid) {
|
---|
[33] | 124 | return GetResult(guid, System.Threading.Timeout.Infinite);
|
---|
| 125 | }
|
---|
[219] | 126 |
|
---|
[33] | 127 | internal byte[] GetResult(Guid guid, int timeout) {
|
---|
[32] | 128 | lock(bigLock) {
|
---|
[219] | 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
|
---|
[33] | 143 | ManualResetEvent waitHandle = waitHandles[guid];
|
---|
[219] | 144 | // wait
|
---|
[35] | 145 | if(waitHandle.WaitOne(timeout, true)) {
|
---|
[219] | 146 | // ok got the result in within the wait time => close and remove the wait-hande and return the result
|
---|
[33] | 147 | waitHandle.Close();
|
---|
| 148 | waitHandles.Remove(guid);
|
---|
| 149 | byte[] result = results[guid];
|
---|
| 150 | return result;
|
---|
| 151 | } else {
|
---|
[219] | 152 | // no result yet return without result
|
---|
[33] | 153 | return null;
|
---|
| 154 | }
|
---|
[2] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[32] | 159 | internal void AbortEngine(Guid guid) {
|
---|
| 160 | lock(bigLock) {
|
---|
[219] | 161 | if(waitingEngines.ContainsKey(guid)) {
|
---|
[34] | 162 | byte[] engine = waitingEngines[guid];
|
---|
| 163 | waitingEngines.Remove(guid);
|
---|
| 164 | engineList.Remove(guid);
|
---|
[35] | 165 | waitHandles[guid].Set();
|
---|
[34] | 166 | results.Add(guid, engine);
|
---|
[32] | 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
[219] | 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 | }
|
---|
[2] | 179 | }
|
---|
| 180 | }
|
---|