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 |
|
---|
48 | namespace HeuristicLab.Hive.Client.Core {
|
---|
49 | /// <summary>
|
---|
50 | /// The core component of the Hive Client
|
---|
51 | /// </summary>
|
---|
52 | public class Core: MarshalByRefObject {
|
---|
53 | public static bool abortRequested { get; set; }
|
---|
54 |
|
---|
55 | private Dictionary<long, Executor> engines = new Dictionary<long, Executor>();
|
---|
56 | private Dictionary<long, AppDomain> appDomains = new Dictionary<long, AppDomain>();
|
---|
57 | private Dictionary<long, Job> jobs = new Dictionary<long, Job>();
|
---|
58 |
|
---|
59 | private WcfService wcfService;
|
---|
60 | private Heartbeat beat;
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Main Method for the client
|
---|
64 | /// </summary>
|
---|
65 | public void Start() {
|
---|
66 | abortRequested = false;
|
---|
67 |
|
---|
68 | Logging.Instance.Info(this.ToString(), "Hive Client started");
|
---|
69 | ClientConsoleServer server = new ClientConsoleServer();
|
---|
70 | server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
|
---|
71 |
|
---|
72 | ConfigManager manager = ConfigManager.Instance;
|
---|
73 | manager.Core = this;
|
---|
74 |
|
---|
75 | //Register all Wcf Service references
|
---|
76 | wcfService = WcfService.Instance;
|
---|
77 | wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
|
---|
78 | wcfService.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(wcfService_SendJobCompleted);
|
---|
79 | wcfService.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(wcfService_StoreFinishedJobResultCompleted);
|
---|
80 | wcfService.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(wcfService_ProcessSnapshotCompleted);
|
---|
81 | wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
|
---|
82 | wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
|
---|
83 | wcfService.Connected += new EventHandler(wcfService_Connected);
|
---|
84 | //Recover Server IP and Port from the Settings Framework
|
---|
85 | ConnectionContainer cc = ConfigManager.Instance.GetServerIPAndPort();
|
---|
86 | if (cc.IPAdress != String.Empty && cc.Port != 0) {
|
---|
87 | wcfService.Connect(cc.IPAdress, cc.Port);
|
---|
88 | }
|
---|
89 |
|
---|
90 | //Initialize the heartbeat
|
---|
91 | beat = new Heartbeat { Interval = 10000 };
|
---|
92 | beat.StartHeartbeat();
|
---|
93 |
|
---|
94 | MessageQueue queue = MessageQueue.GetInstance();
|
---|
95 |
|
---|
96 | //Main processing loop
|
---|
97 | //Todo: own thread for message handling
|
---|
98 | //Rly?!
|
---|
99 | while (!abortRequested) {
|
---|
100 | MessageContainer container = queue.GetMessage();
|
---|
101 | Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
|
---|
102 | Logging.Instance.Info(this.ToString(), container.Message.ToString());
|
---|
103 | DetermineAction(container);
|
---|
104 | }
|
---|
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().ClientId);
|
---|
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 | abortRequested = true;
|
---|
140 | beat.StopHeartBeat();
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | //Asynchronous Threads for interaction with the Execution Engine
|
---|
146 | #region Async Threads for the EE
|
---|
147 |
|
---|
148 | private void GetFinishedJob(object jobId) {
|
---|
149 | long jId = (long)jobId;
|
---|
150 | try {
|
---|
151 | byte[] sJob = engines[jId].GetFinishedJob();
|
---|
152 |
|
---|
153 | if (WcfService.Instance.ConnState == NetworkEnum.WcfConnState.Loggedin) {
|
---|
154 | wcfService.StoreFinishedJobResultAsync(ConfigManager.Instance.GetClientInfo().ClientId,
|
---|
155 | jId,
|
---|
156 | sJob,
|
---|
157 | 1,
|
---|
158 | null,
|
---|
159 | true);
|
---|
160 | } else {
|
---|
161 | JobStorageManager.PersistObjectToDisc(wcfService.ServerIP, wcfService.ServerPort, jId, sJob);
|
---|
162 | lock (engines) {
|
---|
163 | AppDomain.Unload(appDomains[jId]);
|
---|
164 | appDomains.Remove(jId);
|
---|
165 | engines.Remove(jId);
|
---|
166 | jobs.Remove(jId);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 | catch (InvalidStateException ise) {
|
---|
171 | Logging.Instance.Error(this.ToString(), "Exception: ", ise);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void GetSnapshot(object jobId) {
|
---|
176 | long jId = (long)jobId;
|
---|
177 | byte[] obj = engines[jId].GetSnapshot();
|
---|
178 | wcfService.ProcessSnapshotAsync(ConfigManager.Instance.GetClientInfo().ClientId,
|
---|
179 | jId,
|
---|
180 | obj,
|
---|
181 | engines[jId].Progress,
|
---|
182 | null,
|
---|
183 | false);
|
---|
184 | }
|
---|
185 |
|
---|
186 | #endregion
|
---|
187 |
|
---|
188 | //Eventhandlers for the communication with the wcf Layer
|
---|
189 | #region wcfService Events
|
---|
190 |
|
---|
191 | void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
192 | if (e.Result.Success) {
|
---|
193 | Logging.Instance.Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
|
---|
194 | } else
|
---|
195 | Logging.Instance.Error(this.ToString(), e.Result.StatusMessage);
|
---|
196 | }
|
---|
197 |
|
---|
198 | void wcfService_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
|
---|
199 | if (e.Result.StatusMessage != ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT) {
|
---|
200 | bool sandboxed = true;
|
---|
201 |
|
---|
202 | PluginManager.Manager.Initialize();
|
---|
203 | AppDomain appDomain = PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.Job.Id.ToString(), sandboxed, typeof(TestJob));
|
---|
204 | appDomain.UnhandledException += new UnhandledExceptionEventHandler(appDomain_UnhandledException);
|
---|
205 | lock (engines) {
|
---|
206 | if (!jobs.ContainsKey(e.Result.Job.Id)) {
|
---|
207 | jobs.Add(e.Result.Job.Id, e.Result.Job);
|
---|
208 | appDomains.Add(e.Result.Job.Id, appDomain);
|
---|
209 |
|
---|
210 | Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
|
---|
211 | engine.JobId = e.Result.Job.Id;
|
---|
212 | engine.Queue = MessageQueue.GetInstance();
|
---|
213 | engine.Start(e.Result.Job.SerializedJob);
|
---|
214 | engines.Add(e.Result.Job.Id, engine);
|
---|
215 |
|
---|
216 | ClientStatusInfo.JobsFetched++;
|
---|
217 |
|
---|
218 | Debug.WriteLine("Increment FetchedJobs to:" + ClientStatusInfo.JobsFetched);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | void wcfService_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
|
---|
226 | if (e.Result.Success) {
|
---|
227 | lock(engines) {
|
---|
228 | //if the engine is running again -> we sent an snapshot. Otherwise the job was finished
|
---|
229 | //this method has a risk concerning race conditions.
|
---|
230 | //better expand the sendjobresultcompltedeventargs with a boolean "snapshot?" flag
|
---|
231 | AppDomain.Unload(appDomains[e.Result.JobId]);
|
---|
232 | appDomains.Remove(e.Result.JobId);
|
---|
233 | engines.Remove(e.Result.JobId);
|
---|
234 | jobs.Remove(e.Result.JobId);
|
---|
235 | ClientStatusInfo.JobsProcessed++;
|
---|
236 | Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
|
---|
237 | }
|
---|
238 | } else {
|
---|
239 | Logging.Instance.Error(this.ToString(), "Sending of job " + e.Result.JobId + " failed");
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void wcfService_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
|
---|
244 | Logging.Instance.Info(this.ToString(), "Snapshot " + e.Result.JobId + " has been transmitted according to plan");
|
---|
245 | }
|
---|
246 |
|
---|
247 | //Todo: First stop all threads, then terminate
|
---|
248 | void wcfService_ServerChanged(object sender, EventArgs e) {
|
---|
249 | Logging.Instance.Info(this.ToString(), "ServerChanged has been called");
|
---|
250 | lock (engines) {
|
---|
251 | foreach (KeyValuePair<long, AppDomain> entries in appDomains)
|
---|
252 | AppDomain.Unload(appDomains[entries.Key]);
|
---|
253 | appDomains = new Dictionary<long, AppDomain>();
|
---|
254 | engines = new Dictionary<long, Executor>();
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | void wcfService_Connected(object sender, EventArgs e) {
|
---|
259 | wcfService.LoginSync(ConfigManager.Instance.GetClientInfo());
|
---|
260 | JobStorageManager.CheckAndSubmitJobsFromDisc();
|
---|
261 | }
|
---|
262 |
|
---|
263 | //this is a little bit tricky -
|
---|
264 | void wcfService_ConnectionRestored(object sender, EventArgs e) {
|
---|
265 | Logging.Instance.Info(this.ToString(), "Reconnected to old server - checking currently running appdomains");
|
---|
266 |
|
---|
267 | foreach (KeyValuePair<long, Executor> execKVP in engines) {
|
---|
268 | if (!execKVP.Value.Running && execKVP.Value.CurrentMessage == MessageContainer.MessageType.NoMessage) {
|
---|
269 | Logging.Instance.Info(this.ToString(), "Checking for JobId: " + execKVP.Value.JobId);
|
---|
270 | Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
|
---|
271 | finThread.Start(execKVP.Value.JobId);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | #endregion
|
---|
277 |
|
---|
278 | public Dictionary<long, Executor> GetExecutionEngines() {
|
---|
279 | return engines;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
|
---|
283 | Logging.Instance.Error(this.ToString(), " Exception: " + e.ExceptionObject.ToString());
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|