Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6178


Ignore:
Timestamp:
05/10/11 17:58:59 (12 years ago)
Author:
cneumuel
Message:

#1233

  • added semaphores to ensure an appdomain is never unloaded when the start method has not finished
  • HiveEngine uploading and downloading of jobs works and is displayed in the view
Location:
branches/HeuristicLab.Hive-3.4
Files:
4 added
2 deleted
20 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/Core.cs

    r6175 r6178  
    4747    private Dictionary<Guid, Executor> executors = new Dictionary<Guid, Executor>();
    4848    private Dictionary<Guid, AppDomain> appDomains = new Dictionary<Guid, AppDomain>();
     49
     50    // signalizes if the Executor.Start method has properly finished. only then the appdomain may be unloaded
     51    private Dictionary<Guid, Semaphore> semaphores = new Dictionary<Guid, Semaphore>();
    4952
    5053    private WcfService wcfService;
     
    484487            executor.MemoryNeeded = job.MemoryNeeded;
    485488            clientCom.LogMessage("Starting Executor for job " + job.Id);
    486             executor.Start(jobData.Data);
    487489            lock (executors) {
    488490              executors.Add(job.Id, executor);
    489491            }
     492            semaphores[job.Id] = new Semaphore(0, 1);
     493            executor.Start(jobData.Data);
     494            semaphores[job.Id].Release();
    490495          }
    491496          catch (Exception exception) {
     
    535540    /// Kill a appdomain with a specific id.
    536541    /// </summary>
    537     /// <param name="id">the GUID of the job</param>   
    538     public void KillAppDomain(Guid id) {
     542    /// <param name="jobId">the GUID of the job</param>   
     543    public void KillAppDomain(Guid jobId) {
    539544      if (Thread.CurrentThread.ManagedThreadId != this.coreThreadId) {
    540         EnqueueExecutorMessage<Guid>(KillAppDomain, id);
     545        EnqueueExecutorMessage<Guid>(KillAppDomain, jobId);
    541546        return;
    542547      }
    543548
    544       clientCom.LogMessage("Shutting down Appdomain for Job " + id);
     549      clientCom.LogMessage("Shutting down Appdomain for Job " + jobId);
    545550      lock (executors) {
    546551        try {
    547           if (executors.ContainsKey(id)) {
    548             executors[id].Dispose();
    549             executors.Remove(id);
    550           }
    551 
    552           if (appDomains.ContainsKey(id)) {
    553             appDomains[id].UnhandledException -= new UnhandledExceptionEventHandler(AppDomain_UnhandledException);
    554 
     552          if (executors.ContainsKey(jobId)) {
     553            executors[jobId].Dispose();
     554            executors.Remove(jobId);
     555          }
     556
     557          if (appDomains.ContainsKey(jobId)) {
     558            appDomains[jobId].UnhandledException -= new UnhandledExceptionEventHandler(AppDomain_UnhandledException);
    555559            int repeat = 5;
    556560            while (repeat > 0) {
    557561              try {
    558                 AppDomain.Unload(appDomains[id]);
     562                semaphores[jobId].WaitOne();
     563                AppDomain.Unload(appDomains[jobId]);
     564                semaphores[jobId].Dispose();
     565                semaphores.Remove(jobId);
    559566                repeat = 0;
    560567              }
     
    569576              }
    570577            }
    571             appDomains.Remove(id);
    572           }
    573 
    574           PluginCache.Instance.DeletePluginsForJob(id);
     578            appDomains.Remove(jobId);
     579          }
     580
     581          PluginCache.Instance.DeletePluginsForJob(jobId);
    575582          GC.Collect();
    576583        }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/Executor.cs

    r6168 r6178  
    8989        } else {
    9090          Job.Start();
    91           startJobSem.WaitOne();
     91          if (!startJobSem.WaitOne(TimeSpan.FromSeconds(15))) {
     92            throw new TimeoutException("Timeout when starting the job. JobStarted event was not fired.");
     93          }
    9294        }
    9395      }
    9496      catch (Exception e) {
    9597        this.currentException = e;
    96         Job_JobFailed(this, new HeuristicLab.Common.EventArgs<Exception>(e));
     98        Job_JobFailed(this, new EventArgs<Exception>(e));
    9799      }
    98100    }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/PluginCache.cs

    r6107 r6178  
    278278    internal void DeletePluginsForJob(Guid id) {
    279279      try {
    280         SlaveClientCom.Instance.ClientCom.LogMessage("unloading...");
     280        SlaveClientCom.Instance.ClientCom.LogMessage("Deleting plugins...");
    281281        int tries = 5;
    282282        while (tries > 0) {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/HiveJobView.cs

    r6168 r6178  
    6161      base.OnContentChanged();
    6262      if (Content != null && Content.Job != null) {
    63         computeInParallelCheckBox.Checked = Content.JobItem.ComputeInParallel;
     63        computeInParallelCheckBox.Checked = Content.ItemJob.ComputeInParallel;
    6464      } else {
    6565        computeInParallelCheckBox.Checked = false;
     
    7272    protected virtual void RegisterJobEvents() {
    7373      if (Content != null && Content.Job != null) {
    74         Content.JobItem.ComputeInParallelChanged += new EventHandler(OptimizerJob_ComputeInParallelChanged);
    75         Content.JobItem.ItemChanged += new EventHandler(Job_ItemChanged);
     74        Content.ItemJob.ComputeInParallelChanged += new EventHandler(OptimizerJob_ComputeInParallelChanged);
     75        Content.ItemJob.ItemChanged += new EventHandler(Job_ItemChanged);
    7676      }
    7777    }
     
    7979    protected virtual void DeregisterJobEvents() {
    8080      if (Content != null && Content.Job != null) {
    81         Content.JobItem.ComputeInParallelChanged -= new EventHandler(OptimizerJob_ComputeInParallelChanged);
    82         Content.JobItem.ItemChanged -= new EventHandler(Job_ItemChanged);
     81        Content.ItemJob.ComputeInParallelChanged -= new EventHandler(OptimizerJob_ComputeInParallelChanged);
     82        Content.ItemJob.ItemChanged -= new EventHandler(Job_ItemChanged);
    8383      }
    8484    }
     
    111111
    112112    protected virtual void Job_ItemChanged(object sender, EventArgs e) {
    113       if (Content != null && Content.Job != null && Content.JobItem.Item != null) {
    114         optimizerItemView.Content = Content.JobItem.Item;
     113      if (Content != null && Content.Job != null && Content.ItemJob.Item != null) {
     114        optimizerItemView.Content = Content.ItemJob.Item;
    115115      } else {
    116116        optimizerItemView.Content = null;
     
    129129          this.exceptionTextBox.Text = Content.Job.CurrentStateLog != null ? Content.Job.CurrentStateLog.Exception : string.Empty;
    130130          this.lastUpdatedTextBox.Text = Content.Job.LastJobDataUpdate.ToString();
    131           if (Content.JobItem.ComputeInParallel) {
     131          if (Content.ItemJob.ComputeInParallel) {
    132132            this.stateLogViewHost.Content = new StateLogListList(
    133133                this.Content.ChildHiveJobs.Select(child => new StateLogList(child.Job.StateLog)
     
    165165      this.coresNeededTextBox.ReadOnly = this.ReadOnly;
    166166      this.memoryNeededTextBox.ReadOnly = this.ReadOnly;
    167       this.computeInParallelCheckBox.Enabled = !this.ReadOnly && this.Content != null && this.Content.JobItem != null && this.Content.JobItem.IsParallelizable;
    168 
    169       this.modifyItemButton.Enabled = (Content != null && Content.JobItem.Item != null && (Content.Job.State == JobState.Paused || Content.Job.State == JobState.Offline || Content.Job.State == JobState.Finished || Content.Job.State == JobState.Failed || Content.Job.State == JobState.Aborted));
     167      this.computeInParallelCheckBox.Enabled = !this.ReadOnly && this.Content != null && this.Content.ItemJob != null && this.Content.ItemJob.IsParallelizable;
     168
     169      this.modifyItemButton.Enabled = (Content != null && Content.ItemJob.Item != null && (Content.Job.State == JobState.Paused || Content.Job.State == JobState.Offline || Content.Job.State == JobState.Finished || Content.Job.State == JobState.Failed || Content.Job.State == JobState.Aborted));
    170170
    171171      optimizerItemView.ReadOnly = true;
     
    176176        Invoke(new EventHandler(OptimizerJob_ComputeInParallelChanged), sender, e);
    177177      } else {
    178         computeInParallelCheckBox.Checked = Content.JobItem.ComputeInParallel;
     178        computeInParallelCheckBox.Checked = Content.ItemJob.ComputeInParallel;
    179179      }
    180180    }
     
    183183    #region Child Control Events
    184184     protected virtual void computeInParallelCheckBox_CheckedChanged(object sender, EventArgs e) {
    185       if (Content != null && Content.JobItem != null) {
    186         this.Content.JobItem.ComputeInParallel = this.computeInParallelCheckBox.Checked;
     185      if (Content != null && Content.ItemJob != null) {
     186        this.Content.ItemJob.ComputeInParallel = this.computeInParallelCheckBox.Checked;
    187187      }
    188188    }
     
    196196
    197197    protected virtual void modifyItemButton_Click(object sender, EventArgs e) {
    198       MainFormManager.MainForm.ShowContent(Content.JobItem.Item);
     198      MainFormManager.MainForm.ShowContent(Content.ItemJob.Item);
    199199    }
    200200    #endregion
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/OptimizerHiveJobView.cs

    r6168 r6178  
    4343
    4444    protected override void Job_ItemChanged(object sender, EventArgs e) {
    45       if (Content != null && Content.Job != null && Content.JobItem.Item != null) {
    46         runCollectionViewHost.Content = Content.JobItem.Item.Runs;
     45      if (Content != null && Content.Job != null && Content.ItemJob.Item != null) {
     46        runCollectionViewHost.Content = Content.ItemJob.Item.Runs;
    4747      } else {
    4848        runCollectionViewHost.Content = null;
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/RefreshableHiveExperimentView.cs

    r6033 r6178  
    5555    }
    5656
    57     protected override void DeregisterContentEvents() {
    58       Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
    59       Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
    60       Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged);     
    61       base.DeregisterContentEvents();
    62     }
    63 
    6457    protected override void RegisterContentEvents() {
    6558      base.RegisterContentEvents();
     
    6760      Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
    6861      Content.HiveExperimentChanged += new EventHandler(Content_HiveExperimentChanged);
     62      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
     63    }
     64    protected override void DeregisterContentEvents() {
     65      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
     66      Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
     67      Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged);
     68      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
     69      base.DeregisterContentEvents();
    6970    }
    7071
     
    134135        bool jobsLoaded = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.All(x => x.Job.Id != Guid.Empty);
    135136
    136         this.nameTextBox.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
    137         this.resourceNamesTextBox.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
    138         this.jobsTreeView.ReadOnly = Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
    139         this.useLocalPluginsCheckBox.Enabled = !(Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded);
    140         this.refreshAutomaticallyCheckBox.Enabled = alreadyUploaded && jobsLoaded && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
    141         this.refreshButton.Enabled = alreadyUploaded;
    142 
    143         this.Locked = Content.HiveExperiment.ExecutionState == ExecutionState.Started;
     137        this.nameTextBox.ReadOnly =                 !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
     138        this.resourceNamesTextBox.ReadOnly =        !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
     139        this.jobsTreeView.ReadOnly =                !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
     140
     141        this.useLocalPluginsCheckBox.Enabled =      Content.IsControllable && !(Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded);
     142        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
     143        this.refreshButton.Enabled =                Content.IsControllable && alreadyUploaded;
     144        this.Locked =                               !Content.IsControllable || Content.HiveExperiment.ExecutionState == ExecutionState.Started;
    144145      }
    145146      SetEnabledStateOfExecutableButtons();
     
    246247      }
    247248    }
     249    private void Content_IsControllableChanged(object sender, EventArgs e) {
     250      SetEnabledStateOfControls();
     251    }
    248252    #endregion
    249253
     
    302306        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
    303307      } else {
    304         startButton.Enabled = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.Count > 0 && Content.HiveExperiment.ExecutionState == ExecutionState.Prepared;
    305         pauseButton.Enabled = Content.HiveExperiment.ExecutionState == ExecutionState.Started;
    306         stopButton.Enabled = Content.HiveExperiment.ExecutionState == ExecutionState.Started;
     308        startButton.Enabled = Content.IsControllable && Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.Count > 0 && Content.HiveExperiment.ExecutionState == ExecutionState.Prepared;
     309        pauseButton.Enabled = Content.IsControllable && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
     310        stopButton.Enabled = Content.IsControllable && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
    307311        resetButton.Enabled = false;
    308312      }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/TreeView/DeleteJobTreeNodeAction.cs

    r6033 r6178  
    2424        hiveJobs.Remove(item);
    2525      } else {
    26         var experiment = parentItem.JobItem.Item as Experiment;
     26        var experiment = parentItem.ItemJob.Item as Experiment;
    2727        if (experiment != null) {
    28           experiment.Optimizers.Remove(((OptimizerJob)item.JobItem).Item);
     28          experiment.Optimizers.Remove(((OptimizerJob)item.ItemJob).Item);
    2929        }
    3030      }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/TreeView/HiveJobItemTreeView.cs

    r6033 r6178  
    8181          Content.Add(new OptimizerHiveJob(optimizer));
    8282        } else {
    83           var experiment = ((HiveJob)treeView.SelectedNode.Tag).JobItem.Item as Experiment;
     83          var experiment = ((HiveJob)treeView.SelectedNode.Tag).ItemJob.Item as Experiment;
    8484          if (experiment != null) {
    8585            experiment.Optimizers.Add(optimizer);
     
    9696          Content.Remove((HiveJob)treeView.SelectedNode.Tag);
    9797        } else {
    98           var experiment = parentItem.JobItem.Item as Experiment;
     98          var experiment = parentItem.ItemJob.Item as Experiment;
    9999          if (experiment != null) {
    100             experiment.Optimizers.Remove(((OptimizerJob)selectedItem.JobItem).Item);
     100            experiment.Optimizers.Remove(((OptimizerJob)selectedItem.ItemJob).Item);
    101101          }
    102102        }       
     
    108108      var actions = base.GetTreeNodeItemActions(selectedItem);
    109109      if (selectedItem != null) {
    110         if (selectedItem.JobItem.Item is Experiment) {
     110        if (selectedItem.ItemJob.Item is Experiment) {
    111111
    112112        }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/TreeView/NewExperimentTreeNodeAction.cs

    r6033 r6178  
    4343    public void Execute(HiveJob item, HiveJob parentItem) {
    4444      if (item != null) {
    45         var experiment = item.JobItem.Item as Experiment;
     45        var experiment = item.ItemJob.Item as Experiment;
    4646        experiment.Optimizers.Add(new Experiment());
    4747      } else {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/ExperimentManagerClient.cs

    r6111 r6178  
    274274      List<IPluginDescription> plugins;
    275275
    276       if (hiveJob.JobItem.ComputeInParallel &&
    277         (hiveJob.JobItem.Item is Optimization.Experiment || hiveJob.JobItem.Item is Optimization.BatchRun)) {
     276      if (hiveJob.ItemJob.ComputeInParallel &&
     277        (hiveJob.ItemJob.Item is Optimization.Experiment || hiveJob.ItemJob.Item is Optimization.BatchRun)) {
    278278        hiveJob.Job.IsParentJob = true;
    279279        hiveJob.Job.FinishWhenChildJobsFinished = true;
    280         hiveJob.JobItem.CollectChildJobs = false; // don't collect child-jobs on slaves
     280        hiveJob.ItemJob.CollectChildJobs = false; // don't collect child-jobs on slaves
    281281        jobData = hiveJob.GetAsJobData(true, out plugins);
    282282      } else {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJob.cs

    r6168 r6178  
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.Drawing;
    2526using System.Linq;
     
    5556    }
    5657
     58    [Storable]
    5759    protected Job job;
    5860    public Job Job {
     
    6062      set {
    6163        if (job != value) {
     64          DeregisterJobEvents();
    6265          job = value;
     66          RegisterJobEvents();
     67          itemJobDownloaded = false;
    6368          OnJobChanged();
    6469          OnToStringChanged();
    6570          OnItemImageChanged();
    6671        }
     72       
    6773      }
    6874    }
    6975
    7076    [Storable]
    71     protected ItemJob jobItem;
    72     public ItemJob JobItem {
    73       get { return jobItem; }
     77    protected ItemJob itemJob;
     78    public ItemJob ItemJob {
     79      get { return itemJob; }
    7480      internal set {
    75         if (jobItem != null && syncJobsWithOptimizers) {
     81        if (itemJob != null && syncJobsWithOptimizers) {
    7682          this.childHiveJobs.Clear();
    7783        }
    78         if (jobItem != value) {
    79           DergisterJobItemsEvents();
    80           jobItem = value;
    81           RegisterJobItemEvents();
     84        if (itemJob != value) {
     85          DergisterItemJobEvents();
     86          itemJob = value;
     87          RegisterItemJobEvents();
    8288          OnJobItemChanged();
    83         }
    84       }
     89          itemJobDownloaded = true;
     90        }
     91      }
     92    }
     93
     94    // job downloaded since last status change
     95    [Storable]
     96    private bool itemJobDownloaded = false;
     97    public bool ItemJobDownloaded {
     98      get { return itemJobDownloaded; }
    8599    }
    86100
     
    106120    }
    107121
    108     public HiveJob(ItemJob jobItem, bool autoCreateChildHiveJobs)
     122    public HiveJob(ItemJob itemJob, bool autoCreateChildHiveJobs)
    109123      : this() {
    110124      this.syncJobsWithOptimizers = autoCreateChildHiveJobs;
    111       this.JobItem = jobItem;
     125      this.ItemJob = itemJob;
    112126      this.syncJobsWithOptimizers = true;
    113127    }
     
    117131      this.Job = job;
    118132      try {
    119         this.JobItem = PersistenceUtil.Deserialize<ItemJob>(jobData.Data);
     133        this.ItemJob = PersistenceUtil.Deserialize<ItemJob>(jobData.Data);
    120134      }
    121135      catch {
    122         this.JobItem = null;
     136        this.ItemJob = null;
    123137      }
    124138      this.childHiveJobs = new ItemList<HiveJob>();
     
    130144      : base(original, cloner) {
    131145      this.Job = cloner.Clone(original.job);
    132       this.JobItem = cloner.Clone(original.JobItem);
     146      this.ItemJob = cloner.Clone(original.ItemJob);
    133147      this.childHiveJobs = cloner.Clone(original.childHiveJobs);
    134148      this.syncJobsWithOptimizers = original.syncJobsWithOptimizers;
     149      this.itemJobDownloaded = original.itemJobDownloaded;
    135150    }
    136151    public override IDeepCloneable Clone(Cloner cloner) {
     
    141156    protected virtual void UpdateChildHiveJobs() { }
    142157
    143     protected virtual void RegisterJobItemEvents() {
    144       if (JobItem != null) {
    145         JobItem.ComputeInParallelChanged += new EventHandler(JobItem_ComputeInParallelChanged);
    146         JobItem.ToStringChanged += new EventHandler(JobItem_ToStringChanged);
    147       }
    148     }
    149     protected virtual void DergisterJobItemsEvents() {
    150       if (JobItem != null) {
    151         JobItem.ComputeInParallelChanged -= new EventHandler(JobItem_ComputeInParallelChanged);
    152         JobItem.ToStringChanged -= new EventHandler(JobItem_ToStringChanged);
     158    protected virtual void RegisterItemJobEvents() {
     159      if (ItemJob != null) {
     160        ItemJob.ComputeInParallelChanged += new EventHandler(JobItem_ComputeInParallelChanged);
     161        ItemJob.ToStringChanged += new EventHandler(JobItem_ToStringChanged);
     162      }
     163    }
     164    protected virtual void DergisterItemJobEvents() {
     165      if (ItemJob != null) {
     166        ItemJob.ComputeInParallelChanged -= new EventHandler(JobItem_ComputeInParallelChanged);
     167        ItemJob.ToStringChanged -= new EventHandler(JobItem_ToStringChanged);
    153168      }
    154169    }
     
    168183      this.OnToStringChanged();
    169184    }
    170    
     185
    171186    protected virtual void JobItem_ComputeInParallelChanged(object sender, EventArgs e) {
    172       if (JobItem != null && syncJobsWithOptimizers) {
     187      if (ItemJob != null && syncJobsWithOptimizers) {
    173188        this.UpdateChildHiveJobs();
    174189      }
     
    180195
    181196    public override string ToString() {
    182       if (jobItem != null) {
    183         return jobItem.ToString();
     197      if (itemJob != null) {
     198        return itemJob.ToString();
    184199      } else {
    185200        return base.ToString();
     
    196211        job.Command = lightweightJob.Command;
    197212        job.LastJobDataUpdate = lightweightJob.LastJobDataUpdate;
    198        
     213
    199214        OnJobStateChanged();
    200215        OnToStringChanged();
     
    210225    /// </param>
    211226    public virtual JobData GetAsJobData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) {
    212       plugins = null;
    213       return null;
     227      plugins = new List<IPluginDescription>();
     228      if (this.itemJob == null)
     229        return null;
     230
     231      IEnumerable<Type> usedTypes;
     232      byte[] jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
     233
     234      JobData jobData = new JobData() {
     235        JobId = job.Id,
     236        Data = jobByteArray
     237      };
     238
     239      PluginUtil.CollectDeclaringPlugins(plugins, usedTypes);
     240
     241      return jobData;
    214242    }
    215243
     
    239267      if (handler != null) handler(this, EventArgs.Empty);
    240268    }
     269
     270    private void RegisterJobEvents() {
     271      if (job != null)
     272        job.PropertyChanged += new PropertyChangedEventHandler(job_PropertyChanged);
     273    }
     274
     275    private void DeregisterJobEvents() {
     276      if (job != null)
     277        job.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(job_PropertyChanged);
     278    }
     279
     280    private void job_PropertyChanged(object sender, PropertyChangedEventArgs e) {
     281      if (e.PropertyName == "State") {
     282        itemJobDownloaded = false;
     283      }
     284    }
    241285    #endregion
    242    
     286
    243287    /// <summary>
    244288    /// Returns a list of HiveJobs including this and all its child-jobs recursively
     
    326370      } else {
    327371        ServiceLocator.Instance.CallHiveService(s => s.PauseJob(this.job.Id));
    328       }     
     372      }
    329373    }
    330374
     
    343387        JobData jobData = new JobData();
    344388        jobData.JobId = this.job.Id;
    345         jobData.Data = PersistenceUtil.Serialize(this.jobItem);
     389        jobData.Data = PersistenceUtil.Serialize(this.itemJob);
    346390        service.UpdateJobData(this.Job, jobData);
    347391        service.RestartJob(this.job.Id);
     
    350394      });
    351395    }
    352    
     396
    353397    public ICollection<IItemTreeNodeAction<HiveJob>> Actions {
    354398      get {
     
    364408  public class HiveJob<T> : HiveJob where T : ItemJob {
    365409
    366     public new T JobItem {
    367       get { return (T)base.JobItem; }
    368       internal set { base.JobItem = value; }
     410    public new T ItemJob {
     411      get { return (T)base.ItemJob; }
     412      internal set { base.ItemJob = value; }
    369413    }
    370414
    371415    #region Constructors and Cloning
    372416    public HiveJob() : base() { }
     417    public HiveJob(T itemJob) : base(itemJob, true) { }
    373418    protected HiveJob(HiveJob original, Cloner cloner)
    374419      : base(original, cloner) {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJobDownloader.cs

    r6168 r6178  
    3030  public class HiveJobDownloader {
    3131    private IEnumerable<Guid> jobIds;
    32     private JobDownloader<ItemJob> jobDownloader;
     32    private ConcurrentJobDownloader<ItemJob> jobDownloader;
    3333    private IDictionary<Guid, HiveJob> results;
    3434
    3535    public bool IsFinished {
    3636      get {
    37         //return tasks.TrueForAll(t => t.Status == TaskStatus.RanToCompletion ||
    38         //                             t.Status == TaskStatus.Faulted ||
    39         //                             t.Status == TaskStatus.Canceled);
    4037        return results.Count == jobIds.Count();
    4138      }
     
    4441    public int FinishedCount {
    4542      get {
    46         //var faulted = tasks.Where(t => t.Status == TaskStatus.Faulted);
    47         //if (faulted.Count() > 0) {
    48         //  abort = true;
    49         //  throw faulted.First().Exception;
    50         //}
    51         //return tasks.Count(t => t.Status == TaskStatus.RanToCompletion ||
    52         //                        t.Status == TaskStatus.Faulted ||
    53         //                        t.Status == TaskStatus.Canceled);
    5443        return results.Count;
    5544      }
     
    5847    public IDictionary<Guid, HiveJob> Results {
    5948      get {
    60         //var results = new Dictionary<Guid, HiveJob>();
    61         //foreach (var t in tasks) {
    62         //  if (t.Status == TaskStatus.Faulted) {
    63         //    throw t.Exception;
    64         //  }
    65         //  if (t.Result != null)
    66         //    results.Add(t.Result.Job.Id, t.Result);
    67         //}
    6849        return results;
    6950      }
     
    7253    public HiveJobDownloader(IEnumerable<Guid> jobIds) {
    7354      this.jobIds = jobIds;
    74       this.jobDownloader = new JobDownloader<ItemJob>(2, 2);
     55      this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
    7556      this.results = new Dictionary<Guid, HiveJob>();
    7657    }
     
    8162          (id, itemJob, exception) => {
    8263            if (exception != null) {
    83               throw new JobDownloaderException("Downloading job failed", exception);
     64              throw new ConcurrentJobDownloaderException("Downloading job failed", exception);
    8465            }
    8566            Job job = ServiceLocator.Instance.CallHiveService(s => s.GetJob(id));
     
    9576            }
    9677          });
    97       }
    98 
    99       //tasks = new List<Task<HiveJob>>();
    100       //TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);
    101       //foreach (Guid jobId in jobIds) {
    102       //  tasks.Add(Task<JobData>.Factory.StartNew(
    103       //    (x) => DownloadJob(x), jobId)
    104       //    .ContinueWith((x) => DeserializeJob(x.Result)));
    105       //}
     78      }     
    10679    }
    107 
    108     //private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) {
    109     //  e.SetObserved(); // avoid crash of process because task crashes. first exception found is handled in Results property
    110     //}
    111 
    112     //// use semaphore to ensure only few concurrenct connections and few SerializedJob objects in memory
    113     //private Semaphore downloadSemaphore = new Semaphore(2, 2);
    114     //private Semaphore deserializeSemaphore = new Semaphore(2, 2);
    115     //protected JobData DownloadJob(object jobId) {
    116     //  downloadSemaphore.WaitOne();
    117     //  deserializeSemaphore.WaitOne();
    118     //  JobData result;
    119     //  try {
    120     //    if (abort) return null;
    121     //    result = ServiceLocator.Instance.CallHiveService(s => s.GetJobData((Guid)jobId));
    122     //  }
    123     //  finally {
    124     //    downloadSemaphore.Release();
    125     //  }
    126     //  return result;
    127     //}
    128 
    129     //protected HiveJob DeserializeJob(JobData jobData) {
    130     //  try {
    131     //    Job job = ServiceLocator.Instance.CallHiveService(s => s.GetJob(jobData.JobId));
    132     //    if (abort || job == null || jobData == null) return null;
    133 
    134     //    HiveJob hiveJob;
    135     //    var itemJob = PersistenceUtil.Deserialize<ItemJob>(jobData.Data);
    136     //    if (itemJob is OptimizerJob) {
    137     //      hiveJob = new OptimizerHiveJob((OptimizerJob)itemJob);
    138     //    } else {
    139     //      hiveJob = new HiveJob(itemJob, true);
    140     //    }
    141     //    jobData.Data = null; // reduce memory consumption.
    142     //    hiveJob.Job = job;
    143     //    return hiveJob;
    144     //  }
    145     //  finally {
    146     //    deserializeSemaphore.Release();
    147     //  }
    148     //}
    14980  }
    15081}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/OptimizerHiveJob.cs

    r6111 r6178  
    3737    public OptimizerHiveJob(IOptimizer optimizer)
    3838      : this() {
    39       this.JobItem = new OptimizerJob(optimizer);
     39      this.ItemJob = new OptimizerJob(optimizer);
    4040    }
    4141    public OptimizerHiveJob(OptimizerJob optimizerJob)
    4242      : this() {
    43       this.JobItem = optimizerJob;
     43      this.ItemJob = optimizerJob;
    4444    }
    4545    protected OptimizerHiveJob(OptimizerHiveJob original, Cloner cloner)
     
    6060      base.UpdateChildHiveJobs();
    6161      if (Job != null && syncJobsWithOptimizers) {
    62         if (!JobItem.ComputeInParallel) {
     62        if (!ItemJob.ComputeInParallel) {
    6363          this.childHiveJobs.Clear();
    6464        } else {
    65           if (JobItem.Item is Optimization.Experiment) {
    66             Optimization.Experiment experiment = (Optimization.Experiment)JobItem.Item;
     65          if (ItemJob.Item is Optimization.Experiment) {
     66            Optimization.Experiment experiment = (Optimization.Experiment)ItemJob.Item;
    6767            foreach (IOptimizer childOpt in experiment.Optimizers) {
    6868              this.childHiveJobs.Add(new OptimizerHiveJob(childOpt));
    6969            }
    70           } else if (JobItem.Item is Optimization.BatchRun) {
    71             Optimization.BatchRun batchRun = JobItem.OptimizerAsBatchRun;
     70          } else if (ItemJob.Item is Optimization.BatchRun) {
     71            Optimization.BatchRun batchRun = ItemJob.OptimizerAsBatchRun;
    7272            if (batchRun.Optimizer != null) {
    7373              while (this.childHiveJobs.Count < batchRun.Repetitions) {
     
    8383    }
    8484
    85     protected override void RegisterJobItemEvents() {
    86       base.RegisterJobItemEvents();
    87       if (JobItem != null) {
    88         if (JobItem.Item is Optimization.Experiment) {
    89           Optimization.Experiment experiment = JobItem.OptimizerAsExperiment;
     85    protected override void RegisterItemJobEvents() {
     86      base.RegisterItemJobEvents();
     87      if (ItemJob != null) {
     88        if (ItemJob.Item is Optimization.Experiment) {
     89          Optimization.Experiment experiment = ItemJob.OptimizerAsExperiment;
    9090          experiment.Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
    9191          experiment.Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
    9292          experiment.Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
    9393          experiment.Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
    94         } else if (JobItem.Item is Optimization.BatchRun) {
    95           Optimization.BatchRun batchRun = JobItem.OptimizerAsBatchRun;
     94        } else if (ItemJob.Item is Optimization.BatchRun) {
     95          Optimization.BatchRun batchRun = ItemJob.OptimizerAsBatchRun;
    9696          batchRun.RepetitionsChanged += new EventHandler(batchRun_RepetitionsChanged);
    9797          batchRun.OptimizerChanged += new EventHandler(batchRun_OptimizerChanged);
     
    9999      }
    100100    }
    101     protected override void DergisterJobItemsEvents() {
    102       base.DergisterJobItemsEvents();
    103       if (JobItem != null) {
    104         if (JobItem.Item is Optimization.Experiment) {
    105           Optimization.Experiment experiment = JobItem.OptimizerAsExperiment;
     101    protected override void DergisterItemJobEvents() {
     102      base.DergisterItemJobEvents();
     103      if (ItemJob != null) {
     104        if (ItemJob.Item is Optimization.Experiment) {
     105          Optimization.Experiment experiment = ItemJob.OptimizerAsExperiment;
    106106          experiment.Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
    107107          experiment.Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
    108108          experiment.Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
    109109          experiment.Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
    110         } else if (JobItem.Item is Optimization.BatchRun) {
    111           Optimization.BatchRun batchRun = JobItem.OptimizerAsBatchRun;
     110        } else if (ItemJob.Item is Optimization.BatchRun) {
     111          Optimization.BatchRun batchRun = ItemJob.OptimizerAsBatchRun;
    112112          batchRun.RepetitionsChanged -= new EventHandler(batchRun_RepetitionsChanged);
    113113          batchRun.OptimizerChanged -= new EventHandler(batchRun_OptimizerChanged);
     
    129129
    130130    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
    131       if (syncJobsWithOptimizers && this.JobItem.ComputeInParallel) {
     131      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
    132132        foreach (var item in e.Items) {
    133133          if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
     
    138138    }
    139139    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
    140       if (syncJobsWithOptimizers && this.JobItem.ComputeInParallel) {
     140      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
    141141        foreach (var item in e.OldItems) {
    142142          this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
     
    150150    }
    151151    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
    152       if (syncJobsWithOptimizers && this.JobItem.ComputeInParallel) {
     152      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
    153153        foreach (var item in e.Items) {
    154154          this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
     
    157157    }
    158158    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
    159       if (syncJobsWithOptimizers && this.JobItem.ComputeInParallel) {
     159      if (syncJobsWithOptimizers && this.ItemJob.ComputeInParallel) {
    160160        foreach (var item in e.Items) {
    161161          this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
     
    174174      syncJobsWithOptimizers = false; // don't sync with optimizers during this method
    175175
    176       if (this.JobItem != null && this.JobItem.Item != null) {
    177         if (this.JobItem.Item is Optimization.Experiment) {
    178           UpdateOptimizerInExperiment(this.JobItem.OptimizerAsExperiment, optimizerJob);
    179         } else if (this.JobItem.Item is Optimization.BatchRun) {
    180           UpdateOptimizerInBatchRun(this.JobItem.OptimizerAsBatchRun, optimizerJob);
     176      if (this.ItemJob != null && this.ItemJob.Item != null) {
     177        if (this.ItemJob.Item is Optimization.Experiment) {
     178          UpdateOptimizerInExperiment(this.ItemJob.OptimizerAsExperiment, optimizerJob);
     179        } else if (this.ItemJob.Item is Optimization.BatchRun) {
     180          UpdateOptimizerInBatchRun(this.ItemJob.OptimizerAsBatchRun, optimizerJob);
    181181        }
    182182      }
     
    185185      if (!optimizerJob.ComputeInParallel) {
    186186        child.syncJobsWithOptimizers = false;
    187         child.JobItem = optimizerJob;
     187        child.ItemJob = optimizerJob;
    188188        child.syncJobsWithOptimizers = true;
    189189      }
     
    236236    internal void SetIndexInParentOptimizerList(OptimizerHiveJob parentHiveJob) {
    237237      if (parentHiveJob != null) {
    238         if (parentHiveJob.JobItem.Item is Optimization.Experiment) {
    239           this.JobItem.IndexInParentOptimizerList = parentHiveJob.JobItem.OptimizerAsExperiment.Optimizers.IndexOf(this.JobItem.Item);
    240         } else if (parentHiveJob.JobItem.Item is Optimization.BatchRun) {
    241           this.JobItem.IndexInParentOptimizerList = 0;
     238        if (parentHiveJob.ItemJob.Item is Optimization.Experiment) {
     239          this.ItemJob.IndexInParentOptimizerList = parentHiveJob.ItemJob.OptimizerAsExperiment.Optimizers.IndexOf(this.ItemJob.Item);
     240        } else if (parentHiveJob.ItemJob.Item is Optimization.BatchRun) {
     241          this.ItemJob.IndexInParentOptimizerList = 0;
    242242        } else {
    243243          throw new NotSupportedException("Only Experiment and BatchRuns are supported");
     
    253253      var optimizerHiveJob = (OptimizerHiveJob)hiveJob;
    254254      syncJobsWithOptimizers = false;
    255       if (this.JobItem != null && optimizerHiveJob.JobItem != null) {
     255      if (this.ItemJob != null && optimizerHiveJob.ItemJob != null) {
    256256        // if job is in state Paused, it has to preserve its ResultCollection, which is cleared when a optimizer is added to an experiment
    257257        OptimizerJob optimizerJobClone = null;
    258258        if (optimizerHiveJob.Job.State == JobState.Paused) {
    259           optimizerJobClone = (OptimizerJob)optimizerHiveJob.JobItem.Clone();
    260         }
    261 
    262         if (this.JobItem.Item is Optimization.Experiment) {
    263           if (!this.JobItem.OptimizerAsExperiment.Optimizers.Contains(optimizerHiveJob.JobItem.Item)) {
    264             UpdateOptimizerInExperiment(this.JobItem.OptimizerAsExperiment, optimizerHiveJob.JobItem);
     259          optimizerJobClone = (OptimizerJob)optimizerHiveJob.ItemJob.Clone();
     260        }
     261
     262        if (this.ItemJob.Item is Optimization.Experiment) {
     263          if (!this.ItemJob.OptimizerAsExperiment.Optimizers.Contains(optimizerHiveJob.ItemJob.Item)) {
     264            UpdateOptimizerInExperiment(this.ItemJob.OptimizerAsExperiment, optimizerHiveJob.ItemJob);
    265265          }
    266         } else if (this.JobItem.Item is Optimization.BatchRun) {
    267           UpdateOptimizerInBatchRun(this.JobItem.OptimizerAsBatchRun, optimizerHiveJob.JobItem);
     266        } else if (this.ItemJob.Item is Optimization.BatchRun) {
     267          UpdateOptimizerInBatchRun(this.ItemJob.OptimizerAsBatchRun, optimizerHiveJob.ItemJob);
    268268        }
    269269
    270270        if (optimizerHiveJob.Job.State == JobState.Paused) {
    271           optimizerHiveJob.JobItem = optimizerJobClone;
     271          optimizerHiveJob.ItemJob = optimizerJobClone;
    272272        }
    273273      }
     
    283283    public override JobData GetAsJobData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) {
    284284      plugins = new List<IPluginDescription>();
    285       if (this.jobItem == null) // || this.jobItem.Optimizer == null
     285      if (this.itemJob == null) // || this.jobItem.Optimizer == null
    286286        return null;
    287287
    288288      IEnumerable<Type> usedTypes;
    289289      byte[] jobByteArray;
    290       if (withoutChildOptimizers && this.JobItem.Item is Optimization.Experiment) {
    291         OptimizerJob clonedJob = (OptimizerJob)this.JobItem.Clone(); // use a cloned job, so that the childHiveJob don't get confused
     290      if (withoutChildOptimizers && this.ItemJob.Item is Optimization.Experiment) {
     291        OptimizerJob clonedJob = (OptimizerJob)this.ItemJob.Clone(); // use a cloned job, so that the childHiveJob don't get confused
    292292        clonedJob.OptimizerAsExperiment.Optimizers.Clear();
    293293        jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
    294       } else if (withoutChildOptimizers && this.JobItem.Item is Optimization.BatchRun) {
    295         OptimizerJob clonedJob = (OptimizerJob)this.JobItem.Clone();
     294      } else if (withoutChildOptimizers && this.ItemJob.Item is Optimization.BatchRun) {
     295        OptimizerJob clonedJob = (OptimizerJob)this.ItemJob.Clone();
    296296        clonedJob.OptimizerAsBatchRun.Optimizer = null;
    297297        jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes);
    298       } else if (this.JobItem.Item is IAlgorithm) {
    299         ((IAlgorithm)this.JobItem.Item).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size
    300         jobByteArray = PersistenceUtil.Serialize(this.JobItem, out usedTypes);
     298      } else if (this.ItemJob.Item is IAlgorithm) {
     299        ((IAlgorithm)this.ItemJob.Item).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size
     300        jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
    301301      } else {
    302         jobByteArray = PersistenceUtil.Serialize(this.JobItem, out usedTypes);
     302        jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
    303303      }
    304304
     
    315315    public OptimizerHiveJob GetChildByOptimizerJob(OptimizerJob optimizerJob) {
    316316      foreach (OptimizerHiveJob child in ChildHiveJobs) {
    317         if (child.JobItem == optimizerJob)
     317        if (child.ItemJob == optimizerJob)
    318318          return child;
    319319      }
     
    323323    public HiveJob<OptimizerJob> GetChildByOptimizer(IOptimizer optimizer) {
    324324      foreach (OptimizerHiveJob child in ChildHiveJobs) {
    325         if (child.JobItem.Item == optimizer)
     325        if (child.ItemJob.Item == optimizer)
    326326          return child;
    327327      }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/RefreshableHiveExperiment.cs

    r6168 r6178  
    3434  public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter {
    3535    private JobResultPoller jobResultPoller;
    36     private JobDownloader<ItemJob> jobDownloader = new JobDownloader<ItemJob>(2, 2);
     36    private ConcurrentJobDownloader<ItemJob> jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
    3737    private static object locker = new object();
    3838
     
    8383    }
    8484
     85    [Storable]
     86    private bool isControllable = true;
     87    public bool IsControllable {
     88      get { return isControllable; }
     89      set {
     90        if (value != isControllable) {
     91          isControllable = value;
     92          OnIsControllableChanged();
     93        }
     94      }
     95    }
     96
    8597    #region Constructors and Cloning
    8698    public RefreshableHiveExperiment() {
     
    99111      this.RefreshAutomatically = original.RefreshAutomatically;
    100112      this.IncludeJobs = original.IncludeJobs;
     113      this.IsControllable = original.IsControllable;
    101114    }
    102115    public IDeepCloneable Clone(Cloner cloner) {
     
    163176            jobDownloader.DownloadJob(hj.Job.Id, (id, itemJob, exception) => {
    164177              if (exception != null) {
    165                 throw new JobDownloaderException("Downloading job failed.", exception);
     178                throw new ConcurrentJobDownloaderException("Downloading job failed.", exception);
    166179              }
    167180
     
    171184                // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
    172185                if (hj.Job.State == JobState.Paused) {
    173                   hj.JobItem = itemJob;
     186                  hj.ItemJob = itemJob;
    174187                } else {
    175188                  if (lightweightJob.ParentJobId.HasValue) {
     
    177190                    parentHiveJob.IntegrateChild(itemJob, hj.Job.Id);
    178191                  } else {
    179                     hj.JobItem = itemJob;
     192                    hj.ItemJob = itemJob;
    180193                  }
    181194                }
     
    211224
    212225    public bool AllJobsFinished() {
    213       return hiveExperiment.GetAllHiveJobs().All(j => j.Job.State == JobState.Finished
    214                                             || j.Job.State == JobState.Aborted
    215                                             || j.Job.State == JobState.Failed);
     226      return hiveExperiment.GetAllHiveJobs().All(j => (j.Job.State == JobState.Finished
     227                                                   || j.Job.State == JobState.Aborted
     228                                                   || j.Job.State == JobState.Failed)
     229                                                   && j.ItemJobDownloaded);
    216230    }
    217231
     
    299313      if (handler != null) handler(sender, e);
    300314    }
     315
     316    public event EventHandler IsControllableChanged;
     317    private void OnIsControllableChanged() {
     318      var handler = IsControllableChanged;
     319      if (handler != null) handler(this, EventArgs.Empty);
     320    }
    301321    #endregion
    302322
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/HeuristicLab.Clients.Hive-3.4.csproj

    r6168 r6178  
    120120    <Compile Include="Exceptions\JobResultPollingException.cs" />
    121121    <Compile Include="Exceptions\OptimizerNotFoundException.cs" />
    122     <Compile Include="ExperimentManager\JobDownloader.cs" />
     122    <Compile Include="ExperimentManager\ConcurrentJobDownloader.cs" />
     123    <Compile Include="ExperimentManager\EngineHiveJob.cs" />
    123124    <Compile Include="ExperimentManager\ExperimentManagerClient.cs" />
    124125    <Compile Include="ExperimentManager\HiveJobDownloader.cs" />
    125     <Compile Include="ExperimentManager\JobDownloaderException.cs" />
     126    <Compile Include="ExperimentManager\ConcurrentJobDownloaderException.cs" />
    126127    <Compile Include="ExperimentManager\TreeView\IItemTree.cs" />
    127128    <Compile Include="ExperimentManager\TreeView\IItemTreeAction.cs" />
     
    145146    <None Include="Properties\AssemblyInfo.cs.frame" />
    146147    <Compile Include="Exceptions\ServiceClientFactoryException.cs" />
     148    <Compile Include="ScopeExtensions.cs" />
    147149    <Compile Include="ServiceClients\Appointment.cs" />
    148150    <Compile Include="ServiceClients\HiveItemCollection.cs" />
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Jobs/EngineJob.cs

    r6111 r6178  
    7272
    7373    protected override void RegisterItemEvents() {
     74      base.RegisterItemEvents();
    7475      Item.Stopped += new EventHandler(engine_Stopped);
     76      Item.Paused += new EventHandler(Item_Paused);
     77      Item.Started += new EventHandler(Item_Started);
    7578      Item.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(engine_ExceptionOccurred);
     79      Item.ExecutionStateChanged += new EventHandler(Item_ExecutionStateChanged);
     80      Item.ExecutionTimeChanged += new EventHandler(Item_ExecutionTimeChanged);
    7681    }
    7782
    7883    protected override void DeregisterItemEvents() {
    7984      Item.Stopped -= new EventHandler(engine_Stopped);
     85      Item.Paused -= new EventHandler(Item_Paused);
     86      Item.Started -= new EventHandler(Item_Started);
    8087      Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(engine_ExceptionOccurred);
     88      Item.ExecutionStateChanged -= new EventHandler(Item_ExecutionStateChanged);
     89      Item.ExecutionTimeChanged -= new EventHandler(Item_ExecutionTimeChanged);
     90      base.DeregisterItemEvents();
    8191    }
    8292
     
    8797    private void engine_Stopped(object sender, EventArgs e) {
    8898      OnJobStopped();
     99    }
     100
     101    private void Item_Paused(object sender, EventArgs e) {
     102      OnJobPaused();
     103    }
     104
     105    private void Item_ExecutionTimeChanged(object sender, EventArgs e) {
     106      OnExecutionTimeChanged();
     107    }
     108
     109    private void Item_ExecutionStateChanged(object sender, EventArgs e) {
     110      OnExecutionStateChanged();
     111    }
     112
     113    private void Item_Started(object sender, EventArgs e) {
     114      OnJobStarted();
    89115    }
    90116
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.HiveEngine.Test/Program.cs

    r6110 r6178  
    4848      GeneticAlgorithm ga = new GeneticAlgorithm();
    4949      ga.Problem = new SingleObjectiveTestFunctionProblem();
    50       ga.Engine = new HiveEngine();     
    51       ga.PopulationSize.Value = 3;
     50      ga.Engine = new HiveEngine();
     51      ga.Elites.Value = 0;
     52      ga.PopulationSize.Value = 4;
    5253      ga.MaximumGenerations.Value = 3;
    5354
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.HiveEngine/3.4/HiveEngine.cs

    r6111 r6178  
    77using HeuristicLab.Common;
    88using HeuristicLab.Core;
     9using HeuristicLab.Hive;
    910using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    1011using HeuristicLab.PluginInfrastructure;
     
    140141
    141142            IScope[] scopes = ExecuteOnHive(jobs, parentScopeClone, cancellationToken);
    142            
     143            //IScope[] scopes = ExecuteLocally(jobs, parentScopeClone, cancellationToken);
     144
    143145            for (int i = 0; i < coll.Count; i++) {
    144146              if (coll[i] is IAtomicOperation) {
     
    203205
    204206    // testfunction:
    205     //private IScope[] ExecuteLocally(EngineJob[] jobs, IScope parentScopeClone, CancellationToken cancellationToken) {
    206     //  IScope[] scopes = new Scope[jobs.Length];
    207     //  for (int i = 0; i < jobs.Length; i++) {
    208     //    var job = (EngineJob)jobs[i].Clone();
    209     //    job.Start();
    210     //    while (job.ExecutionState != ExecutionState.Stopped) {
    211     //      Thread.Sleep(100);
    212     //    }
    213     //    scopes[i] = ((IAtomicOperation)job.InitialOperation).Scope;
    214     //  }
    215     //  return scopes;
    216     //}
     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;
     222    }
    217223
    218224    /// <summary>
     
    230236      try {
    231237        List<Guid> remainingJobIds = new List<Guid>();
    232         List<LightweightJob> lightweightJobs;
    233 
    234         int finishedCount = 0;
    235         int uploadCount = 0;
    236238
    237239        // create hive experiment
    238240        hiveExperiment.Name = "HiveEngine Run " + hiveExperiments.Count;
     241        hiveExperiment.DateCreated = DateTime.Now;
    239242        hiveExperiment.UseLocalPlugins = this.UseLocalPlugins;
    240243        hiveExperiment.ResourceNames = this.ResourceNames;
    241         hiveExperiment.Id = ServiceLocator.Instance.CallHiveService(s => s.AddHiveExperiment(hiveExperiment));
    242244        var refreshableHiveExperiment = new RefreshableHiveExperiment(hiveExperiment);
     245        refreshableHiveExperiment.IsControllable = false;
    243246        hiveExperiments.Add(refreshableHiveExperiment);
    244247
     
    246249        var uploadTasks = new List<Task<Job>>();
    247250        for (int i = 0; i < jobs.Length; i++) {
    248           var job = jobs[i];
     251          hiveExperiment.HiveJobs.Add(new EngineHiveJob(jobs[i], parentScopeClone));
    249252
    250253          // shuffle random variable to avoid the same random sequence in each operation; todo: does not yet work (it cannot find the random variable)
    251           IRandom random = FindRandomParameter(job.InitialOperation as IExecutionContext);
     254          IRandom random = FindRandomParameter(jobs[i].InitialOperation as IExecutionContext);
    252255          if (random != null)
    253256            random.Reset(random.Next());
    254 
    255           uploadTasks.Add(Task.Factory.StartNew<Job>((keyValuePairObj) => {
    256             return UploadJob(keyValuePairObj, parentScopeClone, cancellationToken, GetResourceIds(), hiveExperiment.Id);
    257           }, new KeyValuePair<int, EngineJob>(i, job), cancellationToken));
    258         }
    259 
    260         Task processUploadedJobsTask = new Task(() => {
    261           // process finished upload-tasks
    262           int uploadTasksCount = uploadTasks.Count;
    263           for (int i = 0; i < uploadTasksCount; i++) {
    264             cancellationToken.ThrowIfCancellationRequested();
    265 
    266             var uploadTasksArray = uploadTasks.ToArray();
    267             var task = uploadTasksArray[Task.WaitAny(uploadTasksArray)];
    268             if (task.Status == TaskStatus.Faulted) {
    269               LogException(task.Exception);
    270               throw task.Exception;
    271             }
    272 
    273             int key = ((KeyValuePair<int, EngineJob>)task.AsyncState).Key;
    274             Job job = task.Result;
    275             lock (locker) {
    276               uploadCount++;
    277               jobIndices.Add(job.Id, key);
    278               remainingJobIds.Add(job.Id);
    279             }
    280             jobs[key] = null; // relax memory
    281             LogMessage(string.Format("Uploaded job #{0}", key + 1, job.Id));
    282             uploadTasks.Remove(task);
    283           }
    284         }, cancellationToken, TaskCreationOptions.PreferFairness);
    285         processUploadedJobsTask.Start();
    286 
    287         refreshableHiveExperiment.RefreshAutomatically = true;
    288        
     257        }
     258        ExperimentManagerClient.StartExperiment((e) => { throw e; }, refreshableHiveExperiment);
     259        // do polling until experiment is finished and all jobs are downloaded
    289260        while (!refreshableHiveExperiment.AllJobsFinished()) {
    290           Thread.Sleep(1000);
    291           // update time
    292           // handle cancellation
    293         }
    294 
    295 
    296 
    297         // poll job-statuses and create tasks for those which are finished
    298         var downloadTasks = new List<Task<EngineJob>>();
    299         var executionTimes = new List<TimeSpan>();
    300         var executionTimeOnHiveBefore = executionTimeOnHive;
    301         while (processUploadedJobsTask.Status != TaskStatus.RanToCompletion || remainingJobIds.Count > 0) {
    302           cancellationToken.ThrowIfCancellationRequested();
    303 
    304           Thread.Sleep(10000);
    305           try {
    306             lightweightJobs = ServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobs(remainingJobIds));
    307 
    308             var jobsFinished = lightweightJobs.Where(j => j.State == JobState.Finished || j.State == JobState.Failed || j.State == JobState.Aborted);
    309             finishedCount += jobsFinished.Count();
    310             if (jobsFinished.Count() > 0) LogMessage(string.Format("Finished: {0}/{1}", finishedCount, jobs.Length));
    311             ExecutionTimeOnHive = executionTimeOnHiveBefore + executionTimes.Sum() + lightweightJobs.Select(x => x.ExecutionTime.HasValue ? x.ExecutionTime.Value : TimeSpan.Zero).Sum();
    312 
    313             foreach (var result in jobsFinished) {
    314               if (result.State == JobState.Finished) {
    315                 downloadTasks.Add(Task.Factory.StartNew<EngineJob>((jobIdObj) => {
    316                   return DownloadJob(jobIndices, jobIdObj, cancellationToken);
    317                 }, result.Id, cancellationToken));
    318               } else if (result.State == JobState.Aborted) {
    319                 LogMessage(string.Format("Job #{0} aborted (id: {1})", jobIndices[result.Id] + 1, result.Id));
    320               } else if (result.State == JobState.Failed) {
    321                 LogMessage(string.Format("Job #{0} failed (id: {1}): {2}", jobIndices[result.Id] + 1, result.Id, result.CurrentStateLog != null ? result.CurrentStateLog.Exception : string.Empty));
    322               }
    323               remainingJobIds.Remove(result.Id);
    324               executionTimes.Add(result.ExecutionTime.HasValue ? result.ExecutionTime.Value : TimeSpan.Zero);
    325             }
    326           }
    327           catch (Exception e) {
    328             LogException(e);
    329           }
    330         }
    331 
    332         // process finished download-tasks
    333         int downloadTasksCount = downloadTasks.Count;
    334         for (int i = 0; i < downloadTasksCount; i++) {
    335           cancellationToken.ThrowIfCancellationRequested();
    336 
    337           var downloadTasksArray = downloadTasks.ToArray();
    338           var task = downloadTasksArray[Task.WaitAny(downloadTasksArray)];
    339           var jobId = (Guid)task.AsyncState;
    340           if (task.Status == TaskStatus.Faulted) {
    341             LogException(task.Exception);
    342             throw task.Exception;
    343           }
    344           scopes[jobIndices[(Guid)task.AsyncState]] = ((IAtomicOperation)task.Result.InitialOperation).Scope;
    345           downloadTasks.Remove(task);
    346         }
    347 
    348         LogMessage(string.Format("All jobs finished (TotalExecutionTime: {0}).", executionTimes.Sum()));
     261          Thread.Sleep(500);
     262          this.ExecutionTimeOnHive = TimeSpan.FromMilliseconds(hiveExperiments.Sum(x => x.HiveExperiment.ExecutionTime.TotalMilliseconds));
     263        }
     264        LogMessage(string.Format("{0} finished (TotalExecutionTime: {1}).", refreshableHiveExperiment.ToString(), refreshableHiveExperiment.HiveExperiment.ExecutionTime));
     265
     266        // get scopes
     267        int j = 0;
     268        foreach (var hiveJob in hiveExperiment.HiveJobs) {
     269          var scope = ((IAtomicOperation) ((EngineJob)hiveJob.ItemJob).InitialOperation).Scope;
     270          scopes[j++] = scope;
     271        }
     272        refreshableHiveExperiment.RefreshAutomatically = false;
    349273        DeleteHiveExperiment(hiveExperiment.Id);
    350 
    351274        return scopes;
    352275      }
     
    505428  }
    506429
    507   public static class ScopeExtensions {
    508     public static void ClearParentScopes(this IScope scope) {
    509       scope.ClearParentScopes(null);
    510     }
    511 
    512     public static void ClearParentScopes(this IScope scope, IScope childScope) {
    513       if (childScope != null) {
    514         scope.SubScopes.Clear();
    515         scope.SubScopes.Add(childScope);
    516       }
    517       if (scope.Parent != null)
    518         scope.Parent.ClearParentScopes(scope);
    519     }
    520   }
    521 
    522430  public static class EnumerableExtensions {
    523431    public static TimeSpan Sum(this IEnumerable<TimeSpan> times) {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Web/Hive-3.4/Web.config

    r6168 r6178  
    2121    </roleManager>
    2222    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
     23    <httpRuntime maxRequestLength="1048576" />
    2324  </system.web>
    2425 
     
    7980    </modules>-->
    8081    <directoryBrowse enabled="true"/>
     82   
    8183  </system.webServer>
    8284 
Note: See TracChangeset for help on using the changeset viewer.