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