Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs @ 5404

Last change on this file since 5404 was 5404, checked in by cneumuel, 13 years ago

#1233

  • changed the workflow of aquireing a new job from server.
    • if a job is available for calculation, the slave receives the jobId already with the heartbeats. The job is then exclusively assigned to this slave.
  • extended the metainfo for a slave by OperatingSystem and CpuArchitecture
  • enhanced the way plugin-dependencies are discovered by using the types used by XmlGenerator. Now only mimimum amount of plugins are transferred.
  • selection of waiting jobs now consideres assigned slave-group
  • more unit tests for service
  • added unit tests for experiment manager
File size: 9.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.ServiceModel;
5using HeuristicLab.Services.Hive.Common;
6using HeuristicLab.Services.Hive.Common.DataTransfer;
7using HeuristicLab.Services.Hive.Common.ServiceContracts;
8
9namespace HeuristicLab.Services.Hive {
10
11  /// <summary>
12  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
13  /// </summary>
14  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
15  public class HiveService : IHiveService {
16    private DataAccess.IHiveDao dao {
17      get { return ServiceLocator.Instance.HiveDao; }
18    }
19    private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
20      get { return ServiceLocator.Instance.TransactionManager; }
21    }
22    private IAuthorizationManager auth {
23      get { return ServiceLocator.Instance.AuthorizationManager; }
24    }
25    private ILifecycleManager lifecycleManager {
26      get { return ServiceLocator.Instance.LifecycleManager; }
27    }
28
29    #region Job Methods
30    //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
31    //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
32    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> slaveGroupIds) {
33      using (trans.OpenTransaction()) {
34        job.UserId = auth.UserId;
35        job.DateCreated = DateTime.Now;
36        job.JobState = JobState.Waiting;
37        job.Id = dao.AddJob(job);
38        jobData.JobId = job.Id;
39        jobData.LastUpdate = DateTime.Now;
40        if (slaveGroupIds != null) {
41          foreach (Guid slaveGroupId in slaveGroupIds) {
42            dao.AssignJobToResource(job.Id, slaveGroupId);
43          }
44        } else {
45          // todo: use default group
46        }
47        dao.AddJobData(jobData);
48        return jobData.JobId;
49      }
50    }
51
52    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
53      using (trans.OpenTransaction()) {
54        job.ParentJobId = parentJobId;
55        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
56      }
57    }
58
59    public Job GetJob(Guid jobId) {
60      return dao.GetJob(jobId);
61    }
62
63    public IEnumerable<Job> GetJobs() {
64      return dao.GetJobs(x => true);
65    }
66
67    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
68      return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
69    }
70
71    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
72      return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
73    }
74
75    public JobData GetJobData(Guid jobId) {
76      return dao.GetJobData(jobId);
77    }
78
79    public void UpdateJob(Job job, JobData jobData) {
80      using (trans.OpenTransaction()) {
81        jobData.LastUpdate = DateTime.Now;
82        dao.UpdateJob(job);
83        dao.UpdateJobData(jobData);
84      }
85    }
86
87    public void DeleteJob(Guid jobId) {
88      using (trans.OpenTransaction()) {
89        dao.DeleteJob(jobId);
90      }
91    }
92
93    public void DeleteChildJobs(Guid parentJobId) {
94      using (trans.OpenTransaction()) {
95        var jobs = GetChildJobs(parentJobId, true, false);
96        foreach (var job in jobs) {
97          dao.DeleteJob(job.Id);
98          dao.DeleteJobData(job.Id);
99        };
100      }
101    }
102   
103    public PluginData GetConfigurationFile() {
104      using (trans.OpenTransaction()) {
105        //TODO: move filename to app.config
106        PluginData configFile = dao.GetPluginDatas(x => x.FileName == "HeuristicLab 3.3.exe.config").SingleOrDefault();
107        if (configFile == null) {
108          //TODO: error handling
109          return null;
110        } else {
111          return configFile;
112        }
113      }
114    }
115
116    #endregion
117
118    #region Job Control Methods
119    public void StopJob(Guid jobId) {
120      using (trans.OpenTransaction()) {
121        throw new NotImplementedException();
122      }
123    }
124    public void PauseJob(Guid jobId) {
125      using (trans.OpenTransaction()) {
126        throw new NotImplementedException();
127      }
128    }
129    #endregion
130
131    #region HiveExperiment Methods
132
133    public HiveExperiment GetHiveExperiment(Guid id) {
134      return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault();
135    }
136
137    public IEnumerable<HiveExperiment> GetHiveExperiments() {
138      return dao.GetHiveExperiments(x => x.UserId == auth.UserId);
139    }
140
141    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
142      using (trans.OpenTransaction()) {
143        hiveExperimentDto.UserId = auth.UserId;
144        hiveExperimentDto.DateCreated = DateTime.Now;
145        return dao.AddHiveExperiment(hiveExperimentDto);
146      }
147    }
148
149    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
150      using (trans.OpenTransaction()) {
151        dao.UpdateHiveExperiment(hiveExperimentDto);
152      }
153    }
154
155    public void DeleteHiveExperiment(Guid hiveExperimentId) {
156      using (trans.OpenTransaction()) {
157        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
158        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
159      }
160    }
161    #endregion
162
163    #region Login Methods
164    public void Hello(Guid slaveId, string name, int cores, int memory) {
165      using (trans.OpenTransaction()) {
166        var slave = dao.GetSlave(slaveId);
167
168        if (slave == null) {
169          slave = new Slave { Id = slaveId, Name = name, Cores = cores, Memory = memory };
170          slave.IsAllowedToCalculate = true; //a little bit to optimistic?
171          slave.SlaveState = SlaveState.Idle;
172          dao.AddSlave(slave);
173        } else {
174          //TODO: error handling?
175        }
176      }
177    }
178
179    public void GoodBye(Guid slaveId) {
180      using (trans.OpenTransaction()) {
181        var slave = dao.GetSlave(slaveId);
182        if (slave != null) {
183          slave.SlaveState = SlaveState.Offline;
184          dao.UpdateSlave(slave);
185        }
186      }
187    }
188    #endregion
189
190    #region Heartbeat Methods
191    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
192      using (trans.OpenTransaction()) {
193        return lifecycleManager.ProcessHeartbeat(heartbeat);
194      }
195    }
196    #endregion
197
198    #region Plugin Methods
199    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
200      using (trans.OpenTransaction()) {
201        plugin.UserId = auth.UserId;
202        plugin.DateCreated = DateTime.Now;
203        Guid pluginId = dao.AddPlugin(plugin);
204        foreach (PluginData pluginData in pluginDatas) {
205          pluginData.PluginId = pluginId;
206          dao.AddPluginData(pluginData);
207        }
208        return pluginId;
209      }
210    }
211    public IEnumerable<Plugin> GetPlugins() {
212      return dao.GetPlugins(x => true);
213    }
214
215    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
216      List<PluginData> pluginDatas = new List<PluginData>();
217
218      using (trans.OpenTransaction()) {
219        foreach (Guid guid in pluginIds) {
220          List<PluginData> pluginData = dao.GetPluginDatas(x => x.PluginId == guid).ToList();
221          if (pluginData != null) {
222            pluginDatas.AddRange(pluginData);
223          } else {
224            //ignore ?
225          }
226        }
227        return pluginDatas;
228      }
229    }
230
231    #endregion
232
233    #region Slave Methods
234    public Guid AddSlave(Slave slave) {
235      using (trans.OpenTransaction()) {
236        return dao.AddSlave(slave);
237      }
238    }
239
240    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
241      using (trans.OpenTransaction()) {
242        return dao.AddSlaveGroup(slaveGroup);
243      }
244    }
245
246    public Slave GetSlave(Guid slaveId) {
247      return dao.GetSlave(slaveId);
248    }
249
250    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
251      return dao.GetSlaveGroup(slaveGroupId);
252    }
253
254    public IEnumerable<Slave> GetSlaves() {
255      return dao.GetSlaves(x => true);
256    }
257
258    public IEnumerable<SlaveGroup> GetSlaveGroups() {
259      return dao.GetSlaveGroups(x => true);
260    }
261
262    public void UpdateSlave(Slave slave) {
263      using (trans.OpenTransaction()) {
264        dao.UpdateSlave(slave);
265      }
266    }
267
268    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
269      using (trans.OpenTransaction()) {
270        dao.UpdateSlaveGroup(slaveGroup);
271      }
272    }
273
274    public void DeleteSlave(Guid slaveId) {
275      using (trans.OpenTransaction()) {
276        dao.DeleteSlave(slaveId);
277      }
278    }
279
280    public void DeleteSlaveGroup(Guid slaveGroupId) {
281      using (trans.OpenTransaction()) {
282        dao.DeleteSlaveGroup(slaveGroupId);
283      }
284    }
285
286    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
287      using (trans.OpenTransaction()) {
288        var resource = dao.GetResource(resourceId);
289        resource.ParentResourceId = slaveGroupId;
290        dao.UpdateResource(resource);
291      }
292    }
293
294    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
295      using (trans.OpenTransaction()) {
296        var resource = dao.GetResource(resourceId);
297        resource.ParentResourceId = null;
298        dao.UpdateResource(resource);
299      }
300    }
301
302    #endregion
303
304    #region Helper Methods
305    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
306      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
307
308      if (includeParent) {
309        jobs.Add(GetJob(parentJobId.Value));
310      }
311
312      if (recursive) {
313        var childs = new List<Job>();
314        foreach (var job in jobs) {
315          childs.AddRange(GetChildJobs(job.Id, recursive, false));
316        }
317        jobs.AddRange(childs);
318      }
319      return jobs;
320    }
321
322    #endregion
323
324  }
325}
Note: See TracBrowser for help on using the repository browser.