[5028] | 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;
|
---|
[5055] | 12 | using System.Transactions;
|
---|
[5028] | 13 |
|
---|
| 14 | namespace HeuristicLab.Services.Hive {
|
---|
| 15 |
|
---|
| 16 | /// <summary>
|
---|
| 17 | /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
|
---|
| 18 | /// </summary>
|
---|
[5106] | 19 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
|
---|
[5028] | 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 | }
|
---|
[5095] | 30 | private ILifecycleManager lifecycleManager {
|
---|
| 31 | get { return ServiceLocator.Instance.LifecycleManager; }
|
---|
[5028] | 32 | }
|
---|
| 33 |
|
---|
| 34 | #region Job Methods
|
---|
[5095] | 35 | //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
|
---|
| 36 | //[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
|
---|
[5155] | 37 | public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> slaveGroupIds) {
|
---|
[5028] | 38 | using (trans.OpenTransaction()) {
|
---|
[5106] | 39 | job.UserId = auth.UserId;
|
---|
| 40 | job.DateCreated = DateTime.Now;
|
---|
| 41 | job.JobState = JobState.Waiting;
|
---|
[5155] | 42 | job.Id = dao.AddJob(job);
|
---|
| 43 | jobData.JobId = job.Id;
|
---|
[5106] | 44 | jobData.LastUpdate = DateTime.Now;
|
---|
[5155] | 45 | if (slaveGroupIds != null) {
|
---|
| 46 | foreach (Guid slaveGroupId in slaveGroupIds) {
|
---|
| 47 | dao.AssignJobToResource(job.Id, slaveGroupId);
|
---|
| 48 | }
|
---|
| 49 | } else {
|
---|
| 50 | // todo: use default group
|
---|
| 51 | }
|
---|
[5028] | 52 | dao.AddJobData(jobData);
|
---|
| 53 | return jobData.JobId;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
|
---|
| 58 | using (trans.OpenTransaction()) {
|
---|
| 59 | job.ParentJobId = parentJobId;
|
---|
[5155] | 60 | return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
|
---|
[5028] | 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public Job GetJob(Guid jobId) {
|
---|
| 65 | return dao.GetJob(jobId);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public IEnumerable<Job> GetJobs() {
|
---|
| 69 | return dao.GetJobs(x => true);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
|
---|
| 73 | return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
|
---|
| 77 | return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public JobData GetJobData(Guid jobId) {
|
---|
| 81 | return dao.GetJobData(jobId);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[5106] | 84 | public void UpdateJob(Job job, JobData jobData) {
|
---|
[5028] | 85 | using (trans.OpenTransaction()) {
|
---|
[5106] | 86 | jobData.LastUpdate = DateTime.Now;
|
---|
| 87 | dao.UpdateJob(job);
|
---|
| 88 | dao.UpdateJobData(jobData);
|
---|
[5028] | 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[5106] | 92 | public void DeleteJob(Guid jobId) {
|
---|
[5028] | 93 | using (trans.OpenTransaction()) {
|
---|
[5106] | 94 | dao.DeleteJob(jobId);
|
---|
[5028] | 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[5106] | 98 | public void DeleteChildJobs(Guid parentJobId) {
|
---|
[5028] | 99 | using (trans.OpenTransaction()) {
|
---|
[5106] | 100 | var jobs = GetChildJobs(parentJobId, true, false);
|
---|
[5028] | 101 | foreach (var job in jobs) {
|
---|
| 102 | dao.DeleteJob(job.Id);
|
---|
| 103 | dao.DeleteJobData(job.Id);
|
---|
| 104 | };
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public Job AquireJob(Guid slaveId) {
|
---|
| 109 | using (trans.OpenTransaction()) {
|
---|
| 110 | var slave = dao.GetSlave(slaveId);
|
---|
| 111 | var availableJobs = dao.GetWaitingJobs(slave);
|
---|
| 112 | var job = availableJobs.FirstOrDefault();
|
---|
| 113 |
|
---|
| 114 | if (job != null) {
|
---|
| 115 | job.SlaveId = slaveId;
|
---|
| 116 | job.JobState = JobState.Calculating;
|
---|
| 117 | }
|
---|
| 118 | return job;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[5102] | 121 |
|
---|
| 122 | public PluginData GetConfigurationFile() {
|
---|
| 123 | throw new NotImplementedException();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[5028] | 126 | #endregion
|
---|
| 127 |
|
---|
| 128 | #region Job Control Methods
|
---|
[5053] | 129 | public void StopJob(Guid jobId) {
|
---|
[5028] | 130 | using (trans.OpenTransaction()) {
|
---|
| 131 | throw new NotImplementedException();
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
[5062] | 134 | public void PauseJob(Guid jobId) {
|
---|
[5028] | 135 | using (trans.OpenTransaction()) {
|
---|
| 136 | throw new NotImplementedException();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | #endregion
|
---|
| 140 |
|
---|
| 141 | #region HiveExperiment Methods
|
---|
| 142 |
|
---|
| 143 | public HiveExperiment GetHiveExperiment(Guid id) {
|
---|
| 144 | return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | public IEnumerable<HiveExperiment> GetHiveExperiments() {
|
---|
| 148 | return dao.GetHiveExperiments(x => x.UserId == auth.UserId);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
| 152 | using (trans.OpenTransaction()) {
|
---|
| 153 | hiveExperimentDto.UserId = auth.UserId;
|
---|
[5106] | 154 | hiveExperimentDto.DateCreated = DateTime.Now;
|
---|
[5028] | 155 | return dao.AddHiveExperiment(hiveExperimentDto);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
| 160 | using (trans.OpenTransaction()) {
|
---|
| 161 | dao.UpdateHiveExperiment(hiveExperimentDto);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | public void DeleteHiveExperiment(Guid hiveExperimentId) {
|
---|
| 166 | using (trans.OpenTransaction()) {
|
---|
| 167 | HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
|
---|
[5106] | 168 | dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
|
---|
[5028] | 169 | }
|
---|
| 170 | }
|
---|
| 171 | #endregion
|
---|
[5106] | 172 |
|
---|
[5028] | 173 | #region Login Methods
|
---|
[5053] | 174 | public void Hello(Guid slaveId, string name, int cores, int memory) {
|
---|
| 175 | throw new NotImplementedException();
|
---|
| 176 | }
|
---|
[5028] | 177 |
|
---|
[5053] | 178 | public void GoodBye() {
|
---|
| 179 | throw new NotImplementedException();
|
---|
| 180 | }
|
---|
[5028] | 181 | #endregion
|
---|
| 182 |
|
---|
| 183 | #region Heartbeat Methods
|
---|
[5053] | 184 | public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
|
---|
[5028] | 185 | using (trans.OpenTransaction()) {
|
---|
[5095] | 186 | return lifecycleManager.ProcessHeartbeat(heartbeat);
|
---|
[5028] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | #endregion
|
---|
| 190 |
|
---|
| 191 | #region Plugin Methods
|
---|
| 192 | public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
|
---|
| 193 | using (trans.OpenTransaction()) {
|
---|
| 194 | Guid pluginId = dao.AddPlugin(plugin);
|
---|
| 195 | foreach (PluginData pluginData in pluginDatas) {
|
---|
| 196 | pluginData.PluginId = pluginId;
|
---|
| 197 | dao.AddPluginData(pluginData);
|
---|
| 198 | }
|
---|
| 199 | return pluginId;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
[5053] | 202 | public IEnumerable<Plugin> GetPlugins() {
|
---|
| 203 | return dao.GetPlugins(x => true);
|
---|
[5028] | 204 | }
|
---|
[5053] | 205 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
[5028] | 206 | throw new NotImplementedException();
|
---|
| 207 | }
|
---|
[5053] | 208 | public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
|
---|
[5028] | 209 | throw new NotImplementedException();
|
---|
| 210 | }
|
---|
| 211 | #endregion
|
---|
[5106] | 212 |
|
---|
[5028] | 213 | #region Slave Methods
|
---|
| 214 | public Guid AddSlave(Slave slave) {
|
---|
| 215 | using (trans.OpenTransaction()) {
|
---|
| 216 | return dao.AddSlave(slave);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
|
---|
| 221 | using (trans.OpenTransaction()) {
|
---|
| 222 | return dao.AddSlaveGroup(slaveGroup);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[5106] | 226 | public Slave GetSlave(Guid slaveId) {
|
---|
| 227 | return dao.GetSlave(slaveId);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
|
---|
| 231 | return dao.GetSlaveGroup(slaveGroupId);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[5028] | 234 | public IEnumerable<Slave> GetSlaves() {
|
---|
| 235 | return dao.GetSlaves(x => true);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | public IEnumerable<SlaveGroup> GetSlaveGroups() {
|
---|
| 239 | return dao.GetSlaveGroups(x => true);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[5106] | 242 | public void UpdateSlave(Slave slave) {
|
---|
[5028] | 243 | using (trans.OpenTransaction()) {
|
---|
[5106] | 244 | dao.UpdateSlave(slave);
|
---|
[5028] | 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[5106] | 248 | public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
|
---|
[5028] | 249 | using (trans.OpenTransaction()) {
|
---|
[5106] | 250 | dao.UpdateSlaveGroup(slaveGroup);
|
---|
[5028] | 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[5106] | 254 | public void DeleteSlave(Guid slaveId) {
|
---|
[5028] | 255 | using (trans.OpenTransaction()) {
|
---|
[5106] | 256 | dao.DeleteSlave(slaveId);
|
---|
[5028] | 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[5106] | 260 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
[5028] | 261 | using (trans.OpenTransaction()) {
|
---|
[5106] | 262 | dao.DeleteSlaveGroup(slaveGroupId);
|
---|
[5028] | 263 | }
|
---|
| 264 | }
|
---|
[5106] | 265 |
|
---|
| 266 | public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
| 267 | using (trans.OpenTransaction()) {
|
---|
| 268 | var resource = dao.GetResource(resourceId);
|
---|
| 269 | resource.ParentResourceId = slaveGroupId;
|
---|
| 270 | dao.UpdateResource(resource);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
| 275 | using (trans.OpenTransaction()) {
|
---|
| 276 | var resource = dao.GetResource(resourceId);
|
---|
| 277 | resource.ParentResourceId = null;
|
---|
| 278 | dao.UpdateResource(resource);
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[5028] | 282 | #endregion
|
---|
| 283 |
|
---|
| 284 | #region Helper Methods
|
---|
| 285 | private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
|
---|
| 286 | var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
|
---|
| 287 |
|
---|
| 288 | if (includeParent) {
|
---|
| 289 | jobs.Add(GetJob(parentJobId.Value));
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | if (recursive) {
|
---|
| 293 | var childs = new List<Job>();
|
---|
| 294 | foreach (var job in jobs) {
|
---|
| 295 | childs.AddRange(GetChildJobs(job.Id, recursive, false));
|
---|
| 296 | }
|
---|
| 297 | jobs.AddRange(childs);
|
---|
| 298 | }
|
---|
| 299 | return jobs;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | #endregion
|
---|
[5102] | 303 |
|
---|
[5028] | 304 | }
|
---|
| 305 | }
|
---|