Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Core/3.2/Core.cs @ 3514

Last change on this file since 3514 was 3220, checked in by kgrading, 14 years ago

improved the DAL further, changed minor details for the presentation (#830)

File size: 19.8 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.Linq;
25using System.Text;
26using HeuristicLab.Hive.Client.ExecutionEngine;
27using HeuristicLab.Hive.Client.Common;
28using System.Threading;
29using System.Reflection;
30using System.Diagnostics;
31using System.Security.Permissions;
32using System.Security.Policy;
33using System.Security;
34using HeuristicLab.Hive.Client.Communication;
35using HeuristicLab.Hive.Contracts.BusinessObjects;
36using HeuristicLab.Hive.Contracts;
37using System.Runtime.Remoting.Messaging;
38using HeuristicLab.PluginInfrastructure;
39using System.ServiceModel;
40using System.ServiceModel.Description;
41using HeuristicLab.Hive.Client.Core.ClientConsoleService;
42using HeuristicLab.Hive.Client.Core.ConfigurationManager;
43using HeuristicLab.Hive.Client.Communication.ServerService;
44using HeuristicLab.Hive.JobBase;
45using HeuristicLab.Hive.Client.Core.JobStorage;
46
47namespace 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    private bool currentlyFetching = false;
54
55    private Dictionary<Guid, Executor> engines = new Dictionary<Guid, Executor>();
56    private Dictionary<Guid, AppDomain> appDomains = new Dictionary<Guid, AppDomain>();
57    private Dictionary<Guid, JobDto> jobs = new Dictionary<Guid, JobDto>();
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      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
75
76      //Register all Wcf Service references
77      wcfService = WcfService.Instance;
78      wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
79      wcfService.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(wcfService_SendJobCompleted);
80      wcfService.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(wcfService_StoreFinishedJobResultCompleted);
81      wcfService.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(wcfService_ProcessSnapshotCompleted);
82      wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
83      wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
84      wcfService.Connected += new EventHandler(wcfService_Connected);
85      //Recover Server IP and Port from the Settings Framework
86      ConnectionContainer cc = ConfigManager.Instance.GetServerIPAndPort();
87      if (cc.IPAdress != String.Empty && cc.Port != 0)
88        wcfService.SetIPAndPort(cc.IPAdress, cc.Port);
89
90      if (!UptimeManager.Instance.CalendarAvailable || UptimeManager.Instance.IsOnline())
91        wcfService.Connect();
92
93      //Initialize the heartbeat
94      beat = new Heartbeat { Interval = 10000 };
95      beat.StartHeartbeat();
96
97      MessageQueue queue = MessageQueue.GetInstance();
98
99      //Main processing loop     
100      //Todo: own thread for message handling
101      //Rly?!
102      while (!abortRequested) {
103        MessageContainer container = queue.GetMessage();       
104        DetermineAction(container);
105      }
106      System.Console.WriteLine("ended");
107    }
108
109    /// <summary>
110    /// Reads and analyzes the Messages from the MessageQueue and starts corresponding actions
111    /// </summary>
112    /// <param name="container">The Container, containing the message</param>
113    private void DetermineAction(MessageContainer container) {
114      Logging.Instance.Info(this.ToString(), "Message: " + container.Message.ToString() + " for job: " + container.JobId);       
115      switch (container.Message) {
116        //Server requests to abort a job
117        case MessageContainer.MessageType.AbortJob:         
118          if (engines.ContainsKey(container.JobId))
119            engines[container.JobId].Abort();
120          else
121            Logging.Instance.Error(this.ToString(), "AbortJob: Engine doesn't exist");
122          break;
123        //Job has been successfully aborted
124
125
126        case MessageContainer.MessageType.JobAborted:         
127        //todo: thread this         
128          lock (engines) {
129            Guid jobId = new Guid(container.JobId.ToString());
130            if (engines.ContainsKey(jobId)) {
131              appDomains[jobId].UnhandledException -= new UnhandledExceptionEventHandler(appDomain_UnhandledException);
132              AppDomain.Unload(appDomains[jobId]);
133              appDomains.Remove(jobId);
134              engines.Remove(jobId);
135              jobs.Remove(jobId);
136              GC.Collect();
137            } else
138              Logging.Instance.Error(this.ToString(), "JobAbort: Engine doesn't exist");
139          }
140          break;
141
142
143        //Request a Snapshot from the Execution Engine
144        case MessageContainer.MessageType.RequestSnapshot:         
145          if (engines.ContainsKey(container.JobId))
146            engines[container.JobId].RequestSnapshot();
147          else
148            Logging.Instance.Error(this.ToString(), "RequestSnapshot: Engine doesn't exist");
149          break;
150
151
152        //Snapshot is ready and can be sent back to the Server
153        case MessageContainer.MessageType.SnapshotReady:         
154          ThreadPool.QueueUserWorkItem(new WaitCallback(GetSnapshot), container.JobId);
155          break;
156
157
158        //Pull a Job from the Server
159        case MessageContainer.MessageType.FetchJob:         
160          if (!currentlyFetching) {
161            wcfService.SendJobAsync(ConfigManager.Instance.GetClientInfo().Id);
162            currentlyFetching = true;
163          } else
164            Logging.Instance.Info(this.ToString(), "Currently fetching, won't fetch this time!");
165          break;         
166       
167       
168        //A Job has finished and can be sent back to the server
169        case MessageContainer.MessageType.FinishedJob:         
170          ThreadPool.QueueUserWorkItem(new WaitCallback(GetFinishedJob), container.JobId);
171          break;
172
173
174        //When the timeslice is up
175        case MessageContainer.MessageType.UptimeLimitDisconnect:
176          Logging.Instance.Info(this.ToString(), "Uptime Limit reached, storing jobs and sending them back");
177
178          //check if there are running jobs
179          if (engines.Count > 0) {
180            //make sure there is no more fetching of jobs while the snapshots get processed
181            currentlyFetching = true;
182            //request a snapshot of each running job
183            foreach (KeyValuePair<Guid, Executor> kvp in engines) {
184              kvp.Value.RequestSnapshot();
185            }
186
187          } else {
188            //Disconnect afterwards
189            WcfService.Instance.Disconnect();
190          }
191          break;
192
193          //Fetch or Force Fetch Calendar!
194        case MessageContainer.MessageType.FetchOrForceFetchCalendar:
195          ResponseCalendar rescal = wcfService.GetCalendarSync(ConfigManager.Instance.GetClientInfo().Id);
196          if(rescal.Success) {
197            if(!UptimeManager.Instance.SetAppointments(false, rescal)) {
198              wcfService.SetCalendarStatus(ConfigManager.Instance.GetClientInfo().Id, CalendarState.NotAllowedToFetch);             
199            } else {
200              wcfService.SetCalendarStatus(ConfigManager.Instance.GetClientInfo().Id, CalendarState.Fetched);             
201            }
202          } else {
203            wcfService.SetCalendarStatus(ConfigManager.Instance.GetClientInfo().Id, CalendarState.NotAllowedToFetch);
204          }
205        break;
206
207        //Hard shutdown of the client
208        case MessageContainer.MessageType.Shutdown:
209          lock (engines) {
210            foreach (KeyValuePair<Guid, AppDomain> kvp in appDomains) {
211              appDomains[kvp.Key].UnhandledException -= new UnhandledExceptionEventHandler(appDomain_UnhandledException);
212              AppDomain.Unload(kvp.Value);
213            }
214          }
215          abortRequested = true;
216          beat.StopHeartBeat();
217          WcfService.Instance.Logout(ConfigManager.Instance.GetClientInfo().Id);
218          break;
219      }
220    }
221
222    //Asynchronous Threads for interaction with the Execution Engine
223    #region Async Threads for the EE
224
225    /// <summary>
226    /// serializes the finished job and submits it to the server. If, at the time, a network connection is unavailable, the Job gets stored on the disk.
227    /// once the connection gets reestablished, the job gets submitted
228    /// </summary>
229    /// <param name="jobId"></param>
230    private void GetFinishedJob(object jobId) {
231      Guid jId = (Guid)jobId;
232      Logging.Instance.Info(this.ToString(), "Getting the finished job with id: " + jId);
233      try {
234        if (!engines.ContainsKey(jId)) {
235          Logging.Instance.Error(this.ToString(), "GetFinishedJob: Engine doesn't exist");
236          return;
237        }
238
239        byte[] sJob = engines[jId].GetFinishedJob();
240
241        if (WcfService.Instance.ConnState == NetworkEnum.WcfConnState.Loggedin) {
242          Logging.Instance.Info(this.ToString(), "Sending the finished job with id: " + jId);
243          wcfService.StoreFinishedJobResultAsync(ConfigManager.Instance.GetClientInfo().Id,
244            jId,
245            sJob,
246            1,
247            null,
248            true);
249        } else {
250          Logging.Instance.Info(this.ToString(), "Storing the finished job with id: " + jId + " to hdd");
251          JobStorageManager.PersistObjectToDisc(wcfService.ServerIP, wcfService.ServerPort, jId, sJob);
252          lock (engines) {
253            appDomains[jId].UnhandledException -= new UnhandledExceptionEventHandler(appDomain_UnhandledException);
254            AppDomain.Unload(appDomains[jId]);
255            appDomains.Remove(jId);
256            engines.Remove(jId);
257            jobs.Remove(jId);
258          }
259        }
260      }
261      catch (InvalidStateException ise) {
262        Logging.Instance.Error(this.ToString(), "Exception: ", ise);
263      }
264    }
265
266    private void GetSnapshot(object jobId) {
267      Logging.Instance.Info(this.ToString(), "Fetching a snapshot for job " + jobId);
268      Guid jId = (Guid)jobId;
269      byte[] obj = engines[jId].GetSnapshot();
270      Logging.Instance.Info(this.ToString(), "BEGIN: Sending snapshot sync");
271      wcfService.ProcessSnapshotSync(ConfigManager.Instance.GetClientInfo().Id,
272        jId,
273        obj,
274        engines[jId].Progress,
275        null);
276      Logging.Instance.Info(this.ToString(), "END: Sended snapshot sync");
277      //Uptime Limit reached, now is a good time to destroy this jobs.
278      if (!UptimeManager.Instance.IsOnline()) {
279        KillAppDomain(jId);
280        //Still anything running?
281        if (engines.Count == 0)
282          WcfService.Instance.Disconnect();
283
284      } else {
285        Logging.Instance.Info(this.ToString(), "Restarting the job" + jobId);
286        engines[jId].StartOnlyJob();
287      }
288    }
289
290    #endregion
291
292    //Eventhandlers for the communication with the wcf Layer
293    #region wcfService Events
294    /// <summary>
295    /// Login has returned
296    /// </summary>
297    /// <param name="sender"></param>
298    /// <param name="e"></param>
299    void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
300      if (e.Result.Success) {
301        currentlyFetching = false;
302        Logging.Instance.Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
303      } else
304        Logging.Instance.Error(this.ToString(), e.Result.StatusMessage);
305    }
306
307    /// <summary>
308    /// A new Job from the wcfService has been received and will be started within a AppDomain.
309    /// </summary>
310    /// <param name="sender"></param>
311    /// <param name="e"></param>
312    void wcfService_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
313      if (e.Result.StatusMessage != ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT) {
314        Logging.Instance.Info(this.ToString(), "Received new job with id " + e.Result.Job.Id);     
315        bool sandboxed = false;
316        List<byte[]> files = new List<byte[]>();
317        Logging.Instance.Info(this.ToString(), "Fetching plugins for job " + e.Result.Job.Id);
318        foreach (CachedHivePluginInfoDto plugininfo in PluginCache.Instance.GetPlugins(e.Result.Job.PluginsNeeded))
319          files.AddRange(plugininfo.PluginFiles);
320        Logging.Instance.Info(this.ToString(), "Plugins fetched for job " + e.Result.Job.Id);
321        AppDomain appDomain = HeuristicLab.PluginInfrastructure.Sandboxing.SandboxManager.CreateAndInitSandbox(e.Result.Job.Id.ToString(), files);
322        appDomain.UnhandledException += new UnhandledExceptionEventHandler(appDomain_UnhandledException);
323        lock (engines) {
324          if (!jobs.ContainsKey(e.Result.Job.Id)) {
325            jobs.Add(e.Result.Job.Id, e.Result.Job);
326            appDomains.Add(e.Result.Job.Id, appDomain);
327            Logging.Instance.Info(this.ToString(), "Creating AppDomain");
328            Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
329            Logging.Instance.Info(this.ToString(), "Created AppDomain");
330            engine.JobId = e.Result.Job.Id;
331            engine.Queue = MessageQueue.GetInstance();
332            Logging.Instance.Info(this.ToString(), "Starting Engine for job " + e.Result.Job.Id);
333            engine.Start(e.Data);
334            engines.Add(e.Result.Job.Id, engine);
335
336            ClientStatusInfo.JobsFetched++;
337
338            Debug.WriteLine("Increment FetchedJobs to:" + ClientStatusInfo.JobsFetched);
339          }
340        }
341      } else
342        Logging.Instance.Info(this.ToString(), "No more jobs left!");
343      currentlyFetching = false;
344    }
345
346    /// <summary>
347    /// A finished job has been stored on the server
348    /// </summary>
349    /// <param name="sender"></param>
350    /// <param name="e"></param>
351    void wcfService_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
352      Logging.Instance.Info(this.ToString(), "Job submitted with id " + e.Result.JobId);
353      KillAppDomain(e.Result.JobId);
354      if (e.Result.Success) {
355        ClientStatusInfo.JobsProcessed++;
356        Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
357      } else {
358        Logging.Instance.Error(this.ToString(), "Sending of job " + e.Result.JobId + " failed, job has been wasted. Message: " + e.Result.StatusMessage);
359      }
360    }
361
362    /// <summary>
363    /// A snapshot has been stored on the server
364    /// </summary>
365    /// <param name="sender"></param>
366    /// <param name="e"></param>
367    void wcfService_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
368      Logging.Instance.Info(this.ToString(), "Snapshot " + e.Result.JobId + " has been transmitted according to plan.");
369    }
370
371    /// <summary>
372    /// The server has been changed. All Appdomains and Jobs must be aborted!
373    /// </summary>
374    /// <param name="sender"></param>
375    /// <param name="e"></param>
376    void wcfService_ServerChanged(object sender, EventArgs e) {
377      Logging.Instance.Info(this.ToString(), "ServerChanged has been called");
378      lock (engines) {
379        foreach (KeyValuePair<Guid, Executor> entries in engines) {
380          engines[entries.Key].Abort();
381          //appDomains[entries.Key].UnhandledException -= new UnhandledExceptionEventHandler(appDomain_UnhandledException);
382          //AppDomain.Unload(appDomains[entries.Key]);
383        }
384        //appDomains = new Dictionary<Guid, AppDomain>();
385        //engines = new Dictionary<Guid, Executor>();
386        //jobs = new Dictionary<Guid, Job>();
387      }
388    }
389
390    /// <summary>
391    /// Connnection to the server has been estabilshed => Login and Send the persistet Jobs from the harddisk.
392    /// </summary>
393    /// <param name="sender"></param>
394    /// <param name="e"></param>
395    void wcfService_Connected(object sender, EventArgs e) {
396      if (!UptimeManager.Instance.CalendarAvailable) {
397        ResponseCalendar calres = wcfService.GetCalendarSync(ConfigManager.Instance.GetClientInfo().Id);
398        if(calres.Success) {
399          if (UptimeManager.Instance.SetAppointments(false, calres))
400            wcfService.SetCalendarStatus(ConfigManager.Instance.GetClientInfo().Id, CalendarState.Fetched);
401          else
402            wcfService.SetCalendarStatus(ConfigManager.Instance.GetClientInfo().Id, CalendarState.NotAllowedToFetch);
403        }
404        else {
405          wcfService.SetCalendarStatus(ConfigManager.Instance.GetClientInfo().Id, CalendarState.NotAllowedToFetch);
406        }
407      }
408      //if the fetching from the server failed - still set the client online... maybe we get
409      //a result within the next few heartbeats
410      if (!UptimeManager.Instance.CalendarAvailable || UptimeManager.Instance.IsOnline()) {
411        wcfService.LoginSync(ConfigManager.Instance.GetClientInfo());
412        JobStorageManager.CheckAndSubmitJobsFromDisc();
413        currentlyFetching = false;
414      }
415    }
416
417    //this is a little bit tricky -
418    void wcfService_ConnectionRestored(object sender, EventArgs e) {
419      Logging.Instance.Info(this.ToString(), "Reconnected to old server - checking currently running appdomains");
420
421      foreach (KeyValuePair<Guid, Executor> execKVP in engines) {
422        if (!execKVP.Value.Running && execKVP.Value.CurrentMessage == MessageContainer.MessageType.NoMessage) {
423          Logging.Instance.Info(this.ToString(), "Checking for JobId: " + execKVP.Value.JobId);
424          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
425          finThread.Start(execKVP.Value.JobId);
426        }
427      }
428    }
429
430    #endregion
431
432    public Dictionary<Guid, Executor> GetExecutionEngines() {
433      return engines;
434    }
435
436    void appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
437      Logging.Instance.Error(this.ToString(), "Exception in AppDomain: " + e.ExceptionObject.ToString());
438    }
439
440    internal Dictionary<Guid, JobDto> GetJobs() {
441      return jobs;
442    }
443
444    /// <summary>
445    /// Kill a appdomain with a specific id.
446    /// </summary>
447    /// <param name="id">the GUID of the job</param>
448    private void KillAppDomain(Guid id) {
449      Logging.Instance.Info(this.ToString(), "Shutting down Appdomain for Job " + id);
450      lock (engines) {
451        try {
452          appDomains[id].UnhandledException -= new UnhandledExceptionEventHandler(appDomain_UnhandledException);
453          AppDomain.Unload(appDomains[id]);
454          appDomains.Remove(id);
455          engines.Remove(id);
456          jobs.Remove(id);
457        }
458        catch (Exception ex) {
459          Logging.Instance.Error(this.ToString(), "Exception when unloading the appdomain: ", ex);
460        }
461      }
462      GC.Collect();
463    }
464  }
465}
Note: See TracBrowser for help on using the repository browser.