Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UnloadJobs/HeuristicLab.Clients.Hive/3.3/HiveClient.cs @ 9168

Last change on this file since 9168 was 9107, checked in by ascheibe, 12 years ago

#1950 fixed handling of IsAllowedPrivileged in the Hive Job Manager

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