Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2695_dataset-ids/HeuristicLab.Clients.Hive/3.3/HiveClient.cs @ 17693

Last change on this file since 17693 was 14901, checked in by jkarder, 8 years ago

#2784: fixed pausing of hive tasks

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