Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6200


Ignore:
Timestamp:
05/16/11 00:18:48 (13 years ago)
Author:
cneumuel
Message:

#1233

  • stability improvements for HiveEngine
Location:
branches/HeuristicLab.Hive-3.4/sources
Files:
1 added
1 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/HiveJobView.cs

    r6178 r6200  
    4646    protected override void RegisterContentEvents() {
    4747      base.RegisterContentEvents();
    48       Content.JobItemChanged += new EventHandler(Content_JobItemChanged);
     48      Content.ItemJobChanged += new EventHandler(Content_JobItemChanged);
    4949      Content.JobChanged += new EventHandler(Content_JobChanged);
    5050      Content.JobStateChanged += new EventHandler(Content_JobStateChanged);
     
    5252
    5353    protected override void DeregisterContentEvents() {
    54       Content.JobItemChanged -= new EventHandler(Content_JobItemChanged);
     54      Content.ItemJobChanged -= new EventHandler(Content_JobItemChanged);
    5555      Content.JobChanged -= new EventHandler(Content_JobChanged);
    5656      Content.JobStateChanged -= new EventHandler(Content_JobStateChanged);
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/RefreshableHiveExperimentView.cs

    r6198 r6200  
    2121
    2222using System;
    23 using System.ComponentModel;
    2423using System.Linq;
    2524using System.Threading;
     
    5857      base.RegisterContentEvents();
    5958      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
    60       Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
    6159      Content.HiveExperimentChanged += new EventHandler(Content_HiveExperimentChanged);
    6260      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
    63     }
     61      Content.JobStatisticsChanged += new EventHandler(Content_JobStatisticsChanged);
     62    }
     63
    6464    protected override void DeregisterContentEvents() {
    6565      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
    66       Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
    6766      Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged);
    6867      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
     68      Content.JobStatisticsChanged -= new EventHandler(Content_JobStatisticsChanged);
    6969      base.DeregisterContentEvents();
    7070    }
     
    104104        //includeJobsCheckBox.Checked = false;
    105105        refreshAutomaticallyCheckBox.Checked = false;
    106         jobsTextBox.Text = "0";
    107         calculatingTextBox.Text = "0";
    108         finishedTextBox.Text = "0";
    109106        logView.Content = null;
    110107      } else {
     
    115112        //includeJobsCheckBox.Checked = Content.IncludeJobs;
    116113        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
    117         jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
    118         calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
    119         finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
    120114        logView.Content = Content.Log;
    121115      }
     116      Content_JobStatisticsChanged(this, EventArgs.Empty);
    122117      Content_HiveExperimentChanged(this, EventArgs.Empty);
    123118      Content_HiveJobChanged(this, EventArgs.Empty);
     
    233228    }
    234229
    235     private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
    236       if (InvokeRequired)
    237         Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e);
    238       else {
    239         jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
    240         calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
    241         finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
    242       }
    243     }
    244 
    245230    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
    246231      if (Content.HiveExperiment != null) {
     
    252237      SetEnabledStateOfControls();
    253238    }
     239    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
     240      if (InvokeRequired)
     241        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
     242      else {
     243        if (Content != null) {
     244          jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
     245          calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
     246          finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
     247        } else {
     248          jobsTextBox.Text = "0";
     249          calculatingTextBox.Text = "0";
     250          finishedTextBox.Text = "0";
     251        }
     252      }
     253    }
    254254    #endregion
    255255
    256256    #region Control events
    257257    private void startButton_Click(object sender, EventArgs e) {
    258       ExperimentManagerClient.StartExperiment(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex)), Content);
     258      ExperimentManagerClient.StartExperiment((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content);
    259259    }
    260260    private void pauseButton_Click(object sender, EventArgs e) {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/ConcurrentJobDownloader.cs

    r6198 r6200  
    2121
    2222    public void DownloadJob(Job job, Action<Job, T, Exception> onFinishedAction) {
    23       Task<JobData>.Factory.StartNew((x) => DownloadJob(x), job.Id)
    24                                      .ContinueWith((x) => DeserializeJob(x.Result))
    25                                      .ContinueWith((x) => OnJobFinished(job, x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously);
     23      Task<T> task = Task<JobData>.Factory.StartNew((x) => DownloadJob(x), job.Id)
     24                                     .ContinueWith((x) => DeserializeJob(x.Result));
     25      task.ContinueWith((x) => OnJobFinished(job, x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);
     26      task.ContinueWith((x) => OnJobFailed(job, x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
    2627    }
    2728
    2829    private void OnJobFinished(Job job, Task<T> task, Action<Job, T, Exception> onFinishedAction) {
    29       onFinishedAction(job, task.Result, task.Exception);
     30      onFinishedAction(job, task.Result, null);
     31    }
     32    private void OnJobFailed(Job job, Task<T> task, Action<Job, T, Exception> onFinishedAction) {
     33      onFinishedAction(job, task.Result, task.Exception.Flatten());
    3034    }
    3135
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/ExperimentManagerClient.cs

    r6178 r6200  
    239239      }
    240240    }
    241 
     241   
    242242    /// <summary>
    243243    /// Uploads the local configuration file as plugin
     
    286286      }
    287287
    288       hiveJob.Job.PluginsNeededIds = PluginUtil.GetPluginDependencies(service, this.onlinePlugins, this.alreadyUploadedPlugins, plugins, useLocalPlugins);
     288      TryAndRepeat(() => {
     289        hiveJob.Job.PluginsNeededIds = PluginUtil.GetPluginDependencies(service, this.onlinePlugins, this.alreadyUploadedPlugins, plugins, useLocalPlugins);
     290      }, -1, "Failed to upload plugins");
    289291      hiveJob.Job.PluginsNeededIds.Add(configPluginId);
    290292      hiveJob.Job.HiveExperimentId = hiveExperimentId;
     
    293295      progress.ProgressValue = (double)jobCount / totalJobCount;
    294296
    295       if (parentHiveJob != null) {
    296         hiveJob.Job.Id = service.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData);
    297       } else {
    298         hiveJob.Job.Id = service.AddJob(hiveJob.Job, jobData, groups.ToList());
    299       }
     297
     298      TryAndRepeat(() => {
     299        if (parentHiveJob != null) {
     300          hiveJob.Job.Id = service.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData);
     301        } else {
     302          hiveJob.Job.Id = service.AddJob(hiveJob.Job, jobData, groups.ToList());
     303        }
     304      }, -1, "Failed to add job");
    300305
    301306      foreach (HiveJob child in hiveJob.ChildHiveJobs) {
     
    382387      }
    383388    }
     389
     390    /// <summary>
     391    /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
     392    /// If repetitions is -1, it is repeated infinitely.
     393    /// </summary>
     394    public static void TryAndRepeat(Action action, int repetitions, string errorMessage) {
     395      try { action(); }
     396      catch (Exception e) {
     397        repetitions--;
     398        if (repetitions == 0)
     399          throw new HiveException(errorMessage, e);
     400        TryAndRepeat(action, repetitions, errorMessage);
     401      }
     402    }
    384403  }
    385404}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJob.cs

    r6198 r6200  
    8686          itemJob = value;
    8787          RegisterItemJobEvents();
    88           OnJobItemChanged();
     88          OnItemJobChanged();
    8989          IsFinishedJobDownloaded = true;
    9090        }
     
    166166    protected virtual void RegisterItemJobEvents() {
    167167      if (ItemJob != null) {
    168         ItemJob.ComputeInParallelChanged += new EventHandler(JobItem_ComputeInParallelChanged);
    169         ItemJob.ToStringChanged += new EventHandler(JobItem_ToStringChanged);
     168        ItemJob.ComputeInParallelChanged += new EventHandler(ItemJob_ComputeInParallelChanged);
     169        ItemJob.ToStringChanged += new EventHandler(ItemJob_ToStringChanged);
    170170      }
    171171    }
    172172    protected virtual void DergisterItemJobEvents() {
    173173      if (ItemJob != null) {
    174         ItemJob.ComputeInParallelChanged -= new EventHandler(JobItem_ComputeInParallelChanged);
    175         ItemJob.ToStringChanged -= new EventHandler(JobItem_ToStringChanged);
     174        ItemJob.ComputeInParallelChanged -= new EventHandler(ItemJob_ComputeInParallelChanged);
     175        ItemJob.ToStringChanged -= new EventHandler(ItemJob_ToStringChanged);
    176176      }
    177177    }
     
    188188    }
    189189
    190     protected virtual void JobItem_ToStringChanged(object sender, EventArgs e) {
     190    protected virtual void ItemJob_ToStringChanged(object sender, EventArgs e) {
    191191      this.OnToStringChanged();
    192192    }
    193193
    194     protected virtual void JobItem_ComputeInParallelChanged(object sender, EventArgs e) {
     194    protected virtual void ItemJob_ComputeInParallelChanged(object sender, EventArgs e) {
    195195      if (ItemJob != null && syncJobsWithOptimizers) {
    196196        this.UpdateChildHiveJobs();
     
    203203
    204204    public override string ToString() {
    205       if (itemJob != null) {
     205      if (itemJob != null && itemJob.Item != null) {
    206206        return itemJob.ToString();
    207207      } else {
    208         return base.ToString();
     208        return Job.Id.ToString();
    209209      }
    210210    }
     
    262262    }
    263263
    264     public event EventHandler JobItemChanged;
    265     private void OnJobItemChanged() {
    266       JobItem_ComputeInParallelChanged(this, EventArgs.Empty);
    267       var handler = JobItemChanged;
     264    public event EventHandler ItemJobChanged;
     265    private void OnItemJobChanged() {
     266      ItemJob_ComputeInParallelChanged(this, EventArgs.Empty);
     267      var handler = ItemJobChanged;
    268268      if (handler != null) handler(this, EventArgs.Empty);
    269269    }
     
    410410    public virtual void IntegrateChild(ItemJob job, Guid childJobId) { }
    411411
     412    /// <summary>
     413    /// Delete ItemJob
     414    /// </summary>
     415    public void ClearData() {
     416      this.ItemJob.Item = null;
     417    }
    412418  }
    413419
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/RefreshableHiveExperiment.cs

    r6199 r6200  
    2828using HeuristicLab.Core;
    2929using HeuristicLab.Hive;
    30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3130
    3231namespace HeuristicLab.Clients.Hive {
    33   [StorableClass]
    3432  public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter {
    3533    private JobResultPoller jobResultPoller;
     
    3735    private static object locker = new object();
    3836
    39     [Storable]
    4037    private HiveExperiment hiveExperiment;
    4138    public HiveExperiment HiveExperiment {
     
    5552
    5653    /** include jobs when refreshing **/
    57     [Storable]
    5854    private bool includeJobs;
    5955    public bool IncludeJobs {
     
    6258    }
    6359
    64     [Storable]
    6560    private bool refreshAutomatically;
    6661    public bool RefreshAutomatically {
     
    8378    }
    8479
    85     [Storable]
    8680    private bool isControllable = true;
    8781    public bool IsControllable {
     
    9589    }
    9690
    97     [Storable]
    9891    private ILog log;
    9992    public ILog Log {
     
    120113      cloner.RegisterClonedObject(original, this);
    121114      this.HiveExperiment = original.HiveExperiment;
    122       this.RefreshAutomatically = original.RefreshAutomatically;
    123115      this.IncludeJobs = original.IncludeJobs;
    124116      this.IsControllable = original.IsControllable;
    125117      this.Log = cloner.Clone(original.Log);
     118      this.RefreshAutomatically = false; // do not start results polling automatically
    126119    }
    127120    public IDeepCloneable Clone(Cloner cloner) {
     
    196189
    197190              if (exception != null) {
    198                 var ex = new ConcurrentJobDownloaderException("Downloading job failed.", exception);
     191                var ex = new ConcurrentJobDownloaderException("Downloading job failed", exception);
    199192                LogException(ex);
    200193                localHiveJob.IsDownloading = false;
     
    206199              } else {
    207200                // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
    208 
    209201
    210202                if (localJob.State == JobState.Paused) {
     
    231223      }
    232224      UpdateTotalExecutionTime();
    233       UpdateStats();
     225      UpdateStatistics();
    234226    }
    235227
     
    237229    private void LogException(Exception exception) {
    238230      lock (logLocker) {
    239         log.LogException(exception);
     231        this.log.LogException(exception);
    240232      }
    241233    }
     
    243235    private void LogMessage(string message) {
    244236      lock (logLocker) {
    245         log.LogMessage(message);
     237        this.log.LogMessage(message);
    246238      }
    247239    }
     
    256248    }
    257249
    258     private void UpdateStats() {
     250    private void UpdateStatistics() {
    259251      var jobs = hiveExperiment.GetAllHiveJobs();
    260252      hiveExperiment.JobCount = jobs.Count();
    261253      hiveExperiment.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating);
    262254      hiveExperiment.FinishedCount = jobs.Count(j => j.Job.State == JobState.Finished);
     255      OnJobStatisticsChanged();
    263256    }
    264257
     
    269262                                                   && j.IsFinishedJobDownloaded);
    270263    }
    271 
    272     //public bool AllJobsFinishedAndDownloaded() {
    273     //  return this.AllJobsFinished() && hiveExperiment.GetAllHiveJobs().All(j => j.JobItem.;
    274     //}
    275264
    276265    private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
     
    359348      if (handler != null) handler(this, EventArgs.Empty);
    360349    }
     350
     351    public event EventHandler JobStatisticsChanged;
     352    private void OnJobStatisticsChanged() {
     353      var handler = JobStatisticsChanged;
     354      if (handler != null) handler(this, EventArgs.Empty);
     355    }
    361356    #endregion
    362357
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/HeuristicLab.Clients.Hive-3.4.csproj

    r6178 r6200  
    123123    <Compile Include="ExperimentManager\EngineHiveJob.cs" />
    124124    <Compile Include="ExperimentManager\ExperimentManagerClient.cs" />
     125    <Compile Include="Exceptions\HiveException.cs" />
    125126    <Compile Include="ExperimentManager\HiveJobDownloader.cs" />
    126127    <Compile Include="ExperimentManager\ConcurrentJobDownloaderException.cs" />
     
    155156    <Compile Include="ServiceClients\HiveItem.cs" />
    156157    <Compile Include="ServiceClients\JobData.cs" />
    157     <Compile Include="ServiceClients\OperationJob.cs" />
    158158    <Compile Include="ExperimentManager\RefreshableHiveExperiment.cs" />
    159159    <Compile Include="ServiceClients\Plugin.cs" />
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Jobs/EngineJob.cs

    r6178 r6200  
    129129
    130130    public override string Name {
    131       get { return Item.ToString(); }
     131      get { return Item != null ? Item.ToString() : null; }
    132132      set { throw new NotSupportedException(); }
    133133    }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Progress/Progress.cs

    r4629 r6200  
    6464    private void OnFinished() {
    6565      var handler = Finished;
    66       if (handler != null) handler(this, EventArgs.Empty);
     66      try {
     67        if (handler != null) handler(this, EventArgs.Empty);
     68      }
     69      catch (Exception) { }
    6770    }
    6871
     
    7073    private void OnStatusChanged() {
    7174      var handler = StatusChanged;
    72       if (handler != null) handler(this, EventArgs.Empty);
     75      try {
     76        if (handler != null) handler(this, EventArgs.Empty);
     77      }
     78      catch (Exception) { }
    7379    }
    7480
     
    7682    private void OnProgressChanged() {
    7783      var handler = ProgressValueChanged;
    78       if (handler != null) handler(this, EventArgs.Empty);
     84      try {
     85        if (handler != null) handler(this, EventArgs.Empty);
     86      }
     87      catch (Exception) { }
    7988    }
    8089    #endregion
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveExperiment.cs

    r6168 r6200  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
    28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2928
    3029namespace HeuristicLab.Clients.Hive {
    31   [StorableClass]
    3230  public partial class HiveExperiment : IDeepCloneable, IContent, IProgressReporter {
    33     [Storable]
    3431    private bool useLocalPlugins;
    3532    public bool UseLocalPlugins {
     
    3835    }
    3936
    40     [Storable]
    4137    private ExecutionState executionState;
    4238    public ExecutionState ExecutionState {
     
    5046    }
    5147
    52     [Storable]
    5348    private TimeSpan executionTime;
    5449    public TimeSpan ExecutionTime {
     
    6257    }
    6358
    64     [Storable]
    6559    private ItemCollection<HiveJob> hiveJobs;
    6660    public ItemCollection<HiveJob> HiveJobs {
     
    7468    }
    7569
    76     [Storable]
    7770    private bool isProgressing;
    7871    public bool IsProgressing {
     
    8679    }
    8780
    88     [Storable]
    8981    private IProgress progress;
    9082    public IProgress Progress {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveItem.cs

    r5955 r6200  
    2828
    2929namespace HeuristicLab.Clients.Hive {
    30 
    3130  public partial class HiveItem : IHiveItem {
    3231    public string ItemName {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveItemCollection.cs

    r5955 r6200  
    66
    77namespace HeuristicLab.Clients.Hive {
    8   [Item("HiveItem Collection", "Represents a collection of OKB items.")]
     8  [Item("HiveItem Collection", "Represents a collection of Hive items.")]
    99  public class HiveItemCollection<T> : ItemCollection<T> where T : class, IHiveItem {
    1010    protected HiveItemCollection(HiveItemCollection<T> original, Cloner cloner) : base(original, cloner) { }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/Job.cs

    r6006 r6200  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Common;
    25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2625
    2726namespace HeuristicLab.Clients.Hive {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/JobData.cs

    r5779 r6200  
    2424
    2525namespace HeuristicLab.Clients.Hive {
    26 
    2726  public partial class JobData : IDeepCloneable, IContent {
    2827    public JobData() { }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/MessageContainer.cs

    r5614 r6200  
    2222using System;
    2323using HeuristicLab.Common;
    24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2524
    2625namespace HeuristicLab.Clients.Hive {
    27   [StorableClass]
    2826  public partial class MessageContainer : IDeepCloneable, IContent {
    2927
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Hive 3.4.sln

    r6116 r6200  
    276276    {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Debug|x86.ActiveCfg = Debug|x86
    277277    {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Debug|x86.Build.0 = Debug|x86
    278     {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Release|Any CPU.ActiveCfg = Release|x86
     278    {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Release|Any CPU.ActiveCfg = Release|Any CPU
     279    {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Release|Any CPU.Build.0 = Release|Any CPU
    279280    {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Release|Mixed Platforms.ActiveCfg = Release|x86
    280281    {BA8001DE-E83C-4B1F-8B2E-2695C4222491}.Release|Mixed Platforms.Build.0 = Release|x86
     
    290291    {28711372-0255-4883-9BED-81E150D73880}.Debug|x86.Build.0 = Debug
    291292    {28711372-0255-4883-9BED-81E150D73880}.Release|Any CPU.ActiveCfg = Release
     293    {28711372-0255-4883-9BED-81E150D73880}.Release|Any CPU.Build.0 = Release
    292294    {28711372-0255-4883-9BED-81E150D73880}.Release|Mixed Platforms.ActiveCfg = Release
    293295    {28711372-0255-4883-9BED-81E150D73880}.Release|x64.ActiveCfg = Release
     
    301303    {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Debug|x86.ActiveCfg = Debug|x86
    302304    {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Debug|x86.Build.0 = Debug|x86
    303     {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Release|Any CPU.ActiveCfg = Release|x86
     305    {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
     306    {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Release|Any CPU.Build.0 = Release|Any CPU
    304307    {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Release|Mixed Platforms.ActiveCfg = Release|x86
    305308    {7C4B1DE4-FC9A-4448-BCF8-3CB3FA3CB8FA}.Release|Mixed Platforms.Build.0 = Release|x86
     
    397400    {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Debug|x86.ActiveCfg = Debug|x86
    398401    {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Debug|x86.Build.0 = Debug|x86
    399     {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Release|Any CPU.ActiveCfg = Release|x86
     402    {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
     403    {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Release|Any CPU.Build.0 = Release|Any CPU
    400404    {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Release|Mixed Platforms.ActiveCfg = Release|x86
    401405    {87D9FBB9-8E54-4770-9C84-B4B571D9EDD5}.Release|Mixed Platforms.Build.0 = Release|x86
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.HiveEngine/3.4/HiveEngine.cs

    r6198 r6200  
    77using HeuristicLab.Common;
    88using HeuristicLab.Core;
    9 using HeuristicLab.Hive;
    109using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    11 using HeuristicLab.PluginInfrastructure;
    1210
    1311namespace HeuristicLab.HiveEngine {
     
    1816  [Item("Hive Engine", "Engine for parallel execution on the hive. You need enable `Parallel` for at least one operator in your operator graph to have all childoperations parallelized. Also those childoperations must not have sideeffects on a higher scope.")]
    1917  public class HiveEngine : Engine {
    20     private Semaphore maxConcurrentConnections = new Semaphore(4, 4); // avoid too many connections
    21     private Semaphore maxSerializedJobsInMemory = new Semaphore(4, 4); // avoid memory problems
     18    private static object logLocker = new object();
    2219    private CancellationToken cancellationToken;
    23 
     20   
    2421    [Storable]
    2522    private IOperator currentOperator;
     
    5451    }
    5552
    56     [Storable]
    57     private ItemCollection<RefreshableHiveExperiment> hiveExperiments;
     53    // [Storable] -> HiveExperiment can't be storable, so RefreshableHiveExperiment can't be stored
     54    private ItemCollection<RefreshableHiveExperiment> hiveExperiments = new ItemCollection<RefreshableHiveExperiment>();
    5855    public ItemCollection<RefreshableHiveExperiment> HiveExperiments {
    5956      get { return hiveExperiments; }
     
    7673    public HiveEngine() {
    7774      ResourceNames = "HEAL";
    78       HiveExperiments = new ItemCollection<RefreshableHiveExperiment>();
    7975      Priority = 0;
    8076    }
     
    8985      this.executionTimeOnHive = original.executionTimeOnHive;
    9086      this.useLocalPlugins = original.useLocalPlugins;
     87      this.hiveExperiments = cloner.Clone(original.hiveExperiments);
    9188    }
    9289    public override IDeepCloneable Clone(Cloner cloner) {
     
    202199      target.SubScopes.AddRange(source.SubScopes);
    203200      // TODO: validate if parent scopes match - otherwise source is invalid
    204     }
    205 
    206     // testfunction:
    207     private IScope[] ExecuteLocally(EngineJob[] jobs, IScope parentScopeClone, CancellationToken cancellationToken) {
    208       IScope[] scopes = new Scope[jobs.Length];
    209       for (int i = 0; i < jobs.Length; i++) {
    210         var serialized = PersistenceUtil.Serialize(jobs[i]);
    211         var deserialized = PersistenceUtil.Deserialize<IJob>(serialized);
    212         deserialized.Start();
    213         while (deserialized.ExecutionState != ExecutionState.Stopped) {
    214           Thread.Sleep(100);
    215         }
    216         var serialized2 = PersistenceUtil.Serialize(deserialized);
    217         var deserialized2 = PersistenceUtil.Deserialize<EngineJob>(serialized2);
    218         var newScope = ((IAtomicOperation)deserialized2.InitialOperation).Scope;
    219         scopes[i] = newScope;
    220       }
    221       return scopes;
    222201    }
    223202
     
    256235            random.Reset(random.Next());
    257236        }
    258         ExperimentManagerClient.StartExperiment((e) => { throw e; }, refreshableHiveExperiment);
     237        ExperimentManagerClient.StartExperiment((e) => {
     238          LogException(e);
     239        }, refreshableHiveExperiment);
     240
    259241        // do polling until experiment is finished and all jobs are downloaded
    260242        while (!refreshableHiveExperiment.AllJobsFinished()) {
     
    273255        refreshableHiveExperiment.RefreshAutomatically = false;
    274256        DeleteHiveExperiment(hiveExperiment.Id);
     257        ClearData(refreshableHiveExperiment);
    275258        return scopes;
    276259      }
     
    290273    }
    291274
     275    private void ClearData(RefreshableHiveExperiment refreshableHiveExperiment) {
     276      var jobs = refreshableHiveExperiment.HiveExperiment.GetAllHiveJobs();
     277      foreach (var job in jobs) {
     278        job.ClearData();
     279      }
     280    }
     281
    292282    private void DeleteHiveExperiment(Guid hiveExperimentId) {
    293       TryAndRepeat(() => {
     283      ExperimentManagerClient.TryAndRepeat(() => {
    294284        ServiceLocator.Instance.CallHiveService(s => s.DeleteHiveExperiment(hiveExperimentId));
    295285      }, 5, string.Format("Could not delete jobs"));
    296286    }
    297 
    298     private static object locker = new object();
    299     private Job UploadJob(object keyValuePairObj, IScope parentScopeClone, CancellationToken cancellationToken, List<Guid> resourceIds, Guid hiveExperimentId) {
    300       var keyValuePair = (KeyValuePair<int, EngineJob>)keyValuePairObj;
    301       Job job = new Job();
    302 
    303       try {
    304         maxSerializedJobsInMemory.WaitOne();
    305         JobData jobData = new JobData();
    306         IEnumerable<Type> usedTypes;
    307 
    308         // clone operation and remove unnecessary scopes; don't do this earlier to avoid memory problems
    309         lock (locker) {
    310           ((IAtomicOperation)keyValuePair.Value.InitialOperation).Scope.Parent = parentScopeClone;
    311           keyValuePair.Value.InitialOperation = (IOperation)keyValuePair.Value.InitialOperation.Clone();
    312           if (keyValuePair.Value.InitialOperation is IAtomicOperation)
    313             ((IAtomicOperation)keyValuePair.Value.InitialOperation).Scope.ClearParentScopes();
    314           jobData.Data = PersistenceUtil.Serialize(keyValuePair.Value, out usedTypes);
    315         }
    316         var neededPlugins = new List<IPluginDescription>();
    317 
    318         bool useAllLocalPlugins = true;
    319         if (useAllLocalPlugins) {
    320           // use all plugins
    321           neededPlugins.AddRange(ApplicationManager.Manager.Plugins);
    322         } else {
    323           // use only
    324           PluginUtil.CollectDeclaringPlugins(neededPlugins, usedTypes);
    325         }
    326 
    327         job.CoresNeeded = 1;
    328         job.PluginsNeededIds = ServiceLocator.Instance.CallHiveService(s => PluginUtil.GetPluginDependencies(s, this.OnlinePlugins, this.AlreadyUploadedPlugins, neededPlugins, useLocalPlugins));
    329         job.Priority = priority;
    330         job.HiveExperimentId = hiveExperimentId;
    331 
    332         try {
    333           maxConcurrentConnections.WaitOne();
    334           while (job.Id == Guid.Empty) { // repeat until success
    335             cancellationToken.ThrowIfCancellationRequested();
    336             try {
    337               job.Id = ServiceLocator.Instance.CallHiveService(s => s.AddJob(job, jobData, resourceIds));
    338             }
    339             catch (Exception e) {
    340               LogException(e);
    341               LogMessage("Repeating upload");
    342             }
    343           }
    344         }
    345         finally {
    346           maxConcurrentConnections.Release();
    347         }
    348       }
    349       finally {
    350         maxSerializedJobsInMemory.Release();
    351       }
    352       return job;
    353     }
    354 
     287   
    355288    private List<Guid> GetResourceIds() {
    356289      return ServiceLocator.Instance.CallHiveService(service => {
     
    368301    }
    369302
    370     private EngineJob DownloadJob(IDictionary<Guid, int> jobIndices, object jobIdObj, CancellationToken cancellationToken) {
    371       Guid jobId = (Guid)jobIdObj;
    372       JobData jobData = null;
    373       EngineJob engineJob = null;
    374       try {
    375         maxSerializedJobsInMemory.WaitOne();
    376         maxConcurrentConnections.WaitOne();
    377         while (jobData == null) { // repeat until success
    378           cancellationToken.ThrowIfCancellationRequested();
    379           try {
    380             jobData = ServiceLocator.Instance.CallHiveService(s => s.GetJobData(jobId));
    381           }
    382           catch (Exception e) {
    383             LogException(e);
    384             LogMessage("Repeating download");
    385           }
    386         }
    387         engineJob = PersistenceUtil.Deserialize<EngineJob>(jobData.Data);
    388         jobData = null;
    389         LogMessage(string.Format("Downloaded job #{0}", jobIndices[jobId] + 1, jobId));
    390       }
    391       finally {
    392         maxConcurrentConnections.Release();
    393         maxSerializedJobsInMemory.Release();
    394       }
    395       return engineJob;
    396     }
    397 
    398303    /// <summary>
    399304    /// Threadsafe message logging
    400305    /// </summary>
    401306    private void LogMessage(string message) {
    402       lock (Log) {
     307      lock (logLocker) {
    403308        Log.LogMessage(message);
    404309      }
     
    409314    /// </summary>
    410315    private void LogException(Exception exception) {
    411       lock (Log) {
     316      lock (logLocker) {
    412317        Log.LogException(exception);
    413318      }
    414319    }
    415320
    416     /// <summary>
    417     /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
    418     /// If repetitions is -1, it is repeated infinitely.
    419     /// </summary>
    420     private static void TryAndRepeat(Action action, int repetitions, string errorMessage) {
    421       try { action(); }
    422       catch (Exception e) {
    423         repetitions--;
    424         if (repetitions <= 0)
    425           throw new HiveEngineException(errorMessage, e);
    426         TryAndRepeat(action, repetitions, errorMessage);
    427       }
    428     }
    429   }
    430 
    431   public static class EnumerableExtensions {
    432     public static TimeSpan Sum(this IEnumerable<TimeSpan> times) {
    433       return TimeSpan.FromMilliseconds(times.Select(e => e.TotalMilliseconds).Sum());
    434     }
     321    // testfunction:
     322    //private IScope[] ExecuteLocally(EngineJob[] jobs, IScope parentScopeClone, CancellationToken cancellationToken) {
     323    //  IScope[] scopes = new Scope[jobs.Length];
     324    //  for (int i = 0; i < jobs.Length; i++) {
     325    //    var serialized = PersistenceUtil.Serialize(jobs[i]);
     326    //    var deserialized = PersistenceUtil.Deserialize<IJob>(serialized);
     327    //    deserialized.Start();
     328    //    while (deserialized.ExecutionState != ExecutionState.Stopped) {
     329    //      Thread.Sleep(100);
     330    //    }
     331    //    var serialized2 = PersistenceUtil.Serialize(deserialized);
     332    //    var deserialized2 = PersistenceUtil.Deserialize<EngineJob>(serialized2);
     333    //    var newScope = ((IAtomicOperation)deserialized2.InitialOperation).Scope;
     334    //    scopes[i] = newScope;
     335    //  }
     336    //  return scopes;
     337    //}
    435338  }
    436339}
Note: See TracChangeset for help on using the changeset viewer.