Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5526


Ignore:
Timestamp:
02/21/11 17:35:42 (13 years ago)
Author:
cneumuel
Message:

#1233

  • fixed handling of StateLog in DataLayer
  • extended unit tests
  • changed style of service calls to OKB-like style (using delegates)
  • added possibility that parent jobs can be finished immediately when child jobs are finished
Location:
branches/HeuristicLab.Hive-3.4/sources
Files:
30 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Calendar/3.3/HeuristicLab.Calendar-3.3.csproj

    r5457 r5526  
    9696    <None Include="HeuristicLabCalendarPlugin.cs.frame" />
    9797    <Compile Include="HeuristicLabCalendarPlugin.cs" />
     98    <Compile Include="Properties\AssemblyInfo.cs" />
    9899    <Compile Include="Properties\CalendarResources.Designer.cs">
    99100      <AutoGen>True</AutoGen>
     
    117118      <SubType>Code</SubType>
    118119    </Compile>
    119     <Compile Include="Properties\AssemblyInfo.cs" />
    120120    <Compile Include="Appointment\ResolveAppointmentsEvent.cs" />
    121121    <Compile Include="ViewTools\SelectionType.cs" />
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockServiceLocator.cs

    r5405 r5526  
    2020#endregion
    2121
    22 using HeuristicLab.Clients.Common;
     22using System;
    2323using HeuristicLab.Services.Hive.Common.ServiceContracts;
    2424
    2525namespace HeuristicLab.Clients.Hive.Slave.Tests {
    2626  public class MockServiceLocator : IServiceLocator {
    27 
    2827    private MockHiveService service;
    2928
    30     public Disposable<IHiveService> GetService() {
     29    private string username;
     30    public string Username {
     31      get { return username; }
     32      set { username = value; }
     33    }
     34
     35    private string password;
     36    public string Password {
     37      get { return password; }
     38      set { password = value; }
     39    }
     40   
     41    public IHiveService GetService() {
    3142      if (service == null)
    3243        service = new MockHiveService();
    33 
    34       return new Disposable<IHiveService>(service);
     44      return service;
    3545    }
    3646
    37     public Disposable<IHiveService> GetService(string username, string password) {
    38       return GetService();
     47    public void CallHiveService(Action<IHiveService> call) {
     48      call(GetService());
     49    }
     50
     51    public T CallHiveService<T>(Func<IHiveService, T> call) {
     52      return call(GetService());
    3953    }
    4054  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/SlaveTest.cs

    r5511 r5526  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.Clients.Common;
    2625using HeuristicLab.Services.Hive.Common;
    2726using HeuristicLab.Services.Hive.Common.DataTransfer;
    28 using HeuristicLab.Services.Hive.Common.ServiceContracts;
    2927using Microsoft.VisualStudio.TestTools.UnitTesting;
    3028
     
    8280      MockJob job = new MockJob(300, false);
    8381
    84       using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
    85         MockHiveService ms = (MockHiveService)service.Obj;
    86         ((MockHiveService)service.Obj).Messages = CreateMsgsForSingleJob();
    87         ((MockHiveService)service.Obj).updateJobs(jobList, job);
     82      ServiceLocator.Instance.CallHiveService(service => {
     83        MockHiveService ms = (MockHiveService)service;
     84        ((MockHiveService)service).Messages = CreateMsgsForSingleJob();
     85        ((MockHiveService)service).updateJobs(jobList, job);
    8886
    8987        HeuristicLab.Clients.Hive.Slave.Core core = new Slave.Core();
     
    9391        Assert.AreEqual<Guid>(testJob.Id, ms.ResultJobs[0].Id);
    9492        core.Shutdown();
    95       }
     93      });
    9694    }
    9795
     
    120118      MockJob job = new MockJob(10000, false);
    121119
    122       using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
    123         MockHiveService ms = (MockHiveService)service.Obj;
    124         ((MockHiveService)service.Obj).Messages = CreateMsgsForShutdownSlaveWhileJobRunning();
    125         ((MockHiveService)service.Obj).updateJobs(jobList, job);
     120      ServiceLocator.Instance.CallHiveService(service => {
     121        MockHiveService ms = (MockHiveService)service;
     122        ((MockHiveService)service).Messages = CreateMsgsForShutdownSlaveWhileJobRunning();
     123        ((MockHiveService)service).updateJobs(jobList, job);
    126124
    127125
    128126        HeuristicLab.Clients.Hive.Slave.Core core = new Slave.Core();
    129127        core.Start();
    130       }
     128      });
    131129    }
    132130
     
    184182      MockJob job = new MockJob(2000, false);
    185183
    186       using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
    187         MockHiveService ms = (MockHiveService)service.Obj;
    188         ((MockHiveService)service.Obj).Messages = CreateMsgsForTwoJobs();
    189         ((MockHiveService)service.Obj).updateJobs(jobList, job);
     184      ServiceLocator.Instance.CallHiveService(service => {
     185        MockHiveService ms = (MockHiveService)service;
     186        ((MockHiveService)service).Messages = CreateMsgsForTwoJobs();
     187        ((MockHiveService)service).updateJobs(jobList, job);
    190188
    191189
     
    194192        Assert.AreEqual<int>(2, ms.ResultJobs.Count);
    195193        core.Shutdown();
    196       }
     194      });
    197195    }
    198196  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/Core.cs

    r5512 r5526  
    371371        }
    372372        Job cJob = jobs[jobId];
    373         cJob.State = JobState.Finished;//TODO: what if failed?
     373        cJob.State = JobState.Finished; // TODO: what if failed?
    374374        cJob.ExecutionTime = engines[jobId].ExecutionTime;
    375375
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/WcfService.cs

    r5511 r5526  
    2222using System;
    2323using System.Collections.Generic;
    24 using HeuristicLab.Clients.Common;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Services.Hive.Common;
     
    4342        if (instance == null) {
    4443          instance = new WcfService();
     44          ServiceLocator.Instance.Username = "hiveslave";
     45          ServiceLocator.Instance.Password = "hiveslave";
    4546        }
    4647        return instance;
     
    7475    /// </summary>
    7576    public void Connect(HeuristicLab.Services.Hive.Common.DataTransfer.Slave slaveInfo) {
    76       using (Disposable<IHiveService> service = GetSlaveService()) {
    77         try {
    78           ConnState = NetworkEnum.WcfConnState.Connected;
    79           ConnectedSince = DateTime.Now;
    80           service.Obj.Hello(slaveInfo);
    81           OnConnected();
    82         }
    83         catch (Exception ex) {
    84           HandleNetworkError(ex);
    85         }
    86       }
     77      CallHiveService(service => {
     78        ConnState = NetworkEnum.WcfConnState.Connected;
     79        ConnectedSince = DateTime.Now;
     80        service.Hello(slaveInfo);
     81        OnConnected();
     82      });
    8783    }
    8884
     
    9187    /// </summary>
    9288    public void Disconnect() {
    93       using (Disposable<IHiveService> service = GetSlaveService()) {
    94         try {
    95           service.Obj.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
    96           ConnState = NetworkEnum.WcfConnState.Disconnected;
    97         }
    98         catch (Exception ex) {
    99           HandleNetworkError(ex);
    100         }
    101       }
     89      CallHiveService(service => {
     90        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
     91        ConnState = NetworkEnum.WcfConnState.Disconnected;
     92      });
    10293    }
    10394
     
    115106    /// </summary>
    116107    public Job GetJob(Guid jobId) {
    117       using (Disposable<IHiveService> service = GetSlaveService()) {
    118         try {
    119           Job job = service.Obj.GetJob(jobId);
    120           return job;
    121         }
    122         catch (Exception ex) {
    123           HandleNetworkError(ex);
    124           return null;
    125         }
    126       }
     108      return CallHiveService(s => s.GetJob(jobId));
    127109    }
    128110
    129111    public JobData GetJobData(Guid jobId) {
    130       using (Disposable<IHiveService> service = GetSlaveService()) {
    131         try {
    132           JobData jobData = service.Obj.GetJobData(jobId);
    133           return jobData;
    134         }
    135         catch (Exception ex) {
    136           HandleNetworkError(ex);
    137           return null;
    138         }
    139       }
     112      return CallHiveService(s => s.GetJobData(jobId));
    140113    }
    141114
    142115    public void UpdateJob(Job job) {
    143       using (Disposable<IHiveService> service = GetSlaveService()) {
    144         try {
    145           service.Obj.UpdateJob(job);
    146         }
    147         catch (Exception ex) {
    148           HandleNetworkError(ex);
    149         }
    150       }
     116      CallHiveService(s => s.UpdateJob(job));
    151117    }
    152118
     
    157123    /// <param name="jobData"></param>   
    158124    public void UpdateJobData(Job job, JobData jobData, Guid slaveId) {
    159       using (Disposable<IHiveService> service = GetSlaveService()) {
    160         try {
    161           JobState before = job.State;
    162           job.SetState(JobState.Transferring, slaveId, "");
    163           service.Obj.UpdateJob(job);
     125      CallHiveService(service => {
     126        JobState before = job.State;
     127        job.SetState(JobState.Transferring, slaveId, "");
     128        service.UpdateJob(job);
    164129
    165           service.Obj.UpdateJobData(job, jobData);
     130        service.UpdateJobData(job, jobData);
    166131
    167           job.SetState(before, slaveId, "");
    168           service.Obj.UpdateJob(job);
    169         }
    170         catch (Exception ex) {
    171           HandleNetworkError(ex);
    172         }
     132        job.SetState(before, slaveId, "");
     133        service.UpdateJob(job);
     134      });
     135    }
     136
     137    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
     138      return CallHiveService(s => s.Heartbeat(heartbeat));
     139    }
     140
     141    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
     142      return CallHiveService(s => s.GetPluginDatas(pluginIds));
     143    }
     144
     145    public IEnumerable<Plugin> GetPlugins() {
     146      return CallHiveService(s => s.GetPlugins());
     147    }
     148
     149    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
     150      return CallHiveService(s => s.AddChildJob(parentJobId, job, jobData));
     151    }
     152
     153    public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
     154      return CallHiveService(service => {
     155        IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
     156
     157        List<JobData> jobs = new List<JobData>();
     158        foreach (LightweightJob ljob in msg)
     159          jobs.Add(service.GetJobData(ljob.Id));
     160
     161        return jobs;
     162      });
     163    }
     164
     165    public void DeleteChildJobs(Guid jobId) {
     166      CallHiveService(s => s.DeleteChildJobs(jobId));
     167    }
     168
     169    public void CallHiveService(Action<IHiveService> call) {
     170      try {
     171        ServiceLocator.Instance.CallHiveService(call);
    173172      }
    174     }
    175    
    176     public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
    177       using (Disposable<IHiveService> service = GetSlaveService()) {
    178         try {
    179           List<MessageContainer> msg = service.Obj.Heartbeat(heartbeat);
    180           return msg;
    181         }
    182         catch (Exception ex) {
    183           HandleNetworkError(ex);
    184           return null;
    185         }
     173      catch (Exception ex) {
     174        HandleNetworkError(ex);
    186175      }
    187176    }
    188177
    189     public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
    190       using (Disposable<IHiveService> service = GetSlaveService()) {
    191         try {
    192           IEnumerable<PluginData> msg = service.Obj.GetPluginDatas(pluginIds);
    193           return msg;
    194         }
    195         catch (Exception ex) {
    196           HandleNetworkError(ex);
    197           return null;
    198         }
     178    private T CallHiveService<T>(Func<IHiveService, T> call) {
     179      try {
     180        return ServiceLocator.Instance.CallHiveService(call);
    199181      }
    200     }
    201 
    202     public IEnumerable<Plugin> GetPlugins() {
    203       using (Disposable<IHiveService> service = GetSlaveService()) {
    204         try {
    205           IEnumerable<Plugin> msg = service.Obj.GetPlugins();
    206           return msg;
    207         }
    208         catch (Exception ex) {
    209           HandleNetworkError(ex);
    210           return null;
    211         }
     182      catch (Exception ex) {
     183        HandleNetworkError(ex);
     184        return default(T);
    212185      }
    213     }
    214 
    215     public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
    216       using (Disposable<IHiveService> service = GetSlaveService()) {
    217         try {
    218           Guid msg = service.Obj.AddChildJob(parentJobId, job, jobData);
    219           return msg;
    220         }
    221         catch (Exception ex) {
    222           HandleNetworkError(ex);
    223           return new Guid();
    224         }
    225       }
    226     }
    227 
    228     public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
    229       using (Disposable<IHiveService> service = GetSlaveService()) {
    230         try {
    231           IEnumerable<LightweightJob> msg = service.Obj.GetLightweightChildJobs(parentJobId, false, false);
    232 
    233           List<JobData> jobs = new List<JobData>();
    234           foreach (LightweightJob ljob in msg)
    235             jobs.Add(service.Obj.GetJobData(ljob.Id));
    236 
    237           return jobs;
    238         }
    239         catch (Exception ex) {
    240           HandleNetworkError(ex);
    241           return null;
    242         }
    243       }
    244     }
    245 
    246     public void DeleteChildJobs(Guid jobId) {
    247       using (Disposable<IHiveService> service = GetSlaveService()) {
    248         try {
    249           service.Obj.DeleteChildJobs(jobId);
    250         }
    251         catch (Exception ex) {
    252           HandleNetworkError(ex);
    253         }
    254       }
    255     }
    256 
    257     private static Disposable<IHiveService> GetSlaveService() {
    258       return ServiceLocator.Instance.GetService("hiveslave", "hiveslave");
    259186    }
    260187  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Tests/Mocks/MockServiceLocator.cs

    r5405 r5526  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using HeuristicLab.Clients.Common;
     2using HeuristicLab.Services.Hive;
    63using HeuristicLab.Services.Hive.Common.ServiceContracts;
    7 using HeuristicLab.Services.Hive;
    84
    95namespace HeuristicLab.Clients.Hive.Tests {
    106  public class MockServiceLocator : IServiceLocator {
    11     public Disposable<IHiveService> GetService() {
    12       return new Disposable<IHiveService>(new HiveService());
     7    private string username;
     8    public string Username {
     9      get { return username; }
     10      set { username = value; }
    1311    }
    1412
    15     public Disposable<IHiveService> GetService(string username, string password) {
    16       return GetService();
     13    private string password;
     14    public string Password {
     15      get { return password; }
     16      set { password = value; }
     17    }
     18
     19    public IHiveService GetService() {
     20      return new HiveService();
     21    }
     22
     23    public void CallHiveService(Action<IHiveService> call) {
     24      call(GetService());
     25    }
     26
     27    public T CallHiveService<T>(Func<IHiveService, T> call) {
     28      return call(GetService());
    1729    }
    1830  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Administration/HiveAdministrationClient.cs

    r5525 r5526  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Services.Hive.Common.DataTransfer;
    27 using HeuristicLab.Services.Hive.Common.ServiceContracts;
    2827
    2928namespace HeuristicLab.Clients.Hive.Administration {
     
    5150
    5251    public void UpdateSlaveGroups() {
    53       using (HeuristicLab.Clients.Common.Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
    54 
     52      ServiceLocator.Instance.CallHiveService(service => {
    5553        /*SlaveGroup sg = new SlaveGroup();
    5654        sg.Name = "Test" + Guid.NewGuid();
     
    5957        service.Obj.AddSlaveGroup(sg);*/
    6058
    61         SlaveGroups = new List<SlaveGroup>(service.Obj.GetSlaveGroups());
    62         Slaves = new List<Slave>(service.Obj.GetSlaves());
     59        SlaveGroups = new List<SlaveGroup>(service.GetSlaveGroups());
     60        Slaves = new List<Slave>(service.GetSlaves());
    6361        OnItemImageChanged();
    64       }
     62      });
    6563    }
    6664  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveExperimentClient.cs

    r5511 r5526  
    2424using System.Linq;
    2525using System.Threading;
    26 using HeuristicLab.Clients.Common;
    2726using HeuristicLab.Clients.Hive.Jobs;
    2827using HeuristicLab.Common;
     
    249248        this.progress = new Progress("Connecting to server...");
    250249        IsProgressing = true;
    251         using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
     250        ServiceLocator.Instance.CallHiveService(service => {
    252251          IEnumerable<string> resourceNames = ToResourceNameList(this.ResourceNames);
    253252          var resourceIds = new List<Guid>();
    254253          foreach (var resourceName in resourceNames) {
    255             Guid resourceId = service.Obj.GetResourceId(resourceName);
     254            Guid resourceId = service.GetResourceId(resourceName);
    256255            if (resourceId == Guid.Empty) {
    257256              throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
     
    266265
    267266          this.progress.Status = "Uploading plugins...";
    268           this.OnlinePlugins = service.Obj.GetPlugins();
     267          this.OnlinePlugins = service.GetPlugins();
    269268          this.AlreadyUploadedPlugins = new List<Plugin>();
    270           Plugin configFilePlugin = UploadConfigurationFile(service.Obj);
     269          Plugin configFilePlugin = UploadConfigurationFile(service);
    271270          this.alreadyUploadedPlugins.Add(configFilePlugin);
    272271
    273272          this.progress.Status = "Uploading jobs...";
    274           UploadJobWithChildren(service.Obj, this.HiveJob, null, resourceIds, ref jobCount, totalJobCount, configFilePlugin.Id);
     273          UploadJobWithChildren(service, this.HiveJob, null, resourceIds, ref jobCount, totalJobCount, configFilePlugin.Id);
    275274          this.rootJobId = this.HiveJob.Job.Id;
    276275          LogMessage("Finished sending jobs to hive");
     
    279278          this.progress.Status = "Uploading HiveExperiment...";
    280279
    281           DT.HiveExperiment he = service.Obj.GetHiveExperiment(service.Obj.AddHiveExperiment(this.ToHiveExperimentDto()));
     280          DT.HiveExperiment he = service.GetHiveExperiment(service.AddHiveExperiment(this.ToHiveExperimentDto()));
    282281          this.UpdateFromDto(he);
    283282
    284283          StartResultPolling();
    285         }
     284        });
    286285      }
    287286      catch (Exception e) {
     
    316315    /// <summary>
    317316    /// Uploads the given job and all its child-jobs while setting the proper parentJobId values for the childs
    318     ///
    319317    /// </summary>
    320318    /// <param name="service"></param>
     
    327325      JobData jobData;
    328326      List<IPluginDescription> plugins;
     327     
    329328      if (hiveJob.OptimizerJob.ComputeInParallel &&
    330329        (hiveJob.OptimizerJob.Optimizer is Optimization.Experiment || hiveJob.OptimizerJob.Optimizer is Optimization.BatchRun)) {
    331         hiveJob.Job.SetState(JobState.FinishOnChildJobsFinished);
     330        hiveJob.Job.IsParentJob = true;
     331        hiveJob.Job.FinishWhenChildJobsFinished = true;
    332332        hiveJob.OptimizerJob.CollectChildJobs = false; // don't collect child-jobs on slaves
    333333        jobData = hiveJob.GetAsJobData(true, out plugins);
    334334      } else {
     335        hiveJob.Job.IsParentJob = false;
     336        hiveJob.Job.FinishWhenChildJobsFinished = false;
    335337        jobData = hiveJob.GetAsJobData(false, out plugins);
    336338      }
     
    342344      this.progress.ProgressValue = (double)jobCount / totalJobCount;
    343345
     346      hiveJob.Job.SetState(JobState.Transferring);
    344347      if (parentHiveJob != null) {
    345348        hiveJob.Job.Id = service.AddChildJob(parentHiveJob.Job.Id, hiveJob.Job, jobData);
     
    367370
    368371    public void Stop() {
    369       using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
     372      ServiceLocator.Instance.CallHiveService(service => {
    370373        foreach (HiveJob hj in HiveJob.GetAllHiveJobs()) {
    371           service.Obj.StopJob(hj.Job.Id);
    372         }
    373       }
     374          service.StopJob(hj.Job.Id);
     375        }
     376      });
    374377    }
    375378
     
    598601        int jobCount = 0;
    599602        progress.Status = "Connecting to Server...";
    600         using (var service = ServiceLocator.Instance.GetService()) {
     603        ServiceLocator.Instance.CallHiveService(service => {
    601604          // fetch all Job objects to create the full tree of tree of HiveJob objects
    602605          progress.Status = "Downloading list of jobs...";
    603           IEnumerable<LightweightJob> allResults = service.Obj.GetLightweightChildJobs(rootJobId, true, true);
     606          IEnumerable<LightweightJob> allResults = service.GetLightweightChildJobs(rootJobId, true, true);
    604607          totalJobCount = allResults.Count();
    605608
     
    610613            jobCount++;
    611614            progress.Status = string.Format("Downloading {0} of {1} jobs...", jobCount, totalJobCount);
    612             allJobs.Add(lightweightJob.Id, service.Obj.GetJob(lightweightJob.Id));
    613             allJobDatas.Add(lightweightJob.Id, service.Obj.GetJobData(lightweightJob.Id));
     615            allJobs.Add(lightweightJob.Id, service.GetJob(lightweightJob.Id));
     616            allJobDatas.Add(lightweightJob.Id, service.GetJobData(lightweightJob.Id));
    614617            progress.ProgressValue = (double)jobCount / totalJobCount;
    615618          }
     
    635638
    636639          // build child-job tree
    637           LoadChildResults(service.Obj, this.HiveJob, allResults, allJobs, allJobDatas, progress, totalJobCount, ref jobCount);
     640          LoadChildResults(service, this.HiveJob, allResults, allJobs, allJobDatas, progress, totalJobCount, ref jobCount);
    638641          StartResultPolling();
    639         }
     642        });
    640643      }
    641644      catch (Exception e) {
     
    674677
    675678    private OptimizerJob LoadOptimizerJob(Guid jobId) {
    676       using (var service = ServiceLocator.Instance.GetService()) {
    677         JobData jobData = service.Obj.GetJobData(jobId);
     679      return ServiceLocator.Instance.CallHiveService(service => {
     680        JobData jobData = service.GetJobData(jobId);
    678681        try {
    679682          return PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data);
     
    682685          return null;
    683686        }
    684       }
     687      });
    685688    }
    686689    #endregion
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveExperimentManagerClient.cs

    r5402 r5526  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Collections;
    2526using HeuristicLab.Core;
    26 using HeuristicLab.Services.Hive.Common.ServiceContracts;
    27 using HeuristicLab.Services.Hive.Common;
    28 using System.Collections.Generic;
    2927
    3028namespace HeuristicLab.Clients.Hive {
     29  using HeuristicLab.Common;
    3130  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
    32   using HeuristicLab.Common;
    33   using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    34   using HeuristicLab.Clients.Common;
    3531
    3632  [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
     
    9793          this.HiveExperiments = new ItemList<HiveExperimentClient>();
    9894        }
    99         using (var service = ServiceLocator.Instance.GetService()) {
    100           currentlyUpdating = true;
    101           IEnumerable<DT.HiveExperiment> response = service.Obj.GetHiveExperiments();
    102           progress.Status = "Populating HiveExperiment list...";
    103           RefreshExperimentList(response);
    104           currentlyUpdating = false;
    105         }
     95        currentlyUpdating = true;
     96        IEnumerable<DT.HiveExperiment> response = ServiceLocator.Instance.CallHiveService(s => s.GetHiveExperiments());
     97        progress.Status = "Populating HiveExperiment list...";
     98        RefreshExperimentList(response);
     99        currentlyUpdating = false;
    106100      }
    107101      catch (Exception) {
     
    137131    }
    138132
    139     void hiveExperiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveExperimentClient>> e) {
     133    private void hiveExperiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveExperimentClient>> e) {
    140134      if (!currentlyUpdating) {
    141         using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
     135        ServiceLocator.Instance.CallHiveService(service => {
    142136          foreach (IndexedItem<HiveExperimentClient> item in e.Items) {
    143137            if (item.Value.HiveExperimentId != Guid.Empty) {
    144               service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId);
     138              service.DeleteHiveExperiment(item.Value.HiveExperimentId);
    145139            }
    146140          }
    147         }
     141        });
    148142      }
    149143    }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJobClient.cs

    r5511 r5526  
    4444        } else {
    4545          if (job.State == JobState.Waiting) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
    46           else if (job.State == JobState.FinishOnChildJobsFinished) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
    4746          else if (job.State == JobState.Calculating) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
     47          else if (job.State == JobState.Transferring) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
    4848          else if (job.State == JobState.Aborted) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
    4949          else if (job.State == JobState.Failed) return HeuristicLab.Common.Resources.VSImageLibrary.Error;
     
    114114    }
    115115
    116     public HiveJob(Job jobDto)
    117       : this() {
    118       this.Job = jobDto;
    119     }
    120 
    121     public HiveJob(LightweightJob lightweightJob)
    122       : this() {
    123       UpdateFromLightweightJob(lightweightJob);
    124     }
     116    //public HiveJob(Job jobDto)
     117    //  : this() {
     118    //  this.Job = jobDto;
     119    //}
     120
     121    //public HiveJob(LightweightJob lightweightJob)
     122    //  : this() {
     123    //  UpdateFromLightweightJob(lightweightJob);
     124    //}
    125125
    126126    public HiveJob(OptimizerJob optimizerJob, bool autoCreateChildHiveJobs)
     
    136136    }
    137137
    138     public HiveJob(Job job, JobData jobData, bool autoCreateChildHiveJobs)
    139       : this() {
     138    public HiveJob(Job job, JobData jobData, bool autoCreateChildHiveJobs) {
    140139      this.syncJobsWithOptimizers = autoCreateChildHiveJobs;
    141140      this.Job = job;
     
    405404        job.Id = lightweightJob.Id;
    406405        job.ExecutionTime = lightweightJob.ExecutionTime;
     406        job.State = lightweightJob.State;
    407407        job.StateLog = new List<StateLog>(lightweightJob.StateLog);
    408408        // what about parentJob
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/JobResultPoller.cs

    r5055 r5526  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Threading;
    2425using HeuristicLab.Common;
    25 using HeuristicLab.Services.Hive.Common;
    26 using HeuristicLab.Services.Hive.Common.ServiceContracts;
    2726using HeuristicLab.Services.Hive.Common.DataTransfer;
    28 using System.Collections.Generic;
    29 using HeuristicLab.Clients.Common;
    3027
    3128namespace HeuristicLab.Clients.Hive {
     
    9693
    9794    private void FetchJobResults() {
    98       using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
    99         IEnumerable<LightweightJob> response = service.Obj.GetLightweightChildJobs(hiveJob.Job.Id, true, true);
     95      ServiceLocator.Instance.CallHiveService(service => {
     96        IEnumerable<LightweightJob> response = service.GetLightweightChildJobs(hiveJob.Job.Id, true, true);
    10097        OnJobResultsReceived(response);
    101       }
     98      });
    10299    }
    103100
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/PluginClient.cs

    r5095 r5526  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
     1using System.Collections.Generic;
    52using HeuristicLab.Services.Hive.Common.DataTransfer;
    63
     
    1613
    1714    public void Load() {
    18       using (var service = ServiceLocator.Instance.GetService()) {
    19        
    20       }
    2115    }
    2216  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/IServiceLocator.cs

    r5405 r5526  
    2020#endregion
    2121
    22 using HeuristicLab.Clients.Common;
     22using System;
    2323using HeuristicLab.Services.Hive.Common.ServiceContracts;
    2424
    2525namespace HeuristicLab.Clients.Hive {
    2626  public interface IServiceLocator {
    27     Disposable<IHiveService> GetService();
    28     Disposable<IHiveService> GetService(string username, string password);
     27    string Username { get; set; }
     28    string Password { get; set; }
     29    void CallHiveService(Action<IHiveService> call);
     30    T CallHiveService<T>(Func<IHiveService, T> call);
    2931  }
    3032}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceLocator.cs

    r5405 r5526  
    2020#endregion
    2121
     22using System;
     23using System.ServiceModel;
    2224using HeuristicLab.Clients.Common;
    2325using HeuristicLab.Services.Hive.Common.ServiceContracts;
     
    3840    }
    3941
    40     public Disposable<IHiveService> GetService() {
    41       return ClientFactory.CreateClient<IHiveService>("wsHttpBinding_IHiveService");
     42    private string username;
     43    public string Username {
     44      get { return username; }
     45      set { username = value; }
    4246    }
    4347
    44     public Disposable<IHiveService> GetService(string username, string password) {
    45       return ClientFactory.CreateClient<IHiveService>("wsHttpBinding_IHiveService", null, username, password);
     48    private string password;
     49    public string Password {
     50      get { return password; }
     51      set { password = value; }
     52    }
     53   
     54    public Disposable<ChannelFactory<IHiveService>> GetService() {
     55      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password)) {
     56        return ClientFactory.CreateChannelFactory<IHiveService>("wsHttpBinding_IHiveService");
     57      } else {
     58        return ClientFactory.CreateChannelFactory<IHiveService>("wsHttpBinding_IHiveService", null, username, password);
     59      }
     60    }
     61
     62    public void CallHiveService(Action<IHiveService> call) {
     63      using (var channelFactory = this.GetService()) {
     64        var service = channelFactory.Obj.CreateChannel();
     65        call(service);
     66      } // disposing the channelfactory is done by the disposable object; the channel gets disposed when the channelfactory is disposed
     67    }
     68
     69    private T CallHiveService<T>(Func<IHiveService, T> call) {
     70      using (var channelFactory = this.GetService()) {
     71        var service = channelFactory.Obj.CreateChannel();
     72        return call(service);
     73      } // disposing the channelfactory is done by the disposable object; the channel gets disposed when the channelfactory is disposed
     74    }
     75
     76    T IServiceLocator.CallHiveService<T>(Func<IHiveService, T> call) {
     77      return CallHiveService<T>(call);
    4678    }
    4779  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/DataTransfer/Job.cs

    r5511 r5526  
    3939    [DataMember]
    4040    public DateTime? LastHeartbeat { get; set; }
     41    [DataMember]
     42    public bool IsParentJob { get; set; }
     43    [DataMember]
     44    public bool FinishWhenChildJobsFinished { get; set; }
    4145   
    4246    public Job() {
     
    4953      this.PluginsNeededIds = new List<Guid>(original.PluginsNeededIds);
    5054      this.LastHeartbeat = original.LastHeartbeat;
     55      this.IsParentJob = original.IsParentJob;
     56      this.FinishWhenChildJobsFinished = original.FinishWhenChildJobsFinished;
    5157    }
    5258    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/DataTransfer/JobState.cs

    r5511 r5526  
    3333    Waiting,
    3434
    35     /// <summary>
    36     /// The job is set to Finished when all child jobs are Finished.
    37     /// </summary>
    38     FinishOnChildJobsFinished,
     35    ///// <summary>
     36    ///// The job is set to Finished when all child jobs are Finished.
     37    ///// </summary>
     38    //FinishOnChildJobsFinished,
    3939
    40     /// <summary>
    41     /// The job is paused and waits on the server to be sent back to a Slave when all of its child jobs are Finished.
    42     /// </summary>
    43     ResumeOnChildJobsFinished,
     40    ///// <summary>
     41    ///// The job is paused and waits on the server to be sent back to a Slave when all of its child jobs are Finished.
     42    ///// </summary>
     43    //ResumeOnChildJobsFinished,
    4444
    4545    /// <summary>
     
    8181    /// </summary>
    8282    public static bool IsWaiting(this JobState jobState) {
    83       return jobState == JobState.Waiting ||
    84         jobState == JobState.FinishOnChildJobsFinished;
     83      return jobState == JobState.Waiting;
    8584    }
    8685
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/DataTransfer/LightweightJob.cs

    r5511 r5526  
    4848
    4949    public LightweightJob(Job job) {
     50      this.Id = job.Id;
    5051      this.ExecutionTime = job.ExecutionTime;
    5152      this.ParentJobId = job.ParentJobId;
    52       this.StateLog = new List<StateLog>();
     53      this.StateLog = new List<StateLog>(job.StateLog);
     54      this.State = job.State;
    5355    }
    5456    protected LightweightJob(LightweightJob original, Cloner cloner)
     
    5759      this.ParentJobId = original.ParentJobId;
    5860      this.StateLog = new List<StateLog>(original.StateLog);
     61      this.State = original.State;
    5962    }
    6063    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/ServiceContracts/IHiveService.cs

    r5511 r5526  
    4343    [OperationContract]
    4444    void DeleteChildJobs(Guid parentJobId);
    45 
    4645    #endregion
    4746
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Convert.cs

    r5511 r5526  
    4040        LastHeartbeat = source.LastHeartbeat,
    4141        State = source.State,
    42         StateLog = source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList()
     42        StateLog = source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList(),
     43        IsParentJob = source.IsParentJob,
     44        FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished
    4345      };
    4446    }
     
    5961        target.State = source.State;
    6062        if (target.StateLogs == null) target.StateLogs = new EntitySet<StateLog>();
    61         target.StateLogs.AddRange(source.StateLog.Select(x => Convert.ToEntity(x)).OrderBy(x => x.DateTime));
     63        foreach (DT.StateLog sl in source.StateLog.Where(x => x.Id == Guid.Empty)) {
     64          target.StateLogs.Add(Convert.ToEntity(sl));
     65        }
     66       
     67        //target.StateLogs.AddRange(source.StateLog.Select(x => Convert.ToEntity(x)).OrderBy(x => x.DateTime));
     68        target.IsParentJob = source.IsParentJob;
     69        target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
    6270        // RequiredPlugins are added by Dao
    6371      }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs

    r5511 r5526  
    8282    }
    8383
    84     public IEnumerable<DT.Job> GetWaitingParentJobs(IEnumerable<Guid> resourceIds, int count) {
     84    /// <summary>
     85    /// returns all parent jobs which are waiting for their child jobs to finish
     86    /// </summary>
     87    /// <param name="resourceIds">list of resourceids which for which the jobs should be valid</param>
     88    /// <param name="count">maximum number of jobs to return</param>
     89    /// <param name="finished">if true, all parent jobs which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
     90    /// <returns></returns>
     91    public IEnumerable<DT.Job> GetParentJobs(IEnumerable<Guid> resourceIds, int count, bool finished) {
    8592      using (var db = CreateContext()) {
    8693        var query = from ar in db.AssignedResources
    87                     join sl in db.StateLogs on ar.JobId equals sl.JobId
    8894                    where resourceIds.Contains(ar.ResourceId)
    89                        && ar.Job.State == JobState.FinishOnChildJobsFinished
     95                       && ar.Job.State == JobState.Waiting
     96                       && ar.Job.IsParentJob
     97                       && (finished ? ar.Job.FinishWhenChildJobsFinished : !ar.Job.FinishWhenChildJobsFinished)
    9098                       && (from child in db.Jobs
    9199                           where child.ParentJobId == ar.Job.JobId
     
    103111      using (var db = CreateContext()) {
    104112        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
    105         var waitingParentJobs = GetWaitingParentJobs(resourceIds, count);
     113        var waitingParentJobs = GetParentJobs(resourceIds, count, false);
    106114        if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
    107115
    108116        var query = from ar in db.AssignedResources
    109117                    where resourceIds.Contains(ar.ResourceId)
     118                       && !(ar.Job.IsParentJob && ar.Job.FinishWhenChildJobsFinished)
    110119                       && ar.Job.State == JobState.Waiting
    111120                       && ar.Job.CoresNeeded <= slave.FreeCores
     
    160169    #endregion
    161170
     171    #region StateLog Methods
     172
     173    public DT.StateLog GetStateLog(Guid id) {
     174      using (var db = CreateContext()) {
     175        return Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
     176      }
     177    }
     178
     179    public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
     180      using (var db = CreateContext()) {
     181        return db.StateLogs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
     182      }
     183    }
     184
     185    public Guid AddStateLog(DT.StateLog dto) {
     186      using (var db = CreateContext()) {
     187        var entity = Convert.ToEntity(dto);
     188        db.StateLogs.InsertOnSubmit(entity);
     189        db.SubmitChanges();
     190        return entity.StateLogId;
     191      }
     192    }
     193
     194    public void UpdateStateLog(DT.StateLog dto) {
     195      using (var db = CreateContext()) {
     196        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
     197        if (entity == null) db.StateLogs.InsertOnSubmit(Convert.ToEntity(dto));
     198        else Convert.ToEntity(dto, entity);
     199        db.SubmitChanges();
     200      }
     201    }
     202
     203    public void DeleteStateLog(Guid id) {
     204      using (var db = CreateContext()) {
     205        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
     206        if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
     207        db.SubmitChanges();
     208      }
     209    }
     210    #endregion
     211
    162212    #region HiveExperiment Methods
    163213    public DT.HiveExperiment GetHiveExperiment(Guid id) {
     
    507557
    508558    #endregion
     559
    509560  }
    510561}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml

    r5511 r5526  
    6060    <Type Name="Job">
    6161      <Column Name="JobId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
     62      <Column Name="JobState" Member="State" Type="global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState" DbType="VarChar(30)" CanBeNull="false" />
     63      <Column Name="ExecutionTime" Type="System.String" DbType="VarChar(30)" CanBeNull="true" />
     64      <Column Name="LastHeartbeat" Type="System.DateTime" DbType="DateTime" CanBeNull="true" />
    6265      <Column Name="ParentJobId" Type="System.Guid" DbType="UniqueIdentifier" CanBeNull="true" />
    63       <Column Name="ExecutionTime" Type="System.String" DbType="VarChar(30)" CanBeNull="true" />
    6466      <Column Name="Priority" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
    6567      <Column Name="CoresNeeded" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
    6668      <Column Name="MemoryNeeded" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
    67       <Column Name="LastHeartbeat" Type="System.DateTime" DbType="DateTime" CanBeNull="true" />
    68       <Column Name="JobState" Member="State" Type="global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState" DbType="VarChar(30)" CanBeNull="false" />
     69      <Column Name="IsParentJob" Type="System.Boolean" DbType="Bit" CanBeNull="false" />
     70      <Column Name="FinishWhenChildJobsFinished" Type="System.Boolean" DbType="Bit" CanBeNull="false" />
    6971      <Association Name="Job_AssignedResource" Member="AssignedResources" ThisKey="JobId" OtherKey="JobId" Type="AssignedResource" />
    7072      <Association Name="Job_RequiredPlugin" Member="RequiredPlugins" ThisKey="JobId" OtherKey="JobId" Type="RequiredPlugin" />
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml.layout

    r5511 r5526  
    2727      </nestedChildShapes>
    2828    </classShape>
    29     <classShape Id="695bfc39-59f3-4e60-8644-f847964bf62c" absoluteBounds="6.5, 1, 2, 2.3478011067708335">
     29    <classShape Id="695bfc39-59f3-4e60-8644-f847964bf62c" absoluteBounds="6.5, 1, 2, 2.7324039713541666">
    3030      <DataClassMoniker Name="/HiveDataContext/Job" />
    3131      <nestedChildShapes>
    32         <elementListCompartment Id="a6a30e11-03d1-4869-82e6-b733f4ef9974" absoluteBounds="6.5150000000000006, 1.46, 1.9700000000000002, 1.7878011067708333" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
     32        <elementListCompartment Id="a6a30e11-03d1-4869-82e6-b733f4ef9974" absoluteBounds="6.5150000000000006, 1.46, 1.9700000000000002, 2.1724039713541665" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
    3333      </nestedChildShapes>
    3434    </classShape>
     
    7676      </nodes>
    7777    </associationConnector>
    78     <associationConnector edgePoints="[(8.5 : 3.34780110677083); (8.875 : 3.875)]" fixedFrom="NotFixed" fixedTo="NotFixed">
     78    <associationConnector edgePoints="[(8.5 : 3.73240397135417); (8.875 : 3.875)]" fixedFrom="NotFixed" fixedTo="NotFixed">
    7979      <AssociationMoniker Name="/HiveDataContext/Job/Job_AssignedResource" />
    8080      <nodes>
     
    8383      </nodes>
    8484    </associationConnector>
    85     <associationConnector edgePoints="[(7.4687475 : 3.34780110677083); (7.4687475 : 5.5)]" fixedFrom="Algorithm" fixedTo="Algorithm">
     85    <associationConnector edgePoints="[(7.4687475 : 3.73240397135417); (7.4687475 : 5.5)]" fixedFrom="Algorithm" fixedTo="Algorithm">
    8686      <AssociationMoniker Name="/HiveDataContext/Job/Job_RequiredPlugin" />
    8787      <nodes>
     
    104104      </nodes>
    105105    </associationConnector>
    106     <associationConnector edgePoints="[(6.5 : 2.98640055338542); (6.125 : 2.98640055338542)]" fixedFrom="Algorithm" fixedTo="Algorithm">
     106    <associationConnector edgePoints="[(6.5 : 3.17870198567708); (6.125 : 3.17870198567708)]" fixedFrom="Algorithm" fixedTo="Algorithm">
    107107      <AssociationMoniker Name="/HiveDataContext/Job/Job_HiveExperiment" />
    108108      <nodes>
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.designer.cs

    r5511 r5526  
    13931393    private System.Guid _JobId;
    13941394   
     1395    private global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState _State;
     1396   
     1397    private string _ExecutionTime;
     1398   
     1399    private System.Nullable<System.DateTime> _LastHeartbeat;
     1400   
    13951401    private System.Nullable<System.Guid> _ParentJobId;
    13961402   
    1397     private string _ExecutionTime;
    1398    
    13991403    private int _Priority;
    14001404   
     
    14031407    private int _MemoryNeeded;
    14041408   
    1405     private System.Nullable<System.DateTime> _LastHeartbeat;
    1406    
    1407     private global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState _State;
     1409    private bool _IsParentJob;
     1410   
     1411    private bool _FinishWhenChildJobsFinished;
    14081412   
    14091413    private EntitySet<AssignedResource> _AssignedResources;
     
    14251429    partial void OnJobIdChanging(System.Guid value);
    14261430    partial void OnJobIdChanged();
     1431    partial void OnStateChanging(global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState value);
     1432    partial void OnStateChanged();
     1433    partial void OnExecutionTimeChanging(string value);
     1434    partial void OnExecutionTimeChanged();
     1435    partial void OnLastHeartbeatChanging(System.Nullable<System.DateTime> value);
     1436    partial void OnLastHeartbeatChanged();
    14271437    partial void OnParentJobIdChanging(System.Nullable<System.Guid> value);
    14281438    partial void OnParentJobIdChanged();
    1429     partial void OnExecutionTimeChanging(string value);
    1430     partial void OnExecutionTimeChanged();
    14311439    partial void OnPriorityChanging(int value);
    14321440    partial void OnPriorityChanged();
     
    14351443    partial void OnMemoryNeededChanging(int value);
    14361444    partial void OnMemoryNeededChanged();
    1437     partial void OnLastHeartbeatChanging(System.Nullable<System.DateTime> value);
    1438     partial void OnLastHeartbeatChanged();
    1439     partial void OnStateChanging(global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState value);
    1440     partial void OnStateChanged();
     1445    partial void OnIsParentJobChanging(bool value);
     1446    partial void OnIsParentJobChanged();
     1447    partial void OnFinishWhenChildJobsFinishedChanging(bool value);
     1448    partial void OnFinishWhenChildJobsFinishedChanged();
    14411449    #endregion
    14421450   
     
    14721480    }
    14731481   
     1482    [global::System.Data.Linq.Mapping.ColumnAttribute(Name="JobState", Storage="_State", DbType="VarChar(30)", CanBeNull=false)]
     1483    public global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState State
     1484    {
     1485      get
     1486      {
     1487        return this._State;
     1488      }
     1489      set
     1490      {
     1491        if ((this._State != value))
     1492        {
     1493          this.OnStateChanging(value);
     1494          this.SendPropertyChanging();
     1495          this._State = value;
     1496          this.SendPropertyChanged("State");
     1497          this.OnStateChanged();
     1498        }
     1499      }
     1500    }
     1501   
     1502    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ExecutionTime", DbType="VarChar(30)")]
     1503    public string ExecutionTime
     1504    {
     1505      get
     1506      {
     1507        return this._ExecutionTime;
     1508      }
     1509      set
     1510      {
     1511        if ((this._ExecutionTime != value))
     1512        {
     1513          this.OnExecutionTimeChanging(value);
     1514          this.SendPropertyChanging();
     1515          this._ExecutionTime = value;
     1516          this.SendPropertyChanged("ExecutionTime");
     1517          this.OnExecutionTimeChanged();
     1518        }
     1519      }
     1520    }
     1521   
     1522    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastHeartbeat", DbType="DateTime")]
     1523    public System.Nullable<System.DateTime> LastHeartbeat
     1524    {
     1525      get
     1526      {
     1527        return this._LastHeartbeat;
     1528      }
     1529      set
     1530      {
     1531        if ((this._LastHeartbeat != value))
     1532        {
     1533          this.OnLastHeartbeatChanging(value);
     1534          this.SendPropertyChanging();
     1535          this._LastHeartbeat = value;
     1536          this.SendPropertyChanged("LastHeartbeat");
     1537          this.OnLastHeartbeatChanged();
     1538        }
     1539      }
     1540    }
     1541   
    14741542    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ParentJobId", DbType="UniqueIdentifier")]
    14751543    public System.Nullable<System.Guid> ParentJobId
     
    14961564    }
    14971565   
    1498     [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ExecutionTime", DbType="VarChar(30)")]
    1499     public string ExecutionTime
    1500     {
    1501       get
    1502       {
    1503         return this._ExecutionTime;
    1504       }
    1505       set
    1506       {
    1507         if ((this._ExecutionTime != value))
    1508         {
    1509           this.OnExecutionTimeChanging(value);
    1510           this.SendPropertyChanging();
    1511           this._ExecutionTime = value;
    1512           this.SendPropertyChanged("ExecutionTime");
    1513           this.OnExecutionTimeChanged();
    1514         }
    1515       }
    1516     }
    1517    
    15181566    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Priority", DbType="Int NOT NULL")]
    15191567    public int Priority
     
    15761624    }
    15771625   
    1578     [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastHeartbeat", DbType="DateTime")]
    1579     public System.Nullable<System.DateTime> LastHeartbeat
    1580     {
    1581       get
    1582       {
    1583         return this._LastHeartbeat;
    1584       }
    1585       set
    1586       {
    1587         if ((this._LastHeartbeat != value))
    1588         {
    1589           this.OnLastHeartbeatChanging(value);
    1590           this.SendPropertyChanging();
    1591           this._LastHeartbeat = value;
    1592           this.SendPropertyChanged("LastHeartbeat");
    1593           this.OnLastHeartbeatChanged();
    1594         }
    1595       }
    1596     }
    1597    
    1598     [global::System.Data.Linq.Mapping.ColumnAttribute(Name="JobState", Storage="_State", DbType="VarChar(30)", CanBeNull=false)]
    1599     public global::HeuristicLab.Services.Hive.Common.DataTransfer.JobState State
    1600     {
    1601       get
    1602       {
    1603         return this._State;
    1604       }
    1605       set
    1606       {
    1607         if ((this._State != value))
    1608         {
    1609           this.OnStateChanging(value);
    1610           this.SendPropertyChanging();
    1611           this._State = value;
    1612           this.SendPropertyChanged("State");
    1613           this.OnStateChanged();
     1626    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsParentJob", DbType="Bit")]
     1627    public bool IsParentJob
     1628    {
     1629      get
     1630      {
     1631        return this._IsParentJob;
     1632      }
     1633      set
     1634      {
     1635        if ((this._IsParentJob != value))
     1636        {
     1637          this.OnIsParentJobChanging(value);
     1638          this.SendPropertyChanging();
     1639          this._IsParentJob = value;
     1640          this.SendPropertyChanged("IsParentJob");
     1641          this.OnIsParentJobChanged();
     1642        }
     1643      }
     1644    }
     1645   
     1646    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_FinishWhenChildJobsFinished", DbType="Bit")]
     1647    public bool FinishWhenChildJobsFinished
     1648    {
     1649      get
     1650      {
     1651        return this._FinishWhenChildJobsFinished;
     1652      }
     1653      set
     1654      {
     1655        if ((this._FinishWhenChildJobsFinished != value))
     1656        {
     1657          this.OnFinishWhenChildJobsFinishedChanging(value);
     1658          this.SendPropertyChanging();
     1659          this._FinishWhenChildJobsFinished = value;
     1660          this.SendPropertyChanged("FinishWhenChildJobsFinished");
     1661          this.OnFinishWhenChildJobsFinishedChanged();
    16141662        }
    16151663      }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Interfaces/IHiveDao.cs

    r5511 r5526  
    1515    void DeleteJob(Guid id);
    1616    IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count);
     17    IEnumerable<DT.Job> GetParentJobs(IEnumerable<Guid> resourceIds, int count, bool finished);
    1718    #endregion
    1819
     
    2324    void UpdateJobData(DT.JobData dto);
    2425    void DeleteJobData(Guid id);
     26    #endregion
     27
     28    #region StateLog Methods
     29    DT.StateLog GetStateLog(Guid id);
     30    IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate);
     31    Guid AddStateLog(DT.StateLog dto);
     32    void UpdateStateLog(DT.StateLog dto);
     33    void DeleteStateLog(Guid id);
    2534    #endregion
    2635
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/DaoTests.cs

    r5511 r5526  
    77
    88namespace HeuristicLab.Services.Hive.Tests {
     9  using System.Threading;
    910  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
    1011
     
    1718      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
    1819    }
    19    
     20
    2021    private IHiveService GetLocalService() {
    2122      return new HiveService();
     
    2526    public void TestJobDao() {
    2627      IHiveDao dao = ServiceLocator.Instance.HiveDao;
    27      
     28
    2829      DT.Job job1 = new DT.Job();
    2930      job1.SetState(JobState.Offline, Guid.NewGuid());
     
    6465        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
    6566      }
     67     
     68      job1 = job1loaded;
     69
     70      // test jobstates
     71      job1.SetState(JobState.Transferring); Thread.Sleep(10);
     72      job1.SetState(JobState.Calculating); Thread.Sleep(10);
     73      job1.SetState(JobState.Transferring); Thread.Sleep(10);
     74      job1.SetState(JobState.Finished); Thread.Sleep(10);
     75      dao.UpdateJob(job1);
     76
     77      job1loaded = dao.GetJob(job1.Id);
     78      for (int i = 0; i < job1.StateLog.Count; i++) {
     79        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
     80        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
     81        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
     82        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
     83        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
     84        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
     85      }
    6686
    6787      dao.DeleteJob(job1.Id);
     
    175195      //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
    176196      //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
    177      
     197
    178198      DT.Job jobLoaded = dao.GetJob(he.RootJobId);
    179199      Assert.AreEqual(job.Id, jobLoaded.Id);
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/HeuristicLab 3.3.exe.config

    r5095 r5526  
    2727    </roleManager>
    2828  </system.web>
     29 
    2930  <system.serviceModel>
    3031    <behaviors>
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/HeuristicLab.Services.Hive.Tests-3.4.csproj

    r5402 r5526  
    333333    <Reference Include="System.Drawing" />
    334334    <Reference Include="System.Runtime.Serialization" />
     335    <Reference Include="System.Security" />
    335336    <Reference Include="System.ServiceModel" />
    336337    <Reference Include="System.ServiceProcess" />
     338    <Reference Include="System.Web" />
     339    <Reference Include="System.Web.ApplicationServices" />
    337340    <Reference Include="System.Windows.Forms" />
    338341    <Reference Include="System.Xml" />
     
    397400    <None Include="HeuristicLab 3.3.exe.config">
    398401      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     402      <SubType>Designer</SubType>
    399403    </None>
    400404    <Shadow Include="Test References\HeuristicLab.Clients.Hive.Views-3.4.accessor" />
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/ServiceTests.cs

    r5511 r5526  
    2424using System.Linq;
    2525using System.Threading;
    26 using HeuristicLab.Clients.Hive;
    27 using HeuristicLab.Clients.Hive.Slave.Tests;
    28 using HeuristicLab.Hive;
    2926using HeuristicLab.Services.Hive.Common;
    3027using HeuristicLab.Services.Hive.Common.DataTransfer;
     
    3431namespace HeuristicLab.Services.Hive.Tests {
    3532
    36   using System.Diagnostics;
    3733  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
    3834
     
    7066      DT.JobData jobData = new DT.JobData() {
    7167        //Data = PersistenceUtil.Serialize(new MockJob(500, true))
    72         Data = new byte[10000000]
     68        Data = new byte[10000]
    7369      };
    7470
     
    119115      Assert.AreEqual(JobState.Waiting, jobLoaded.State);
    120116      Assert.IsTrue(job.PluginsNeededIds.SequenceEqual(jobLoaded.PluginsNeededIds));
    121      
     117
    122118      DT.JobData jobDataLoaded = service.GetJobData(job.Id);
    123119      Assert.AreEqual(job.Id, jobDataLoaded.JobId);
     
    132128
    133129      // test assigned ressources
    134       var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid,TimeSpan>() });
     130      var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
    135131      Assert.AreEqual(1, actions.Count);
    136132      Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
     
    169165    List<DT.Job> jobs = new List<DT.Job>();
    170166
     167
    171168    [TestMethod]
    172     public void TestHeartbeats() {
     169    public void TestParentJobs() {
    173170      var service = GetLocalService();
    174       // check if group already exists and delete
    175       var existingSlaveGroup = service.GetSlaveGroups().SingleOrDefault(g => g.Name == "TestGroup");
    176       if (existingSlaveGroup != null) {
    177         var slavesToDelete = service.GetSlaves().Where(s => s.ParentResourceId == existingSlaveGroup.Id);
    178         foreach (var slave in slavesToDelete) service.DeleteSlave(slave.Id);
    179         service.DeleteSlaveGroup(existingSlaveGroup.Id);
    180       }
    181 
    182       Guid groupId = service.AddSlaveGroup(new SlaveGroup() { Name = "TestGroup", Description = "Used for unit tests" });
    183 
    184       // create slaves
    185       var slaves = new List<DT.Slave>();
    186       for (int i = 0; i < 1; i++) {
    187         DT.Slave slave = new DT.Slave() {
    188           Cores = 2,
    189           Memory = 4096,
    190           Name = "Slave " + i,
    191           IsAllowedToCalculate = true,
    192           SlaveState = SlaveState.Idle,
    193           CpuSpeed = 2800,
    194           FreeCores = 2,
    195           FreeMemory = 3000
    196         };
    197         // check if slave with this name already exists and delete
    198         var existingSlave = service.GetSlaves().Where(s => s.Name == slave.Name).SingleOrDefault();
    199         if (existingSlave != null) service.DeleteSlave(existingSlave.Id);
    200 
    201         slave.Id = service.AddSlave(slave);
    202         service.AddResourceToGroup(groupId, slave.Id);
    203         slaves.Add(slave);
    204       }
    205 
    206       // create jobs with different group, they should not be assigned
    207       existingSlaveGroup = service.GetSlaveGroups().SingleOrDefault(g => g.Name == "DummyGroup");
    208       if (existingSlaveGroup != null) service.DeleteSlaveGroup(existingSlaveGroup.Id);
    209 
    210       Guid dummyGroupId = service.AddSlaveGroup(new SlaveGroup() { Name = "DummyGroup", Description = "Used for unit tests; jobs from this group shall not be calculated" });
    211       // create dummy jobs
    212       var dummyJobs = new List<Job>();
    213       for (int i = 0; i < 2; i++) {
    214         Job job = new Job() {
    215           CoresNeeded = 1, MemoryNeeded = 0
    216         };
    217         JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
    218         job.Id = service.AddJob(job, jobData, new List<Guid> { dummyGroupId });
    219         dummyJobs.Add(job);
    220       }
    221 
    222       // create jobs
    223       for (int i = 0; i < 2; i++) {
    224         Job job = new Job() {
    225           CoresNeeded = 1, MemoryNeeded = 0
    226         };
    227         JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
    228         job.Id = service.AddJob(job, jobData, new List<Guid> { groupId });
    229         jobs.Add(job);
    230       }
    231 
    232       // send heartbeats
    233       foreach (var slave in slaves) {
    234         new Thread(new ParameterizedThreadStart(RunSlaveThread)).Start(slave);
    235       }
    236 
    237       IEnumerable<LightweightJob> lightweightJobs;
    238       do {
    239         Thread.Sleep(500);
    240         lightweightJobs = service.GetLightweightJobs(jobs.Select(x => x.Id));
    241       } while (!lightweightJobs.All(x => x.State == JobState.Finished));
    242 
    243       // delete slaves
    244       foreach (var slave in slaves) {
    245         service.DeleteSlave(slave.Id);
    246         Assert.AreEqual(null, service.GetSlave(slave.Id));
    247       }
    248 
    249       // delete groups
    250       service.DeleteSlaveGroup(groupId);
    251       service.DeleteSlaveGroup(dummyGroupId);
    252 
    253       // delete jobs
    254       foreach (var job in jobs) {
    255         service.DeleteJob(job.Id);
    256       }
    257 
    258       // delete dummy jobs
    259       foreach (var job in dummyJobs) {
    260         service.DeleteJob(job.Id);
    261       }
     171
     172      // create hive experiment
     173      DT.HiveExperiment experiment = new DT.HiveExperiment() {
     174        Name = "TestExperiment",
     175        Description = ""
     176      };
     177
     178      // create parent job
     179      DT.Job parentJob = new DT.Job() {
     180        CoresNeeded = 1,
     181        MemoryNeeded = 0,
     182        Priority = 0,
     183        IsParentJob = true,
     184        FinishWhenChildJobsFinished = true
     185      };
     186      parentJob.SetState(JobState.Offline);
     187
     188      DT.JobData parentJobData = new DT.JobData() { Data = new byte[0] };
     189
     190      // create child job
     191      DT.Job childJob = new DT.Job() {
     192        CoresNeeded = 1,
     193        MemoryNeeded = 0,
     194        Priority = 0
     195      };
     196      parentJob.SetState(JobState.Offline);
     197
     198      DT.JobData childJobData = new DT.JobData() { Data = new byte[1000] };
     199
     200      // create slave
     201      DT.Slave slave = new Slave();
     202      slave.Id = Guid.NewGuid();
     203      slave.Name = "TestSlave";
     204      slave.Memory = 1024;
     205      slave.Cores = 4;
     206      slave.CpuSpeed = 2800;
     207      slave.OperatingSystem = "Windows 3.11";
     208      slave.CpuArchitecture = CpuArchitecture.x64;
     209
     210      // add slave
     211      service.AddSlave(slave);
     212
     213      // add parent job
     214      parentJob.Id = service.AddJob(parentJob, parentJobData, new List<Guid> { slave.Id });
     215      experiment.RootJobId = parentJob.Id;
     216
     217      // add child job
     218      childJob.Id = service.AddChildJob(parentJob.Id, childJob, childJobData);
     219      childJob.ParentJobId = parentJob.Id;
     220
     221      // add hive experiment
     222      experiment.Id = service.AddHiveExperiment(experiment);
     223
     224      // test child job
     225      var childJobLoaded = service.GetJob(childJob.Id);
     226      Assert.AreEqual(childJob.ParentJobId, childJobLoaded.ParentJobId);
     227      Assert.AreEqual(JobState.Waiting, childJobLoaded.State);
     228      Assert.AreEqual(false, childJobLoaded.FinishWhenChildJobsFinished);
     229      Assert.AreEqual(false, childJobLoaded.IsParentJob);
     230
     231      // test parent job
     232      var parentJobLoaded = service.GetJob(parentJob.Id);
     233      Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
     234      Assert.AreEqual(true, parentJobLoaded.FinishWhenChildJobsFinished);
     235      Assert.AreEqual(true, parentJobLoaded.IsParentJob);
     236
     237      // test heartbeat
     238      var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
     239      Assert.AreEqual(1, actions.Count); // only the child job should be assigned
     240      Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
     241      Assert.AreEqual(childJob.Id, actions[0].JobId);
     242
     243      // lifecycle - let it process one server-heartbeat; the parent job must NOT be set to finished
     244      var lifecycleManager = new LifecycleManager();
     245      lifecycleManager.Start();
     246      Thread.Sleep(1000);
     247      lifecycleManager.Stop();
     248
     249      parentJobLoaded = service.GetJob(parentJob.Id);
     250      Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
     251
     252      // set child job to finished
     253      childJobLoaded.SetState(JobState.Finished, slave.Id, "");
     254      service.UpdateJob(childJobLoaded);
     255
     256      // lifecycle - let it process one server-heartbeat; this should set the parent job to finished
     257      lifecycleManager.Start();
     258      Thread.Sleep(1000);
     259      lifecycleManager.Stop();
     260
     261      // test if parent job is finished
     262      parentJobLoaded = service.GetJob(parentJob.Id);
     263      Assert.AreEqual(JobState.Finished, parentJobLoaded.State);
     264
     265      // delete experiment
     266      service.DeleteHiveExperiment(experiment.Id);
     267      Assert.AreEqual(null, service.GetJob(parentJob.Id));
     268      Assert.AreEqual(null, service.GetJob(childJob.Id));
     269
     270      service.DeleteSlave(slave.Id);
    262271    }
    263272
    264     public void RunSlaveThread(object slaveobj) {
    265       try {
    266         var service = GetLocalService();
    267         Slave slave = (Slave)slaveobj;
    268         int freeCores = slave.Cores.Value;
    269 
    270         for (int i = 0; i < 10; i++) {
    271 
    272           var messages = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, FreeMemory = 2423, FreeCores = freeCores, JobProgress = new Dictionary<Guid, TimeSpan>() });
    273           if (messages.Count == 0) {
    274             Debug.WriteLine("No job available");
    275             return; // no more jobs
    276           }
    277 
    278           Debug.WriteLine("Messages: {0}", string.Join(", ", messages.Select(m => m.Message)));
    279 
    280           Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.AbortJob).Count() == 0);
    281           Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.SayHello).Count() == 0);
    282           Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.PauseJob).Count() == 0);
    283 
    284           var calculateJobMessage = messages.Where(x => x.Message == MessageContainer.MessageType.CalculateJob).SingleOrDefault();
    285           if (calculateJobMessage != null) {
    286             if (!jobs.Select(j => j.Id).Contains(calculateJobMessage.JobId))
    287               Assert.Fail("Got job which was not assigned to the slavegroup");
    288 
    289             Debug.WriteLine("Job available, calculating");
    290             Job job = service.GetJob(calculateJobMessage.JobId);
    291 
    292             JobData jobData = service.GetJobData(job.Id);
    293             IJob deserializedJob = PersistenceUtil.Deserialize<IJob>(jobData.Data);
    294             deserializedJob.Start();
    295             job.SetState(JobState.Finished);
    296             jobs.Where(x => x.Id == job.Id).Single().SetState(JobState.Finished);
    297             jobData.Data = PersistenceUtil.Serialize(deserializedJob);
    298             service.UpdateJobData(job, jobData);
    299             Debug.WriteLine("finished calculating");
    300           }
    301         }
    302       }
    303       catch (Exception e) {
    304         Assert.Fail(e.Message, e);
    305       }
    306     }
     273    //[TestMethod]
     274    //public void TestHeartbeats() {
     275    //  var service = GetLocalService();
     276    //  // check if group already exists and delete
     277    //  var existingSlaveGroup = service.GetSlaveGroups().SingleOrDefault(g => g.Name == "TestGroup");
     278    //  if (existingSlaveGroup != null) {
     279    //    var slavesToDelete = service.GetSlaves().Where(s => s.ParentResourceId == existingSlaveGroup.Id);
     280    //    foreach (var slave in slavesToDelete) service.DeleteSlave(slave.Id);
     281    //    service.DeleteSlaveGroup(existingSlaveGroup.Id);
     282    //  }
     283
     284    //  Guid groupId = service.AddSlaveGroup(new SlaveGroup() { Name = "TestGroup", Description = "Used for unit tests" });
     285
     286    //  // create slaves
     287    //  var slaves = new List<DT.Slave>();
     288    //  for (int i = 0; i < 1; i++) {
     289    //    DT.Slave slave = new DT.Slave() {
     290    //      Cores = 2,
     291    //      Memory = 4096,
     292    //      Name = "Slave " + i,
     293    //      IsAllowedToCalculate = true,
     294    //      SlaveState = SlaveState.Idle,
     295    //      CpuSpeed = 2800,
     296    //      FreeCores = 2,
     297    //      FreeMemory = 3000
     298    //    };
     299    //    // check if slave with this name already exists and delete
     300    //    var existingSlave = service.GetSlaves().Where(s => s.Name == slave.Name).SingleOrDefault();
     301    //    if (existingSlave != null) service.DeleteSlave(existingSlave.Id);
     302
     303    //    slave.Id = service.AddSlave(slave);
     304    //    service.AddResourceToGroup(groupId, slave.Id);
     305    //    slaves.Add(slave);
     306    //  }
     307
     308    //  // create jobs with different group, they should not be assigned
     309    //  existingSlaveGroup = service.GetSlaveGroups().SingleOrDefault(g => g.Name == "DummyGroup");
     310    //  if (existingSlaveGroup != null) service.DeleteSlaveGroup(existingSlaveGroup.Id);
     311
     312    //  Guid dummyGroupId = service.AddSlaveGroup(new SlaveGroup() { Name = "DummyGroup", Description = "Used for unit tests; jobs from this group shall not be calculated" });
     313    //  // create dummy jobs
     314    //  var dummyJobs = new List<Job>();
     315    //  for (int i = 0; i < 2; i++) {
     316    //    Job job = new Job() {
     317    //      CoresNeeded = 1, MemoryNeeded = 0
     318    //    };
     319    //    JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
     320    //    job.Id = service.AddJob(job, jobData, new List<Guid> { dummyGroupId });
     321    //    dummyJobs.Add(job);
     322    //  }
     323
     324    //  // create jobs
     325    //  for (int i = 0; i < 2; i++) {
     326    //    Job job = new Job() {
     327    //      CoresNeeded = 1, MemoryNeeded = 0
     328    //    };
     329    //    JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
     330    //    job.Id = service.AddJob(job, jobData, new List<Guid> { groupId });
     331    //    jobs.Add(job);
     332    //  }
     333
     334    //  // send heartbeats
     335    //  foreach (var slave in slaves) {
     336    //    new Thread(new ParameterizedThreadStart(RunSlaveThread)).Start(slave);
     337    //  }
     338
     339    //  IEnumerable<LightweightJob> lightweightJobs;
     340    //  do {
     341    //    Thread.Sleep(500);
     342    //    lightweightJobs = service.GetLightweightJobs(jobs.Select(x => x.Id));
     343    //  } while (!lightweightJobs.All(x => x.State == JobState.Finished));
     344
     345    //  // delete slaves
     346    //  foreach (var slave in slaves) {
     347    //    service.DeleteSlave(slave.Id);
     348    //    Assert.AreEqual(null, service.GetSlave(slave.Id));
     349    //  }
     350
     351    //  // delete groups
     352    //  service.DeleteSlaveGroup(groupId);
     353    //  service.DeleteSlaveGroup(dummyGroupId);
     354
     355    //  // delete jobs
     356    //  foreach (var job in jobs) {
     357    //    service.DeleteJob(job.Id);
     358    //  }
     359
     360    //  // delete dummy jobs
     361    //  foreach (var job in dummyJobs) {
     362    //    service.DeleteJob(job.Id);
     363    //  }
     364    //}
     365
     366    //public void RunSlaveThread(object slaveobj) {
     367    //  try {
     368    //    var service = GetLocalService();
     369    //    Slave slave = (Slave)slaveobj;
     370    //    int freeCores = slave.Cores.Value;
     371
     372    //    for (int i = 0; i < 10; i++) {
     373
     374    //      var messages = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, FreeMemory = 2423, FreeCores = freeCores, JobProgress = new Dictionary<Guid, TimeSpan>() });
     375    //      if (messages.Count == 0) {
     376    //        Debug.WriteLine("No job available");
     377    //        return; // no more jobs
     378    //      }
     379
     380    //      Debug.WriteLine("Messages: {0}", string.Join(", ", messages.Select(m => m.Message)));
     381
     382    //      Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.AbortJob).Count() == 0);
     383    //      Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.SayHello).Count() == 0);
     384    //      Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.PauseJob).Count() == 0);
     385
     386    //      var calculateJobMessage = messages.Where(x => x.Message == MessageContainer.MessageType.CalculateJob).SingleOrDefault();
     387    //      if (calculateJobMessage != null) {
     388    //        if (!jobs.Select(j => j.Id).Contains(calculateJobMessage.JobId))
     389    //          Assert.Fail("Got job which was not assigned to the slavegroup");
     390
     391    //        Debug.WriteLine("Job available, calculating");
     392    //        Job job = service.GetJob(calculateJobMessage.JobId);
     393
     394    //        JobData jobData = service.GetJobData(job.Id);
     395    //        IJob deserializedJob = PersistenceUtil.Deserialize<IJob>(jobData.Data);
     396    //        deserializedJob.Start();
     397    //        job.SetState(JobState.Finished);
     398    //        jobs.Where(x => x.Id == job.Id).Single().SetState(JobState.Finished);
     399    //        jobData.Data = PersistenceUtil.Serialize(deserializedJob);
     400    //        service.UpdateJobData(job, jobData);
     401    //        Debug.WriteLine("finished calculating");
     402    //      }
     403    //    }
     404    //  }
     405    //  catch (Exception e) {
     406    //    Assert.Fail(e.Message, e);
     407    //  }
     408    //}
    307409  }
    308410}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs

    r5511 r5526  
    3131
    3232    #region Job Methods
    33     //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
    34     //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     33    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     34    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    3535    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> resourceIds) {
    3636      using (trans.OpenTransaction()) {
     
    5151    }
    5252
     53    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     54    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    5355    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
    5456      using (trans.OpenTransaction()) {
     
    5860    }
    5961
     62    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     63    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     64    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    6065    public Job GetJob(Guid jobId) {
    6166      return dao.GetJob(jobId);
    6267    }
    6368
     69    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     70    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    6471    public IEnumerable<Job> GetJobs() {
    6572      return dao.GetJobs(x => true);
    6673    }
    6774
     75    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     76    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    6877    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
    6978      return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
    7079    }
    7180
     81    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     82    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    7283    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
    7384      return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
    7485    }
    7586
     87    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     88    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     89    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    7690    public JobData GetJobData(Guid jobId) {
    7791      return dao.GetJobData(jobId);
    7892    }
    7993
     94    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     95    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     96    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    8097    public void UpdateJob(Job job) {
    8198      using (trans.OpenTransaction()) {
     
    84101    }
    85102
     103    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     104    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     105    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    86106    public void UpdateJobData(Job job, JobData jobData) {
    87107      using (trans.OpenTransaction()) {
     
    92112    }
    93113
     114    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     115    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     116    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    94117    public void DeleteJob(Guid jobId) {
    95118      using (trans.OpenTransaction()) {
     
    98121    }
    99122
     123    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     124    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     125    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    100126    public void DeleteChildJobs(Guid parentJobId) {
    101127      using (trans.OpenTransaction()) {
     
    111137
    112138    #region Job Control Methods
     139    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     140    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     141    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    113142    public void StopJob(Guid jobId) {
    114143      using (trans.OpenTransaction()) {
     
    116145      }
    117146    }
     147
     148    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     149    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     150    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    118151    public void PauseJob(Guid jobId) {
    119152      using (trans.OpenTransaction()) {
     
    124157
    125158    #region HiveExperiment Methods
    126 
     159    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     160    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    127161    public HiveExperiment GetHiveExperiment(Guid id) {
    128162      return dao.GetHiveExperiments(x =>
     
    132166    }
    133167
     168    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     169    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    134170    public IEnumerable<HiveExperiment> GetHiveExperiments() {
    135171      return dao.GetHiveExperiments(x => x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0);
    136172    }
    137173
     174    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     175    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    138176    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
    139177      using (trans.OpenTransaction()) {
     
    144182    }
    145183
     184    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     185    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    146186    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
    147187      using (trans.OpenTransaction()) {
     
    150190    }
    151191
     192    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     193    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    152194    public void DeleteHiveExperiment(Guid hiveExperimentId) {
    153195      using (trans.OpenTransaction()) {
     
    159201
    160202    #region Login Methods
     203    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    161204    public void Hello(Slave slaveInfo) {
    162205      using (trans.OpenTransaction()) {
     
    171214    }
    172215
     216    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    173217    public void GoodBye(Guid slaveId) {
    174218      using (trans.OpenTransaction()) {
     
    183227
    184228    #region Heartbeat Methods
     229    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    185230    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
    186231      using (trans.OpenTransaction()) {
     
    191236
    192237    #region Plugin Methods
     238    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     239    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    193240    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
    194241      using (trans.OpenTransaction()) {
     
    203250      }
    204251    }
     252
     253    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     254    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     255    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    205256    public IEnumerable<Plugin> GetPlugins() {
    206257      return dao.GetPlugins(x => true);
    207258    }
    208259
     260    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     261    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
     262    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    209263    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
    210264      List<PluginData> pluginDatas = new List<PluginData>();
     
    226280
    227281    #region Slave Methods
     282    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     283    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    228284    public Guid AddSlave(Slave slave) {
    229285      using (trans.OpenTransaction()) {
     
    232288    }
    233289
     290    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     291    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    234292    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
    235293      using (trans.OpenTransaction()) {
     
    238296    }
    239297
     298    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     299    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    240300    public Slave GetSlave(Guid slaveId) {
    241301      return dao.GetSlave(slaveId);
     
    246306    }
    247307
     308    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     309    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    248310    public IEnumerable<Slave> GetSlaves() {
    249311      return dao.GetSlaves(x => true);
    250312    }
    251313
     314    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     315    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    252316    public IEnumerable<SlaveGroup> GetSlaveGroups() {
    253317      return dao.GetSlaveGroups(x => true);
    254318    }
    255319
     320    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     321    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    256322    public void UpdateSlave(Slave slave) {
    257323      using (trans.OpenTransaction()) {
     
    260326    }
    261327
     328    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     329    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    262330    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
    263331      using (trans.OpenTransaction()) {
     
    266334    }
    267335
     336    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     337    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    268338    public void DeleteSlave(Guid slaveId) {
    269339      using (trans.OpenTransaction()) {
     
    272342    }
    273343
     344    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     345    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    274346    public void DeleteSlaveGroup(Guid slaveGroupId) {
    275347      using (trans.OpenTransaction()) {
     
    278350    }
    279351
     352    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     353    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    280354    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
    281355      using (trans.OpenTransaction()) {
     
    286360    }
    287361
     362    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     363    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    288364    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
    289365      using (trans.OpenTransaction()) {
     
    294370    }
    295371
     372    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
     373    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    296374    public Guid GetResourceId(string resourceName) {
    297375      using (trans.OpenTransaction()) {
     
    325403
    326404    #endregion
    327 
    328 
    329405  }
    330406}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/LifecycleManager.cs

    r5511 r5526  
    3636    public void Start() {
    3737      if (ExecutionState == Core.ExecutionState.Stopped) {
     38        // kick off the event immediately
     39        timer_Tick(this, EventArgs.Empty);
     40
     41        // start the timer
    3842        this.timer.Interval = (int)new TimeSpan(0, 0, 30).TotalMilliseconds;
    3943        this.timer.Start();
     
    5458      lock (locker) {
    5559        using (trans.OpenTransaction()) {
    56           var slaves = dao.GetSlaves(x => x.SlaveState != SlaveState.Offline);
    57           foreach (Slave slave in slaves) {
    58             if (!slave.LastHeartbeat.HasValue || (DateTime.Now - slave.LastHeartbeat.Value).TotalSeconds > ApplicationConstants.HeartbeatTimeout) {
    59               slave.SlaveState = SlaveState.Offline;
    60               AbortJobs(slave.Id);
    61               dao.UpdateSlave(slave);
    62             }
    63           }
     60          SetTimeoutSlavesOffline();
     61          FinishParentJobs();
    6462        }
    6563      }
    6664    }
    6765
    68     private void AbortJobs(Guid slaveId) {
    69       var jobs = dao.GetJobs(x => x.StateLogs.Last().SlaveId == slaveId);
     66    /// <summary>
     67    /// Searches for slaves which are timed out, puts them and their jobs offline
     68    /// </summary>
     69    private void SetTimeoutSlavesOffline() {
     70      var slaves = dao.GetSlaves(x => x.SlaveState != SlaveState.Offline);
     71      foreach (Slave slave in slaves) {
     72        if (!slave.LastHeartbeat.HasValue || (DateTime.Now - slave.LastHeartbeat.Value).TotalSeconds > ApplicationConstants.HeartbeatTimeout) {
     73          slave.SlaveState = SlaveState.Offline;
     74          SetJobsWaiting(slave.Id);
     75          dao.UpdateSlave(slave);
     76        }
     77      }
     78    }
     79
     80    /// <summary>
     81    /// Looks for parent jobs which have FinishWhenChildJobsFinished and set their state to finished
     82    /// </summary>
     83    private void FinishParentJobs() {
     84      var parentJobsToFinish = dao.GetParentJobs(dao.GetResources(x => true).Select(x => x.Id), 0, true);
     85      foreach (var job in parentJobsToFinish) {
     86        job.SetState(JobState.Finished);
     87        dao.UpdateJob(job);
     88      }
     89    }
     90
     91    private void SetJobsWaiting(Guid slaveId) {
     92      var jobs = dao.GetJobs(x => x.State == JobState.Calculating).Where(x => x.StateLog.Last().SlaveId == slaveId);
    7093      foreach (var j in jobs) {
    7194        j.StateLog.Add(new StateLog() {
    72           UserId = auth.UserId,
    7395          State = JobState.Waiting,
    7496          JobId = j.Id,
Note: See TracChangeset for help on using the changeset viewer.