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.PollingStarted += new EventHandler(jobResultPoller_PollingStarted);
|
---|
498 | jobResultPoller.PollingFinished += new EventHandler(jobResultPoller_PollingFinished);
|
---|
499 | jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
500 | }
|
---|
501 | private void DeregisterResultPollingEvents() {
|
---|
502 | jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
503 | jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
504 | jobResultPoller.PollingStarted -= new EventHandler(jobResultPoller_PollingStarted);
|
---|
505 | jobResultPoller.PollingFinished -= new EventHandler(jobResultPoller_PollingFinished);
|
---|
506 | jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
507 | }
|
---|
508 | private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
|
---|
509 | this.IsPollingResults = jobResultPoller.IsPolling;
|
---|
510 | }
|
---|
511 | private void jobResultPoller_PollingFinished(object sender, EventArgs e) {
|
---|
512 | LogMessage("Polling results finished");
|
---|
513 | }
|
---|
514 | private void jobResultPoller_PollingStarted(object sender, EventArgs e) {
|
---|
515 | LogMessage("Polling results started");
|
---|
516 | }
|
---|
517 | private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightJob>> e) {
|
---|
518 | foreach (LightweightJob lightweightJob in e.Value) {
|
---|
519 | HiveJob hj = hiveJob.GetHiveJobByJobId(lightweightJob.Id);
|
---|
520 | if (hj != null) {
|
---|
521 | DateTime lastJobDataUpdate = hj.Job.LastJobDataUpdate;
|
---|
522 | hj.UpdateFromLightweightJob(lightweightJob);
|
---|
523 |
|
---|
524 | // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
|
---|
525 | if (lastJobDataUpdate != DateTime.MinValue && lastJobDataUpdate < hj.Job.LastJobDataUpdate) {
|
---|
526 | LogMessage(hj.Job.Id, "Downloading optimizer for job");
|
---|
527 | OptimizerJob optimizerJob = LoadOptimizerJob(hj.Job.Id);
|
---|
528 | if (optimizerJob == null) {
|
---|
529 | // something bad happened to this job. set to finished to allow the rest beeing downloaded
|
---|
530 | //hj.IsFinishedOptimizerDownloaded = true;
|
---|
531 | } else {
|
---|
532 | // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
|
---|
533 | if (hj.Job.State == JobState.Paused) {
|
---|
534 |
|
---|
535 | } else {
|
---|
536 | if (lightweightJob.ParentJobId.HasValue) {
|
---|
537 | HiveJob parentHiveJob = HiveJob.GetHiveJobByJobId(lightweightJob.ParentJobId.Value);
|
---|
538 | parentHiveJob.UpdateChildOptimizer(optimizerJob, hj.Job.Id);
|
---|
539 | } else {
|
---|
540 | //this.HiveJob.IsFinishedOptimizerDownloaded = true;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 | }
|
---|
547 | GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
|
---|
548 | if (AllJobsFinished()) {
|
---|
549 | this.ExecutionState = Core.ExecutionState.Stopped;
|
---|
550 | StopResultPolling();
|
---|
551 | OnStopped();
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | private bool AllJobsFinished() {
|
---|
556 | //return HiveJob.GetAllHiveJobs().All(hj => hj.IsFinishedOptimizerDownloaded);
|
---|
557 | return HiveJob.GetAllHiveJobs().All(j => j.Job.State == JobState.Finished
|
---|
558 | || j.Job.State == JobState.Aborted
|
---|
559 | || j.Job.State == JobState.Failed);
|
---|
560 | }
|
---|
561 |
|
---|
562 | private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
563 | OnExceptionOccured(e.Value);
|
---|
564 | }
|
---|
565 | #endregion
|
---|
566 |
|
---|
567 | #region Execution Time Timer
|
---|
568 | private void InitTimer() {
|
---|
569 | timer = new System.Timers.Timer(100);
|
---|
570 | timer.AutoReset = true;
|
---|
571 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
572 | }
|
---|
573 |
|
---|
574 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
575 | DateTime now = DateTime.Now;
|
---|
576 | ExecutionTime += now - lastUpdateTime;
|
---|
577 | lastUpdateTime = now;
|
---|
578 | }
|
---|
579 | #endregion
|
---|
580 |
|
---|
581 | #region Logging
|
---|
582 | private void LogMessage(string message) {
|
---|
583 | // HeuristicLab.Log is not Thread-Safe, so lock on every call
|
---|
584 | lock (locker) {
|
---|
585 | log.LogMessage(message);
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | private void LogMessage(Guid jobId, string message) {
|
---|
590 | //GetJobItemById(jobId).LogMessage(message);
|
---|
591 | LogMessage(message + " (jobId: " + jobId + ")");
|
---|
592 | }
|
---|
593 |
|
---|
594 | #endregion
|
---|
595 |
|
---|
596 | #region Job Loading
|
---|
597 | /// <summary>
|
---|
598 | /// Downloads the root job from hive and sets the experiment, rootJob and rootJobItem
|
---|
599 | /// </summary>
|
---|
600 | public void LoadHiveJob() {
|
---|
601 | progress = new Progress();
|
---|
602 | try {
|
---|
603 | IsProgressing = true;
|
---|
604 | int totalJobCount = 0;
|
---|
605 | IEnumerable<LightweightJob> allJobs;
|
---|
606 |
|
---|
607 | progress.Status = "Connecting to Server...";
|
---|
608 | // fetch all Job objects to create the full tree of tree of HiveJob objects
|
---|
609 | progress.Status = "Downloading list of jobs...";
|
---|
610 | allJobs = ServiceLocator.Instance.CallHiveService(s => s.GetLightweightChildJobs(rootJobId, true, true));
|
---|
611 | totalJobCount = allJobs.Count();
|
---|
612 |
|
---|
613 | HiveJobDownloader downloader = new HiveJobDownloader(allJobs.Select(x => x.Id));
|
---|
614 | downloader.StartAsync();
|
---|
615 |
|
---|
616 | while (!downloader.IsFinished) {
|
---|
617 | progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
|
---|
618 | progress.Status = string.Format("Downloading/deserializing jobs... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
|
---|
619 | Thread.Sleep(500);
|
---|
620 | }
|
---|
621 | IDictionary<Guid, HiveJob> allHiveJobs = downloader.Results;
|
---|
622 |
|
---|
623 | this.HiveJob = allHiveJobs[this.rootJobId];
|
---|
624 |
|
---|
625 |
|
---|
626 | //// download them first
|
---|
627 | //IDictionary<Guid, Job> allJobs = new Dictionary<Guid, Job>();
|
---|
628 | //IDictionary<Guid, JobData> allJobDatas = new Dictionary<Guid, JobData>();
|
---|
629 | //foreach (LightweightJob lightweightJob in allResults) {
|
---|
630 | // jobCount++;
|
---|
631 | // progress.Status = string.Format("Downloading {0} of {1} jobs...", jobCount, totalJobCount);
|
---|
632 | // allJobs.Add(lightweightJob.Id, service.GetJob(lightweightJob.Id));
|
---|
633 | // allJobDatas.Add(lightweightJob.Id, service.GetJobData(lightweightJob.Id));
|
---|
634 | // progress.ProgressValue = (double)jobCount / totalJobCount;
|
---|
635 | //}
|
---|
636 |
|
---|
637 | //jobCount = 1;
|
---|
638 | //progress.Status = string.Format("Deserializing {0} of {1} jobs... ({2} kb)", jobCount, totalJobCount, allJobDatas[this.rootJobId].Data.Count() / 1024);
|
---|
639 | //this.HiveJob = new HiveJob(allJobs[this.rootJobId], allJobDatas[this.rootJobId], false);
|
---|
640 | //allJobDatas.Remove(this.rootJobId); // reduce memory footprint
|
---|
641 | //allJobs.Remove(this.rootJobId);
|
---|
642 | //progress.ProgressValue = (double)jobCount / totalJobCount;
|
---|
643 |
|
---|
644 |
|
---|
645 | if (this.HiveJob.Job.DateFinished.HasValue && this.HiveJob.Job.DateCreated.HasValue) {
|
---|
646 | this.ExecutionTime = this.HiveJob.Job.DateFinished.Value - this.HiveJob.Job.DateCreated.Value;
|
---|
647 | this.lastUpdateTime = this.HiveJob.Job.DateFinished.Value;
|
---|
648 | this.ExecutionState = Core.ExecutionState.Stopped;
|
---|
649 | OnStopped();
|
---|
650 | } else {
|
---|
651 | this.ExecutionTime = this.HiveJob.Job.DateCreated.HasValue ? DateTime.Now - this.HiveJob.Job.DateCreated.Value : TimeSpan.Zero;
|
---|
652 | this.lastUpdateTime = DateTime.Now;
|
---|
653 | this.ExecutionState = Core.ExecutionState.Started;
|
---|
654 | OnStarted();
|
---|
655 | }
|
---|
656 |
|
---|
657 | // build child-job tree
|
---|
658 | //LoadChildResults(service, this.HiveJob, allResults, allJobs, allJobDatas, progress, totalJobCount, ref jobCount);
|
---|
659 | BuildHiveJobTree(this.HiveJob, allJobs, allHiveJobs);
|
---|
660 | StartResultPolling();
|
---|
661 | }
|
---|
662 | catch (Exception e) {
|
---|
663 | OnExceptionOccured(e);
|
---|
664 | }
|
---|
665 | finally {
|
---|
666 | IsProgressing = false;
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | private void BuildHiveJobTree(HiveJob parentHiveJob, IEnumerable<LightweightJob> allJobs, IDictionary<Guid, HiveJob> allHiveJobs) {
|
---|
671 | IEnumerable<LightweightJob> childJobs = from job in allJobs
|
---|
672 | where job.ParentJobId.HasValue && job.ParentJobId.Value == parentHiveJob.Job.Id
|
---|
673 | orderby job.DateCreated ascending
|
---|
674 | select job;
|
---|
675 | foreach (LightweightJob job in childJobs) {
|
---|
676 | HiveJob childHiveJob = allHiveJobs[job.Id];
|
---|
677 | parentHiveJob.AddChildHiveJob(childHiveJob);
|
---|
678 | BuildHiveJobTree(childHiveJob, allJobs, allHiveJobs);
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | private OptimizerJob LoadOptimizerJob(Guid jobId) {
|
---|
683 | JobData jobData = ServiceLocator.Instance.CallHiveService(s => s.GetJobData(jobId));
|
---|
684 | try {
|
---|
685 | return PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data);
|
---|
686 | }
|
---|
687 | catch {
|
---|
688 | return null;
|
---|
689 | }
|
---|
690 | }
|
---|
691 | #endregion
|
---|
692 |
|
---|
693 | #region Plugin Management
|
---|
694 | /// <summary>
|
---|
695 | /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
|
---|
696 | /// </summary>
|
---|
697 | /// <param name="service">An active service-proxy</param>
|
---|
698 | /// <param name="onlinePlugins">List of plugins which are available online</param>
|
---|
699 | /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this HiveExperiment</param>
|
---|
700 | /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
|
---|
701 | /// <param name="useLocalPlugins">If true, the plugins which are already online are ignored. All local plugins are uploaded, but only once.</param>
|
---|
702 | /// <returns></returns>
|
---|
703 | private static List<Guid> GetPluginDependencies(IHiveService service, IEnumerable<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins, IEnumerable<IPluginDescription> neededPlugins, bool useLocalPlugins) {
|
---|
704 | var pluginIds = new List<Guid>();
|
---|
705 | foreach (var neededPlugin in neededPlugins) {
|
---|
706 | Plugin foundPlugin = alreadyUploadedPlugins.SingleOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
|
---|
707 | if (foundPlugin == null) {
|
---|
708 | foundPlugin = onlinePlugins.SingleOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
|
---|
709 | if (useLocalPlugins || foundPlugin == null) {
|
---|
710 | Plugin p = CreatePlugin(neededPlugin, useLocalPlugins);
|
---|
711 | List<PluginData> pd = CreatePluginDatas(neededPlugin);
|
---|
712 | p.Id = service.AddPlugin(p, pd);
|
---|
713 | alreadyUploadedPlugins.Add(p);
|
---|
714 | pluginIds.Add(p.Id);
|
---|
715 | } else {
|
---|
716 | pluginIds.Add(foundPlugin.Id);
|
---|
717 | }
|
---|
718 | } else {
|
---|
719 | pluginIds.Add(foundPlugin.Id);
|
---|
720 | }
|
---|
721 | }
|
---|
722 | return pluginIds;
|
---|
723 | }
|
---|
724 |
|
---|
725 | private static Plugin CreatePlugin(IPluginDescription plugin, bool useLocalPlugins) {
|
---|
726 | return new Plugin() { Name = plugin.Name, Version = plugin.Version, IsLocal = useLocalPlugins };
|
---|
727 | }
|
---|
728 |
|
---|
729 | private static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
|
---|
730 | List<PluginData> pluginDatas = new List<PluginData>();
|
---|
731 |
|
---|
732 | foreach (IPluginFile pf in plugin.Files) {
|
---|
733 | PluginData pluginData = new PluginData();
|
---|
734 |
|
---|
735 | pluginData.Data = File.ReadAllBytes(pf.Name);
|
---|
736 | pluginData.FileName = Path.GetFileName(pf.Name);
|
---|
737 | pluginDatas.Add(pluginData);
|
---|
738 | }
|
---|
739 | return pluginDatas;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /// <summary>
|
---|
743 | /// Gets the Ids of all plugins needed for executing the job.
|
---|
744 | /// All loaded plugins are assumed to be necessary.
|
---|
745 | /// If a plugin with the same name and version is already online, it is used. Otherwise the local plugin is uploaded.
|
---|
746 | /// If useLocalPlugins is true, all local plugins are uploaded regardless of the existence of the same plugin online.
|
---|
747 | /// </summary>
|
---|
748 | //public static List<Guid> GetPluginsNeededIds(bool useLocalPlugins) {
|
---|
749 | // IEnumerable<IPluginDescription> localPlugins = ApplicationManager.Manager.Plugins;
|
---|
750 | // List<Guid> pluginsNeededIds = new List<Guid>();
|
---|
751 |
|
---|
752 | // using (var service = ServiceLocator.Instance.GetService()) {
|
---|
753 | // IEnumerable<Plugin> onlinePlugins = service.Obj.GetPlugins();
|
---|
754 |
|
---|
755 | // foreach (IPluginDescription localPlugin in localPlugins) {
|
---|
756 | // Plugin found = onlinePlugins.Where(onlinePlugin => onlinePlugin.Name == localPlugin.Name && onlinePlugin.Version == localPlugin.Version).SingleOrDefault();
|
---|
757 | // if (!useLocalPlugins && found != null) {
|
---|
758 | // // plugin is available online; reuse
|
---|
759 | // pluginsNeededIds.Add(found.Id);
|
---|
760 | // } else {
|
---|
761 | // // upload the plugin
|
---|
762 | // Plugin p = new Plugin() { Name = localPlugin.Name, Version = localPlugin.Version, IsLocal = useLocalPlugins };
|
---|
763 | // List<PluginData> pluginDatas = new List<PluginData>();
|
---|
764 |
|
---|
765 | // foreach (IPluginFile pf in localPlugin.Files) {
|
---|
766 | // PluginData pluginData = new PluginData();
|
---|
767 |
|
---|
768 | // pluginData.Data = File.ReadAllBytes(pf.Name);
|
---|
769 | // pluginDatas.Add(pluginData);
|
---|
770 | // }
|
---|
771 | // pluginsNeededIds.Add(service.Obj.AddPlugin(p, pluginDatas));
|
---|
772 | // }
|
---|
773 | // }
|
---|
774 | // }
|
---|
775 | // return pluginsNeededIds;
|
---|
776 | //}
|
---|
777 | #endregion
|
---|
778 |
|
---|
779 | #region IItemTree Members
|
---|
780 | public IObservableCollection<IItemTree> GetChildItems() {
|
---|
781 | return new ObservableCollection<IItemTree> { hiveJob };
|
---|
782 | }
|
---|
783 | IEnumerable<IItemTree> IItemTree.GetChildItems() {
|
---|
784 | return this.GetChildItems();
|
---|
785 | }
|
---|
786 | #endregion
|
---|
787 |
|
---|
788 | #region INotifyObservableCollectionItemsChanged<IItemTree> Members
|
---|
789 | public event CollectionItemsChangedEventHandler<IItemTree> CollectionReset;
|
---|
790 | public event CollectionItemsChangedEventHandler<IItemTree> ItemsAdded;
|
---|
791 | public event CollectionItemsChangedEventHandler<IItemTree> ItemsRemoved;
|
---|
792 | #endregion
|
---|
793 | }
|
---|
794 | }
|
---|