source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/HiveClient.cs @ 6463

Last change on this file since 6463 was 6463, checked in by cneumuel, 8 years ago

#1233

  • created user interface for experiment sharing
  • created UserManager which provides access to the users
  • inserted a lot of security and authorization checks serverside
  • minor fixes in experiment manager
File size: 20.8 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.Security.Cryptography;
28using System.Threading;
29using System.Threading.Tasks;
30using HeuristicLab.Common;
31using HeuristicLab.Core;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Clients.Hive {
35  [Item("HiveClient", "Hive client.")]
36  public sealed class HiveClient : IContent {
37    private static HiveClient instance;
38    public static HiveClient Instance {
39      get {
40        if (instance == null) instance = new HiveClient();
41        return instance;
42      }
43    }
44
45    #region Properties
46    private ItemCollection<RefreshableHiveExperiment> hiveExperiments;
47    public ItemCollection<RefreshableHiveExperiment> HiveExperiments {
48      get { return hiveExperiments; }
49      set {
50        if (value != hiveExperiments) {
51          hiveExperiments = value;
52          OnHiveExperimentsChanged();
53        }
54      }
55    }
56
57    private List<Plugin> onlinePlugins;
58    public List<Plugin> OnlinePlugins {
59      get { return onlinePlugins; }
60      set { onlinePlugins = value; }
61    }
62
63    private List<Plugin> alreadyUploadedPlugins;
64    public List<Plugin> AlreadyUploadedPlugins {
65      get { return alreadyUploadedPlugins; }
66      set { alreadyUploadedPlugins = value; }
67    }
68    #endregion
69
70    public HiveClient() { }
71
72    #region Refresh
73    public void Refresh() {
74      OnRefreshing();
75
76      try {
77        hiveExperiments = new HiveItemCollection<RefreshableHiveExperiment>();
78        var he = ServiceLocator.Instance.CallHiveService<IEnumerable<HiveExperiment>>(s => s.GetHiveExperiments());
79        hiveExperiments.AddRange(he.Select(x => new RefreshableHiveExperiment(x)).OrderBy(x => x.HiveExperiment.Name));
80      }
81      catch {
82        hiveExperiments = null;
83        throw;
84      }
85      finally {
86        OnRefreshed();
87      }
88    }
89    public void RefreshAsync(Action<Exception> exceptionCallback) {
90      var call = new Func<Exception>(delegate() {
91        try {
92          Refresh();
93        }
94        catch (Exception ex) {
95          return ex;
96        }
97        return null;
98      });
99      call.BeginInvoke(delegate(IAsyncResult result) {
100        Exception ex = call.EndInvoke(result);
101        if (ex != null) exceptionCallback(ex);
102      }, null);
103    }
104    #endregion
105
106    #region Store
107    public static void Store(IHiveItem item, CancellationToken cancellationToken) {
108      if (item.Id == Guid.Empty) {
109        if (item is RefreshableHiveExperiment) {
110          HiveClient.Instance.UploadExperiment((RefreshableHiveExperiment)item, cancellationToken);
111        }
112        if (item is HiveExperimentPermission) {
113          var hep = (HiveExperimentPermission)item;
114          hep.GrantedUserId = ServiceLocator.Instance.CallHiveService((s) => s.GetUserIdByUsername(hep.GrantedUserName));
115          if (hep.GrantedUserId == Guid.Empty) {
116            throw new ArgumentException(string.Format("The user {0} was not found.", hep.GrantedUserName));
117          }
118          ServiceLocator.Instance.CallHiveService((s) => s.GrantPermission(hep.HiveExperimentId, hep.GrantedUserId, hep.Permission));
119        }
120      } else {
121        if (item is HiveExperiment)
122          ServiceLocator.Instance.CallHiveService(s => s.UpdateHiveExperiment((HiveExperiment)item));
123      }
124    }
125    public static void StoreAsync(Action<Exception> exceptionCallback, IHiveItem item, CancellationToken cancellationToken) {
126      var call = new Func<Exception>(delegate() {
127        try {
128          Store(item, cancellationToken);
129        }
130        catch (Exception ex) {
131          return ex;
132        }
133        return null;
134      });
135      call.BeginInvoke(delegate(IAsyncResult result) {
136        Exception ex = call.EndInvoke(result);
137        if (ex != null) exceptionCallback(ex);
138      }, null);
139    }
140    #endregion
141
142    #region Delete
143    public static void Delete(IHiveItem item) {
144      if (item is HiveExperiment)
145        ServiceLocator.Instance.CallHiveService(s => s.DeleteHiveExperiment(item.Id));
146      if (item is RefreshableHiveExperiment)
147        ServiceLocator.Instance.CallHiveService(s => s.DeleteHiveExperiment(item.Id));
148      if (item is HiveExperimentPermission) {
149        var hep = (HiveExperimentPermission)item;
150        ServiceLocator.Instance.CallHiveService(s => s.RevokePermission(hep.HiveExperimentId, hep.GrantedUserId));
151      }
152      item.Id = Guid.Empty;
153    }
154    #endregion
155
156    #region Events
157    public event EventHandler Refreshing;
158    private void OnRefreshing() {
159      EventHandler handler = Refreshing;
160      if (handler != null) handler(this, EventArgs.Empty);
161    }
162    public event EventHandler Refreshed;
163    private void OnRefreshed() {
164      var handler = Refreshed;
165      if (handler != null) handler(this, EventArgs.Empty);
166    }
167    public event EventHandler HiveExperimentsChanged;
168    private void OnHiveExperimentsChanged() {
169      var handler = HiveExperimentsChanged;
170      if (handler != null) handler(this, EventArgs.Empty);
171    }
172    #endregion
173
174    public static void StartExperiment(Action<Exception> exceptionCallback, RefreshableHiveExperiment refreshableHiveExperiment, CancellationToken cancellationToken) {
175      HiveClient.StoreAsync(
176        new Action<Exception>((Exception ex) => {
177          refreshableHiveExperiment.HiveExperiment.ExecutionState = ExecutionState.Prepared;
178          exceptionCallback(ex);
179        }), refreshableHiveExperiment, cancellationToken);
180      refreshableHiveExperiment.HiveExperiment.ExecutionState = ExecutionState.Started;
181    }
182
183    public static void PauseExperiment(HiveExperiment hiveExperiment) {
184      ServiceLocator.Instance.CallHiveService(service => {
185        foreach (HiveJob job in hiveExperiment.GetAllHiveJobs()) {
186          if (job.Job.State != JobState.Finished && job.Job.State != JobState.Aborted && job.Job.State != JobState.Failed)
187            service.PauseJob(job.Job.Id);
188        }
189      });
190      hiveExperiment.ExecutionState = ExecutionState.Paused;
191    }
192
193    public static void StopExperiment(HiveExperiment hiveExperiment) {
194      ServiceLocator.Instance.CallHiveService(service => {
195        foreach (HiveJob job in hiveExperiment.GetAllHiveJobs()) {
196          if (job.Job.State != JobState.Finished && job.Job.State != JobState.Aborted && job.Job.State != JobState.Failed)
197            service.StopJob(job.Job.Id);
198        }
199      });
200      // execution state does not need to be set. it will be set to Stopped, when all jobs have been downloaded
201    }
202
203    #region Upload Experiment
204    private Semaphore jobUploadSemaphore = new Semaphore(4, 4); // todo: take magic number into config
205    private static object jobCountLocker = new object();
206    private static object pluginLocker = new object();
207    private void UploadExperiment(RefreshableHiveExperiment refreshableHiveExperiment, CancellationToken cancellationToken) {
208      try {
209        refreshableHiveExperiment.HiveExperiment.Progress = new Progress("Connecting to server...");
210        refreshableHiveExperiment.HiveExperiment.IsProgressing = true;
211
212        IEnumerable<string> resourceNames = ToResourceNameList(refreshableHiveExperiment.HiveExperiment.ResourceNames);
213        var resourceIds = new List<Guid>();
214        foreach (var resourceName in resourceNames) {
215          Guid resourceId = ServiceLocator.Instance.CallHiveService((s) => s.GetResourceId(resourceName));
216          if (resourceId == Guid.Empty) {
217            throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
218          }
219          resourceIds.Add(resourceId);
220        }
221
222        foreach (OptimizerHiveJob hiveJob in refreshableHiveExperiment.HiveExperiment.HiveJobs.OfType<OptimizerHiveJob>()) {
223          hiveJob.SetIndexInParentOptimizerList(null);
224        }
225
226        // upload HiveExperiment
227        refreshableHiveExperiment.HiveExperiment.Progress.Status = "Uploading HiveExperiment...";
228        refreshableHiveExperiment.HiveExperiment.Id = ServiceLocator.Instance.CallHiveService((s) => s.AddHiveExperiment(refreshableHiveExperiment.HiveExperiment));
229        cancellationToken.ThrowIfCancellationRequested();
230
231        int totalJobCount = refreshableHiveExperiment.HiveExperiment.GetAllHiveJobs().Count();
232        int[] jobCount = new int[1]; // use a reference type (int-array) instead of value type (int) in order to pass the value via a delegate to task-parallel-library
233        cancellationToken.ThrowIfCancellationRequested();
234
235        // upload plugins
236        refreshableHiveExperiment.HiveExperiment.Progress.Status = "Uploading plugins...";
237        this.OnlinePlugins = ServiceLocator.Instance.CallHiveService((s) => s.GetPlugins());
238        this.AlreadyUploadedPlugins = new List<Plugin>();
239        Plugin configFilePlugin = ServiceLocator.Instance.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins));
240        this.alreadyUploadedPlugins.Add(configFilePlugin);
241        cancellationToken.ThrowIfCancellationRequested();
242
243        if (refreshableHiveExperiment.RefreshAutomatically) refreshableHiveExperiment.StartResultPolling();
244
245        // upload jobs
246        refreshableHiveExperiment.HiveExperiment.Progress.Status = "Uploading jobs...";
247
248        var tasks = new List<Task>();
249        foreach (HiveJob hiveJob in refreshableHiveExperiment.HiveExperiment.HiveJobs) {
250          tasks.Add(Task.Factory.StartNew((hj) => {
251            UploadJobWithChildren(refreshableHiveExperiment.HiveExperiment.Progress, (HiveJob)hj, null, resourceIds, jobCount, totalJobCount, configFilePlugin.Id, refreshableHiveExperiment.HiveExperiment.Id, refreshableHiveExperiment.Log, refreshableHiveExperiment.HiveExperiment.IsPrivileged, cancellationToken);
252          }, hiveJob)
253          .ContinueWith((x) => refreshableHiveExperiment.Log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
254        }
255        try {
256          Task.WaitAll(tasks.ToArray());
257        }
258        catch (AggregateException ae) {
259          if (!ae.InnerExceptions.All(e => e is TaskCanceledException)) throw ae; // for some reason the WaitAll throws a AggregateException containg a TaskCanceledException. i don't know where it comes from, however the tasks all finish properly, so for now just ignore it
260        }
261      }
262      finally {
263        refreshableHiveExperiment.HiveExperiment.IsProgressing = false;
264      }
265    }
266
267    /// <summary>
268    /// Uploads the local configuration file as plugin
269    /// </summary>
270    private static Plugin UploadConfigurationFile(IHiveService service, List<Plugin> onlinePlugins) {
271      string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HeuristicLab 3.3.exe");
272      string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
273      string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
274      byte[] hash;
275
276      byte[] data = File.ReadAllBytes(configFilePath);
277      using (SHA1 sha1 = SHA1.Create()) {
278        hash = sha1.ComputeHash(data);
279      }
280
281      Plugin configPlugin = new Plugin() { Name = "Configuration", Version = new Version(), Hash = hash };
282      PluginData configFile = new PluginData() { FileName = configFileName, Data = data };
283
284      IEnumerable<Plugin> onlineConfig = onlinePlugins.Where(p => p.Hash.SequenceEqual(hash));
285
286      if (onlineConfig.Count() > 0) {
287        return onlineConfig.First();
288      } else {
289        configPlugin.Id = service.AddPlugin(configPlugin, new List<PluginData> { configFile });
290        return configPlugin;
291      }
292    }
293
294    /// <summary>
295    /// Uploads the given job and all its child-jobs while setting the proper parentJobId values for the childs
296    /// </summary>
297    /// <param name="parentHiveJob">shall be null if its the root job</param>
298    private void UploadJobWithChildren(IProgress progress, HiveJob hiveJob, HiveJob parentHiveJob, IEnumerable<Guid> groups, int[] jobCount, int totalJobCount, Guid configPluginId, Guid hiveExperimentId, ILog log, bool isPrivileged, CancellationToken cancellationToken) {
299      jobUploadSemaphore.WaitOne();
300      bool semaphoreReleased = false;
301      try {
302        cancellationToken.ThrowIfCancellationRequested();
303        lock (jobCountLocker) {
304          jobCount[0]++;
305        }
306        JobData jobData;
307        List<IPluginDescription> plugins;
308
309        if (hiveJob.ItemJob.ComputeInParallel && (hiveJob.ItemJob.Item is Optimization.Experiment || hiveJob.ItemJob.Item is Optimization.BatchRun)) {
310          hiveJob.Job.IsParentJob = true;
311          hiveJob.Job.FinishWhenChildJobsFinished = true;
312          jobData = hiveJob.GetAsJobData(true, out plugins);
313        } else {
314          hiveJob.Job.IsParentJob = false;
315          hiveJob.Job.FinishWhenChildJobsFinished = false;
316          jobData = hiveJob.GetAsJobData(false, out plugins);
317        }
318        cancellationToken.ThrowIfCancellationRequested();
319
320        TryAndRepeat(() => {
321          if (!cancellationToken.IsCancellationRequested) {
322            lock (pluginLocker) {
323              ServiceLocator.Instance.CallHiveService((s) => hiveJob.Job.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins));
324            }
325          }
326        }, -1, "Failed to upload plugins");
327        cancellationToken.ThrowIfCancellationRequested();
328        hiveJob.Job.PluginsNeededIds.Add(configPluginId);
329        hiveJob.Job.HiveExperimentId = hiveExperimentId;
330        hiveJob.Job.IsPrivileged = isPrivileged;
331
332        log.LogMessage(string.Format("Uploading job ({0} kb, {1} objects)", jobData.Data.Count() / 1024, hiveJob.ItemJob.GetObjectGraphObjects().Count()));
333        TryAndRepeat(() => {
334          if (!cancellationToken.IsCancellationRequested) {
335            if (parentHiveJob != null) {
336              hiveJob.Job.Id = ServiceLocator.Instance.CallHiveService((s) => s.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData));
337            } else {
338              hiveJob.Job.Id = ServiceLocator.Instance.CallHiveService((s) => s.AddJob(hiveJob.Job, jobData, groups.ToList()));
339            }
340          }
341        }, 50, "Failed to add job", log);
342        cancellationToken.ThrowIfCancellationRequested();
343
344        lock (jobCountLocker) {
345          progress.ProgressValue = (double)jobCount[0] / totalJobCount;
346          progress.Status = string.Format("Uploaded job ({0} of {1})", jobCount[0], totalJobCount);
347        }
348
349        var tasks = new List<Task>();
350        foreach (HiveJob child in hiveJob.ChildHiveJobs) {
351          tasks.Add(Task.Factory.StartNew((tuple) => {
352            var arguments = (Tuple<HiveJob, HiveJob>)tuple;
353            UploadJobWithChildren(progress, arguments.Item1, arguments.Item2, groups, jobCount, totalJobCount, configPluginId, hiveExperimentId, log, isPrivileged, cancellationToken);
354          }, new Tuple<HiveJob, HiveJob>(child, hiveJob))
355          .ContinueWith((x) => log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
356        }
357        jobUploadSemaphore.Release(); semaphoreReleased = true; // the semaphore has to be release before waitall!
358        try {
359          Task.WaitAll(tasks.ToArray());
360        }
361        catch (AggregateException ae) {
362          if (!ae.InnerExceptions.All(e => e is TaskCanceledException)) throw ae; // for some reason the WaitAll throws a AggregateException containg a TaskCanceledException. i don't know where it comes from, however the tasks all finish properly, so for now just ignore it
363        }
364      }
365      finally {
366        if(!semaphoreReleased) jobUploadSemaphore.Release();
367      }
368    }
369    #endregion
370
371    #region Download Experiment
372    public static void LoadExperiment(HiveExperiment hiveExperiment) {
373      hiveExperiment.Progress = new Progress();
374      try {
375        hiveExperiment.IsProgressing = true;
376        int totalJobCount = 0;
377        IEnumerable<LightweightJob> allJobs;
378
379        hiveExperiment.Progress.Status = "Connecting to Server...";
380        // fetch all Job objects to create the full tree of tree of HiveJob objects
381        hiveExperiment.Progress.Status = "Downloading list of jobs...";
382        allJobs = ServiceLocator.Instance.CallHiveService(s => s.GetLightweightExperimentJobs(hiveExperiment.Id));
383        totalJobCount = allJobs.Count();
384
385        HiveJobDownloader downloader = new HiveJobDownloader(allJobs.Select(x => x.Id));
386        downloader.StartAsync();
387
388        while (!downloader.IsFinished) {
389          hiveExperiment.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
390          hiveExperiment.Progress.Status = string.Format("Downloading/deserializing jobs... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
391          Thread.Sleep(500);
392
393          if (downloader.IsFaulted) {
394            throw downloader.Exception;
395          }
396        }
397        IDictionary<Guid, HiveJob> allHiveJobs = downloader.Results;
398
399        hiveExperiment.HiveJobs = new ItemCollection<HiveJob>(allHiveJobs.Values.Where(x => !x.Job.ParentJobId.HasValue));
400
401        if (hiveExperiment.IsFinished()) {
402          hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;
403        } else {
404          hiveExperiment.ExecutionState = Core.ExecutionState.Started;
405        }
406
407        // build child-job tree
408        foreach (HiveJob hiveJob in hiveExperiment.HiveJobs) {
409          BuildHiveJobTree(hiveJob, allJobs, allHiveJobs);
410        }
411
412        hiveExperiment.OnLoaded();
413      }
414      finally {
415        hiveExperiment.IsProgressing = false;
416      }
417    }
418
419    private static void BuildHiveJobTree(HiveJob parentHiveJob, IEnumerable<LightweightJob> allJobs, IDictionary<Guid, HiveJob> allHiveJobs) {
420      IEnumerable<LightweightJob> childJobs = from job in allJobs
421                                              where job.ParentJobId.HasValue && job.ParentJobId.Value == parentHiveJob.Job.Id
422                                              orderby job.DateCreated ascending
423                                              select job;
424      foreach (LightweightJob job in childJobs) {
425        HiveJob childHiveJob = allHiveJobs[job.Id];
426        parentHiveJob.AddChildHiveJob(childHiveJob);
427        BuildHiveJobTree(childHiveJob, allJobs, allHiveJobs);
428      }
429    }
430    #endregion
431
432    /// <summary>
433    /// Converts a string which can contain Ids separated by ';' to a enumerable
434    /// </summary>
435    private static IEnumerable<string> ToResourceNameList(string resourceNames) {
436      if (!string.IsNullOrEmpty(resourceNames)) {
437        return resourceNames.Split(';');
438      } else {
439        return new List<string>();
440      }
441    }
442
443    public static ItemJob LoadItemJob(Guid jobId) {
444      JobData jobData = ServiceLocator.Instance.CallHiveService(s => s.GetJobData(jobId));
445      try {
446        return PersistenceUtil.Deserialize<ItemJob>(jobData.Data);
447      }
448      catch {
449        return null;
450      }
451    }
452
453    /// <summary>
454    /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
455    /// If repetitions is -1, it is repeated infinitely.
456    /// </summary>
457    public static void TryAndRepeat(Action action, int repetitions, string errorMessage, ILog log = null) {
458      while (true) {
459        try { action(); return; }
460        catch (Exception e) {
461          if (repetitions == 0) throw new HiveException(errorMessage, e);
462          if (log != null) log.LogMessage(string.Format("{0}: {1} - will try again!", errorMessage, e.ToString()));
463          repetitions--;
464        }
465      }
466    }
467
468    public static HiveItemCollection<HiveExperimentPermission> GetHiveExperimentPermissions(Guid hiveExperimentId) {
469      return ServiceLocator.Instance.CallHiveService((service) => {
470        IEnumerable<HiveExperimentPermission> heps = service.GetHiveExperimentPermissions(hiveExperimentId);
471        foreach (var hep in heps) {
472          hep.GrantedUserName = service.GetUsernameByUserId(hep.GrantedUserId);
473        }
474        return new HiveItemCollection<HiveExperimentPermission>(heps);
475      });
476    }
477  }
478}
Note: See TracBrowser for help on using the repository browser.