Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Hive.Engine/3.2/HiveEngine.cs @ 5726

Last change on this file since 5726 was 2111, checked in by gkronber, 15 years ago

Implemented discrimination of snapshots vs. end results in HiveEngine and Job. #545 (Engine which can be executed in the Hive)

File size: 12.4 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 HeuristicLab.Core;
26using System.Threading;
27using HeuristicLab.Hive.JobBase;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Contracts;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using System.IO;
33using System.Xml;
34using System.IO.Compression;
35using HeuristicLab.Tracing;
36
37namespace HeuristicLab.Hive.Engine {
38  /// <summary>
39  /// Represents an engine that executes its operator-graph on the hive.
40  /// in parallel.
41  /// </summary>
42  public class HiveEngine : ItemBase, IEngine, IEditable {
43    private const int SNAPSHOT_POLLING_INTERVAL_MS = 1000;
44    private const int RESULT_POLLING_INTERVAL_MS = 10000;
45    private const int MAX_SNAPSHOT_RETRIES = 20;
46    private Guid jobId;
47    private Job job;
48    private object locker = new object();
49    private volatile bool abortRequested;
50
51    public string HiveServerUrl { get; set; }
52
53    public HiveEngine() {
54      job = new Job();
55      abortRequested = false;
56      Priority = ThreadPriority.Lowest;
57    }
58
59    #region IEngine Members
60
61    public IOperatorGraph OperatorGraph {
62      get { return job.Engine.OperatorGraph; }
63    }
64
65    public IScope GlobalScope {
66      get { return job.Engine.GlobalScope; }
67    }
68
69    public TimeSpan ExecutionTime {
70      get { return job.Engine.ExecutionTime; }
71    }
72
73    public ThreadPriority Priority {
74      get { return job.Engine.Priority; }
75      set { job.Engine.Priority = value; }
76    }
77
78    public bool Running {
79      get { return job.Engine.Running; }
80    }
81
82    public bool Canceled {
83      get { return job.Engine.Canceled; }
84    }
85
86    public bool Terminated {
87      get { return job.Engine.Terminated; }
88    }
89
90    public void Execute() {
91      var jobObj = CreateJobObj();
92      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
93      ResponseObject<Contracts.BusinessObjects.Job> res = executionEngineFacade.AddJob(jobObj);
94      jobId = res.Obj.Id;
95
96      StartResultPollingThread();
97    }
98
99    private void StartResultPollingThread() {
100      // start a backgroud thread to poll the final result of the job
101      Thread t = new Thread(() => {
102        IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
103        ResponseObject<SerializedJobResult> response = null;
104        Job restoredJob = null;
105        do {
106          Thread.Sleep(RESULT_POLLING_INTERVAL_MS);
107          lock (locker) {
108            HiveLogger.Debug("HiveEngine: Results-polling - GetLastResult");
109            response = executionEngineFacade.GetLastSerializedResult(jobId, false);
110            HiveLogger.Debug("HiveEngine: Results-polling - Server: " + response.StatusMessage + " success: " + response.Success);
111            // loop while
112            // 1. the user doesn't request an abort
113            // 2. there is a problem with server communication (success==false)
114            // 3. no result for the job is available yet (response.Obj==null)
115            // 4. the result that we get from the server is a snapshot and not the final result
116            if (abortRequested) return;
117            if (response.Success && response.Obj != null) {
118              HiveLogger.Debug("HiveEngine: Results-polling - Got result!");
119              restoredJob = (Job)PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
120              HiveLogger.Debug("HiveEngine: Results-polling - IsSnapshotResult: " + (restoredJob.Progress<1.0));
121            }
122          }
123        } while (restoredJob == null || (restoredJob.Progress < 1.0));
124
125        job = restoredJob;
126        PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
127        OnChanged();
128        OnFinished();
129      });
130      HiveLogger.Debug("HiveEngine: Starting results-polling thread");
131      t.Start();
132    }
133
134    public void RequestSnapshot() {
135      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
136      int retryCount = 0;
137      ResponseObject<SerializedJobResult> response;
138      lock (locker) {
139        HiveLogger.Debug("HiveEngine: Abort - RequestSnapshot");
140        Response snapShotResponse = executionEngineFacade.RequestSnapshot(jobId);
141        if (snapShotResponse.StatusMessage == ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED) {
142          // job is finished already
143          HiveLogger.Debug("HiveEngine: Abort - GetLastResult(false)");
144          response = executionEngineFacade.GetLastSerializedResult(jobId, false);
145          HiveLogger.Debug("HiveEngine: Abort - Server: " + response.StatusMessage + " success: " + response.Success);
146        } else {
147          // server sent snapshot request to client
148          // poll until snapshot is ready
149          do {
150            Thread.Sleep(SNAPSHOT_POLLING_INTERVAL_MS);
151            HiveLogger.Debug("HiveEngine: Abort - GetLastResult(true)");
152            response = executionEngineFacade.GetLastSerializedResult(jobId, true);
153            HiveLogger.Debug("HiveEngine: Abort - Server: " + response.StatusMessage + " success: " + response.Success);
154            retryCount++;
155            // loop while
156            // 1. problem with communication with server
157            // 2. job result not yet ready
158          } while (
159            (retryCount < MAX_SNAPSHOT_RETRIES) && (
160            !response.Success ||
161            response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE)
162            );
163        }
164      }
165      SerializedJobResult jobResult = response.Obj;
166      if (jobResult != null) {
167        HiveLogger.Debug("HiveEngine: Results-polling - Got result!");
168        job = (Job)PersistenceManager.RestoreFromGZip(jobResult.SerializedJobResultData);
169        PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
170      }
171      //HiveLogger.Debug("HiveEngine: Results-polling - Exception!");
172      //Exception ex = new Exception(response.Obj.Exception.Message);
173      //ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
174    }
175
176    public void ExecuteStep() {
177      throw new NotSupportedException();
178    }
179
180    public void ExecuteSteps(int steps) {
181      throw new NotSupportedException();
182    }
183
184    public void Abort() {
185      abortRequested = true;
186      // RequestSnapshot();
187      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
188      executionEngineFacade.AbortJob(jobId);
189      OnChanged();
190      OnFinished();
191    }
192
193    public void Reset() {
194      abortRequested = false;
195      job.Engine.Reset();
196      jobId = Guid.NewGuid();
197      OnInitialized();
198    }
199
200    private HeuristicLab.Hive.Contracts.BusinessObjects.SerializedJob CreateJobObj() {
201      HeuristicLab.Hive.Contracts.BusinessObjects.Job jobObj =
202        new HeuristicLab.Hive.Contracts.BusinessObjects.Job();
203
204      MemoryStream memStream = new MemoryStream();
205      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
206      XmlDocument document = PersistenceManager.CreateXmlDocument();
207      Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
208      XmlNode rootNode = document.CreateElement("Root");
209      document.AppendChild(rootNode);
210      rootNode.AppendChild(PersistenceManager.Persist(job, document, dictionary));
211      document.Save(stream);
212      stream.Close();
213
214      HeuristicLab.Hive.Contracts.BusinessObjects.SerializedJob executableJobObj =
215        new HeuristicLab.Hive.Contracts.BusinessObjects.SerializedJob();
216      executableJobObj.JobInfo = jobObj;
217      executableJobObj.SerializedJobData = memStream.ToArray();
218
219      DiscoveryService service = new DiscoveryService();
220      List<PluginInfo> plugins = new List<PluginInfo>();
221
222      foreach (IStorable storeable in dictionary.Values) {
223        PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType());
224        if (!plugins.Contains(pluginInfo)) {
225          plugins.Add(pluginInfo);
226          foreach (var dependency in pluginInfo.Dependencies) {
227            if (!plugins.Contains(dependency)) plugins.Add(dependency);
228          }
229        }
230      }
231
232      List<HivePluginInfo> pluginsNeeded =
233        new List<HivePluginInfo>();
234      foreach (PluginInfo uniquePlugin in plugins) {
235        HivePluginInfo pluginInfo =
236          new HivePluginInfo();
237        pluginInfo.Name = uniquePlugin.Name;
238        pluginInfo.Version = uniquePlugin.Version.ToString();
239        pluginInfo.BuildDate = uniquePlugin.BuildDate;
240        pluginsNeeded.Add(pluginInfo);
241      }
242
243      jobObj.CoresNeeded = 1;
244      jobObj.PluginsNeeded = pluginsNeeded;
245      jobObj.State = HeuristicLab.Hive.Contracts.BusinessObjects.State.offline;
246      return executableJobObj;
247    }
248
249    public event EventHandler Initialized;
250    /// <summary>
251    /// Fires a new <c>Initialized</c> event.
252    /// </summary>
253    protected virtual void OnInitialized() {
254      if (Initialized != null)
255        Initialized(this, new EventArgs());
256    }
257
258    public event EventHandler<OperationEventArgs> OperationExecuted;
259    /// <summary>
260    /// Fires a new <c>OperationExecuted</c> event.
261    /// </summary>
262    /// <param name="operation">The operation that has been executed.</param>
263    protected virtual void OnOperationExecuted(IOperation operation) {
264      if (OperationExecuted != null)
265        OperationExecuted(this, new OperationEventArgs(operation));
266    }
267
268    public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
269    /// <summary>
270    /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event.
271    /// </summary>
272    /// <param name="exception">The exception that was thrown.</param>
273    protected virtual void OnExceptionOccurred(Exception exception) {
274      Abort();
275      if (ExceptionOccurred != null)
276        ExceptionOccurred(this, new ExceptionEventArgs(exception));
277    }
278
279    public event EventHandler ExecutionTimeChanged;
280    /// <summary>
281    /// Fires a new <c>ExecutionTimeChanged</c> event.
282    /// </summary>
283    protected virtual void OnExecutionTimeChanged() {
284      if (ExecutionTimeChanged != null)
285        ExecutionTimeChanged(this, new EventArgs());
286    }
287
288    public event EventHandler Finished;
289    /// <summary>
290    /// Fires a new <c>Finished</c> event.
291    /// </summary>
292    protected virtual void OnFinished() {
293      if (Finished != null)
294        Finished(this, new EventArgs());
295    }
296
297    #endregion
298
299    public override IView CreateView() {
300      return new HiveEngineEditor(this);
301    }
302
303    #region IEditable Members
304
305    public IEditor CreateEditor() {
306      return new HiveEngineEditor(this);
307    }
308    #endregion
309
310    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
311      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
312      XmlAttribute attr = document.CreateAttribute("HiveServerUrl");
313      attr.Value = HiveServerUrl;
314      node.Attributes.Append(attr);
315      node.AppendChild(PersistenceManager.Persist("Job", job, document, persistedObjects));
316      return node;
317    }
318
319    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
320      base.Populate(node, restoredObjects);
321      HiveServerUrl = node.Attributes["HiveServerUrl"].Value;
322      job = (Job)PersistenceManager.Restore(node.SelectSingleNode("Job"), restoredObjects);
323    }
324  }
325}
Note: See TracBrowser for help on using the repository browser.