Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FunctionsAndStructIdRefactoring/HeuristicLab.Grid/EngineStore.cs @ 153

Last change on this file since 153 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
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, string> runningClients;
37    private object bigLock;
38    private ChannelFactory<IClient> clientChannelFactory;
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() {
58      engineList = new List<Guid>();
59      waitingEngines = new Dictionary<Guid, byte[]>();
60      runningEngines = new Dictionary<Guid, byte[]>();
61      runningClients = new Dictionary<Guid, string>();
62      waitHandles = new Dictionary<Guid, ManualResetEvent>();
63      results = new Dictionary<Guid, byte[]>();
64      bigLock = new object();
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);
73    }
74
75    public bool TryTakeEngine(string clientUrl, out Guid guid, out byte[] engine) {
76      lock(bigLock) {
77        if(engineList.Count == 0) {
78          guid = Guid.Empty;
79          engine = null;
80          return false;
81        } else {
82          guid = engineList[0];
83          engineList.RemoveAt(0);
84          engine = waitingEngines[guid];
85          waitingEngines.Remove(guid);
86          runningEngines[guid] = engine;
87          runningClients[guid] = clientUrl;
88          return true;
89        }
90      }
91    }
92
93    public void StoreResult(Guid guid, byte[] result) {
94      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);
99        results[guid] = result;
100        waitHandles[guid].Set();
101      }
102    }
103
104    internal void AddEngine(Guid guid, byte[] engine) {
105      lock(bigLock) {
106        engineList.Add(guid);
107        waitingEngines.Add(guid, engine);
108        waitHandles.Add(guid, new ManualResetEvent(false));
109      }
110    }
111
112    internal byte[] GetResult(Guid guid) {
113      return GetResult(guid, System.Threading.Timeout.Infinite);
114    }
115    internal byte[] GetResult(Guid guid, int timeout) {
116      lock(bigLock) {
117        if(waitHandles.ContainsKey(guid)) {
118          ManualResetEvent waitHandle = waitHandles[guid];
119          if(waitHandle.WaitOne(timeout, true)) {
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          }
128        } else {
129          return null;
130        }
131      }
132    }
133
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);
141        } else if(waitingEngines.ContainsKey(guid)) {
142          byte[] engine = waitingEngines[guid];
143          waitingEngines.Remove(guid);
144          engineList.Remove(guid);
145          waitHandles[guid].Set();
146          results.Add(guid, engine);
147        }
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.