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.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Hive.Client.ExecutionEngine;
|
---|
27 | using HeuristicLab.Hive.Client.Common;
|
---|
28 | using System.Threading;
|
---|
29 | using System.Reflection;
|
---|
30 | using System.Diagnostics;
|
---|
31 | using System.Security.Permissions;
|
---|
32 | using System.Security.Policy;
|
---|
33 | using System.Security;
|
---|
34 | using HeuristicLab.Hive.Client.Communication;
|
---|
35 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
36 | using HeuristicLab.Hive.Contracts;
|
---|
37 | using System.Runtime.Remoting.Messaging;
|
---|
38 | using HeuristicLab.PluginInfrastructure;
|
---|
39 | using System.ServiceModel;
|
---|
40 | using System.ServiceModel.Description;
|
---|
41 | using HeuristicLab.Hive.Client.Core.ClientConsoleService;
|
---|
42 | using HeuristicLab.Hive.Client.Core.ConfigurationManager;
|
---|
43 | using HeuristicLab.Hive.Client.Communication.ServerService;
|
---|
44 | using HeuristicLab.Hive.JobBase;
|
---|
45 | using HeuristicLab.Hive.Client.Core.JobStorage;
|
---|
46 |
|
---|
47 | namespace HeuristicLab.Hive.Client.Core {
|
---|
48 | /// <summary>
|
---|
49 | /// The core component of the Hive Client
|
---|
50 | /// </summary>
|
---|
51 | public class Core: MarshalByRefObject {
|
---|
52 | public static bool abortRequested { get; set; }
|
---|
53 |
|
---|
54 | private Dictionary<Guid, Executor> engines = new Dictionary<Guid, Executor>();
|
---|
55 | private Dictionary<Guid, AppDomain> appDomains = new Dictionary<Guid, AppDomain>();
|
---|
56 | private Dictionary<Guid, Job> jobs = new Dictionary<Guid, Job>();
|
---|
57 |
|
---|
58 | private WcfService wcfService;
|
---|
59 | private Heartbeat beat;
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Main Method for the client
|
---|
63 | /// </summary>
|
---|
64 | public void Start() {
|
---|
65 | abortRequested = false;
|
---|
66 |
|
---|
67 | Logging.Instance.Info(this.ToString(), "Hive Client started");
|
---|
68 | ClientConsoleServer server = new ClientConsoleServer();
|
---|
69 | server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
|
---|
70 |
|
---|
71 | ConfigManager manager = ConfigManager.Instance;
|
---|
72 | manager.Core = this;
|
---|
73 |
|
---|
74 | //Register all Wcf Service references
|
---|
75 | wcfService = WcfService.Instance;
|
---|
76 | wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
|
---|
77 | wcfService.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(wcfService_SendJobCompleted);
|
---|
78 | wcfService.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(wcfService_StoreFinishedJobResultCompleted);
|
---|
79 | wcfService.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(wcfService_ProcessSnapshotCompleted);
|
---|
80 | wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
|
---|
81 | wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
|
---|
82 | wcfService.Connected += new EventHandler(wcfService_Connected);
|
---|
83 | //Recover Server IP and Port from the Settings Framework
|
---|
84 | ConnectionContainer cc = ConfigManager.Instance.GetServerIPAndPort();
|
---|
85 | if (cc.IPAdress != String.Empty && cc.Port != 0) {
|
---|
86 | wcfService.Connect(cc.IPAdress, cc.Port);
|
---|
87 | }
|
---|
88 |
|
---|
89 | //Initialize the heartbeat
|
---|
90 | beat = new Heartbeat { Interval = 10000 };
|
---|
91 | beat.StartHeartbeat();
|
---|
92 |
|
---|
93 | MessageQueue queue = MessageQueue.GetInstance();
|
---|
94 |
|
---|
95 | //Main processing loop
|
---|
96 | //Todo: own thread for message handling
|
---|
97 | //Rly?!
|
---|
98 | while (!abortRequested) {
|
---|
99 | MessageContainer container = queue.GetMessage();
|
---|
100 | Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
|
---|
101 | Logging.Instance.Info(this.ToString(), container.Message.ToString());
|
---|
102 | DetermineAction(container);
|
---|
103 | }
|
---|
104 | Console.WriteLine("ended!");
|
---|
105 | }
|
---|
106 |
|
---|
107 | /// <summary>
|
---|
108 | /// Reads and analyzes the Messages from the MessageQueue and starts corresponding actions
|
---|
109 | /// </summary>
|
---|
110 | /// <param name="container">The Container, containing the message</param>
|
---|
111 | private void DetermineAction(MessageContainer container) {
|
---|
112 | switch (container.Message) {
|
---|
113 | //Server requests to abort a job
|
---|
114 | case MessageContainer.MessageType.AbortJob:
|
---|
115 | engines[container.JobId].Abort();
|
---|
116 | break;
|
---|
117 | //Job has been successfully aborted
|
---|
118 | case MessageContainer.MessageType.JobAborted:
|
---|
119 | Debug.WriteLine("-- Job Aborted Message received");
|
---|
120 | break;
|
---|
121 | //Request a Snapshot from the Execution Engine
|
---|
122 | case MessageContainer.MessageType.RequestSnapshot:
|
---|
123 | engines[container.JobId].RequestSnapshot();
|
---|
124 | break;
|
---|
125 | //Snapshot is ready and can be sent back to the Server
|
---|
126 | case MessageContainer.MessageType.SnapshotReady:
|
---|
127 | ThreadPool.QueueUserWorkItem(new WaitCallback(GetSnapshot), container.JobId);
|
---|
128 | break;
|
---|
129 | //Pull a Job from the Server
|
---|
130 | case MessageContainer.MessageType.FetchJob:
|
---|
131 | wcfService.SendJobAsync(ConfigManager.Instance.GetClientInfo().Id);
|
---|
132 | break;
|
---|
133 | //A Job has finished and can be sent back to the server
|
---|
134 | case MessageContainer.MessageType.FinishedJob:
|
---|
135 | ThreadPool.QueueUserWorkItem(new WaitCallback(GetFinishedJob), container.JobId);
|
---|
136 | break;
|
---|
137 | //Hard shutdown of the client
|
---|
138 | case MessageContainer.MessageType.Shutdown:
|
---|
139 | lock (engines) {
|
---|
140 | foreach (KeyValuePair<Guid, AppDomain> kvp in appDomains)
|
---|
141 | AppDomain.Unload(kvp.Value);
|
---|
142 | }
|
---|
143 | abortRequested = true;
|
---|
144 | beat.StopHeartBeat();
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | //Asynchronous Threads for interaction with the Execution Engine
|
---|
150 | #region Async Threads for the EE
|
---|
151 |
|
---|
152 | private void GetFinishedJob(object jobId) {
|
---|
153 | Guid jId = (Guid)jobId;
|
---|
154 | try {
|
---|
155 | byte[] sJob = engines[jId].GetFinishedJob();
|
---|
156 |
|
---|
157 | if (WcfService.Instance.ConnState == NetworkEnum.WcfConnState.Loggedin) {
|
---|
158 | wcfService.StoreFinishedJobResultAsync(ConfigManager.Instance.GetClientInfo().Id,
|
---|
159 | jId,
|
---|
160 | sJob,
|
---|
161 | 1,
|
---|
162 | null,
|
---|
163 | true);
|
---|
164 | } else {
|
---|
165 | JobStorageManager.PersistObjectToDisc(wcfService.ServerIP, wcfService.ServerPort, jId, sJob);
|
---|
166 | lock (engines) {
|
---|
167 | AppDomain.Unload(appDomains[jId]);
|
---|
168 | appDomains.Remove(jId);
|
---|
169 | engines.Remove(jId);
|
---|
170 | jobs.Remove(jId);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | catch (InvalidStateException ise) {
|
---|
175 | Logging.Instance.Error(this.ToString(), "Exception: ", ise);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void GetSnapshot(object jobId) {
|
---|
180 | Guid jId = (Guid)jobId;
|
---|
181 | byte[] obj = engines[jId].GetSnapshot();
|
---|
182 | wcfService.ProcessSnapshotAsync(ConfigManager.Instance.GetClientInfo().Id,
|
---|
183 | jId,
|
---|
184 | obj,
|
---|
185 | engines[jId].Progress,
|
---|
186 | null,
|
---|
187 | false);
|
---|
188 | }
|
---|
189 |
|
---|
190 | #endregion
|
---|
191 |
|
---|
192 | //Eventhandlers for the communication with the wcf Layer
|
---|
193 | #region wcfService Events
|
---|
194 |
|
---|
195 | void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
196 | if (e.Result.Success) {
|
---|
197 | Logging.Instance.Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
|
---|
198 | } else
|
---|
199 | Logging.Instance.Error(this.ToString(), e.Result.StatusMessage);
|
---|
200 | }
|
---|
201 |
|
---|
202 | void wcfService_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
|
---|
203 | if (e.Result.StatusMessage != ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT) {
|
---|
204 | bool sandboxed = false;
|
---|
205 |
|
---|
206 | PluginManager.Manager.Initialize();
|
---|
207 |
|
---|
208 | AppDomain appDomain = PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.Job.Id.ToString(), sandboxed, typeof(HeuristicLab.Hive.Engine.HiveEngine));
|
---|
209 | appDomain.UnhandledException += new UnhandledExceptionEventHandler(appDomain_UnhandledException);
|
---|
210 | lock (engines) {
|
---|
211 | if (!jobs.ContainsKey(e.Result.Job.Id)) {
|
---|
212 | jobs.Add(e.Result.Job.Id, e.Result.Job);
|
---|
213 | appDomains.Add(e.Result.Job.Id, appDomain);
|
---|
214 |
|
---|
215 | Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
|
---|
216 | engine.JobId = e.Result.Job.Id;
|
---|
217 | engine.Queue = MessageQueue.GetInstance();
|
---|
218 | engine.Start(e.Result.Job.SerializedJob);
|
---|
219 | engines.Add(e.Result.Job.Id, engine);
|
---|
220 |
|
---|
221 | ClientStatusInfo.JobsFetched++;
|
---|
222 |
|
---|
223 | Debug.WriteLine("Increment FetchedJobs to:" + ClientStatusInfo.JobsFetched);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | void wcfService_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
|
---|
231 | if (e.Result.Success) {
|
---|
232 | lock(engines) {
|
---|
233 | //if the engine is running again -> we sent an snapshot. Otherwise the job was finished
|
---|
234 | //this method has a risk concerning race conditions.
|
---|
235 | //better expand the sendjobresultcompltedeventargs with a boolean "snapshot?" flag
|
---|
236 | AppDomain.Unload(appDomains[e.Result.JobId]);
|
---|
237 | appDomains.Remove(e.Result.JobId);
|
---|
238 | engines.Remove(e.Result.JobId);
|
---|
239 | jobs.Remove(e.Result.JobId);
|
---|
240 | ClientStatusInfo.JobsProcessed++;
|
---|
241 | Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
|
---|
242 | }
|
---|
243 | } else {
|
---|
244 | Logging.Instance.Error(this.ToString(), "Sending of job " + e.Result.JobId + " failed");
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | void wcfService_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
|
---|
249 | Logging.Instance.Info(this.ToString(), "Snapshot " + e.Result.JobId + " has been transmitted according to plan");
|
---|
250 | }
|
---|
251 |
|
---|
252 | //Todo: First stop all threads, then terminate
|
---|
253 | void wcfService_ServerChanged(object sender, EventArgs e) {
|
---|
254 | Logging.Instance.Info(this.ToString(), "ServerChanged has been called");
|
---|
255 | lock (engines) {
|
---|
256 | foreach (KeyValuePair<Guid, AppDomain> entries in appDomains)
|
---|
257 | AppDomain.Unload(appDomains[entries.Key]);
|
---|
258 | appDomains = new Dictionary<Guid, AppDomain>();
|
---|
259 | engines = new Dictionary<Guid, Executor>();
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | void wcfService_Connected(object sender, EventArgs e) {
|
---|
264 | wcfService.LoginSync(ConfigManager.Instance.GetClientInfo());
|
---|
265 | JobStorageManager.CheckAndSubmitJobsFromDisc();
|
---|
266 | }
|
---|
267 |
|
---|
268 | //this is a little bit tricky -
|
---|
269 | void wcfService_ConnectionRestored(object sender, EventArgs e) {
|
---|
270 | Logging.Instance.Info(this.ToString(), "Reconnected to old server - checking currently running appdomains");
|
---|
271 |
|
---|
272 | foreach (KeyValuePair<Guid, Executor> execKVP in engines) {
|
---|
273 | if (!execKVP.Value.Running && execKVP.Value.CurrentMessage == MessageContainer.MessageType.NoMessage) {
|
---|
274 | Logging.Instance.Info(this.ToString(), "Checking for JobId: " + execKVP.Value.JobId);
|
---|
275 | Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
|
---|
276 | finThread.Start(execKVP.Value.JobId);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | #endregion
|
---|
282 |
|
---|
283 | public Dictionary<Guid, Executor> GetExecutionEngines() {
|
---|
284 | return engines;
|
---|
285 | }
|
---|
286 |
|
---|
287 | void appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
|
---|
288 | Logging.Instance.Error(this.ToString(), " Exception: " + e.ExceptionObject.ToString());
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|