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 | //Requests the last result.
|
---|
128 | //false: There will always be a result that is been sent back
|
---|
129 | //true: if you hit "requestsnapshot" before - it won't send you the job back if
|
---|
130 | // the snapshot hasn't been submitted to the server (because the client needs
|
---|
131 | // more time).
|
---|
132 | var result = executionEngineFacade.GetLastResult(jobId, false);
|
---|
133 | if (result.Success) {
|
---|
134 | JobResult jobResult = result.Obj;
|
---|
135 | if (jobResult != null) {
|
---|
136 | job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
|
---|
137 | PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
|
---|
138 | }
|
---|
139 | } else {
|
---|
140 | Exception ex = new Exception(result.Obj.Exception.Message);
|
---|
141 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | public void ExecuteStep() {
|
---|
147 | throw new NotSupportedException();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void ExecuteSteps(int steps) {
|
---|
151 | throw new NotSupportedException();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void Abort() {
|
---|
155 | IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
|
---|
156 |
|
---|
157 | //This are just Stubs on the server right now. There won't be any effect right now...
|
---|
158 | executionEngineFacade.AbortJob(jobId);
|
---|
159 | OnFinished();
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void Reset() {
|
---|
163 | job.Engine.Reset();
|
---|
164 | jobId = Guid.NewGuid();
|
---|
165 | OnInitialized();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public event EventHandler Initialized;
|
---|
169 | /// <summary>
|
---|
170 | /// Fires a new <c>Initialized</c> event.
|
---|
171 | /// </summary>
|
---|
172 | protected virtual void OnInitialized() {
|
---|
173 | if (Initialized != null)
|
---|
174 | Initialized(this, new EventArgs());
|
---|
175 | }
|
---|
176 |
|
---|
177 | public event EventHandler<OperationEventArgs> OperationExecuted;
|
---|
178 | /// <summary>
|
---|
179 | /// Fires a new <c>OperationExecuted</c> event.
|
---|
180 | /// </summary>
|
---|
181 | /// <param name="operation">The operation that has been executed.</param>
|
---|
182 | protected virtual void OnOperationExecuted(IOperation operation) {
|
---|
183 | if (OperationExecuted != null)
|
---|
184 | OperationExecuted(this, new OperationEventArgs(operation));
|
---|
185 | }
|
---|
186 |
|
---|
187 | public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
|
---|
188 | /// <summary>
|
---|
189 | /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event.
|
---|
190 | /// </summary>
|
---|
191 | /// <param name="exception">The exception that was thrown.</param>
|
---|
192 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
193 | Abort();
|
---|
194 | if (ExceptionOccurred != null)
|
---|
195 | ExceptionOccurred(this, new ExceptionEventArgs(exception));
|
---|
196 | }
|
---|
197 |
|
---|
198 | public event EventHandler ExecutionTimeChanged;
|
---|
199 | /// <summary>
|
---|
200 | /// Fires a new <c>ExecutionTimeChanged</c> event.
|
---|
201 | /// </summary>
|
---|
202 | protected virtual void OnExecutionTimeChanged() {
|
---|
203 | if (ExecutionTimeChanged != null)
|
---|
204 | ExecutionTimeChanged(this, new EventArgs());
|
---|
205 | }
|
---|
206 |
|
---|
207 | public event EventHandler Finished;
|
---|
208 | /// <summary>
|
---|
209 | /// Fires a new <c>Finished</c> event.
|
---|
210 | /// </summary>
|
---|
211 | protected virtual void OnFinished() {
|
---|
212 | if (Finished != null)
|
---|
213 | Finished(this, new EventArgs());
|
---|
214 | }
|
---|
215 |
|
---|
216 | #endregion
|
---|
217 |
|
---|
218 | public override IView CreateView() {
|
---|
219 | return new HiveEngineEditor(this);
|
---|
220 | }
|
---|
221 |
|
---|
222 | #region IEditable Members
|
---|
223 |
|
---|
224 | public IEditor CreateEditor() {
|
---|
225 | return new HiveEngineEditor(this);
|
---|
226 | }
|
---|
227 | #endregion
|
---|
228 |
|
---|
229 | public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
230 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
231 | XmlAttribute attr = document.CreateAttribute("HiveServerUrl");
|
---|
232 | attr.Value = HiveServerUrl;
|
---|
233 | node.Attributes.Append(attr);
|
---|
234 | node.AppendChild(PersistenceManager.Persist("Job", job, document, persistedObjects));
|
---|
235 | return node;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
239 | base.Populate(node, restoredObjects);
|
---|
240 | HiveServerUrl = node.Attributes["HiveServerUrl"].Value;
|
---|
241 | job = (Job)PersistenceManager.Restore(node.SelectSingleNode("Job"), restoredObjects);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|