Changeset 2032


Ignore:
Timestamp:
06/08/09 18:34:28 (12 years ago)
Author:
gkronber
Message:

Worked on asynchronous result polling in HiveEngine. #545 (Engine which can be executed in the Hive).

Location:
trunk/sources/HeuristicLab.Hive.Engine/3.2
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Hive.Engine/3.2/HeuristicLab.Hive.Engine-3.2.csproj

    r1761 r2032  
    126126      <Name>HeuristicLab.SequentialEngine-3.2</Name>
    127127    </ProjectReference>
     128    <ProjectReference Include="..\..\HeuristicLab.Tracing\3.2\HeuristicLab.Tracing-3.2.csproj">
     129      <Project>{EE2034D9-6E27-48A1-B855-42D45F69A4FC}</Project>
     130      <Name>HeuristicLab.Tracing-3.2</Name>
     131    </ProjectReference>
    128132  </ItemGroup>
    129133  <ItemGroup>
  • trunk/sources/HeuristicLab.Hive.Engine/3.2/HeuristicLabHiveEnginePlugin.cs

    r1927 r2032  
    3737  [Dependency(Dependency = "HeuristicLab.Hive.JobBase-3.2")]
    3838  [Dependency(Dependency = "HeuristicLab.SequentialEngine-3.2")]
     39  [Dependency(Dependency = "HeuristicLab.Tracing-3.2")]
    3940  public class HeuristicLabHiveEnginePlugin : PluginBase {
    4041  }
  • trunk/sources/HeuristicLab.Hive.Engine/3.2/HiveEngine.cs

    r2018 r2032  
    3333using System.Xml;
    3434using System.IO.Compression;
     35using HeuristicLab.Tracing;
    3536
    3637namespace HeuristicLab.Hive.Engine {
     
    4445    private Guid jobId;
    4546    private Job job;
     47    private object locker = new object();
     48    private volatile bool abortRequested;
     49
    4650    public string HiveServerUrl { get; set; }
    4751
    4852    public HiveEngine() {
    4953      job = new Job();
     54      abortRequested = false;
    5055    }
    5156
     
    96101        IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
    97102        ResponseObject<JobResult> response = null;
     103        Job restoredJob = null;
    98104        do {
    99           response = executionEngineFacade.GetLastResult(jobId, true);
    100           if (response.Success && response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE) {
    101             Thread.Sleep(RESULT_POLLING_INTERVAL_MS);
     105          Thread.Sleep(RESULT_POLLING_INTERVAL_MS);
     106          lock (locker) {
     107            HiveLogger.Debug("HiveEngine: Results-polling - GetLastResult");
     108            response = executionEngineFacade.GetLastResult(jobId, false);
     109            HiveLogger.Debug("HiveEngine: Results-polling - Server: " + response.StatusMessage + " success: " + response.Success);
     110            // loop while
     111            // 1. the user doesn't request an abort
     112            // 2. there is a problem with server communication (success==false)
     113            // 3. no result for the job is available yet (response.Obj==null)
     114            // 4. the result that we get from the server is a snapshot and not the final result
     115            if (abortRequested) return;
     116            if (response.Success && response.Obj != null) {
     117              HiveLogger.Debug("HiveEngine: Results-polling - Got result!");
     118              restoredJob = (Job)PersistenceManager.RestoreFromGZip(response.Obj.Result);
     119              HiveLogger.Debug("HiveEngine: Results-polling - IsSnapshotResult: " + restoredJob.Engine.Canceled);
     120            }
    102121          }
    103         } while (response.Success && response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE);
    104         if (response.Success) {
    105           JobResult jobResult = response.Obj;
    106           if (jobResult != null) {
    107             job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
    108             OnFinished();
    109           }
    110         } else {
    111           Exception ex = new Exception(response.Obj.Exception.Message);
    112           ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
    113         }
     122        } while (restoredJob == null || restoredJob.Engine.Canceled);
     123
     124        job = restoredJob;
     125        OnChanged();
     126        OnFinished();
    114127      });
     128      HiveLogger.Debug("HiveEngine: Starting results-polling thread");
    115129      t.Start();
    116130    }
     
    119133      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
    120134
    121       // poll until snapshot is ready
    122135      ResponseObject<JobResult> response;
    123 
    124       // request snapshot
    125       Response snapShotResponse = executionEngineFacade.RequestSnapshot(jobId);
    126       if (snapShotResponse.StatusMessage == ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED) {
    127         response = executionEngineFacade.GetLastResult(jobId, false);
    128       } else {
    129         do {
    130           response = executionEngineFacade.GetLastResult(jobId, true);
    131           if (response.Success && response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE) {
     136      lock (locker) {
     137        HiveLogger.Debug("HiveEngine: Abort - RequestSnapshot");
     138        Response snapShotResponse = executionEngineFacade.RequestSnapshot(jobId);
     139        if (snapShotResponse.StatusMessage == ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED) {
     140          // job is finished already
     141          HiveLogger.Debug("HiveEngine: Abort - GetLastResult(false)");
     142          response = executionEngineFacade.GetLastResult(jobId, false);
     143          HiveLogger.Debug("HiveEngine: Abort - Server: " + response.StatusMessage + " success: " + response.Success);
     144        } else {
     145          // server sent snapshot request to client
     146          // poll until snapshot is ready
     147          do {
    132148            Thread.Sleep(SNAPSHOT_POLLING_INTERVAL_MS);
    133           }
    134         } while (response.Success && response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE);
     149            HiveLogger.Debug("HiveEngine: Abort - GetLastResult(true)");
     150            response = executionEngineFacade.GetLastResult(jobId, true);
     151            HiveLogger.Debug("HiveEngine: Abort - Server: " + response.StatusMessage + " success: " + response.Success);
     152            // loop while
     153            // 1. problem with communication with server
     154            // 2. job result not yet ready
     155          } while (
     156            !response.Success ||
     157            response.StatusMessage == ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE);
     158        }
    135159      }
    136       if (response.Success) {
    137         JobResult jobResult = response.Obj;
    138         if (jobResult != null) {
    139           job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
    140           //PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
    141         }
    142       } else {
    143         Exception ex = new Exception(response.Obj.Exception.Message);
    144         ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
     160      JobResult jobResult = response.Obj;
     161      if (jobResult != null) {
     162        HiveLogger.Debug("HiveEngine: Results-polling - Got result!");
     163        job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
     164        //PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
    145165      }
     166      //HiveLogger.Debug("HiveEngine: Results-polling - Exception!");
     167      //Exception ex = new Exception(response.Obj.Exception.Message);
     168      //ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
    146169    }
    147170
     
    155178
    156179    public void Abort() {
     180      abortRequested = true;
     181      RequestSnapshot();
    157182      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
    158183      executionEngineFacade.AbortJob(jobId);
     184      OnChanged();
    159185      OnFinished();
    160186    }
    161187
    162188    public void Reset() {
     189      abortRequested = false;
    163190      job.Engine.Reset();
    164191      jobId = Guid.NewGuid();
  • trunk/sources/HeuristicLab.Hive.Engine/3.2/HiveEngineEditor.Designer.cs

    r2018 r2032  
    4747      this.urlTextBox = new System.Windows.Forms.TextBox();
    4848      this.urlLabel = new System.Windows.Forms.Label();
     49      this.snapshotButton = new System.Windows.Forms.Button();
    4950      this.splitContainer1.Panel1.SuspendLayout();
    5051      this.splitContainer1.Panel2.SuspendLayout();
     
    101102      this.urlLabel.Text = "Hive Server Url:";
    102103      //
     104      // snapshotButton
     105      //
     106      this.snapshotButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
     107      this.snapshotButton.Enabled = false;
     108      this.snapshotButton.Location = new System.Drawing.Point(294, 457);
     109      this.snapshotButton.Name = "snapshotButton";
     110      this.snapshotButton.Size = new System.Drawing.Size(75, 23);
     111      this.snapshotButton.TabIndex = 9;
     112      this.snapshotButton.Text = "Sna&pshot";
     113      this.snapshotButton.UseVisualStyleBackColor = true;
     114      this.snapshotButton.Click += new System.EventHandler(this.snapshotButton_Click);
     115      //
    103116      // HiveEngineEditor
    104117      //
    105118      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    106119      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     120      this.Controls.Add(this.snapshotButton);
    107121      this.Controls.Add(this.urlTextBox);
    108122      this.Controls.Add(this.urlLabel);
     
    117131      this.Controls.SetChildIndex(this.urlLabel, 0);
    118132      this.Controls.SetChildIndex(this.urlTextBox, 0);
     133      this.Controls.SetChildIndex(this.snapshotButton, 0);
    119134      this.splitContainer1.Panel1.ResumeLayout(false);
    120135      this.splitContainer1.Panel2.ResumeLayout(false);
     
    131146    private System.Windows.Forms.TextBox urlTextBox;
    132147    private System.Windows.Forms.Label urlLabel;
     148    private System.Windows.Forms.Button snapshotButton;
    133149  }
    134150}
  • trunk/sources/HeuristicLab.Hive.Engine/3.2/HiveEngineEditor.cs

    r2018 r2032  
    2828using System.Windows.Forms;
    2929using HeuristicLab.Core;
     30using HeuristicLab.Tracing;
    3031
    3132namespace HeuristicLab.Hive.Engine {
     
    6869
    6970    void abortButton_Click(object sender, EventArgs e) {
    70       BackgroundWorker worker = new BackgroundWorker();
    71       worker.DoWork += (s, args) => {
    72         HiveEngine.RequestSnapshot();
    73       };
    74       worker.RunWorkerCompleted += (s, args) => {
    75         this.Cursor = Cursors.Default;
    76         abortButton.Enabled = true;
    77       };
    78       this.Cursor = Cursors.WaitCursor;
    79       abortButton.Enabled = false;
    80       worker.RunWorkerAsync();
     71      snapshotButton.Enabled = false;
    8172    }
    8273
    8374    void executeButton_Click(object sender, EventArgs e) {
    8475      abortButton.Enabled = true;
     76      snapshotButton.Enabled = true;
    8577    }
    8678
     
    10294
    10395    void Engine_Initialized(object sender, EventArgs e) {
    104       abortButton.Enabled = false;
     96      if (InvokeRequired) {
     97        Invoke((EventHandler)Engine_Initialized, sender, e);
     98      } else {
     99        abortButton.Enabled = false;
     100        snapshotButton.Enabled = false;
     101      }
    105102    }
    106103
    107104    void Engine_Finished(object sender, EventArgs e) {
     105      if (InvokeRequired) {
     106        Invoke((EventHandler)Engine_Initialized, sender, e);
     107      } else {
     108        abortButton.Enabled = false;
     109        snapshotButton.Enabled = false;
     110      }
     111    }
     112
     113    private void snapshotButton_Click(object sender, EventArgs e) {
     114      BackgroundWorker worker = new BackgroundWorker();
     115      worker.DoWork += (s, args) => {
     116        HiveEngine.RequestSnapshot();
     117      };
     118      worker.RunWorkerCompleted += (s, args) => {
     119        Logger.Debug("HiveEngineEditor: RunWorkerCompleted");
     120        this.Cursor = Cursors.Default;
     121        abortButton.Enabled = true;
     122        snapshotButton.Enabled = true;
     123      };
     124      this.Cursor = Cursors.WaitCursor;
    108125      abortButton.Enabled = false;
     126      snapshotButton.Enabled = false;
     127      Logger.Debug("HiveEngineEditor: RunWorkerAsync");
     128      worker.RunWorkerAsync();
    109129    }
    110130  }
Note: See TracChangeset for help on using the changeset viewer.