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 | #endregion
|
---|
114 |
|
---|
115 | #region Job Control Methods
|
---|
116 | public void StopJob(Guid jobId) {
|
---|
117 | using (trans.OpenTransaction()) {
|
---|
118 | throw new NotImplementedException();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | public void PauseJob(Guid jobId) {
|
---|
122 | using (trans.OpenTransaction()) {
|
---|
123 | throw new NotImplementedException();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | #region HiveExperiment Methods
|
---|
129 |
|
---|
130 | public HiveExperiment GetHiveExperiment(Guid id) {
|
---|
131 | return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public IEnumerable<HiveExperiment> GetHiveExperiments() {
|
---|
135 | return dao.GetHiveExperiments(x => x.UserId == auth.UserId);
|
---|
136 | }
|
---|
137 |
|
---|
138 | public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
139 | using (trans.OpenTransaction()) {
|
---|
140 | hiveExperimentDto.UserId = auth.UserId;
|
---|
141 | return dao.AddHiveExperiment(hiveExperimentDto);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
146 | using (trans.OpenTransaction()) {
|
---|
147 | dao.UpdateHiveExperiment(hiveExperimentDto);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void DeleteHiveExperiment(Guid hiveExperimentId) {
|
---|
152 | using (trans.OpenTransaction()) {
|
---|
153 | HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
|
---|
154 | if (he.RootJobId.HasValue) {
|
---|
155 | var jobs = GetChildJobs(he.RootJobId.Value, true, true);
|
---|
156 | foreach (var j in jobs) {
|
---|
157 | dao.DeleteJobData(j.Id);
|
---|
158 | dao.DeleteJob(j.Id);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | dao.DeleteHiveExperiment(hiveExperimentId);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | #endregion
|
---|
165 |
|
---|
166 | #region Login Methods
|
---|
167 | public void Hello(Guid slaveId, string name, int cores, int memory) {
|
---|
168 | throw new NotImplementedException();
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void GoodBye() {
|
---|
172 | throw new NotImplementedException();
|
---|
173 | }
|
---|
174 | #endregion
|
---|
175 |
|
---|
176 | #region Heartbeat Methods
|
---|
177 | public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
|
---|
178 | using (trans.OpenTransaction()) {
|
---|
179 | return lifecycleManager.ProcessHeartbeat(heartbeat);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | #endregion
|
---|
183 |
|
---|
184 | #region Plugin Methods
|
---|
185 | public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
|
---|
186 | using (trans.OpenTransaction()) {
|
---|
187 | Guid pluginId = dao.AddPlugin(plugin);
|
---|
188 | foreach (PluginData pluginData in pluginDatas) {
|
---|
189 | pluginData.PluginId = pluginId;
|
---|
190 | dao.AddPluginData(pluginData);
|
---|
191 | }
|
---|
192 | return pluginId;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | public IEnumerable<Plugin> GetPlugins() {
|
---|
196 | return dao.GetPlugins(x => true);
|
---|
197 | }
|
---|
198 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
199 | throw new NotImplementedException();
|
---|
200 | }
|
---|
201 | public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
|
---|
202 | throw new NotImplementedException();
|
---|
203 | }
|
---|
204 | #endregion
|
---|
205 |
|
---|
206 | #region Slave Methods
|
---|
207 | public Guid AddSlave(Slave slave) {
|
---|
208 | using (trans.OpenTransaction()) {
|
---|
209 | return dao.AddSlave(slave);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
|
---|
214 | using (trans.OpenTransaction()) {
|
---|
215 | return dao.AddSlaveGroup(slaveGroup);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public IEnumerable<Slave> GetSlaves() {
|
---|
220 | return dao.GetSlaves(x => true);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public IEnumerable<SlaveGroup> GetSlaveGroups() {
|
---|
224 | return dao.GetSlaveGroups(x => true);
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
228 | using (trans.OpenTransaction()) {
|
---|
229 | dao.DeleteSlaveGroup(slaveGroupId);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | public void AddResourceToGroup(Guid slaveGroupId, Resource resource) {
|
---|
234 | using (trans.OpenTransaction()) {
|
---|
235 | throw new NotImplementedException();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | public void RemoveResourceFromGroup(Guid clientGroupId, Guid resourceId) {
|
---|
240 | using (trans.OpenTransaction()) {
|
---|
241 | throw new NotImplementedException();
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | public void UpdateSlave(Slave slave) {
|
---|
246 | using (trans.OpenTransaction()) {
|
---|
247 | dao.UpdateSlave(slave);
|
---|
248 | }
|
---|
249 | }
|
---|
250 | #endregion
|
---|
251 |
|
---|
252 | #region Helper Methods
|
---|
253 | private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
|
---|
254 | var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
|
---|
255 |
|
---|
256 | if (includeParent) {
|
---|
257 | jobs.Add(GetJob(parentJobId.Value));
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (recursive) {
|
---|
261 | var childs = new List<Job>();
|
---|
262 | foreach (var job in jobs) {
|
---|
263 | childs.AddRange(GetChildJobs(job.Id, recursive, false));
|
---|
264 | }
|
---|
265 | jobs.AddRange(childs);
|
---|
266 | }
|
---|
267 | return jobs;
|
---|
268 | }
|
---|
269 |
|
---|
270 | #endregion
|
---|
271 |
|
---|
272 | }
|
---|
273 | }
|
---|