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