Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Clients.Hive/3.3/HiveClient.cs @ 13243

Last change on this file since 13243 was 12931, checked in by gkronber, 9 years ago

#2434 merged r12878:12927 from trunk to branch (hive changes)

File size: 20.9 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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;
[7582]32using HeuristicLab.MainForm;
[6976]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
[9219]48    private HiveItemCollection<RefreshableJob> jobs;
49    public HiveItemCollection<RefreshableJob> Jobs {
[6976]50      get { return jobs; }
51      set {
52        if (value != jobs) {
53          jobs = value;
[9219]54          OnHiveJobsChanged();
[6976]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
[12579]72    private HiveClient() { }
[6976]73
[9219]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
[6976]90    #region Refresh
91    public void Refresh() {
92      OnRefreshing();
93
94      try {
95        jobs = new HiveItemCollection<RefreshableJob>();
[7132]96        var jobsLoaded = HiveServiceLocator.Instance.CallHiveService<IEnumerable<Job>>(s => s.GetJobs());
[6976]97
98        foreach (var j in jobsLoaded) {
[9436]99          jobs.Add(new RefreshableJob(j));
[6976]100        }
101      }
102      catch {
103        jobs = null;
104        throw;
[7582]105      }
106      finally {
[6976]107        OnRefreshed();
108      }
109    }
[9219]110
[6976]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;
[7132]136          hep.GrantedUserId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetUserIdByUsername(hep.GrantedUserName));
[6976]137          if (hep.GrantedUserId == Guid.Empty) {
138            throw new ArgumentException(string.Format("The user {0} was not found.", hep.GrantedUserName));
139          }
[7132]140          HiveServiceLocator.Instance.CallHiveService((s) => s.GrantPermission(hep.JobId, hep.GrantedUserId, hep.Permission));
[6976]141        }
142      } else {
143        if (item is Job)
[7132]144          HiveServiceLocator.Instance.CallHiveService(s => s.UpdateJob((Job)item));
[6976]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) {
[7059]166      if (item.Id == Guid.Empty && item.GetType() != typeof(JobPermission))
[6976]167        return;
168
169      if (item is Job)
[7132]170        HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(item.Id));
[7144]171      if (item is RefreshableJob) {
172        RefreshableJob job = (RefreshableJob)item;
173        if (job.RefreshAutomatically) {
174          job.StopResultPolling();
175        }
[7132]176        HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(item.Id));
[7144]177      }
[6976]178      if (item is JobPermission) {
179        var hep = (JobPermission)item;
[7132]180        HiveServiceLocator.Instance.CallHiveService(s => s.RevokePermission(hep.JobId, hep.GrantedUserId));
[6976]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    }
[9219]197    public event EventHandler HiveJobsChanged;
198    private void OnHiveJobsChanged() {
199      var handler = HiveJobsChanged;
[6976]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) {
[7132]214      HiveServiceLocator.Instance.CallHiveService(service => {
[6976]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) {
[7132]224      HiveServiceLocator.Instance.CallHiveService(service => {
[6976]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      });
[7156]230      refreshableJob.ExecutionState = ExecutionState.Stopped;
[6976]231    }
232
[7156]233    public static void ResumeJob(RefreshableJob refreshableJob) {
234      HiveServiceLocator.Instance.CallHiveService(service => {
235        foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
[7165]236          if (task.Task.State == TaskState.Paused) {
237            service.RestartTask(task.Task.Id);
[7156]238          }
239        }
240      });
241      refreshableJob.ExecutionState = ExecutionState.Started;
242    }
243
[6976]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 {
[8156]250        refreshableJob.IsProgressing = true;
[9893]251        refreshableJob.Progress.Start("Connecting to server...");
[6976]252        IEnumerable<string> resourceNames = ToResourceNameList(refreshableJob.Job.ResourceNames);
253        var resourceIds = new List<Guid>();
254        foreach (var resourceName in resourceNames) {
[7132]255          Guid resourceId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetResourceId(resourceName));
[6976]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...";
[7132]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
[6976]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...";
[7132]278        this.OnlinePlugins = HiveServiceLocator.Instance.CallHiveService((s) => s.GetPlugins());
[6976]279        this.AlreadyUploadedPlugins = new List<Plugin>();
[7132]280        Plugin configFilePlugin = HiveServiceLocator.Instance.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins));
[6976]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) {
[8939]289          var task = TS.Task.Factory.StartNew((hj) => {
[12931]290            UploadTaskWithChildren(refreshableJob.Progress, (HiveTask)hj, null, resourceIds, jobCount, totalJobCount, configFilePlugin.Id, refreshableJob.Job.Id, refreshableJob.Log, cancellationToken);
[8939]291          }, hiveTask);
292          task.ContinueWith((x) => refreshableJob.Log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted);
293          tasks.Add(task);
[6976]294        }
[8939]295        TS.Task.WaitAll(tasks.ToArray());
[7582]296      }
297      finally {
[8939]298        refreshableJob.Job.Modified = false;
[8159]299        refreshableJob.IsProgressing = false;
[8156]300        refreshableJob.Progress.Finish();
[6976]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>
[12931]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) {
[6976]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
[10130]346        if (hiveTask.ItemTask.ComputeInParallel) {
[6976]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) {
[7132]360              HiveServiceLocator.Instance.CallHiveService((s) => hiveTask.Task.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins));
[6976]361            }
362          }
[7125]363        }, Settings.Default.MaxRepeatServiceCalls, "Failed to upload plugins");
[6976]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) {
[7132]372              hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddChildTask(parentHiveTask.Task.Id, hiveTask.Task, taskData));
[6976]373            } else {
[7132]374              hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddTask(hiveTask.Task, taskData, groups.ToList()));
[6976]375            }
376          }
[7125]377        }, Settings.Default.MaxRepeatServiceCalls, "Failed to add task", log);
[6976]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) {
[8939]387          var task = TS.Task.Factory.StartNew((tuple) => {
[6976]388            var arguments = (Tuple<HiveTask, HiveTask>)tuple;
[12931]389            UploadTaskWithChildren(progress, arguments.Item1, arguments.Item2, groups, taskCount, totalJobCount, configPluginId, jobId, log, cancellationToken);
[8939]390          }, new Tuple<HiveTask, HiveTask>(child, hiveTask));
391          task.ContinueWith((x) => log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted);
392          tasks.Add(task);
[6976]393        }
394        taskUploadSemaphore.Release(); semaphoreReleased = true; // the semaphore has to be release before waitall!
[8939]395        TS.Task.WaitAll(tasks.ToArray());
[7582]396      }
397      finally {
[6976]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;
[8156]406      refreshableJob.IsProgressing = true;
[9219]407      TaskDownloader downloader = null;
[6976]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
[9893]414        refreshableJob.Progress.Start("Downloading list of tasks...");
[9219]415        allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(hiveExperiment.Id));
[6976]416        totalJobCount = allTasks.Count();
417
[7125]418        refreshableJob.Progress.Status = "Downloading tasks...";
[9219]419        downloader = new TaskDownloader(allTasks.Select(x => x.Id));
[6976]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;
[7165]432        var parents = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
[6976]433
[7165]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        }
[6976]439
[7165]440        refreshableJob.HiveTasks = new ItemCollection<HiveTask>(parents);
[6976]441        if (refreshableJob.IsFinished()) {
442          refreshableJob.ExecutionState = Core.ExecutionState.Stopped;
443        } else {
444          refreshableJob.ExecutionState = Core.ExecutionState.Started;
445        }
446        refreshableJob.OnLoaded();
[7582]447      }
448      finally {
[8159]449        refreshableJob.IsProgressing = false;
[8156]450        refreshableJob.Progress.Finish();
[9219]451        if (downloader != null) {
452          downloader.Dispose();
453        }
[6976]454      }
455    }
456
[7125]457    private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
458      IEnumerable<LightweightTask> childTasks = from job in allTasks
459                                                where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
[6976]460                                                orderby job.DateCreated ascending
461                                                select job;
462      foreach (LightweightTask task in childTasks) {
[7125]463        HiveTask childHiveTask = allHiveTasks[task.Id];
[8700]464        BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
[7125]465        parentHiveTask.AddChildHiveTask(childHiveTask);
[6976]466      }
467    }
468    #endregion
469
470    /// <summary>
471    /// Converts a string which can contain Ids separated by ';' to a enumerable
472    /// </summary>
473    private static IEnumerable<string> ToResourceNameList(string resourceNames) {
474      if (!string.IsNullOrEmpty(resourceNames)) {
[8109]475        return resourceNames.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
[6976]476      } else {
477        return new List<string>();
478      }
479    }
480
481    public static ItemTask LoadItemJob(Guid jobId) {
[7132]482      TaskData taskData = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(jobId));
[6976]483      try {
484        return PersistenceUtil.Deserialize<ItemTask>(taskData.Data);
485      }
486      catch {
487        return null;
488      }
489    }
490
491    /// <summary>
492    /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
493    /// If repetitions is -1, it is repeated infinitely.
494    /// </summary>
495    public static void TryAndRepeat(Action action, int repetitions, string errorMessage, ILog log = null) {
496      while (true) {
497        try { action(); return; }
498        catch (Exception e) {
499          if (repetitions == 0) throw new HiveException(errorMessage, e);
500          if (log != null) log.LogMessage(string.Format("{0}: {1} - will try again!", errorMessage, e.ToString()));
501          repetitions--;
502        }
503      }
504    }
505
506    public static HiveItemCollection<JobPermission> GetJobPermissions(Guid jobId) {
[7132]507      return HiveServiceLocator.Instance.CallHiveService((service) => {
[6976]508        IEnumerable<JobPermission> jps = service.GetJobPermissions(jobId);
509        foreach (var hep in jps) {
[7059]510          hep.UnmodifiedGrantedUserNameUpdate(service.GetUsernameByUserId(hep.GrantedUserId));
[6976]511        }
512        return new HiveItemCollection<JobPermission>(jps);
513      });
514    }
515  }
[10130]516}
Note: See TracBrowser for help on using the repository browser.