1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Configuration;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Threading;
|
---|
28 | using HeuristicLab.Clients.Hive.Jobs;
|
---|
29 | using HeuristicLab.Collections;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Clients.Hive {
|
---|
36 | /// <summary>
|
---|
37 | /// An experiment which contains multiple batch runs of algorithms.
|
---|
38 | /// </summary>
|
---|
39 | [Item(itemName, itemDescription)]
|
---|
40 | public class HiveExperimentClient : NamedItem, IExecutable, IProgressReporter, IItemTree {
|
---|
41 | private object locker = new object();
|
---|
42 | private const string itemName = "Hive Experiment";
|
---|
43 | private const string itemDescription = "A runner for a single experiment, which's algorithms are executed in the Hive.";
|
---|
44 | private System.Timers.Timer timer;
|
---|
45 | private DateTime lastUpdateTime;
|
---|
46 | private Guid rootJobId;
|
---|
47 | private JobResultPoller jobResultPoller;
|
---|
48 |
|
---|
49 | private Guid hiveExperimentId;
|
---|
50 | public Guid HiveExperimentId {
|
---|
51 | get { return hiveExperimentId; }
|
---|
52 | set { hiveExperimentId = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | private HiveJob hiveJob;
|
---|
56 | public HiveJob HiveJob {
|
---|
57 | get { return hiveJob; }
|
---|
58 | set {
|
---|
59 | DeregisterHiveJobEvents();
|
---|
60 | if (hiveJob != value) {
|
---|
61 | hiveJob = value;
|
---|
62 | RegisterHiveJobEvents();
|
---|
63 | OnHiveJobChanged();
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private ILog log;
|
---|
69 | public ILog Log {
|
---|
70 | get { return log; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private string resourceNames;
|
---|
74 | public string ResourceNames {
|
---|
75 | get { return resourceNames; }
|
---|
76 | set {
|
---|
77 | if (resourceNames != value) {
|
---|
78 | resourceNames = value;
|
---|
79 | OnResourceNamesChanged();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | private bool isPollingResults;
|
---|
85 | public bool IsPollingResults {
|
---|
86 | get { return isPollingResults; }
|
---|
87 | private set {
|
---|
88 | if (isPollingResults != value) {
|
---|
89 | isPollingResults = value;
|
---|
90 | OnIsPollingResultsChanged();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private bool isProgressing;
|
---|
96 | public bool IsProgressing {
|
---|
97 | get { return isProgressing; }
|
---|
98 | set {
|
---|
99 | if (isProgressing != value) {
|
---|
100 | isProgressing = value;
|
---|
101 | OnIsProgressingChanged();
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | private IProgress progress;
|
---|
107 | public IProgress Progress {
|
---|
108 | get { return progress; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | private IEnumerable<Plugin> onlinePlugins;
|
---|
112 | public IEnumerable<Plugin> OnlinePlugins {
|
---|
113 | get { return onlinePlugins; }
|
---|
114 | set { onlinePlugins = value; }
|
---|
115 | }
|
---|
116 |
|
---|
117 | private List<Plugin> alreadyUploadedPlugins;
|
---|
118 | public List<Plugin> AlreadyUploadedPlugins {
|
---|
119 | get { return alreadyUploadedPlugins; }
|
---|
120 | set { alreadyUploadedPlugins = value; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private bool useLocalPlugins;
|
---|
124 | public bool UseLocalPlugins {
|
---|
125 | get { return useLocalPlugins; }
|
---|
126 | set { useLocalPlugins = value; }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public HiveExperimentClient()
|
---|
130 | : base(itemName, itemDescription) {
|
---|
131 | this.ResourceNames = "HEAL";
|
---|
132 | this.log = new Log();
|
---|
133 | InitTimer();
|
---|
134 | }
|
---|
135 | public HiveExperimentClient(HiveExperiment hiveExperimentDto)
|
---|
136 | : this() {
|
---|
137 | UpdateFromDto(hiveExperimentDto);
|
---|
138 | }
|
---|
139 | protected HiveExperimentClient(HiveExperimentClient original, Cloner cloner)
|
---|
140 | : base(original, cloner) {
|
---|
141 | this.ResourceNames = original.resourceNames;
|
---|
142 | this.ExecutionState = original.executionState;
|
---|
143 | this.ExecutionTime = original.executionTime;
|
---|
144 | this.log = cloner.Clone(original.log);
|
---|
145 | this.lastUpdateTime = original.lastUpdateTime;
|
---|
146 | this.rootJobId = original.rootJobId;
|
---|
147 | }
|
---|
148 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
149 | return new HiveExperimentClient(this, cloner);
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void UpdateFromDto(HiveExperiment hiveExperimentDto) {
|
---|
153 | this.HiveExperimentId = hiveExperimentDto.Id;
|
---|
154 | this.Name = hiveExperimentDto.Name;
|
---|
155 | this.Description = hiveExperimentDto.Description;
|
---|
156 | this.ResourceNames = hiveExperimentDto.ResourceNames;
|
---|
157 | this.rootJobId = hiveExperimentDto.RootJobId;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public HiveExperiment ToHiveExperimentDto() {
|
---|
161 | return new HiveExperiment() {
|
---|
162 | Id = this.HiveExperimentId,
|
---|
163 | Name = this.Name,
|
---|
164 | Description = this.Description,
|
---|
165 | ResourceNames = this.ResourceNames,
|
---|
166 | RootJobId = this.rootJobId
|
---|
167 | };
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void SetExperiment(Experiment experiment) {
|
---|
171 | this.HiveJob = new HiveJob(experiment);
|
---|
172 | Prepare();
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void RegisterHiveJobEvents() {
|
---|
176 | if (HiveJob != null) {
|
---|
177 | HiveJob.JobStateChanged += new EventHandler(HiveJob_JobStateChanged);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void DeregisterHiveJobEvents() {
|
---|
182 | if (HiveJob != null) {
|
---|
183 | HiveJob.JobStateChanged -= new EventHandler(HiveJob_JobStateChanged);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /// <summary>
|
---|
188 | /// Returns the experiment from the root HiveJob
|
---|
189 | /// </summary>
|
---|
190 | public Experiment GetExperiment() {
|
---|
191 | if (this.HiveJob != null) {
|
---|
192 | return HiveJob.OptimizerJob.OptimizerAsExperiment;
|
---|
193 | }
|
---|
194 | return null;
|
---|
195 | }
|
---|
196 |
|
---|
197 | #region IExecutable Members
|
---|
198 | private ExecutionState executionState;
|
---|
199 | public ExecutionState ExecutionState {
|
---|
200 | get { return executionState; }
|
---|
201 | private set {
|
---|
202 | if (executionState != value) {
|
---|
203 | executionState = value;
|
---|
204 | OnExecutionStateChanged();
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | private TimeSpan executionTime;
|
---|
210 | public TimeSpan ExecutionTime {
|
---|
211 | get { return executionTime; }
|
---|
212 | private set {
|
---|
213 | if (executionTime != value) {
|
---|
214 | executionTime = value;
|
---|
215 | OnExecutionTimeChanged();
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void Pause() {
|
---|
221 | throw new NotSupportedException();
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void Prepare() {
|
---|
225 | this.timer.Stop();
|
---|
226 | this.ExecutionState = Core.ExecutionState.Prepared;
|
---|
227 | this.ExecutionTime = TimeSpan.Zero;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public void Start() {
|
---|
231 | OnStarted();
|
---|
232 | ExecutionTime = TimeSpan.Zero;
|
---|
233 | lastUpdateTime = DateTime.Now;
|
---|
234 | this.ExecutionState = Core.ExecutionState.Started;
|
---|
235 |
|
---|
236 | Thread t = new Thread(RunUploadExperiment);
|
---|
237 | t.Name = "RunUploadExperimentThread";
|
---|
238 | t.Start();
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void RunUploadExperiment() {
|
---|
242 | try {
|
---|
243 | this.progress = new Progress("Connecting to server...");
|
---|
244 | IsProgressing = true;
|
---|
245 | ServiceLocator.Instance.CallHiveService(service => {
|
---|
246 | IEnumerable<string> resourceNames = ToResourceNameList(this.ResourceNames);
|
---|
247 | var resourceIds = new List<Guid>();
|
---|
248 | foreach (var resourceName in resourceNames) {
|
---|
249 | Guid resourceId = service.GetResourceId(resourceName);
|
---|
250 | if (resourceId == Guid.Empty) {
|
---|
251 | throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
|
---|
252 | }
|
---|
253 | resourceIds.Add(resourceId);
|
---|
254 | }
|
---|
255 |
|
---|
256 | this.HiveJob.SetIndexInParentOptimizerList(null);
|
---|
257 |
|
---|
258 | int totalJobCount = this.HiveJob.GetAllHiveJobs().Count();
|
---|
259 | int jobCount = 0;
|
---|
260 |
|
---|
261 | this.progress.Status = "Uploading plugins...";
|
---|
262 | this.OnlinePlugins = service.GetPlugins();
|
---|
263 | this.AlreadyUploadedPlugins = new List<Plugin>();
|
---|
264 | Plugin configFilePlugin = UploadConfigurationFile(service);
|
---|
265 | this.alreadyUploadedPlugins.Add(configFilePlugin);
|
---|
266 |
|
---|
267 | this.progress.Status = "Uploading jobs...";
|
---|
268 | UploadJobWithChildren(service, this.HiveJob, null, resourceIds, ref jobCount, totalJobCount, configFilePlugin.Id);
|
---|
269 | this.rootJobId = this.HiveJob.Job.Id;
|
---|
270 | LogMessage("Finished sending jobs to hive");
|
---|
271 |
|
---|
272 | // insert or update HiveExperiment
|
---|
273 | this.progress.Status = "Uploading HiveExperiment...";
|
---|
274 |
|
---|
275 | HiveExperiment he = service.GetHiveExperiment(service.AddHiveExperiment(this.ToHiveExperimentDto()));
|
---|
276 | this.UpdateFromDto(he);
|
---|
277 |
|
---|
278 | StartResultPolling();
|
---|
279 | });
|
---|
280 | }
|
---|
281 | catch (Exception e) {
|
---|
282 | OnExceptionOccured(e);
|
---|
283 | this.Prepare();
|
---|
284 | }
|
---|
285 | finally {
|
---|
286 | IsProgressing = false;
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | /// <summary>
|
---|
291 | /// Uploads the local configuration file as plugin
|
---|
292 | /// </summary>
|
---|
293 | private static Plugin UploadConfigurationFile(IHiveService service) {
|
---|
294 | string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HeuristicLab 3.3.exe");
|
---|
295 | string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
|
---|
296 | string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
|
---|
297 |
|
---|
298 | Plugin configPlugin = new Plugin() {
|
---|
299 | Name = "Configuration",
|
---|
300 | IsLocal = true,
|
---|
301 | Version = new Version()
|
---|
302 | };
|
---|
303 | PluginData configFile = new PluginData() {
|
---|
304 | FileName = configFileName,
|
---|
305 | Data = File.ReadAllBytes(configFilePath)
|
---|
306 | };
|
---|
307 | configPlugin.Id = service.AddPlugin(configPlugin, new List<PluginData> { configFile });
|
---|
308 | return configPlugin;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /// <summary>
|
---|
312 | /// Uploads the given job and all its child-jobs while setting the proper parentJobId values for the childs
|
---|
313 | /// </summary>
|
---|
314 | /// <param name="service"></param>
|
---|
315 | /// <param name="hiveJob"></param>
|
---|
316 | /// <param name="parentHiveJob">shall be null if its the root job</param>
|
---|
317 | /// <param name="groups"></param>
|
---|
318 | private void UploadJobWithChildren(IHiveService service, HiveJob hiveJob, HiveJob parentHiveJob, IEnumerable<Guid> groups, ref int jobCount, int totalJobCount, Guid configPluginId) {
|
---|
319 | jobCount++;
|
---|
320 | this.progress.Status = string.Format("Serializing job {0} of {1}", jobCount, totalJobCount);
|
---|
321 | JobData jobData;
|
---|
322 | List<IPluginDescription> plugins;
|
---|
323 |
|
---|
324 | if (hiveJob.OptimizerJob.ComputeInParallel &&
|
---|
325 | (hiveJob.OptimizerJob.Optimizer is Optimization.Experiment || hiveJob.OptimizerJob.Optimizer is Optimization.BatchRun)) {
|
---|
326 | hiveJob.Job.IsParentJob = true;
|
---|
327 | hiveJob.Job.FinishWhenChildJobsFinished = true;
|
---|
328 | hiveJob.OptimizerJob.CollectChildJobs = false; // don't collect child-jobs on slaves
|
---|
329 | jobData = hiveJob.GetAsJobData(true, out plugins);
|
---|
330 | } else {
|
---|
331 | hiveJob.Job.IsParentJob = false;
|
---|
332 | hiveJob.Job.FinishWhenChildJobsFinished = false;
|
---|
333 | jobData = hiveJob.GetAsJobData(false, out plugins);
|
---|
334 | }
|
---|
335 |
|
---|
336 | hiveJob.Job.PluginsNeededIds = GetPluginDependencies(service, onlinePlugins, alreadyUploadedPlugins, plugins, useLocalPlugins);
|
---|
337 | hiveJob.Job.PluginsNeededIds.Add(configPluginId);
|
---|
338 |
|
---|
339 | this.progress.Status = string.Format("Uploading job {0} of {1} ({2} kb)", jobCount, totalJobCount, jobData.Data.Count() / 1024);
|
---|
340 | this.progress.ProgressValue = (double)jobCount / totalJobCount;
|
---|
341 |
|
---|
342 | //service.UpdateJobState(hiveJob.Job.Id, JobState.Transferring, null, null, null);
|
---|
343 |
|
---|
344 | if (parentHiveJob != null) {
|
---|
345 | hiveJob.Job.Id = service.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData);
|
---|
346 | } else {
|
---|
347 | hiveJob.Job.Id = service.AddJob(hiveJob.Job, jobData, groups.ToList());
|
---|
348 | }
|
---|
349 | LogMessage(hiveJob.Job.Id, "Job sent to Hive");
|
---|
350 |
|
---|
351 | foreach (HiveJob child in hiveJob.ChildHiveJobs) {
|
---|
352 | UploadJobWithChildren(service, child, hiveJob, groups, ref jobCount, totalJobCount, configPluginId);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | /// <summary>
|
---|
357 | /// Converts a string which can contain Ids separated by ';' to a enumerable
|
---|
358 | /// </summary>
|
---|
359 | private IEnumerable<string> ToResourceNameList(string resourceGroups) {
|
---|
360 | if (!string.IsNullOrEmpty(resourceGroups)) {
|
---|
361 | return resourceNames.Split(';');
|
---|
362 | } else {
|
---|
363 | return new List<string>();
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | public void Stop() {
|
---|
368 | ServiceLocator.Instance.CallHiveService(service => {
|
---|
369 | foreach (HiveJob hj in HiveJob.GetAllHiveJobs()) {
|
---|
370 | service.StopJob(hj.Job.Id);
|
---|
371 | }
|
---|
372 | });
|
---|
373 | }
|
---|
374 |
|
---|
375 | #endregion
|
---|
376 |
|
---|
377 |
|
---|
378 | #region HiveJob Events
|
---|
379 | private void HiveJob_JobStateChanged(object sender, EventArgs e) {
|
---|
380 | if (HiveJob != null) {
|
---|
381 | rootJobId = HiveJob.Job.Id;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | #endregion
|
---|
385 |
|
---|
386 | #region Eventhandler
|
---|
387 |
|
---|
388 | public event EventHandler ExecutionTimeChanged;
|
---|
389 | private void OnExecutionTimeChanged() {
|
---|
390 | EventHandler handler = ExecutionTimeChanged;
|
---|
391 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
392 | }
|
---|
393 |
|
---|
394 | public event EventHandler ExecutionStateChanged;
|
---|
395 | private void OnExecutionStateChanged() {
|
---|
396 | LogMessage("ExecutionState changed to " + executionState.ToString());
|
---|
397 | EventHandler handler = ExecutionStateChanged;
|
---|
398 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
399 | }
|
---|
400 |
|
---|
401 | public event EventHandler Started;
|
---|
402 | private void OnStarted() {
|
---|
403 | LogMessage("Started");
|
---|
404 | timer.Start();
|
---|
405 | EventHandler handler = Started;
|
---|
406 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
407 | }
|
---|
408 |
|
---|
409 | public event EventHandler Stopped;
|
---|
410 | private void OnStopped() {
|
---|
411 | LogMessage("Stopped");
|
---|
412 | timer.Stop();
|
---|
413 | EventHandler handler = Stopped;
|
---|
414 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
415 | }
|
---|
416 |
|
---|
417 | public event EventHandler Paused;
|
---|
418 | private void OnPaused() {
|
---|
419 | LogMessage("Paused");
|
---|
420 | EventHandler handler = Paused;
|
---|
421 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
422 | }
|
---|
423 |
|
---|
424 | public event EventHandler Prepared;
|
---|
425 | protected virtual void OnPrepared() {
|
---|
426 | LogMessage("Prepared");
|
---|
427 | EventHandler handler = Prepared;
|
---|
428 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
429 | }
|
---|
430 |
|
---|
431 | public event EventHandler ResourceNamesChanged;
|
---|
432 | protected virtual void OnResourceNamesChanged() {
|
---|
433 | EventHandler handler = ResourceNamesChanged;
|
---|
434 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
435 | }
|
---|
436 |
|
---|
437 | public event EventHandler IsResultsPollingChanged;
|
---|
438 | private void OnIsPollingResultsChanged() {
|
---|
439 | if (this.IsPollingResults) {
|
---|
440 | LogMessage("Results Polling Started");
|
---|
441 | } else {
|
---|
442 | LogMessage("Results Polling Stopped");
|
---|
443 | }
|
---|
444 | EventHandler handler = IsResultsPollingChanged;
|
---|
445 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
446 | }
|
---|
447 |
|
---|
448 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
449 | private void OnExceptionOccured(Exception e) {
|
---|
450 | var handler = ExceptionOccurred;
|
---|
451 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
452 | }
|
---|
453 |
|
---|
454 | public event EventHandler HiveJobChanged;
|
---|
455 | private void OnHiveJobChanged() {
|
---|
456 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
457 | jobResultPoller.Stop();
|
---|
458 | DeregisterResultPollingEvents();
|
---|
459 | }
|
---|
460 | if (HiveJob != null) {
|
---|
461 | //TODO: find a better place for ApplicationConstants
|
---|
462 | jobResultPoller = new JobResultPoller(HiveJob, /*ApplicationConstants.ResultPollingInterval*/new TimeSpan(0, 0, 5));
|
---|
463 | RegisterResultPollingEvents();
|
---|
464 | }
|
---|
465 | EventHandler handler = HiveJobChanged;
|
---|
466 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
467 | }
|
---|
468 |
|
---|
469 | public event EventHandler IsProgressingChanged;
|
---|
470 | private void OnIsProgressingChanged() {
|
---|
471 | var handler = IsProgressingChanged;
|
---|
472 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
473 | }
|
---|
474 | #endregion
|
---|
475 |
|
---|
476 | #region JobResultPoller Events
|
---|
477 |
|
---|
478 | public void StartResultPolling() {
|
---|
479 | if (!jobResultPoller.IsPolling) {
|
---|
480 | jobResultPoller.Start();
|
---|
481 | } else {
|
---|
482 | throw new JobResultPollingException("Result polling already running");
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | public void StopResultPolling() {
|
---|
487 | if (jobResultPoller.IsPolling) {
|
---|
488 | jobResultPoller.Stop();
|
---|
489 | } else {
|
---|
490 | throw new JobResultPollingException("Result polling not running");
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | private void RegisterResultPollingEvents() {
|
---|
495 | jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
496 | jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
497 | jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
498 | }
|
---|
499 | private void DeregisterResultPollingEvents() {
|
---|
500 | jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
501 | jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
502 | jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
503 | }
|
---|
504 | private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
|
---|
505 | this.IsPollingResults = jobResultPoller.IsPolling;
|
---|
506 | }
|
---|
507 | private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightJob>> e) {
|
---|
508 | foreach (LightweightJob lightweightJob in e.Value) {
|
---|
509 | HiveJob hj = hiveJob.GetHiveJobByJobId(lightweightJob.Id);
|
---|
510 | if (hj != null) {
|
---|
511 | DateTime lastJobDataUpdate = hj.Job.LastJobDataUpdate;
|
---|
512 | hj.UpdateFromLightweightJob(lightweightJob);
|
---|
513 |
|
---|
514 | // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
|
---|
515 | if (lastJobDataUpdate != DateTime.MinValue && lastJobDataUpdate < hj.Job.LastJobDataUpdate) {
|
---|
516 | LogMessage(hj.Job.Id, "Downloading optimizer for job");
|
---|
517 | OptimizerJob optimizerJob = LoadOptimizerJob(hj.Job.Id);
|
---|
518 | if (optimizerJob == null) {
|
---|
519 | // something bad happened to this job. bad job, BAAAD job!
|
---|
520 | } else {
|
---|
521 | // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
|
---|
522 | if (hj.Job.State == JobState.Paused) {
|
---|
523 | hj.OptimizerJob = optimizerJob;
|
---|
524 | } else {
|
---|
525 | if (lightweightJob.ParentJobId.HasValue) {
|
---|
526 | HiveJob parentHiveJob = HiveJob.GetHiveJobByJobId(lightweightJob.ParentJobId.Value);
|
---|
527 | parentHiveJob.UpdateChildOptimizer(optimizerJob, hj.Job.Id);
|
---|
528 | }
|
---|
529 | }
|
---|
530 | }
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
|
---|
535 | if (AllJobsFinished()) {
|
---|
536 | this.ExecutionState = Core.ExecutionState.Stopped;
|
---|
537 | StopResultPolling();
|
---|
538 | OnStopped();
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | private bool AllJobsFinished() {
|
---|
543 | return HiveJob.GetAllHiveJobs().All(j => j.Job.State == JobState.Finished
|
---|
544 | || j.Job.State == JobState.Aborted
|
---|
545 | || j.Job.State == JobState.Failed);
|
---|
546 | }
|
---|
547 |
|
---|
548 | private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
549 | OnExceptionOccured(e.Value);
|
---|
550 | }
|
---|
551 | #endregion
|
---|
552 |
|
---|
553 | #region Execution Time Timer
|
---|
554 | private void InitTimer() {
|
---|
555 | timer = new System.Timers.Timer(100);
|
---|
556 | timer.AutoReset = true;
|
---|
557 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
558 | }
|
---|
559 |
|
---|
560 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
561 | DateTime now = DateTime.Now;
|
---|
562 | ExecutionTime += now - lastUpdateTime;
|
---|
563 | lastUpdateTime = now;
|
---|
564 | }
|
---|
565 | #endregion
|
---|
566 |
|
---|
567 | #region Logging
|
---|
568 | private void LogMessage(string message) {
|
---|
569 | // HeuristicLab.Log is not Thread-Safe, so lock on every call
|
---|
570 | lock (locker) {
|
---|
571 | log.LogMessage(message);
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | private void LogMessage(Guid jobId, string message) {
|
---|
576 | //GetJobItemById(jobId).LogMessage(message);
|
---|
577 | LogMessage(message + " (jobId: " + jobId + ")");
|
---|
578 | }
|
---|
579 |
|
---|
580 | #endregion
|
---|
581 |
|
---|
582 | #region Job Loading
|
---|
583 | /// <summary>
|
---|
584 | /// Downloads the root job from hive and sets the experiment, rootJob and rootJobItem
|
---|
585 | /// </summary>
|
---|
586 | public void LoadHiveJob() {
|
---|
587 | progress = new Progress();
|
---|
588 | try {
|
---|
589 | IsProgressing = true;
|
---|
590 | int totalJobCount = 0;
|
---|
591 | IEnumerable<LightweightJob> allJobs;
|
---|
592 |
|
---|
593 | progress.Status = "Connecting to Server...";
|
---|
594 | // fetch all Job objects to create the full tree of tree of HiveJob objects
|
---|
595 | progress.Status = "Downloading list of jobs...";
|
---|
596 | allJobs = ServiceLocator.Instance.CallHiveService(s => s.GetLightweightChildJobs(rootJobId, true, true));
|
---|
597 | totalJobCount = allJobs.Count();
|
---|
598 |
|
---|
599 | HiveJobDownloader downloader = new HiveJobDownloader(allJobs.Select(x => x.Id));
|
---|
600 | downloader.StartAsync();
|
---|
601 |
|
---|
602 | while (!downloader.IsFinished) {
|
---|
603 | progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
|
---|
604 | progress.Status = string.Format("Downloading/deserializing jobs... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
|
---|
605 | Thread.Sleep(500);
|
---|
606 | }
|
---|
607 | IDictionary<Guid, HiveJob> allHiveJobs = downloader.Results;
|
---|
608 |
|
---|
609 | this.HiveJob = allHiveJobs[this.rootJobId];
|
---|
610 |
|
---|
611 | if (this.HiveJob.Job.DateFinished.HasValue && this.HiveJob.Job.DateCreated.HasValue) {
|
---|
612 | this.ExecutionTime = this.HiveJob.Job.DateFinished.Value - this.HiveJob.Job.DateCreated.Value;
|
---|
613 | this.lastUpdateTime = this.HiveJob.Job.DateFinished.Value;
|
---|
614 | this.ExecutionState = Core.ExecutionState.Stopped;
|
---|
615 | OnStopped();
|
---|
616 | } else {
|
---|
617 | this.ExecutionTime = this.HiveJob.Job.DateCreated.HasValue ? DateTime.Now - this.HiveJob.Job.DateCreated.Value : TimeSpan.Zero;
|
---|
618 | this.lastUpdateTime = DateTime.Now;
|
---|
619 | this.ExecutionState = Core.ExecutionState.Started;
|
---|
620 | OnStarted();
|
---|
621 | }
|
---|
622 |
|
---|
623 | // build child-job tree
|
---|
624 | BuildHiveJobTree(this.HiveJob, allJobs, allHiveJobs);
|
---|
625 | StartResultPolling();
|
---|
626 | }
|
---|
627 | catch (Exception e) {
|
---|
628 | OnExceptionOccured(e);
|
---|
629 | }
|
---|
630 | finally {
|
---|
631 | IsProgressing = false;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | private void BuildHiveJobTree(HiveJob parentHiveJob, IEnumerable<LightweightJob> allJobs, IDictionary<Guid, HiveJob> allHiveJobs) {
|
---|
636 | IEnumerable<LightweightJob> childJobs = from job in allJobs
|
---|
637 | where job.ParentJobId.HasValue && job.ParentJobId.Value == parentHiveJob.Job.Id
|
---|
638 | orderby job.DateCreated ascending
|
---|
639 | select job;
|
---|
640 | foreach (LightweightJob job in childJobs) {
|
---|
641 | HiveJob childHiveJob = allHiveJobs[job.Id];
|
---|
642 | parentHiveJob.AddChildHiveJob(childHiveJob);
|
---|
643 | BuildHiveJobTree(childHiveJob, allJobs, allHiveJobs);
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | private OptimizerJob LoadOptimizerJob(Guid jobId) {
|
---|
648 | JobData jobData = ServiceLocator.Instance.CallHiveService(s => s.GetJobData(jobId));
|
---|
649 | try {
|
---|
650 | return PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data);
|
---|
651 | }
|
---|
652 | catch {
|
---|
653 | return null;
|
---|
654 | }
|
---|
655 | }
|
---|
656 | #endregion
|
---|
657 |
|
---|
658 | #region Plugin Management
|
---|
659 | /// <summary>
|
---|
660 | /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
|
---|
661 | /// </summary>
|
---|
662 | /// <param name="service">An active service-proxy</param>
|
---|
663 | /// <param name="onlinePlugins">List of plugins which are available online</param>
|
---|
664 | /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this HiveExperiment</param>
|
---|
665 | /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
|
---|
666 | /// <param name="useLocalPlugins">If true, the plugins which are already online are ignored. All local plugins are uploaded, but only once.</param>
|
---|
667 | /// <returns></returns>
|
---|
668 | private static List<Guid> GetPluginDependencies(IHiveService service, IEnumerable<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins, IEnumerable<IPluginDescription> neededPlugins, bool useLocalPlugins) {
|
---|
669 | var pluginIds = new List<Guid>();
|
---|
670 | foreach (var neededPlugin in neededPlugins) {
|
---|
671 | Plugin foundPlugin = alreadyUploadedPlugins.SingleOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
|
---|
672 | if (foundPlugin == null) {
|
---|
673 | foundPlugin = onlinePlugins.SingleOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
|
---|
674 | if (useLocalPlugins || foundPlugin == null) {
|
---|
675 | Plugin p = CreatePlugin(neededPlugin, useLocalPlugins);
|
---|
676 | List<PluginData> pd = CreatePluginDatas(neededPlugin);
|
---|
677 | p.Id = service.AddPlugin(p, pd);
|
---|
678 | alreadyUploadedPlugins.Add(p);
|
---|
679 | pluginIds.Add(p.Id);
|
---|
680 | } else {
|
---|
681 | pluginIds.Add(foundPlugin.Id);
|
---|
682 | }
|
---|
683 | } else {
|
---|
684 | pluginIds.Add(foundPlugin.Id);
|
---|
685 | }
|
---|
686 | }
|
---|
687 | return pluginIds;
|
---|
688 | }
|
---|
689 |
|
---|
690 | private static Plugin CreatePlugin(IPluginDescription plugin, bool useLocalPlugins) {
|
---|
691 | return new Plugin() { Name = plugin.Name, Version = plugin.Version, IsLocal = useLocalPlugins };
|
---|
692 | }
|
---|
693 |
|
---|
694 | private static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
|
---|
695 | List<PluginData> pluginDatas = new List<PluginData>();
|
---|
696 |
|
---|
697 | foreach (IPluginFile pf in plugin.Files) {
|
---|
698 | PluginData pluginData = new PluginData();
|
---|
699 |
|
---|
700 | pluginData.Data = File.ReadAllBytes(pf.Name);
|
---|
701 | pluginData.FileName = Path.GetFileName(pf.Name);
|
---|
702 | pluginDatas.Add(pluginData);
|
---|
703 | }
|
---|
704 | return pluginDatas;
|
---|
705 | }
|
---|
706 | #endregion
|
---|
707 |
|
---|
708 | #region IItemTree Members
|
---|
709 | public IObservableCollection<IItemTree> GetChildItems() {
|
---|
710 | return new ObservableCollection<IItemTree> { hiveJob };
|
---|
711 | }
|
---|
712 | IEnumerable<IItemTree> IItemTree.GetChildItems() {
|
---|
713 | return this.GetChildItems();
|
---|
714 | }
|
---|
715 | #endregion
|
---|
716 |
|
---|
717 | #region INotifyObservableCollectionItemsChanged<IItemTree> Members
|
---|
718 | public event CollectionItemsChangedEventHandler<IItemTree> CollectionReset;
|
---|
719 | public event CollectionItemsChangedEventHandler<IItemTree> ItemsAdded;
|
---|
720 | public event CollectionItemsChangedEventHandler<IItemTree> ItemsRemoved;
|
---|
721 | #endregion
|
---|
722 | }
|
---|
723 | }
|
---|