- Timestamp:
- 09/12/11 18:04:25 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs
r6725 r6743 36 36 public HiveDao() { } 37 37 38 #region JobMethods39 public DT.Task Get Job(Guid id) {38 #region Task Methods 39 public DT.Task GetTask(Guid id) { 40 40 using (var db = CreateContext()) { 41 41 return DT.Convert.ToDto(db.Tasks.SingleOrDefault(x => x.TaskId == id)); … … 43 43 } 44 44 45 public IEnumerable<DT.Task> Get Jobs(Expression<Func<Task, bool>> predicate) {45 public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) { 46 46 using (var db = CreateContext()) { 47 47 return db.Tasks.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray(); … … 49 49 } 50 50 51 public Guid Add Job(DT.Task dto) {51 public Guid AddTask(DT.Task dto) { 52 52 using (var db = CreateContext()) { 53 53 var entity = DT.Convert.ToEntity(dto); … … 62 62 } 63 63 64 public void Update Job(DT.Task dto) {64 public void UpdateTask(DT.Task dto) { 65 65 using (var db = CreateContext()) { 66 66 var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id); … … 76 76 } 77 77 78 public void Delete Job(Guid id) {78 public void DeleteTask(Guid id) { 79 79 using (var db = CreateContext()) { 80 80 var entity = db.Tasks.FirstOrDefault(x => x.TaskId == id); 81 81 if (entity != null) db.Tasks.DeleteOnSubmit(entity); 82 db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger82 db.SubmitChanges(); // taskData and child tasks are deleted by db-trigger 83 83 } 84 84 } 85 85 86 86 /// <summary> 87 /// returns all parent jobs which are waiting for their child jobs to finish87 /// returns all parent tasks which are waiting for their child tasks to finish 88 88 /// </summary> 89 /// <param name="resourceIds">list of resourceids which for which the jobsshould be valid</param>90 /// <param name="count">maximum number of jobsto return</param>91 /// <param name="finished">if true, all parent jobswhich have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>89 /// <param name="resourceIds">list of resourceids which for which the task should be valid</param> 90 /// <param name="count">maximum number of task to return</param> 91 /// <param name="finished">if true, all parent task which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param> 92 92 /// <returns></returns> 93 public IEnumerable<DT.Task> GetParent Jobs(IEnumerable<Guid> resourceIds, int count, bool finished) {93 public IEnumerable<DT.Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) { 94 94 using (var db = CreateContext()) { 95 95 var query = from ar in db.AssignedResources … … 103 103 || child.State == TaskState.Aborted 104 104 || child.State == TaskState.Failed).All(x => x) 105 && (from child in db.Tasks // avoid returning WaitForChild Jobs jobs where no child-jobsexist (yet)105 && (from child in db.Tasks // avoid returning WaitForChildTasks task where no child-task exist (yet) 106 106 where child.ParentTaskId == ar.Task.TaskId 107 107 select child).Count() > 0 … … 112 112 } 113 113 114 public IEnumerable<DT.Task> GetWaiting Jobs(DT.Slave slave, int count) {114 public IEnumerable<DT.Task> GetWaitingTasks(DT.Slave slave, int count) { 115 115 using (var db = CreateContext()) { 116 116 var resourceIds = GetParentResources(slave.Id).Select(r => r.Id); 117 var waitingParentJobs = GetParent Jobs(resourceIds, count, false);117 var waitingParentJobs = GetParentTasks(resourceIds, count, false); 118 118 if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray(); 119 119 … … 124 124 && ar.Task.CoresNeeded <= slave.FreeCores 125 125 && ar.Task.MemoryNeeded <= slave.FreeMemory 126 orderby ar.Task.Priority descending, db.Random() // take random job to avoid the race condition that occurs when this method is called concurrently (the same jobwould be returned)126 orderby ar.Task.Priority descending, db.Random() // take random task to avoid the race condition that occurs when this method is called concurrently (the same task would be returned) 127 127 select DT.Convert.ToDto(ar.Task); 128 var waiting Jobs = (count == 0 ? query : query.Take(count)).ToArray();129 return waiting Jobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);130 } 131 } 132 133 public DT.Task Update JobState(Guid jobId, TaskState jobState, Guid? slaveId, Guid? userId, string exception) {134 using (var db = CreateContext()) { 135 var job = db.Tasks.SingleOrDefault(x => x.TaskId == jobId);136 job.State = jobState;128 var waitingTasks = (count == 0 ? query : query.Take(count)).ToArray(); 129 return waitingTasks.Union(waitingParentJobs).OrderByDescending(x => x.Priority); 130 } 131 } 132 133 public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) { 134 using (var db = CreateContext()) { 135 var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId); 136 job.State = taskState; 137 137 db.StateLogs.InsertOnSubmit(new StateLog { 138 TaskId = jobId,139 State = jobState,138 TaskId = taskId, 139 State = taskState, 140 140 SlaveId = slaveId, 141 141 UserId = userId, … … 144 144 }); 145 145 db.SubmitChanges(); 146 job = db.Tasks.SingleOrDefault(x => x.TaskId == jobId);146 job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId); 147 147 return DT.Convert.ToDto(job); 148 148 } … … 151 151 152 152 #region TaskData Methods 153 public DT.TaskData Get JobData(Guid id) {153 public DT.TaskData GetTaskData(Guid id) { 154 154 using (var db = CreateContext(true)) { 155 155 return DT.Convert.ToDto(db.TaskDatas.SingleOrDefault(x => x.TaskId == id)); … … 157 157 } 158 158 159 public IEnumerable<DT.TaskData> Get JobDatas(Expression<Func<TaskData, bool>> predicate) {159 public IEnumerable<DT.TaskData> GetTaskDatas(Expression<Func<TaskData, bool>> predicate) { 160 160 using (var db = CreateContext(true)) { 161 161 return db.TaskDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray(); … … 163 163 } 164 164 165 public Guid Add JobData(DT.TaskData dto) {165 public Guid AddTaskData(DT.TaskData dto) { 166 166 using (var db = CreateContext(true)) { 167 167 var entity = DT.Convert.ToEntity(dto); … … 172 172 } 173 173 174 public void Update JobData(DT.TaskData dto) {174 public void UpdateTaskData(DT.TaskData dto) { 175 175 using (var db = CreateContext(true)) { 176 176 var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == dto.TaskId); … … 181 181 } 182 182 183 public void Delete JobData(Guid id) {183 public void DeleteTaskData(Guid id) { 184 184 using (var db = CreateContext()) { 185 185 var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it … … 231 231 232 232 #region Job Methods 233 public DT.Job Get HiveExperiment(Guid id) {233 public DT.Job GetJob(Guid id) { 234 234 using (var db = CreateContext()) { 235 235 return AddStatsToExperiment(db, DT.Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id))); … … 248 248 } 249 249 250 public IEnumerable<DT.Job> Get HiveExperiments(Expression<Func<Job, bool>> predicate) {250 public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) { 251 251 using (var db = CreateContext()) { 252 252 return db.Jobs.Where(predicate).Select(x => AddStatsToExperiment(db, DT.Convert.ToDto(x))).ToArray(); … … 254 254 } 255 255 256 public Guid Add HiveExperiment(DT.Job dto) {256 public Guid AddJob(DT.Job dto) { 257 257 using (var db = CreateContext()) { 258 258 var entity = DT.Convert.ToEntity(dto); … … 263 263 } 264 264 265 public void Update HiveExperiment(DT.Job dto) {265 public void UpdateJob(DT.Job dto) { 266 266 using (var db = CreateContext()) { 267 267 var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id); … … 272 272 } 273 273 274 public void Delete HiveExperiment(Guid id) {274 public void DeleteJob(Guid id) { 275 275 using (var db = CreateContext()) { 276 276 var entity = db.Jobs.FirstOrDefault(x => x.JobId == id); … … 282 282 283 283 #region JobPermission Methods 284 public DT.JobPermission Get HiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {285 using (var db = CreateContext()) { 286 return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == hiveExperimentId && x.GrantedUserId == grantedUserId));287 } 288 } 289 290 public IEnumerable<DT.JobPermission> Get HiveExperimentPermissions(Expression<Func<JobPermission, bool>> predicate) {284 public DT.JobPermission GetJobPermission(Guid jobId, Guid grantedUserId) { 285 using (var db = CreateContext()) { 286 return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId)); 287 } 288 } 289 290 public IEnumerable<DT.JobPermission> GetJobPermissions(Expression<Func<JobPermission, bool>> predicate) { 291 291 using (var db = CreateContext()) { 292 292 return db.JobPermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray(); … … 294 294 } 295 295 296 public void Add HiveExperimentPermission(DT.JobPermission dto) {296 public void AddJobPermission(DT.JobPermission dto) { 297 297 using (var db = CreateContext()) { 298 298 var entity = DT.Convert.ToEntity(dto); … … 302 302 } 303 303 304 public void Update HiveExperimentPermission(DT.JobPermission dto) {304 public void UpdateJobPermission(DT.JobPermission dto) { 305 305 using (var db = CreateContext()) { 306 306 var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == dto.JobId && x.GrantedUserId == dto.GrantedUserId); … … 311 311 } 312 312 313 public void Delete HiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {314 using (var db = CreateContext()) { 315 var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == hiveExperimentId && x.GrantedUserId == grantedUserId);313 public void DeleteJobPermission(Guid jobId, Guid grantedUserId) { 314 using (var db = CreateContext()) { 315 var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId); 316 316 if (entity != null) db.JobPermissions.DeleteOnSubmit(entity); 317 317 db.SubmitChanges(); … … 322 322 /// Sets the permissions for a experiment. makes sure that only one permission per user exists. 323 323 /// </summary> 324 public void Set HiveExperimentPermission(Guid hiveExperimentId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {325 using (var db = CreateContext()) { 326 JobPermission hiveExperimentPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == hiveExperimentId && x.GrantedUserId == grantedUserId);327 if ( hiveExperimentPermission != null) {324 public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) { 325 using (var db = CreateContext()) { 326 JobPermission jobPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId); 327 if (jobPermission != null) { 328 328 if (permission == Permission.NotAllowed) { 329 329 // not allowed, delete 330 db.JobPermissions.DeleteOnSubmit( hiveExperimentPermission);330 db.JobPermissions.DeleteOnSubmit(jobPermission); 331 331 } else { 332 332 // update 333 hiveExperimentPermission.Permission = permission;334 hiveExperimentPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored333 jobPermission.Permission = permission; 334 jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored 335 335 } 336 336 } else { 337 337 // insert 338 338 if (permission != Permission.NotAllowed) { 339 hiveExperimentPermission = new JobPermission() { JobId = hiveExperimentId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };340 db.JobPermissions.InsertOnSubmit( hiveExperimentPermission);339 jobPermission = new JobPermission() { JobId = jobId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission }; 340 db.JobPermissions.InsertOnSubmit(jobPermission); 341 341 } 342 342 } … … 613 613 614 614 #region Authorization Methods 615 public Permission GetPermissionForTask(Guid taskId, Guid userId) { 616 using (var db = CreateContext()) { 617 return GetPermissionForJob(GetJobForTask(taskId), userId); 618 } 619 } 620 615 621 public Permission GetPermissionForJob(Guid jobId, Guid userId) { 616 622 using (var db = CreateContext()) { 617 return GetPermissionForExperiment(GetExperimentForJob(jobId), userId); 618 } 619 } 620 621 public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) { 622 using (var db = CreateContext()) { 623 Job hiveExperiment = db.Jobs.SingleOrDefault(x => x.JobId == experimentId); 624 if (hiveExperiment == null) return Permission.NotAllowed; 625 if (hiveExperiment.OwnerUserId == userId) return Permission.Full; 626 JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == experimentId && p.GrantedUserId == userId); 623 Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId); 624 if (job == null) return Permission.NotAllowed; 625 if (job.OwnerUserId == userId) return Permission.Full; 626 JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId); 627 627 return permission != null ? permission.Permission : Permission.NotAllowed; 628 628 } 629 629 } 630 630 631 public Guid Get ExperimentForJob(Guid jobId) {632 using (var db = CreateContext()) { 633 return db.Tasks.Single(j => j.TaskId == jobId).JobId;631 public Guid GetJobForTask(Guid taskId) { 632 using (var db = CreateContext()) { 633 return db.Tasks.Single(j => j.TaskId == taskId).JobId; 634 634 } 635 635 } … … 765 765 } 766 766 767 // execution times only of finished jobs- necessary to compute efficieny767 // execution times only of finished task - necessary to compute efficieny 768 768 var executionTimesFinishedJobs = from job in db.Tasks 769 769 where job.State == TaskState.Finished … … 778 778 } 779 779 780 // start to end times only of finished jobs- necessary to compute efficiency780 // start to end times only of finished task - necessary to compute efficiency 781 781 var startToEndTimesFinishedJobs = from job in db.Tasks 782 782 where job.State == TaskState.Finished … … 817 817 818 818 #region Helpers 819 private void CollectChild Jobs(HiveDataContext db, Guid parentJobId, List<Task> collection) {820 var jobs = db.Tasks.Where(j => j.ParentTaskId == parentJobId);821 foreach (var job in jobs) {822 collection.Add( job);823 if ( job.IsParentTask)824 CollectChild Jobs(db, job.TaskId, collection);819 private void CollectChildTasks(HiveDataContext db, Guid parentTaskId, List<Task> collection) { 820 var tasks = db.Tasks.Where(j => j.ParentTaskId == parentTaskId); 821 foreach (var task in tasks) { 822 collection.Add(task); 823 if (task.IsParentTask) 824 CollectChildTasks(db, task.TaskId, collection); 825 825 } 826 826 }
Note: See TracChangeset
for help on using the changeset viewer.