source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/ExperimentManagerClient.cs @ 6006

Last change on this file since 6006 was 6006, checked in by cneumuel, 9 years ago

#1233

  • changed relationship between Job and HiveExperiment. There is no more HiveExperiment.RootJobId, instead there is Job.HiveExperimentId.
  • One HiveExperiment can now have multiple Experiments.
  • TreeView supports multiple root nodes
  • HiveEngine creates a HiveExperiment for each set of jobs, so jobs cannot be without an parent experiment anymore (no more loose jobs)
  • updated ExperimentManager binaries
File size: 15.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Collections.Generic;
24using System.Configuration;
25using System.IO;
26using System.Linq;
27using System.Threading;
28using HeuristicLab.Clients.Hive.Jobs;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Clients.Hive {
34  // todo: rename from ExpMgrClient to ExperimentManagerClient
35  [Item("ExperimentManagerClient", "Hive experiment manager client.")]
36  public sealed class ExperimentManagerClient : IContent {
37    private static ExperimentManagerClient instance;
38    public static ExperimentManagerClient Instance {
39      get {
40        if (instance == null) instance = new ExperimentManagerClient();
41        return instance;
42      }
43    }
44
45    #region Properties
46    private ItemCollection<HiveExperiment> hiveExperiments;
47    public ItemCollection<HiveExperiment> HiveExperiments {
48      get { return hiveExperiments; }
49      set { hiveExperiments = value; }
50    }
51
52    private List<Plugin> onlinePlugins;
53    public List<Plugin> OnlinePlugins {
54      get { return onlinePlugins; }
55      set { onlinePlugins = value; }
56    }
57
58    private List<Plugin> alreadyUploadedPlugins;
59    public List<Plugin> AlreadyUploadedPlugins {
60      get { return alreadyUploadedPlugins; }
61      set { alreadyUploadedPlugins = value; }
62    }
63    #endregion
64
65    public ExperimentManagerClient() { }
66
67    #region Refresh
68    public void Refresh() {
69      OnRefreshing();
70
71      hiveExperiments = new HiveItemCollection<HiveExperiment>();
72
73      try {
74        hiveExperiments.AddRange(ServiceLocator.Instance.CallHiveService<List<HiveExperiment>>(s => s.GetHiveExperiments()).OrderBy(x => x.Name));
75      }
76      finally {
77        OnRefreshed();
78      }
79    }
80    public void RefreshAsync(Action<Exception> exceptionCallback) {
81      var call = new Func<Exception>(delegate() {
82        try {
83          Refresh();
84        }
85        catch (Exception ex) {
86          return ex;
87        }
88        return null;
89      });
90      call.BeginInvoke(delegate(IAsyncResult result) {
91        Exception ex = call.EndInvoke(result);
92        if (ex != null) exceptionCallback(ex);
93      }, null);
94    }
95    #endregion
96
97    #region Store
98    public static void Store(IHiveItem item) {
99      if (item.Id == Guid.Empty) {
100        if (item is HiveExperiment) {
101          ExperimentManagerClient.Instance.UploadExperiment((HiveExperiment)item);
102        }
103      } else {
104        if (item is HiveExperiment)
105          ServiceLocator.Instance.CallHiveService(s => s.UpdateHiveExperiment((HiveExperiment)item));
106      }
107    }
108    public static void StoreAsync(Action<Exception> exceptionCallback, IHiveItem item) {
109      var call = new Func<Exception>(delegate() {
110        try {
111          Store(item);
112        }
113        catch (Exception ex) {
114          return ex;
115        }
116        return null;
117      });
118      call.BeginInvoke(delegate(IAsyncResult result) {
119        Exception ex = call.EndInvoke(result);
120        if (ex != null) exceptionCallback(ex);
121      }, null);
122    }
123    #endregion
124
125    #region Delete
126    public static void Delete(IHiveItem item) {
127      if (item is HiveExperiment)
128        ServiceLocator.Instance.CallHiveService(s => s.DeleteHiveExperiment(item.Id));
129      item.Id = Guid.Empty;
130    }
131    #endregion
132
133    #region Events
134    public event EventHandler Refreshing;
135    private void OnRefreshing() {
136      EventHandler handler = Refreshing;
137      if (handler != null) handler(this, EventArgs.Empty);
138    }
139    public event EventHandler Refreshed;
140    private void OnRefreshed() {
141      var handler = Refreshed;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144    #endregion
145
146    public static void StartExperiment(Action<Exception> exceptionCallback, HiveExperiment hiveExperiment) {
147      ExperimentManagerClient.StoreAsync(
148        new Action<Exception>((Exception ex) => {
149          hiveExperiment.ExecutionState = ExecutionState.Prepared;
150          exceptionCallback(ex);
151        }), hiveExperiment);
152      hiveExperiment.ExecutionState = ExecutionState.Started;
153    }
154
155    public static void PauseExperiment(HiveExperiment hiveExperiment) {
156      ServiceLocator.Instance.CallHiveService(service => {
157        foreach (HiveJob job in hiveExperiment.GetAllHiveJobs()) {
158          if (job.Job.State != JobState.Finished && job.Job.State != JobState.Aborted && job.Job.State != JobState.Failed)
159            service.PauseJob(job.Job.Id);
160        }
161      });
162      hiveExperiment.ExecutionState = ExecutionState.Paused;
163    }
164
165    public static void StopExperiment(HiveExperiment hiveExperiment) {
166      ServiceLocator.Instance.CallHiveService(service => {
167        foreach (HiveJob job in hiveExperiment.GetAllHiveJobs()) {
168          if (job.Job.State != JobState.Finished && job.Job.State != JobState.Aborted && job.Job.State != JobState.Failed)
169            service.StopJob(job.Job.Id);
170        }
171      });
172      // execution state does not need to be set. it will be set to Stopped, when all jobs have been downloaded
173    }
174
175    #region Upload Experiment
176    private void UploadExperiment(HiveExperiment hiveExperiment) {
177      try {
178        hiveExperiment.Progress = new Progress("Connecting to server...");
179        hiveExperiment.IsProgressing = true;
180        ServiceLocator.Instance.CallHiveService(service => {
181          IEnumerable<string> resourceNames = ToResourceNameList(hiveExperiment.ResourceNames);
182          var resourceIds = new List<Guid>();
183          foreach (var resourceName in resourceNames) {
184            Guid resourceId = service.GetResourceId(resourceName);
185            if (resourceId == Guid.Empty) {
186              throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
187            }
188            resourceIds.Add(resourceId);
189          }
190
191          foreach (HiveJob hiveJob in hiveExperiment.HiveJobs) {
192            hiveJob.SetIndexInParentOptimizerList(null);
193          }
194
195          // upload HiveExperiment
196          hiveExperiment.Progress.Status = "Uploading HiveExperiment...";
197          hiveExperiment.Id = service.AddHiveExperiment(hiveExperiment);
198
199          int totalJobCount = hiveExperiment.GetAllHiveJobs().Count();
200          int jobCount = 0;
201
202          // upload plugins
203          hiveExperiment.Progress.Status = "Uploading plugins...";
204          this.OnlinePlugins = service.GetPlugins();
205          this.AlreadyUploadedPlugins = new List<Plugin>();
206          Plugin configFilePlugin = UploadConfigurationFile(service);
207          this.alreadyUploadedPlugins.Add(configFilePlugin);
208
209          // upload jobs
210          hiveExperiment.Progress.Status = "Uploading jobs...";
211
212          foreach (HiveJob hiveJob in hiveExperiment.HiveJobs) {
213            UploadJobWithChildren(hiveExperiment.Progress, service, hiveJob, null, resourceIds, ref jobCount, totalJobCount, configFilePlugin.Id, hiveExperiment.UseLocalPlugins, hiveExperiment.Id);
214          }
215
216          if (hiveExperiment.RefreshAutomatically) hiveExperiment.StartResultPolling();
217        });
218      }
219      finally {
220        hiveExperiment.IsProgressing = false;
221      }
222    }
223
224    /// <summary>
225    /// Uploads the local configuration file as plugin
226    /// </summary>
227    private static Plugin UploadConfigurationFile(IHiveService service) {
228      string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HeuristicLab 3.3.exe");
229      string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
230      string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
231
232      Plugin configPlugin = new Plugin() {
233        Name = "Configuration",
234        IsLocal = true,
235        Version = new Version()
236      };
237      PluginData configFile = new PluginData() {
238        FileName = configFileName,
239        Data = File.ReadAllBytes(configFilePath)
240      };
241      configPlugin.Id = service.AddPlugin(configPlugin, new List<PluginData> { configFile });
242      return configPlugin;
243    }
244
245    /// <summary>
246    /// Uploads the given job and all its child-jobs while setting the proper parentJobId values for the childs
247    /// </summary>
248    /// <param name="service"></param>
249    /// <param name="hiveJob"></param>
250    /// <param name="parentHiveJob">shall be null if its the root job</param>
251    /// <param name="groups"></param>
252    private void UploadJobWithChildren(IProgress progress, IHiveService service, HiveJob hiveJob, HiveJob parentHiveJob, IEnumerable<Guid> groups, ref int jobCount, int totalJobCount, Guid configPluginId, bool useLocalPlugins, Guid hiveExperimentId) {
253      jobCount++;
254      progress.Status = string.Format("Serializing job {0} of {1}", jobCount, totalJobCount);
255      JobData jobData;
256      List<IPluginDescription> plugins;
257
258      if (hiveJob.OptimizerJob.ComputeInParallel &&
259        (hiveJob.OptimizerJob.Optimizer is Optimization.Experiment || hiveJob.OptimizerJob.Optimizer is Optimization.BatchRun)) {
260        hiveJob.Job.IsParentJob = true;
261        hiveJob.Job.FinishWhenChildJobsFinished = true;
262        hiveJob.OptimizerJob.CollectChildJobs = false; // don't collect child-jobs on slaves
263        jobData = hiveJob.GetAsJobData(true, out plugins);
264      } else {
265        hiveJob.Job.IsParentJob = false;
266        hiveJob.Job.FinishWhenChildJobsFinished = false;
267        jobData = hiveJob.GetAsJobData(false, out plugins);
268      }
269
270      hiveJob.Job.PluginsNeededIds = PluginUtil.GetPluginDependencies(service, this.onlinePlugins, this.alreadyUploadedPlugins, plugins, useLocalPlugins);
271      hiveJob.Job.PluginsNeededIds.Add(configPluginId);
272      hiveJob.Job.HiveExperimentId = hiveExperimentId;
273
274      progress.Status = string.Format("Uploading job {0} of {1} ({2} kb)", jobCount, totalJobCount, jobData.Data.Count() / 1024);
275      progress.ProgressValue = (double)jobCount / totalJobCount;
276
277      if (parentHiveJob != null) {
278        hiveJob.Job.Id = service.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData);
279      } else {
280        hiveJob.Job.Id = service.AddJob(hiveJob.Job, jobData, groups.ToList());
281      }
282
283      foreach (HiveJob child in hiveJob.ChildHiveJobs) {
284        UploadJobWithChildren(progress, service, child, hiveJob, groups, ref jobCount, totalJobCount, configPluginId, useLocalPlugins, hiveExperimentId);
285      }
286    }
287    #endregion
288
289    #region Download Experiment
290    public static void LoadExperiment(HiveExperiment hiveExperiment) {
291      hiveExperiment.Progress = new Progress();
292      try {
293        hiveExperiment.IsProgressing = true;
294        int totalJobCount = 0;
295        IEnumerable<LightweightJob> allJobs;
296
297        hiveExperiment.Progress.Status = "Connecting to Server...";
298        // fetch all Job objects to create the full tree of tree of HiveJob objects
299        hiveExperiment.Progress.Status = "Downloading list of jobs...";
300        allJobs = ServiceLocator.Instance.CallHiveService(s => s.GetLightweightExperimentJobs(hiveExperiment.Id));
301        totalJobCount = allJobs.Count();
302
303        HiveJobDownloader downloader = new HiveJobDownloader(allJobs.Select(x => x.Id));
304        downloader.StartAsync();
305
306        while (!downloader.IsFinished) {
307          hiveExperiment.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
308          hiveExperiment.Progress.Status = string.Format("Downloading/deserializing jobs... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
309          Thread.Sleep(500);
310        }
311        IDictionary<Guid, HiveJob> allHiveJobs = downloader.Results;
312
313        hiveExperiment.HiveJobs = new ItemCollection<HiveJob>(allHiveJobs.Values.Where(x => !x.Job.ParentJobId.HasValue));
314        //hiveExperiment.HiveJobs = allHiveJobs[hiveExperiment.RootJobId];
315
316        if (hiveExperiment.IsFinished()) {
317          //hiveExperiment.ExecutionTime = hiveExperiment.HiveJobs.Max(.Job.DateFinished.Value - hiveExperiment.HiveJobs.Job.DateCreated.Value;
318          //hiveExperiment.lastUpdateTime = hiveExperiment.HiveJob.Job.DateFinished.Value;
319          hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;
320          //OnStopped(); // todo: stop timer
321        } else {
322          //hiveExperiment.ExecutionTime = hiveExperiment.HiveJobs.Job.DateCreated.HasValue ? DateTime.Now - hiveExperiment.HiveJobs.Job.DateCreated.Value : TimeSpan.Zero;
323          //hiveExperiment.lastUpdateTime = DateTime.Now;
324          hiveExperiment.ExecutionState = Core.ExecutionState.Started;
325          //OnStarted(); // todo: start timer
326        }
327        hiveExperiment.UpdateTotalExecutionTime();
328
329        // build child-job tree
330        foreach (HiveJob hiveJob in hiveExperiment.HiveJobs) {
331          BuildHiveJobTree(hiveJob, allJobs, allHiveJobs);
332        }
333        hiveExperiment.UpdateTotalExecutionTime();
334
335        if (hiveExperiment.ExecutionState != ExecutionState.Stopped) {
336          hiveExperiment.RefreshAutomatically = true;
337        }
338      }
339      finally {
340        hiveExperiment.IsProgressing = false;
341      }
342    }
343
344    private static void BuildHiveJobTree(HiveJob parentHiveJob, IEnumerable<LightweightJob> allJobs, IDictionary<Guid, HiveJob> allHiveJobs) {
345      IEnumerable<LightweightJob> childJobs = from job in allJobs
346                                              where job.ParentJobId.HasValue && job.ParentJobId.Value == parentHiveJob.Job.Id
347                                              orderby job.DateCreated ascending
348                                              select job;
349      foreach (LightweightJob job in childJobs) {
350        HiveJob childHiveJob = allHiveJobs[job.Id];
351        parentHiveJob.AddChildHiveJob(childHiveJob);
352        BuildHiveJobTree(childHiveJob, allJobs, allHiveJobs);
353      }
354    }
355    #endregion
356
357    /// <summary>
358    /// Converts a string which can contain Ids separated by ';' to a enumerable
359    /// </summary>
360    private static IEnumerable<string> ToResourceNameList(string resourceNames) {
361      if (!string.IsNullOrEmpty(resourceNames)) {
362        return resourceNames.Split(';');
363      } else {
364        return new List<string>();
365      }
366    }
367
368    public static OptimizerJob LoadOptimizerJob(Guid jobId) {
369      JobData jobData = ServiceLocator.Instance.CallHiveService(s => s.GetJobData(jobId));
370      try {
371        return PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data);
372      }
373      catch {
374        return null;
375      }
376    }
377  }
378}
Note: See TracBrowser for help on using the repository browser.