Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Engine/3.2/HiveEngine.cs @ 1799

Last change on this file since 1799 was 1773, checked in by gkronber, 16 years ago

worked on snapshotting functionality of HiveEngine. #545 (Engine which can be executed in the Hive)

File size: 8.5 KB
RevLine 
[1432]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;
[1440]27using HeuristicLab.Hive.JobBase;
28using HeuristicLab.Hive.Contracts.Interfaces;
[1487]29using HeuristicLab.Hive.Contracts;
[1503]30using HeuristicLab.PluginInfrastructure;
[1580]31using HeuristicLab.Hive.Contracts.BusinessObjects;
[1591]32using System.IO;
[1726]33using System.Xml;
[1730]34using System.IO.Compression;
[1432]35
36namespace HeuristicLab.Hive.Engine {
37  /// <summary>
38  /// Represents an engine that executes its operator-graph on the hive.
39  /// in parallel.
40  /// </summary>
[1440]41  public class HiveEngine : ItemBase, IEngine, IEditable {
[1510]42    private Guid jobId;
[1440]43    private Job job;
44    public string HiveServerUrl { get; set; }
[1432]45
[1440]46    public HiveEngine() {
47      job = new Job();
[1432]48    }
49
[1440]50    #region IEngine Members
51
52    public IOperatorGraph OperatorGraph {
53      get { return job.Engine.OperatorGraph; }
[1432]54    }
55
[1440]56    public IScope GlobalScope {
57      get { return job.Engine.GlobalScope; }
[1432]58    }
59
[1440]60    public TimeSpan ExecutionTime {
61      get { return job.Engine.ExecutionTime; }
[1432]62    }
[1440]63
64    public bool Running {
65      get { return job.Engine.Running; }
66    }
67
68    public bool Canceled {
69      get { return job.Engine.Canceled; }
70    }
71
72    public bool Terminated {
73      get { return job.Engine.Terminated; }
74    }
75
76    public void Execute() {
77      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
[1726]78
[1730]79      var jobObj = CreateJobObj();
[1503]80
[1730]81      ResponseObject<Contracts.BusinessObjects.Job> res = executionEngineFacade.AddJob(jobObj);
82      jobId = res.Obj.Id;
83    }
84
85    private HeuristicLab.Hive.Contracts.BusinessObjects.Job CreateJobObj() {
[1440]86      HeuristicLab.Hive.Contracts.BusinessObjects.Job jobObj = new HeuristicLab.Hive.Contracts.BusinessObjects.Job();
[1580]87
[1730]88      MemoryStream memStream = new MemoryStream();
89      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
90      XmlDocument document = PersistenceManager.CreateXmlDocument();
91      Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
92      XmlNode rootNode = document.CreateElement("Root");
93      document.AppendChild(rootNode);
94      rootNode.AppendChild(PersistenceManager.Persist(job, document, dictionary));
95      document.Save(stream);
96      stream.Close();
97      jobObj.SerializedJob = memStream.ToArray();
98
99      DiscoveryService service = new DiscoveryService();
100      List<PluginInfo> plugins = new List<PluginInfo>();
101
102      foreach (IStorable storeable in dictionary.Values) {
103        PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType());
104        if (!plugins.Contains(pluginInfo)) plugins.Add(pluginInfo);
105      }
106
[1580]107      List<HivePluginInfo> pluginsNeeded =
108        new List<HivePluginInfo>();
[1730]109      foreach (PluginInfo uniquePlugin in plugins) {
[1726]110        HivePluginInfo pluginInfo =
[1580]111          new HivePluginInfo();
[1730]112        pluginInfo.Name = uniquePlugin.Name;
113        pluginInfo.Version = uniquePlugin.Version.ToString();
114        pluginInfo.BuildDate = uniquePlugin.BuildDate;
[1580]115        pluginsNeeded.Add(pluginInfo);
116      }
117
[1754]118      jobObj.CoresNeeded = 1;
[1580]119      jobObj.PluginsNeeded = pluginsNeeded;
[1730]120      jobObj.State = HeuristicLab.Hive.Contracts.BusinessObjects.State.offline;
121      return jobObj;
[1440]122    }
123
[1726]124    public void RequestSnapshot() {
125      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
126
[1773]127      // request snapshot
128      executionEngineFacade.RequestSnapshot(jobId);
129
130      // poll until snapshot is ready
131      ResponseObject<JobResult> response;
132      do {
133        response = executionEngineFacade.GetLastResult(jobId, true);
134        if (response.Success && response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE) {
135          Thread.Sleep(1000);
136        }
137      } while (response.Success && response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE);
138
139      if (response.Success) {
140        JobResult jobResult = response.Obj;
[1726]141        if (jobResult != null) {
142          job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
143          PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
144        }
145      } else {
[1773]146        Exception ex = new Exception(response.Obj.Exception.Message);
[1726]147        ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
148      }
149    }
150
151
[1440]152    public void ExecuteStep() {
153      throw new NotSupportedException();
154    }
155
156    public void ExecuteSteps(int steps) {
157      throw new NotSupportedException();
158    }
159
160    public void Abort() {
[1510]161      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
[1726]162
[1510]163      //This are just Stubs on the server right now. There won't be any effect right now...
[1726]164      executionEngineFacade.AbortJob(jobId);
165      OnFinished();
[1440]166    }
167
168    public void Reset() {
[1726]169      job.Engine.Reset();
170      jobId = Guid.NewGuid();
171      OnInitialized();
[1440]172    }
173
174    public event EventHandler Initialized;
[1726]175    /// <summary>
176    /// Fires a new <c>Initialized</c> event.
177    /// </summary>
178    protected virtual void OnInitialized() {
179      if (Initialized != null)
180        Initialized(this, new EventArgs());
181    }
[1440]182
183    public event EventHandler<OperationEventArgs> OperationExecuted;
[1726]184    /// <summary>
185    /// Fires a new <c>OperationExecuted</c> event.
186    /// </summary>
187    /// <param name="operation">The operation that has been executed.</param>
188    protected virtual void OnOperationExecuted(IOperation operation) {
189      if (OperationExecuted != null)
190        OperationExecuted(this, new OperationEventArgs(operation));
191    }
[1440]192
193    public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
[1726]194    /// <summary>
195    /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event.
196    /// </summary>
197    /// <param name="exception">The exception that was thrown.</param>
198    protected virtual void OnExceptionOccurred(Exception exception) {
199      Abort();
200      if (ExceptionOccurred != null)
201        ExceptionOccurred(this, new ExceptionEventArgs(exception));
202    }
[1440]203
204    public event EventHandler ExecutionTimeChanged;
[1726]205    /// <summary>
206    /// Fires a new <c>ExecutionTimeChanged</c> event.
207    /// </summary>
208    protected virtual void OnExecutionTimeChanged() {
209      if (ExecutionTimeChanged != null)
210        ExecutionTimeChanged(this, new EventArgs());
211    }
[1440]212
213    public event EventHandler Finished;
[1726]214    /// <summary>
215    /// Fires a new <c>Finished</c> event.
216    /// </summary>
217    protected virtual void OnFinished() {
218      if (Finished != null)
219        Finished(this, new EventArgs());
220    }
[1440]221
222    #endregion
223
224    public override IView CreateView() {
225      return new HiveEngineEditor(this);
226    }
227
228    #region IEditable Members
229
230    public IEditor CreateEditor() {
231      return new HiveEngineEditor(this);
232    }
233    #endregion
[1726]234
235    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
236      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
237      XmlAttribute attr = document.CreateAttribute("HiveServerUrl");
238      attr.Value = HiveServerUrl;
239      node.Attributes.Append(attr);
240      node.AppendChild(PersistenceManager.Persist("Job", job, document, persistedObjects));
241      return node;
242    }
243
244    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
245      base.Populate(node, restoredObjects);
246      HiveServerUrl = node.Attributes["HiveServerUrl"].Value;
247      job = (Job)PersistenceManager.Restore(node.SelectSingleNode("Job"), restoredObjects);
248    }
[1432]249  }
250}
Note: See TracBrowser for help on using the repository browser.