Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 125 was 35, checked in by gkronber, 17 years ago

fixed some synchronization issues in DistributedEngine and grid infrastructure

(ticket ref #2)

File size: 5.0 KB
RevLine 
[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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Threading;
26using System.ServiceModel;
27
28namespace 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    private Dictionary<Guid, byte[]> runningEngines;
[33]34    private Dictionary<Guid, ManualResetEvent> waitHandles;
[2]35    private Dictionary<Guid, byte[]> results;
[32]36    private Dictionary<Guid, string> runningClients;
[2]37    private object bigLock;
[32]38    private ChannelFactory<IClient> clientChannelFactory;
[2]39    public int WaitingJobs {
40      get {
41        return waitingEngines.Count;
42      }
43    }
44
45    public int RunningJobs {
46      get {
47        return runningEngines.Count;
48      }
49    }
50
51    public int WaitingResults {
52      get {
53        return results.Count;
54      }
55    }
56
57    public EngineStore() {
[34]58      engineList = new List<Guid>();
[2]59      waitingEngines = new Dictionary<Guid, byte[]>();
60      runningEngines = new Dictionary<Guid, byte[]>();
[32]61      runningClients = new Dictionary<Guid, string>();
[33]62      waitHandles = new Dictionary<Guid, ManualResetEvent>();
[2]63      results = new Dictionary<Guid, byte[]>();
64      bigLock = new object();
[32]65
66      NetTcpBinding binding = new NetTcpBinding();
67      binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
68      binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
69      binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
70      binding.Security.Mode = SecurityMode.None;
71
72      clientChannelFactory = new ChannelFactory<IClient>(binding);
[2]73    }
74
[32]75    public bool TryTakeEngine(string clientUrl, out Guid guid, out byte[] engine) {
76      lock(bigLock) {
[34]77        if(engineList.Count == 0) {
[2]78          guid = Guid.Empty;
79          engine = null;
80          return false;
81        } else {
[34]82          guid = engineList[0];
83          engineList.RemoveAt(0);
[2]84          engine = waitingEngines[guid];
85          waitingEngines.Remove(guid);
86          runningEngines[guid] = engine;
[32]87          runningClients[guid] = clientUrl;
[2]88          return true;
89        }
90      }
91    }
92
93    public void StoreResult(Guid guid, byte[] result) {
[32]94      lock(bigLock) {
95        if(!runningEngines.ContainsKey(guid)) return; // ignore result when the engine is not known to be running
[2]96
97        runningEngines.Remove(guid);
[32]98        runningClients.Remove(guid);
[2]99        results[guid] = result;
[33]100        waitHandles[guid].Set();
[2]101      }
102    }
103
104    internal void AddEngine(Guid guid, byte[] engine) {
[32]105      lock(bigLock) {
[34]106        engineList.Add(guid);
[2]107        waitingEngines.Add(guid, engine);
[33]108        waitHandles.Add(guid, new ManualResetEvent(false));
[2]109      }
110    }
111
112    internal byte[] GetResult(Guid guid) {
[33]113      return GetResult(guid, System.Threading.Timeout.Infinite);
114    }
115    internal byte[] GetResult(Guid guid, int timeout) {
[32]116      lock(bigLock) {
[33]117        if(waitHandles.ContainsKey(guid)) {
118          ManualResetEvent waitHandle = waitHandles[guid];
[35]119          if(waitHandle.WaitOne(timeout, true)) {
[33]120            waitHandle.Close();
121            waitHandles.Remove(guid);
122            byte[] result = results[guid];
123            results.Remove(guid);
124            return result;
125          } else {
126            return null;
127          }
[2]128        } else {
[33]129          return null;
[2]130        }
131      }
132    }
133
[32]134    internal void AbortEngine(Guid guid) {
135      string clientUrl = "";
136      lock(bigLock) {
137        if(runningClients.ContainsKey(guid)) {
138          clientUrl = runningClients[guid];
139          IClient client = clientChannelFactory.CreateChannel(new EndpointAddress(clientUrl));
140          client.Abort(guid);
[34]141        } else if(waitingEngines.ContainsKey(guid)) {
142          byte[] engine = waitingEngines[guid];
143          waitingEngines.Remove(guid);
144          engineList.Remove(guid);
[35]145          waitHandles[guid].Set();
[34]146          results.Add(guid, engine);
[32]147        }
148      }
149    }
[2]150  }
151}
Note: See TracBrowser for help on using the repository browser.