1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.ServiceModel;
|
---|
6 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
7 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
8 | using System.IO;
|
---|
9 | using System.Security.Permissions;
|
---|
10 | using System.Data.Linq;
|
---|
11 | using HeuristicLab.Services.Hive.Common;
|
---|
12 | using System.Transactions;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Services.Hive {
|
---|
15 |
|
---|
16 | /// <summary>
|
---|
17 | /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
|
---|
18 | /// </summary>
|
---|
19 | [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
|
---|
20 | public class HiveService : IHiveService {
|
---|
21 | private DataAccess.IHiveDao dao {
|
---|
22 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
23 | }
|
---|
24 | private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
|
---|
25 | get { return ServiceLocator.Instance.TransactionManager; }
|
---|
26 | }
|
---|
27 | private IAuthorizationManager auth {
|
---|
28 | get { return ServiceLocator.Instance.AuthorizationManager; }
|
---|
29 | }
|
---|
30 | private ILifecycleManager lifecycleManager {
|
---|
31 | get { return ServiceLocator.Instance.LifecycleManager; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | #region Job Methods
|
---|
35 | //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
|
---|
36 | //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
|
---|
37 | public Guid AddJob(Job job, JobData jobData) {
|
---|
38 | using (trans.OpenTransaction()) {
|
---|
39 | jobData.JobId = dao.AddJob(job);
|
---|
40 | dao.AddJobData(jobData);
|
---|
41 | return jobData.JobId;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
|
---|
46 | using (trans.OpenTransaction()) {
|
---|
47 | job.ParentJobId = parentJobId;
|
---|
48 | return AddJob(job, jobData);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public Job GetJob(Guid jobId) {
|
---|
53 | return dao.GetJob(jobId);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IEnumerable<Job> GetJobs() {
|
---|
57 | return dao.GetJobs(x => true);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
|
---|
61 | return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
|
---|
65 | return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
|
---|
66 | }
|
---|
67 |
|
---|
68 | // formerly GetLastSerializedResult
|
---|
69 | public JobData GetJobData(Guid jobId) {
|
---|
70 | return dao.GetJobData(jobId);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public Stream GetJobDataStreamed(Guid jobId) {
|
---|
74 | throw new NotImplementedException();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void UpdateJob(Job jobDto, JobData jobDataDto) {
|
---|
78 | using (trans.OpenTransaction()) {
|
---|
79 | dao.UpdateJob(jobDto);
|
---|
80 | dao.UpdateJobData(jobDataDto);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void UpdateJobDataStreamed(Stream stream) {
|
---|
85 | using (trans.OpenTransaction()) {
|
---|
86 | throw new NotImplementedException();
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void DeleteChildJobs(Guid jobId) {
|
---|
91 | using (trans.OpenTransaction()) {
|
---|
92 | var jobs = GetChildJobs(jobId, true, false);
|
---|
93 | foreach (var job in jobs) {
|
---|
94 | dao.DeleteJob(job.Id);
|
---|
95 | dao.DeleteJobData(job.Id);
|
---|
96 | };
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public Job AquireJob(Guid slaveId) {
|
---|
101 | using (trans.OpenTransaction()) {
|
---|
102 | var slave = dao.GetSlave(slaveId);
|
---|
103 | var availableJobs = dao.GetWaitingJobs(slave);
|
---|
104 | var job = availableJobs.FirstOrDefault();
|
---|
105 |
|
---|
106 | if (job != null) {
|
---|
107 | job.SlaveId = slaveId;
|
---|
108 | job.JobState = JobState.Calculating;
|
---|
109 | }
|
---|
110 | return job;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public PluginData GetConfigurationFile() {
|
---|
115 | throw new NotImplementedException();
|
---|
116 | }
|
---|
117 |
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Job Control Methods
|
---|
121 | public void StopJob(Guid jobId) {
|
---|
122 | using (trans.OpenTransaction()) {
|
---|
123 | throw new NotImplementedException();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | public void PauseJob(Guid jobId) {
|
---|
127 | using (trans.OpenTransaction()) {
|
---|
128 | throw new NotImplementedException();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | #endregion
|
---|
132 |
|
---|
133 | #region HiveExperiment Methods
|
---|
134 |
|
---|
135 | public HiveExperiment GetHiveExperiment(Guid id) {
|
---|
136 | return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault();
|
---|
137 | }
|
---|
138 |
|
---|
139 | public IEnumerable<HiveExperiment> GetHiveExperiments() {
|
---|
140 | return dao.GetHiveExperiments(x => x.UserId == auth.UserId);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
144 | using (trans.OpenTransaction()) {
|
---|
145 | hiveExperimentDto.UserId = auth.UserId;
|
---|
146 | return dao.AddHiveExperiment(hiveExperimentDto);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
151 | using (trans.OpenTransaction()) {
|
---|
152 | dao.UpdateHiveExperiment(hiveExperimentDto);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void DeleteHiveExperiment(Guid hiveExperimentId) {
|
---|
157 | using (trans.OpenTransaction()) {
|
---|
158 | HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
|
---|
159 | if (he.RootJobId.HasValue) {
|
---|
160 | var jobs = GetChildJobs(he.RootJobId.Value, true, true);
|
---|
161 | foreach (var j in jobs) {
|
---|
162 | dao.DeleteJobData(j.Id);
|
---|
163 | dao.DeleteJob(j.Id);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | dao.DeleteHiveExperiment(hiveExperimentId);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | #endregion
|
---|
170 |
|
---|
171 | #region Login Methods
|
---|
172 | public void Hello(Guid slaveId, string name, int cores, int memory) {
|
---|
173 | throw new NotImplementedException();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public void GoodBye() {
|
---|
177 | throw new NotImplementedException();
|
---|
178 | }
|
---|
179 | #endregion
|
---|
180 |
|
---|
181 | #region Heartbeat Methods
|
---|
182 | public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
|
---|
183 | using (trans.OpenTransaction()) {
|
---|
184 | return lifecycleManager.ProcessHeartbeat(heartbeat);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | #endregion
|
---|
188 |
|
---|
189 | #region Plugin Methods
|
---|
190 | public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
|
---|
191 | using (trans.OpenTransaction()) {
|
---|
192 | Guid pluginId = dao.AddPlugin(plugin);
|
---|
193 | foreach (PluginData pluginData in pluginDatas) {
|
---|
194 | pluginData.PluginId = pluginId;
|
---|
195 | dao.AddPluginData(pluginData);
|
---|
196 | }
|
---|
197 | return pluginId;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | public IEnumerable<Plugin> GetPlugins() {
|
---|
201 | return dao.GetPlugins(x => true);
|
---|
202 | }
|
---|
203 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
204 | throw new NotImplementedException();
|
---|
205 | }
|
---|
206 | public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
|
---|
207 | throw new NotImplementedException();
|
---|
208 | }
|
---|
209 | #endregion
|
---|
210 |
|
---|
211 | #region Slave Methods
|
---|
212 | public Guid AddSlave(Slave slave) {
|
---|
213 | using (trans.OpenTransaction()) {
|
---|
214 | return dao.AddSlave(slave);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
|
---|
219 | using (trans.OpenTransaction()) {
|
---|
220 | return dao.AddSlaveGroup(slaveGroup);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | public IEnumerable<Slave> GetSlaves() {
|
---|
225 | return dao.GetSlaves(x => true);
|
---|
226 | }
|
---|
227 |
|
---|
228 | public IEnumerable<SlaveGroup> GetSlaveGroups() {
|
---|
229 | return dao.GetSlaveGroups(x => true);
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
233 | using (trans.OpenTransaction()) {
|
---|
234 | dao.DeleteSlaveGroup(slaveGroupId);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | public void AddResourceToGroup(Guid slaveGroupId, Resource resource) {
|
---|
239 | using (trans.OpenTransaction()) {
|
---|
240 | throw new NotImplementedException();
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | public void RemoveResourceFromGroup(Guid clientGroupId, Guid resourceId) {
|
---|
245 | using (trans.OpenTransaction()) {
|
---|
246 | throw new NotImplementedException();
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | public void UpdateSlave(Slave slave) {
|
---|
251 | using (trans.OpenTransaction()) {
|
---|
252 | dao.UpdateSlave(slave);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | #endregion
|
---|
256 |
|
---|
257 | #region Helper Methods
|
---|
258 | private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
|
---|
259 | var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
|
---|
260 |
|
---|
261 | if (includeParent) {
|
---|
262 | jobs.Add(GetJob(parentJobId.Value));
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (recursive) {
|
---|
266 | var childs = new List<Job>();
|
---|
267 | foreach (var job in jobs) {
|
---|
268 | childs.AddRange(GetChildJobs(job.Id, recursive, false));
|
---|
269 | }
|
---|
270 | jobs.AddRange(childs);
|
---|
271 | }
|
---|
272 | return jobs;
|
---|
273 | }
|
---|
274 |
|
---|
275 | #endregion
|
---|
276 |
|
---|
277 | }
|
---|
278 | }
|
---|