Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Clients.Hive/3.3/HiveClient.cs @ 7955

Last change on this file since 7955 was 7582, checked in by ascheibe, 13 years ago

#1762 moved ProgressView from Hive to MainForms

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.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        this.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.Progress = new Progress("Connecting to server...");
261        refreshableJob.IsProgressing = true;
262
263        IEnumerable<string> resourceNames = ToResourceNameList(refreshableJob.Job.ResourceNames);
264        var resourceIds = new List<Guid>();
265        foreach (var resourceName in resourceNames) {
266          Guid resourceId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetResourceId(resourceName));
267          if (resourceId == Guid.Empty) {
268            throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
269          }
270          resourceIds.Add(resourceId);
271        }
272
273        foreach (OptimizerHiveTask hiveJob in refreshableJob.HiveTasks.OfType<OptimizerHiveTask>()) {
274          hiveJob.SetIndexInParentOptimizerList(null);
275        }
276
277        // upload Job
278        refreshableJob.Progress.Status = "Uploading Job...";
279        refreshableJob.Job.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddJob(refreshableJob.Job));
280        bool isPrivileged = refreshableJob.Job.IsPrivileged;
281        refreshableJob.Job = HiveServiceLocator.Instance.CallHiveService((s) => s.GetJob(refreshableJob.Job.Id)); // update owner and permissions
282        refreshableJob.Job.IsPrivileged = isPrivileged;
283        cancellationToken.ThrowIfCancellationRequested();
284
285        int totalJobCount = refreshableJob.GetAllHiveTasks().Count();
286        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
287        cancellationToken.ThrowIfCancellationRequested();
288
289        // upload plugins
290        refreshableJob.Progress.Status = "Uploading plugins...";
291        this.OnlinePlugins = HiveServiceLocator.Instance.CallHiveService((s) => s.GetPlugins());
292        this.AlreadyUploadedPlugins = new List<Plugin>();
293        Plugin configFilePlugin = HiveServiceLocator.Instance.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins));
294        this.alreadyUploadedPlugins.Add(configFilePlugin);
295        cancellationToken.ThrowIfCancellationRequested();
296
297        if (refreshableJob.RefreshAutomatically) refreshableJob.StartResultPolling();
298
299        // upload tasks
300        refreshableJob.Progress.Status = "Uploading tasks...";
301
302        var tasks = new List<TS.Task>();
303        foreach (HiveTask hiveTask in refreshableJob.HiveTasks) {
304          tasks.Add(TS.Task.Factory.StartNew((hj) => {
305            UploadTaskWithChildren(refreshableJob.Progress, (HiveTask)hj, null, resourceIds, jobCount, totalJobCount, configFilePlugin.Id, refreshableJob.Job.Id, refreshableJob.Log, refreshableJob.Job.IsPrivileged, cancellationToken);
306          }, hiveTask)
307          .ContinueWith((x) => refreshableJob.Log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
308        }
309        try {
310          TS.Task.WaitAll(tasks.ToArray());
311        }
312        catch (AggregateException ae) {
313          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
314        }
315        refreshableJob.Job.Modified = false;
316      }
317      finally {
318        refreshableJob.IsProgressing = false;
319      }
320    }
321
322    /// <summary>
323    /// Uploads the local configuration file as plugin
324    /// </summary>
325    private static Plugin UploadConfigurationFile(IHiveService service, List<Plugin> onlinePlugins) {
326      string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.HLBinaryName);
327      string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
328      string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
329      byte[] hash;
330
331      byte[] data = File.ReadAllBytes(configFilePath);
332      using (SHA1 sha1 = SHA1.Create()) {
333        hash = sha1.ComputeHash(data);
334      }
335
336      Plugin configPlugin = new Plugin() { Name = "Configuration", Version = new Version(), Hash = hash };
337      PluginData configFile = new PluginData() { FileName = configFileName, Data = data };
338
339      IEnumerable<Plugin> onlineConfig = onlinePlugins.Where(p => p.Hash.SequenceEqual(hash));
340
341      if (onlineConfig.Count() > 0) {
342        return onlineConfig.First();
343      } else {
344        configPlugin.Id = service.AddPlugin(configPlugin, new List<PluginData> { configFile });
345        return configPlugin;
346      }
347    }
348
349    /// <summary>
350    /// Uploads the given task and all its child-jobs while setting the proper parentJobId values for the childs
351    /// </summary>
352    /// <param name="parentHiveTask">shall be null if its the root task</param>
353    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) {
354      taskUploadSemaphore.WaitOne();
355      bool semaphoreReleased = false;
356      try {
357        cancellationToken.ThrowIfCancellationRequested();
358        lock (jobCountLocker) {
359          taskCount[0]++;
360        }
361        TaskData taskData;
362        List<IPluginDescription> plugins;
363
364        if (hiveTask.ItemTask.ComputeInParallel && (hiveTask.ItemTask.Item is Optimization.Experiment || hiveTask.ItemTask.Item is Optimization.BatchRun)) {
365          hiveTask.Task.IsParentTask = true;
366          hiveTask.Task.FinishWhenChildJobsFinished = true;
367          taskData = hiveTask.GetAsTaskData(true, out plugins);
368        } else {
369          hiveTask.Task.IsParentTask = false;
370          hiveTask.Task.FinishWhenChildJobsFinished = false;
371          taskData = hiveTask.GetAsTaskData(false, out plugins);
372        }
373        cancellationToken.ThrowIfCancellationRequested();
374
375        TryAndRepeat(() => {
376          if (!cancellationToken.IsCancellationRequested) {
377            lock (pluginLocker) {
378              HiveServiceLocator.Instance.CallHiveService((s) => hiveTask.Task.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins));
379            }
380          }
381        }, Settings.Default.MaxRepeatServiceCalls, "Failed to upload plugins");
382        cancellationToken.ThrowIfCancellationRequested();
383        hiveTask.Task.PluginsNeededIds.Add(configPluginId);
384        hiveTask.Task.JobId = jobId;
385        hiveTask.Task.IsPrivileged = isPrivileged;
386
387        log.LogMessage(string.Format("Uploading task ({0} kb, {1} objects)", taskData.Data.Count() / 1024, hiveTask.ItemTask.GetObjectGraphObjects().Count()));
388        TryAndRepeat(() => {
389          if (!cancellationToken.IsCancellationRequested) {
390            if (parentHiveTask != null) {
391              hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddChildTask(parentHiveTask.Task.Id, hiveTask.Task, taskData));
392            } else {
393              hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddTask(hiveTask.Task, taskData, groups.ToList()));
394            }
395          }
396        }, Settings.Default.MaxRepeatServiceCalls, "Failed to add task", log);
397        cancellationToken.ThrowIfCancellationRequested();
398
399        lock (jobCountLocker) {
400          progress.ProgressValue = (double)taskCount[0] / totalJobCount;
401          progress.Status = string.Format("Uploaded task ({0} of {1})", taskCount[0], totalJobCount);
402        }
403
404        var tasks = new List<TS.Task>();
405        foreach (HiveTask child in hiveTask.ChildHiveTasks) {
406          tasks.Add(TS.Task.Factory.StartNew((tuple) => {
407            var arguments = (Tuple<HiveTask, HiveTask>)tuple;
408            UploadTaskWithChildren(progress, arguments.Item1, arguments.Item2, groups, taskCount, totalJobCount, configPluginId, jobId, log, isPrivileged, cancellationToken);
409          }, new Tuple<HiveTask, HiveTask>(child, hiveTask))
410          .ContinueWith((x) => log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
411        }
412        taskUploadSemaphore.Release(); semaphoreReleased = true; // the semaphore has to be release before waitall!
413        try {
414          TS.Task.WaitAll(tasks.ToArray());
415        }
416        catch (AggregateException ae) {
417          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
418        }
419      }
420      finally {
421        if (!semaphoreReleased) taskUploadSemaphore.Release();
422      }
423    }
424    #endregion
425
426    #region Download Experiment
427    public static void LoadJob(RefreshableJob refreshableJob) {
428      var hiveExperiment = refreshableJob.Job;
429      refreshableJob.Progress = new Progress();
430
431      try {
432        refreshableJob.IsProgressing = true;
433        int totalJobCount = 0;
434        IEnumerable<LightweightTask> allTasks;
435
436        refreshableJob.Progress.Status = "Connecting to Server...";
437        // fetch all task objects to create the full tree of tree of HiveTask objects
438        refreshableJob.Progress.Status = "Downloading list of tasks...";
439        allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasks(hiveExperiment.Id));
440        totalJobCount = allTasks.Count();
441
442        refreshableJob.Progress.Status = "Downloading tasks...";
443        TaskDownloader downloader = new TaskDownloader(allTasks.Select(x => x.Id));
444        downloader.StartAsync();
445
446        while (!downloader.IsFinished) {
447          refreshableJob.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
448          refreshableJob.Progress.Status = string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
449          Thread.Sleep(500);
450
451          if (downloader.IsFaulted) {
452            throw downloader.Exception;
453          }
454        }
455        IDictionary<Guid, HiveTask> allHiveTasks = downloader.Results;
456        var parents = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
457
458        refreshableJob.Progress.Status = "Downloading/deserializing complete. Displaying tasks...";
459        // build child-task tree
460        foreach (HiveTask hiveTask in parents) {
461          BuildHiveJobTree(hiveTask, allTasks, allHiveTasks);
462        }
463
464        refreshableJob.HiveTasks = new ItemCollection<HiveTask>(parents);
465        if (refreshableJob.IsFinished()) {
466          refreshableJob.ExecutionState = Core.ExecutionState.Stopped;
467        } else {
468          refreshableJob.ExecutionState = Core.ExecutionState.Started;
469        }
470        refreshableJob.OnLoaded();
471      }
472      finally {
473        refreshableJob.IsProgressing = false;
474      }
475    }
476
477    private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
478      IEnumerable<LightweightTask> childTasks = from job in allTasks
479                                                where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
480                                                orderby job.DateCreated ascending
481                                                select job;
482      foreach (LightweightTask task in childTasks) {
483        HiveTask childHiveTask = allHiveTasks[task.Id];
484        parentHiveTask.AddChildHiveTask(childHiveTask);
485        BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
486      }
487    }
488    #endregion
489
490    /// <summary>
491    /// Converts a string which can contain Ids separated by ';' to a enumerable
492    /// </summary>
493    private static IEnumerable<string> ToResourceNameList(string resourceNames) {
494      if (!string.IsNullOrEmpty(resourceNames)) {
495        return resourceNames.Split(';');
496      } else {
497        return new List<string>();
498      }
499    }
500
501    public static ItemTask LoadItemJob(Guid jobId) {
502      TaskData taskData = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(jobId));
503      try {
504        return PersistenceUtil.Deserialize<ItemTask>(taskData.Data);
505      }
506      catch {
507        return null;
508      }
509    }
510
511    /// <summary>
512    /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
513    /// If repetitions is -1, it is repeated infinitely.
514    /// </summary>
515    public static void TryAndRepeat(Action action, int repetitions, string errorMessage, ILog log = null) {
516      while (true) {
517        try { action(); return; }
518        catch (Exception e) {
519          if (repetitions == 0) throw new HiveException(errorMessage, e);
520          if (log != null) log.LogMessage(string.Format("{0}: {1} - will try again!", errorMessage, e.ToString()));
521          repetitions--;
522        }
523      }
524    }
525
526    public static HiveItemCollection<JobPermission> GetJobPermissions(Guid jobId) {
527      return HiveServiceLocator.Instance.CallHiveService((service) => {
528        IEnumerable<JobPermission> jps = service.GetJobPermissions(jobId);
529        foreach (var hep in jps) {
530          hep.UnmodifiedGrantedUserNameUpdate(service.GetUsernameByUserId(hep.GrantedUserId));
531        }
532        return new HiveItemCollection<JobPermission>(jps);
533      });
534    }
535  }
536}
Note: See TracBrowser for help on using the repository browser.