source: trunk/sources/HeuristicLab.Clients.Hive/3.3/HiveClient.cs @ 7259

Last change on this file since 7259 was 7259, checked in by swagner, 9 years ago

Updated year of copyrights to 2012 (#1716)

File size: 22.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
33using TS = System.Threading.Tasks;
34
35namespace HeuristicLab.Clients.Hive {
36  [Item("HiveClient", "Hive client.")]
37  public sealed class HiveClient : IContent {
38    private static HiveClient instance;
39    public static HiveClient Instance {
40      get {
41        if (instance == null) instance = new HiveClient();
42        return instance;
43      }
44    }
45
46    #region Properties
47    private ItemCollection<RefreshableJob> jobs;
48    public ItemCollection<RefreshableJob> Jobs {
49      get { return jobs; }
50      set {
51        if (value != jobs) {
52          jobs = value;
53          OnHiveExperimentsChanged();
54        }
55      }
56    }
57
58    private List<Plugin> onlinePlugins;
59    public List<Plugin> OnlinePlugins {
60      get { return onlinePlugins; }
61      set { onlinePlugins = value; }
62    }
63
64    private List<Plugin> alreadyUploadedPlugins;
65    public List<Plugin> AlreadyUploadedPlugins {
66      get { return alreadyUploadedPlugins; }
67      set { alreadyUploadedPlugins = value; }
68    }
69
70    private bool isAllowedPrivileged;
71    public bool IsAllowedPrivileged {
72      get { return isAllowedPrivileged; }
73      set { isAllowedPrivileged = value; }
74    }
75    #endregion
76
77    private HiveClient() { }
78
79    #region Refresh
80    public void Refresh() {
81      OnRefreshing();
82
83      try {
84        this.IsAllowedPrivileged = HiveServiceLocator.Instance.CallHiveService((s) => s.IsAllowedPrivileged());
85
86        var oldJobs = jobs ?? new ItemCollection<RefreshableJob>();
87        jobs = new HiveItemCollection<RefreshableJob>();
88        var jobsLoaded = HiveServiceLocator.Instance.CallHiveService<IEnumerable<Job>>(s => s.GetJobs());
89
90        foreach (var j in jobsLoaded) {
91          var job = oldJobs.SingleOrDefault(x => x.Id == j.Id);
92          if (job == null) {
93            // new
94            jobs.Add(new RefreshableJob(j) { IsAllowedPrivileged = this.isAllowedPrivileged });
95          } else {
96            // update
97            job.Job = j;
98            job.IsAllowedPrivileged = this.isAllowedPrivileged;
99            jobs.Add(job);
100          }
101        }
102        // remove those which were not in the list of loaded hiveexperiments
103        foreach (var job in oldJobs) {
104          if (job.Id == Guid.Empty) {
105            // experiment not uploaded... keep
106            jobs.Add(job);
107          } else {
108            job.RefreshAutomatically = false; // stop results polling
109          }
110        }
111      }
112      catch {
113        jobs = null;
114        throw;
115      } finally {
116        OnRefreshed();
117      }
118    }
119    public void RefreshAsync(Action<Exception> exceptionCallback) {
120      var call = new Func<Exception>(delegate() {
121        try {
122          Refresh();
123        }
124        catch (Exception ex) {
125          return ex;
126        }
127        return null;
128      });
129      call.BeginInvoke(delegate(IAsyncResult result) {
130        Exception ex = call.EndInvoke(result);
131        if (ex != null) exceptionCallback(ex);
132      }, null);
133    }
134    #endregion
135
136    #region Store
137    public static void Store(IHiveItem item, CancellationToken cancellationToken) {
138      if (item.Id == Guid.Empty) {
139        if (item is RefreshableJob) {
140          HiveClient.Instance.UploadJob((RefreshableJob)item, cancellationToken);
141        }
142        if (item is JobPermission) {
143          var hep = (JobPermission)item;
144          hep.GrantedUserId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetUserIdByUsername(hep.GrantedUserName));
145          if (hep.GrantedUserId == Guid.Empty) {
146            throw new ArgumentException(string.Format("The user {0} was not found.", hep.GrantedUserName));
147          }
148          HiveServiceLocator.Instance.CallHiveService((s) => s.GrantPermission(hep.JobId, hep.GrantedUserId, hep.Permission));
149        }
150      } else {
151        if (item is Job)
152          HiveServiceLocator.Instance.CallHiveService(s => s.UpdateJob((Job)item));
153      }
154    }
155    public static void StoreAsync(Action<Exception> exceptionCallback, IHiveItem item, CancellationToken cancellationToken) {
156      var call = new Func<Exception>(delegate() {
157        try {
158          Store(item, cancellationToken);
159        }
160        catch (Exception ex) {
161          return ex;
162        }
163        return null;
164      });
165      call.BeginInvoke(delegate(IAsyncResult result) {
166        Exception ex = call.EndInvoke(result);
167        if (ex != null) exceptionCallback(ex);
168      }, null);
169    }
170    #endregion
171
172    #region Delete
173    public static void Delete(IHiveItem item) {
174      if (item.Id == Guid.Empty && item.GetType() != typeof(JobPermission))
175        return;
176
177      if (item is Job)
178        HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(item.Id));
179      if (item is RefreshableJob) {
180        RefreshableJob job = (RefreshableJob)item;
181        if (job.RefreshAutomatically) {
182          job.StopResultPolling();
183        }
184        HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(item.Id));
185      }
186      if (item is JobPermission) {
187        var hep = (JobPermission)item;
188        HiveServiceLocator.Instance.CallHiveService(s => s.RevokePermission(hep.JobId, hep.GrantedUserId));
189      }
190      item.Id = Guid.Empty;
191    }
192    #endregion
193
194    #region Events
195    public event EventHandler Refreshing;
196    private void OnRefreshing() {
197      EventHandler handler = Refreshing;
198      if (handler != null) handler(this, EventArgs.Empty);
199    }
200    public event EventHandler Refreshed;
201    private void OnRefreshed() {
202      var handler = Refreshed;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205    public event EventHandler HiveExperimentsChanged;
206    private void OnHiveExperimentsChanged() {
207      var handler = HiveExperimentsChanged;
208      if (handler != null) handler(this, EventArgs.Empty);
209    }
210    #endregion
211
212    public static void StartJob(Action<Exception> exceptionCallback, RefreshableJob refreshableJob, CancellationToken cancellationToken) {
213      HiveClient.StoreAsync(
214        new Action<Exception>((Exception ex) => {
215          refreshableJob.ExecutionState = ExecutionState.Prepared;
216          exceptionCallback(ex);
217        }), refreshableJob, cancellationToken);
218      refreshableJob.ExecutionState = ExecutionState.Started;
219    }
220
221    public static void PauseJob(RefreshableJob refreshableJob) {
222      HiveServiceLocator.Instance.CallHiveService(service => {
223        foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
224          if (task.Task.State != TaskState.Finished && task.Task.State != TaskState.Aborted && task.Task.State != TaskState.Failed)
225            service.PauseTask(task.Task.Id);
226        }
227      });
228      refreshableJob.ExecutionState = ExecutionState.Paused;
229    }
230
231    public static void StopJob(RefreshableJob refreshableJob) {
232      HiveServiceLocator.Instance.CallHiveService(service => {
233        foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
234          if (task.Task.State != TaskState.Finished && task.Task.State != TaskState.Aborted && task.Task.State != TaskState.Failed)
235            service.StopTask(task.Task.Id);
236        }
237      });
238      refreshableJob.ExecutionState = ExecutionState.Stopped;
239    }
240
241    public static void ResumeJob(RefreshableJob refreshableJob) {
242      HiveServiceLocator.Instance.CallHiveService(service => {
243        foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
244          if (task.Task.State == TaskState.Paused) {
245            service.RestartTask(task.Task.Id);
246          }
247        }
248      });
249      refreshableJob.ExecutionState = ExecutionState.Started;
250    }
251
252    #region Upload Job
253    private Semaphore taskUploadSemaphore = new Semaphore(Settings.Default.MaxParallelUploads, Settings.Default.MaxParallelUploads);
254    private static object jobCountLocker = new object();
255    private static object pluginLocker = new object();
256    private void UploadJob(RefreshableJob refreshableJob, CancellationToken cancellationToken) {
257      try {
258        refreshableJob.Progress = new Progress("Connecting to server...");
259        refreshableJob.IsProgressing = true;
260
261        IEnumerable<string> resourceNames = ToResourceNameList(refreshableJob.Job.ResourceNames);
262        var resourceIds = new List<Guid>();
263        foreach (var resourceName in resourceNames) {
264          Guid resourceId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetResourceId(resourceName));
265          if (resourceId == Guid.Empty) {
266            throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
267          }
268          resourceIds.Add(resourceId);
269        }
270
271        foreach (OptimizerHiveTask hiveJob in refreshableJob.HiveTasks.OfType<OptimizerHiveTask>()) {
272          hiveJob.SetIndexInParentOptimizerList(null);
273        }
274
275        // upload Job
276        refreshableJob.Progress.Status = "Uploading Job...";
277        refreshableJob.Job.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddJob(refreshableJob.Job));
278        bool isPrivileged = refreshableJob.Job.IsPrivileged;
279        refreshableJob.Job = HiveServiceLocator.Instance.CallHiveService((s) => s.GetJob(refreshableJob.Job.Id)); // update owner and permissions
280        refreshableJob.Job.IsPrivileged = isPrivileged;
281        cancellationToken.ThrowIfCancellationRequested();
282
283        int totalJobCount = refreshableJob.GetAllHiveTasks().Count();
284        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
285        cancellationToken.ThrowIfCancellationRequested();
286
287        // upload plugins
288        refreshableJob.Progress.Status = "Uploading plugins...";
289        this.OnlinePlugins = HiveServiceLocator.Instance.CallHiveService((s) => s.GetPlugins());
290        this.AlreadyUploadedPlugins = new List<Plugin>();
291        Plugin configFilePlugin = HiveServiceLocator.Instance.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins));
292        this.alreadyUploadedPlugins.Add(configFilePlugin);
293        cancellationToken.ThrowIfCancellationRequested();
294
295        if (refreshableJob.RefreshAutomatically) refreshableJob.StartResultPolling();
296
297        // upload tasks
298        refreshableJob.Progress.Status = "Uploading tasks...";
299
300        var tasks = new List<TS.Task>();
301        foreach (HiveTask hiveTask in refreshableJob.HiveTasks) {
302          tasks.Add(TS.Task.Factory.StartNew((hj) => {
303            UploadTaskWithChildren(refreshableJob.Progress, (HiveTask)hj, null, resourceIds, jobCount, totalJobCount, configFilePlugin.Id, refreshableJob.Job.Id, refreshableJob.Log, refreshableJob.Job.IsPrivileged, cancellationToken);
304          }, hiveTask)
305          .ContinueWith((x) => refreshableJob.Log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
306        }
307        try {
308          TS.Task.WaitAll(tasks.ToArray());
309        }
310        catch (AggregateException ae) {
311          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
312        }
313        refreshableJob.Job.Modified = false;
314      } finally {
315        refreshableJob.IsProgressing = false;
316      }
317    }
318
319    /// <summary>
320    /// Uploads the local configuration file as plugin
321    /// </summary>
322    private static Plugin UploadConfigurationFile(IHiveService service, List<Plugin> onlinePlugins) {
323      string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.HLBinaryName);
324      string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
325      string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
326      byte[] hash;
327
328      byte[] data = File.ReadAllBytes(configFilePath);
329      using (SHA1 sha1 = SHA1.Create()) {
330        hash = sha1.ComputeHash(data);
331      }
332
333      Plugin configPlugin = new Plugin() { Name = "Configuration", Version = new Version(), Hash = hash };
334      PluginData configFile = new PluginData() { FileName = configFileName, Data = data };
335
336      IEnumerable<Plugin> onlineConfig = onlinePlugins.Where(p => p.Hash.SequenceEqual(hash));
337
338      if (onlineConfig.Count() > 0) {
339        return onlineConfig.First();
340      } else {
341        configPlugin.Id = service.AddPlugin(configPlugin, new List<PluginData> { configFile });
342        return configPlugin;
343      }
344    }
345
346    /// <summary>
347    /// Uploads the given task and all its child-jobs while setting the proper parentJobId values for the childs
348    /// </summary>
349    /// <param name="parentHiveTask">shall be null if its the root task</param>
350    private void UploadTaskWithChildren(IProgress progress, HiveTask hiveTask, HiveTask parentHiveTask, IEnumerable<Guid> groups, int[] taskCount, int totalJobCount, Guid configPluginId, Guid jobId, ILog log, bool isPrivileged, CancellationToken cancellationToken) {
351      taskUploadSemaphore.WaitOne();
352      bool semaphoreReleased = false;
353      try {
354        cancellationToken.ThrowIfCancellationRequested();
355        lock (jobCountLocker) {
356          taskCount[0]++;
357        }
358        TaskData taskData;
359        List<IPluginDescription> plugins;
360
361        if (hiveTask.ItemTask.ComputeInParallel && (hiveTask.ItemTask.Item is Optimization.Experiment || hiveTask.ItemTask.Item is Optimization.BatchRun)) {
362          hiveTask.Task.IsParentTask = true;
363          hiveTask.Task.FinishWhenChildJobsFinished = true;
364          taskData = hiveTask.GetAsTaskData(true, out plugins);
365        } else {
366          hiveTask.Task.IsParentTask = false;
367          hiveTask.Task.FinishWhenChildJobsFinished = false;
368          taskData = hiveTask.GetAsTaskData(false, out plugins);
369        }
370        cancellationToken.ThrowIfCancellationRequested();
371
372        TryAndRepeat(() => {
373          if (!cancellationToken.IsCancellationRequested) {
374            lock (pluginLocker) {
375              HiveServiceLocator.Instance.CallHiveService((s) => hiveTask.Task.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins));
376            }
377          }
378        }, Settings.Default.MaxRepeatServiceCalls, "Failed to upload plugins");
379        cancellationToken.ThrowIfCancellationRequested();
380        hiveTask.Task.PluginsNeededIds.Add(configPluginId);
381        hiveTask.Task.JobId = jobId;
382        hiveTask.Task.IsPrivileged = isPrivileged;
383
384        log.LogMessage(string.Format("Uploading task ({0} kb, {1} objects)", taskData.Data.Count() / 1024, hiveTask.ItemTask.GetObjectGraphObjects().Count()));
385        TryAndRepeat(() => {
386          if (!cancellationToken.IsCancellationRequested) {
387            if (parentHiveTask != null) {
388              hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddChildTask(parentHiveTask.Task.Id, hiveTask.Task, taskData));
389            } else {
390              hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddTask(hiveTask.Task, taskData, groups.ToList()));
391            }
392          }
393        }, Settings.Default.MaxRepeatServiceCalls, "Failed to add task", log);
394        cancellationToken.ThrowIfCancellationRequested();
395
396        lock (jobCountLocker) {
397          progress.ProgressValue = (double)taskCount[0] / totalJobCount;
398          progress.Status = string.Format("Uploaded task ({0} of {1})", taskCount[0], totalJobCount);
399        }
400
401        var tasks = new List<TS.Task>();
402        foreach (HiveTask child in hiveTask.ChildHiveTasks) {
403          tasks.Add(TS.Task.Factory.StartNew((tuple) => {
404            var arguments = (Tuple<HiveTask, HiveTask>)tuple;
405            UploadTaskWithChildren(progress, arguments.Item1, arguments.Item2, groups, taskCount, totalJobCount, configPluginId, jobId, log, isPrivileged, cancellationToken);
406          }, new Tuple<HiveTask, HiveTask>(child, hiveTask))
407          .ContinueWith((x) => log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
408        }
409        taskUploadSemaphore.Release(); semaphoreReleased = true; // the semaphore has to be release before waitall!
410        try {
411          TS.Task.WaitAll(tasks.ToArray());
412        }
413        catch (AggregateException ae) {
414          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
415        }
416      } finally {
417        if (!semaphoreReleased) taskUploadSemaphore.Release();
418      }
419    }
420    #endregion
421
422    #region Download Experiment
423    public static void LoadJob(RefreshableJob refreshableJob) {
424      var hiveExperiment = refreshableJob.Job;
425      refreshableJob.Progress = new Progress();
426
427      try {
428        refreshableJob.IsProgressing = true;
429        int totalJobCount = 0;
430        IEnumerable<LightweightTask> allTasks;
431
432        refreshableJob.Progress.Status = "Connecting to Server...";
433        // fetch all task objects to create the full tree of tree of HiveTask objects
434        refreshableJob.Progress.Status = "Downloading list of tasks...";
435        allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasks(hiveExperiment.Id));
436        totalJobCount = allTasks.Count();
437
438        refreshableJob.Progress.Status = "Downloading tasks...";
439        TaskDownloader downloader = new TaskDownloader(allTasks.Select(x => x.Id));
440        downloader.StartAsync();
441
442        while (!downloader.IsFinished) {
443          refreshableJob.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
444          refreshableJob.Progress.Status = string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
445          Thread.Sleep(500);
446
447          if (downloader.IsFaulted) {
448            throw downloader.Exception;
449          }
450        }
451        IDictionary<Guid, HiveTask> allHiveTasks = downloader.Results;
452        var parents = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
453
454        refreshableJob.Progress.Status = "Downloading/deserializing complete. Displaying tasks...";
455        // build child-task tree
456        foreach (HiveTask hiveTask in parents) {
457          BuildHiveJobTree(hiveTask, allTasks, allHiveTasks);
458        }
459
460        refreshableJob.HiveTasks = new ItemCollection<HiveTask>(parents);
461        if (refreshableJob.IsFinished()) {
462          refreshableJob.ExecutionState = Core.ExecutionState.Stopped;
463        } else {
464          refreshableJob.ExecutionState = Core.ExecutionState.Started;
465        }
466        refreshableJob.OnLoaded();
467      } finally {
468        refreshableJob.IsProgressing = false;
469      }
470    }
471
472    private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
473      IEnumerable<LightweightTask> childTasks = from job in allTasks
474                                                where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
475                                                orderby job.DateCreated ascending
476                                                select job;
477      foreach (LightweightTask task in childTasks) {
478        HiveTask childHiveTask = allHiveTasks[task.Id];
479        parentHiveTask.AddChildHiveTask(childHiveTask);
480        BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
481      }
482    }
483    #endregion
484
485    /// <summary>
486    /// Converts a string which can contain Ids separated by ';' to a enumerable
487    /// </summary>
488    private static IEnumerable<string> ToResourceNameList(string resourceNames) {
489      if (!string.IsNullOrEmpty(resourceNames)) {
490        return resourceNames.Split(';');
491      } else {
492        return new List<string>();
493      }
494    }
495
496    public static ItemTask LoadItemJob(Guid jobId) {
497      TaskData taskData = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(jobId));
498      try {
499        return PersistenceUtil.Deserialize<ItemTask>(taskData.Data);
500      }
501      catch {
502        return null;
503      }
504    }
505
506    /// <summary>
507    /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
508    /// If repetitions is -1, it is repeated infinitely.
509    /// </summary>
510    public static void TryAndRepeat(Action action, int repetitions, string errorMessage, ILog log = null) {
511      while (true) {
512        try { action(); return; }
513        catch (Exception e) {
514          if (repetitions == 0) throw new HiveException(errorMessage, e);
515          if (log != null) log.LogMessage(string.Format("{0}: {1} - will try again!", errorMessage, e.ToString()));
516          repetitions--;
517        }
518      }
519    }
520
521    public static HiveItemCollection<JobPermission> GetJobPermissions(Guid jobId) {
522      return HiveServiceLocator.Instance.CallHiveService((service) => {
523        IEnumerable<JobPermission> jps = service.GetJobPermissions(jobId);
524        foreach (var hep in jps) {
525          hep.UnmodifiedGrantedUserNameUpdate(service.GetUsernameByUserId(hep.GrantedUserId));
526        }
527        return new HiveItemCollection<JobPermission>(jps);
528      });
529    }
530  }
531}
Note: See TracBrowser for help on using the repository browser.