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.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using System.Threading;
|
---|
27 | using HeuristicLab.Hive.JobBase;
|
---|
28 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
29 | using HeuristicLab.Hive.Contracts;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
32 | using System.IO;
|
---|
33 | using System.Xml;
|
---|
34 | using System.IO.Compression;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Hive.Engine {
|
---|
37 | /// <summary>
|
---|
38 | /// Represents an engine that executes its operator-graph on the hive.
|
---|
39 | /// in parallel.
|
---|
40 | /// </summary>
|
---|
41 | public class HiveEngine : ItemBase, IEngine, IEditable {
|
---|
42 | private Guid jobId;
|
---|
43 | private Job job;
|
---|
44 | public string HiveServerUrl { get; set; }
|
---|
45 |
|
---|
46 | public HiveEngine() {
|
---|
47 | job = new Job();
|
---|
48 | }
|
---|
49 |
|
---|
50 | #region IEngine Members
|
---|
51 |
|
---|
52 | public IOperatorGraph OperatorGraph {
|
---|
53 | get { return job.Engine.OperatorGraph; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IScope GlobalScope {
|
---|
57 | get { return job.Engine.GlobalScope; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public TimeSpan ExecutionTime {
|
---|
61 | get { return job.Engine.ExecutionTime; }
|
---|
62 | }
|
---|
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);
|
---|
78 |
|
---|
79 | var jobObj = CreateJobObj();
|
---|
80 |
|
---|
81 | ResponseObject<Contracts.BusinessObjects.Job> res = executionEngineFacade.AddJob(jobObj);
|
---|
82 | jobId = res.Obj.Id;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private HeuristicLab.Hive.Contracts.BusinessObjects.Job CreateJobObj() {
|
---|
86 | HeuristicLab.Hive.Contracts.BusinessObjects.Job jobObj = new HeuristicLab.Hive.Contracts.BusinessObjects.Job();
|
---|
87 |
|
---|
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 |
|
---|
107 | List<HivePluginInfo> pluginsNeeded =
|
---|
108 | new List<HivePluginInfo>();
|
---|
109 | foreach (PluginInfo uniquePlugin in plugins) {
|
---|
110 | HivePluginInfo pluginInfo =
|
---|
111 | new HivePluginInfo();
|
---|
112 | pluginInfo.Name = uniquePlugin.Name;
|
---|
113 | pluginInfo.Version = uniquePlugin.Version.ToString();
|
---|
114 | pluginInfo.BuildDate = uniquePlugin.BuildDate;
|
---|
115 | pluginsNeeded.Add(pluginInfo);
|
---|
116 | }
|
---|
117 |
|
---|
118 | jobObj.CoresNeeded = 1;
|
---|
119 | jobObj.PluginsNeeded = pluginsNeeded;
|
---|
120 | jobObj.State = HeuristicLab.Hive.Contracts.BusinessObjects.State.offline;
|
---|
121 | return jobObj;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void RequestSnapshot() {
|
---|
125 | IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
|
---|
126 |
|
---|
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;
|
---|
141 | if (jobResult != null) {
|
---|
142 | job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
|
---|
143 | PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | Exception ex = new Exception(response.Obj.Exception.Message);
|
---|
147 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
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() {
|
---|
161 | IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
|
---|
162 |
|
---|
163 | //This are just Stubs on the server right now. There won't be any effect right now...
|
---|
164 | executionEngineFacade.AbortJob(jobId);
|
---|
165 | OnFinished();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void Reset() {
|
---|
169 | job.Engine.Reset();
|
---|
170 | jobId = Guid.NewGuid();
|
---|
171 | OnInitialized();
|
---|
172 | }
|
---|
173 |
|
---|
174 | public event EventHandler Initialized;
|
---|
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 | }
|
---|
182 |
|
---|
183 | public event EventHandler<OperationEventArgs> OperationExecuted;
|
---|
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 | }
|
---|
192 |
|
---|
193 | public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
|
---|
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 | }
|
---|
203 |
|
---|
204 | public event EventHandler ExecutionTimeChanged;
|
---|
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 | }
|
---|
212 |
|
---|
213 | public event EventHandler Finished;
|
---|
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 | }
|
---|
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
|
---|
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 | }
|
---|
249 | }
|
---|
250 | }
|
---|