Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/10 16:58:35 (14 years ago)
Author:
cneumuel
Message:

further improvement and stabilisation of HiveExperiment (#1115)

Location:
branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3
Files:
2 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3

    • Property svn:ignore
      •  

        old new  
        11bin
        22obj
         3HeuristicLab.Hive.Experiment-3.3.csproj.user
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/HeuristicLab.Hive.Experiment-3.3.csproj

    r4119 r4120  
    9696  </ItemGroup>
    9797  <ItemGroup>
     98    <Compile Include="JobItemList.cs" />
     99    <Compile Include="JobItem.cs" />
    98100    <Compile Include="OptimizerJob.cs" />
    99101    <Compile Include="HeuristicLabHiveExperimentPlugin.cs" />
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/HiveExperiment.cs

    r4119 r4120  
    5454    private const int resultPollingIntervalMs = 10000;
    5555
     56    private object locker = new object();
    5657    private const int maxSnapshotRetries = 20;
    57 
    58     [Storable]
    59     private IDictionary<Guid, IOptimizer> sentOptimizers = null;
    60 
    61     private object locker = new object();
    62     private DateTime startTime;
     58    private System.Timers.Timer timer;
     59    private bool pausePending, stopPending;
     60    private DateTime lastUpdateTime;
     61
     62    [Storable]
     63    private IDictionary<Guid, IOptimizer> pendingOptimizers = new Dictionary<Guid, IOptimizer>();
     64
     65    [Storable]
     66    private JobItemList jobItems;
     67    public JobItemList JobItems {
     68      get { return jobItems; }
     69    }
     70   
    6371
    6472    [Storable]
     
    6674    public string ServerUrl {
    6775      get { return serverUrl; }
    68       set { serverUrl = value; OnServerUrlChanged(); }
     76      set {
     77        if (serverUrl != value) {
     78          serverUrl = value;
     79          OnServerUrlChanged();
     80        }
     81      }
    6982    }
    7083
     
    7386    public string ResourceIds {
    7487      get { return resourceIds; }
    75       set { resourceIds = value; OnResourceIdsChanged(); }
     88      set {
     89        if (resourceIds != value) {
     90          resourceIds = value;
     91          OnResourceIdsChanged();
     92        }
     93      }
    7694    }
    7795
     
    8098    public HeuristicLab.Optimization.Experiment Experiment {
    8199      get { return experiment; }
    82       set { experiment = value; OnExperimentChanged(); }
     100      set {
     101        if (experiment != value) {
     102          experiment = value;
     103          OnExperimentChanged();
     104        }
     105      }
     106    }
     107
     108    [Storable]
     109    private ILog log;
     110    public ILog Log {
     111      get { return log; }
    83112    }
    84113
     
    92121      this.ServerUrl = HeuristicLab.Hive.Experiment.Properties.Settings.Default.HiveServerUrl;
    93122      this.ResourceIds = HeuristicLab.Hive.Experiment.Properties.Settings.Default.ResourceIds;
     123      this.log = new Log();
     124      pausePending = stopPending = false;
     125      jobItems = new JobItemList();
     126      InitTimer();
    94127    }
    95128
    96129    public override IDeepCloneable Clone(Cloner cloner) {
     130      log.LogMessage("I am beeing cloned");
    97131      HiveExperiment clone = (HiveExperiment)base.Clone(cloner);
    98132      clone.resourceIds = this.resourceIds;
     
    101135      clone.executionState = this.executionState;
    102136      clone.executionTime = this.executionTime;
    103       clone.sentOptimizers = new Dictionary<Guid, IOptimizer>();
    104       foreach (var pair in this.sentOptimizers)
    105         clone.sentOptimizers[pair.Key] = (IOptimizer)cloner.Clone(pair.Value);
     137      clone.pendingOptimizers = new Dictionary<Guid, IOptimizer>();
     138      foreach (var pair in this.pendingOptimizers)
     139        clone.pendingOptimizers[pair.Key] = (IOptimizer)cloner.Clone(pair.Value);
     140      clone.log = (ILog)cloner.Clone(log);
     141      clone.stopPending = this.stopPending;
     142      clone.pausePending = this.pausePending;
     143      clone.jobItems = (JobItemList)cloner.Clone(jobItems);
    106144      return clone;
     145    }
     146
     147    [StorableHook(HookType.AfterDeserialization)]
     148    private void AfterDeserialization() {
     149      InitTimer();
     150      log.LogMessage("I was deserialized.");
     151    }
     152
     153    private void InitTimer() {
     154      timer = new System.Timers.Timer(100);
     155      timer.AutoReset = true;
     156      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
     157    }
     158
     159    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
     160      DateTime now = DateTime.Now;
     161      ExecutionTime += now - lastUpdateTime;
     162      lastUpdateTime = now;
    107163    }
    108164
     
    135191      get { return executionTime; }
    136192      private set {
    137         executionTime = value;
    138         OnExecutionTimeChanged();
     193        if (executionTime != value) {
     194          executionTime = value;
     195          OnExecutionTimeChanged();
     196        }
    139197      }
    140198    }
     
    145203
    146204    public void Prepare() {
    147       // [chn] needed?
    148205      if (experiment != null) {
    149206        experiment.Prepare();
     207        this.ExecutionState = Core.ExecutionState.Prepared;
    150208        OnPrepared();
    151209      }
     
    153211
    154212    public void Start() {
    155       this.startTime = DateTime.Now;
     213      OnStarted();
     214      lastUpdateTime = DateTime.Now;
    156215      this.ExecutionState = Core.ExecutionState.Started;
    157       IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(ServerUrl);
    158 
    159       sentOptimizers = new Dictionary<Guid, IOptimizer>();
    160       IEnumerable<string> groups = ResourceGroups;
    161 
    162       foreach (IOptimizer optimizer in Experiment.Optimizers) {
    163         SerializedJob serializedJob = CreateSerializedJob(optimizer);
    164         ResponseObject<JobDto> response = executionEngineFacade.AddJobWithGroupStrings(serializedJob, groups);
    165         sentOptimizers.Add(response.Obj.Id, optimizer);
    166         StartResultPollingThread(response.Obj);
    167       }
     216      Thread t = new Thread(() => {
     217        IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(ServerUrl);
     218
     219        pendingOptimizers = new Dictionary<Guid, IOptimizer>();
     220        IEnumerable<string> groups = ResourceGroups;
     221
     222        foreach (IOptimizer optimizer in GetOptimizers(false)) {
     223          SerializedJob serializedJob = CreateSerializedJob(optimizer);
     224          ResponseObject<JobDto> response = executionEngineFacade.AddJobWithGroupStrings(serializedJob, groups);
     225          pendingOptimizers.Add(response.Obj.Id, optimizer);
     226          StartResultPollingThread(response.Obj);
     227
     228          JobItem jobItem = new JobItem() {
     229            JobDto = response.Obj,
     230            LatestSnapshot = new ResponseObject<SerializedJob>() {
     231              Obj = serializedJob,
     232              StatusMessage = "Initial Snapshot",
     233              Success = true
     234            }
     235          };
     236          jobItems.Add(jobItem);
     237
     238          log.LogMessage("Sent job to server (jobId: " + response.Obj.Id + ")");
     239        }
     240      });
     241      t.Start();
     242    }
     243
     244    /// <summary>
     245    /// Returns all optimizers in the current Experiment
     246    /// </summary>
     247    /// <param name="flatout">if false only top level optimizers are returned, if true the optimizer-tree is flatted</param>
     248    /// <returns></returns>
     249    private IEnumerable<IOptimizer> GetOptimizers(bool flatout) {
     250      if (!flatout) {
     251        return experiment.Optimizers;
     252      } else {
     253        throw new NotImplementedException();
     254      }
     255    }
     256
     257    private void ReplaceOptimizer(IOptimizer originalOptimizer, IOptimizer newOptimizer) {
     258      int originalOptimizerIndex = experiment.Optimizers.IndexOf(originalOptimizer);
     259      experiment.Optimizers[originalOptimizerIndex] = newOptimizer;
    168260    }
    169261
     
    171263      // todo
    172264    }
    173 
    174     private bool abortRequested;
    175 
     265   
    176266    #endregion
    177267
     
    219309          Thread.Sleep(resultPollingIntervalMs);
    220310          lock (locker) {
    221             if (abortRequested) return;
    222 
     311            if (stopPending) return;
     312           
    223313            ResponseObject<SerializedJob> response = executionEngineFacade.GetLastSerializedResult(job.Id, false, false);
    224 
     314            log.LogMessage("Received response for job: " + response.StatusMessage + " (jobId: " + job.Id + ")");
     315           
     316           
    225317            // loop while
    226318            // 1. the user doesn't request an abort
     
    229321            // 4. the result that we get from the server is a snapshot and not the final result
    230322            if (response.Success && response.Obj != null && response.StatusMessage != ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE) {
    231 
    232               // [chn] todo: deserialization into interface is not possible! how to find out correct static type?
    233323              restoredObject = XmlParser.Deserialize<IJob>(new MemoryStream(response.Obj.SerializedJobData));
    234324            }
     
    236326        } while (restoredObject == null || restoredObject.ExecutionState != Core.ExecutionState.Stopped);
    237327
     328        log.LogMessage("Job finished (jobId: " + job.Id + ")");
    238329        // job retrieved... replace the existing optimizers with the finished one
    239         IOptimizer originalOptimizer = sentOptimizers[job.Id];
     330        IOptimizer originalOptimizer = pendingOptimizers[job.Id];
    240331        IOptimizer restoredOptimizer = ((OptimizerJob)restoredObject).Optimizer;
    241332
    242         int originalOptimizerIndex = experiment.Optimizers.IndexOf(originalOptimizer);
    243         experiment.Optimizers[originalOptimizerIndex] = restoredOptimizer;
    244         sentOptimizers.Remove(job.Id);
    245 
    246         if (sentOptimizers.Count == 0) {
     333        ReplaceOptimizer(originalOptimizer, restoredOptimizer);
     334        pendingOptimizers.Remove(job.Id);
     335
     336        if (pendingOptimizers.Count == 0) {
    247337          // finished
    248338          this.ExecutionState = Core.ExecutionState.Stopped;
     
    254344      t.Start();
    255345    }
    256 
    257 
     346   
    258347    #region Required Plugin Search
    259348    /// <summary>
     
    351440    public event EventHandler ExecutionStateChanged;
    352441    private void OnExecutionStateChanged() {
     442      log.LogMessage("ExecutionState changed to " + executionState.ToString());
    353443      EventHandler handler = ExecutionStateChanged;
    354444      if (handler != null) handler(this, EventArgs.Empty);
     
    359449    public event EventHandler Started;
    360450    private void OnStarted() {
     451      log.LogMessage("Started");
     452      timer.Start();
    361453      EventHandler handler = Started;
    362454      if (handler != null) handler(this, EventArgs.Empty);
     
    365457    public event EventHandler Stopped;
    366458    private void OnStopped() {
     459      timer.Stop();
     460      log.LogMessage("Stopped");
    367461      EventHandler handler = Stopped;
    368462      if (handler != null) handler(this, EventArgs.Empty);
     
    371465    public event EventHandler Paused;
    372466    private void OnPaused() {
     467      timer.Stop();
     468      log.LogMessage("Paused");
    373469      EventHandler handler = Paused;
    374470      if (handler != null) handler(this, EventArgs.Empty);
     
    377473    public event EventHandler Prepared;
    378474    protected virtual void OnPrepared() {
     475      log.LogMessage("Prepared");
    379476      EventHandler handler = Prepared;
    380477      if (handler != null) handler(this, EventArgs.Empty);
     
    389486    public event EventHandler ExperimentChanged;
    390487    protected virtual void OnExperimentChanged() {
     488      log.LogMessage("Experiment changed");
    391489      EventHandler handler = ExperimentChanged;
    392490      if (handler != null) handler(this, EventArgs.Empty);
     
    399497    }
    400498
    401 
    402499    #endregion
    403500  }
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/OptimizerJob.cs

    r4119 r4120  
    1010namespace HeuristicLab.Hive.Experiment {
    1111  [StorableClass]
    12   class OptimizerJob : IJob {
     12  public class OptimizerJob : IJob {
    1313
    1414    [Storable]
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/Properties/AssemblyInfo.cs

    r4119 r4120  
    5555// You can specify all the values or you can default the Revision and Build Numbers
    5656// by using the '*' as shown below:
    57 [assembly: AssemblyVersion("3.3.0.4116")]
    58 [assembly: AssemblyFileVersion("3.3.0.4116")]
    59 [assembly: AssemblyBuildDate("2010/07/28 17:42:02")]
     57[assembly: AssemblyVersion("3.3.0.4119")]
     58[assembly: AssemblyFileVersion("3.3.0.4119")]
     59[assembly: AssemblyBuildDate("2010/07/29 16:49:01")]
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/ServiceLocator.cs

    r4116 r4120  
    3131  internal class ServiceLocator {
    3232    internal static IExecutionEngineFacade CreateExecutionEngineFacade(string url) {
    33       ChannelFactory<IExecutionEngineFacade> factory =
    34         new ChannelFactory<IExecutionEngineFacade>(
     33      ChannelFactory<IExecutionEngineFacade> factory = new ChannelFactory<IExecutionEngineFacade>(
    3534          WcfSettings.GetStreamedBinding(),
    3635          new EndpointAddress(url));
    37 
    3836      return factory.CreateChannel();
    3937    }
Note: See TracChangeset for help on using the changeset viewer.