Changeset 9363 for branches/OaaS/HeuristicLab.Services.Hive/3.3/HiveDao.cs
- Timestamp:
- 04/16/13 13:13:41 (12 years ago)
- Location:
- branches/OaaS
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll 23 24 packages
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/OaaS/HeuristicLab.Services.Hive
-
Property
svn:mergeinfo
set to
(toggle deleted branches)
/trunk/sources/HeuristicLab.Services.Hive merged eligible /branches/Algorithms.GradientDescent/HeuristicLab.Services.Hive 5516-5520 /branches/Benchmarking/sources/HeuristicLab.Services.Hive 6917-7005 /branches/CloningRefactoring/HeuristicLab.Services.Hive 4656-4721 /branches/DataAnalysis Refactoring/HeuristicLab.Services.Hive 5471-5808 /branches/DataAnalysis SolutionEnsembles/HeuristicLab.Services.Hive 5815-6180 /branches/DataAnalysis/HeuristicLab.Services.Hive 4458-4459,4462,4464 /branches/GP.Grammar.Editor/HeuristicLab.Services.Hive 6284-6795 /branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Services.Hive 5060 /branches/HiveTaskScheduler/HeuristicLab.Services.Hive 8687-9106 /branches/NET40/sources/HeuristicLab.Services.Hive 5138-5162 /branches/ParallelEngine/HeuristicLab.Services.Hive 5175-5192 /branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Services.Hive 7568-7810 /branches/QAPAlgorithms/HeuristicLab.Services.Hive 6350-6627 /branches/Restructure trunk solution/HeuristicLab.Services.Hive 6828 /branches/RuntimeOptimizer/HeuristicLab.Services.Hive 8943-9078 /branches/ScatterSearch (trunk integration)/HeuristicLab.Services.Hive 7787-8333 /branches/SlaveShutdown/HeuristicLab.Services.Hive 8944-8956 /branches/SuccessProgressAnalysis/HeuristicLab.Services.Hive 5370-5682 /branches/Trunk/HeuristicLab.Services.Hive 6829-6865 /branches/UnloadJobs/HeuristicLab.Services.Hive 9168-9215 /branches/VNS/HeuristicLab.Services.Hive 5594-5752 /branches/histogram/HeuristicLab.Services.Hive 5959-6341
-
Property
svn:mergeinfo
set to
(toggle deleted branches)
-
branches/OaaS/HeuristicLab.Services.Hive/3.3/HiveDao.cs
r9215 r9363 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Configuration; 25 using System.Data.Common; 24 26 using System.Linq; 25 27 using System.Linq.Expressions; 28 using HeuristicLab.Services.Hive.Interfaces; 26 29 using DT = HeuristicLab.Services.Hive.DataTransfer; 27 using System.ServiceModel;28 using HeuristicLab.Services.Hive.Interfaces;29 using System.Data.Common;30 using System.Configuration;31 30 32 31 namespace HeuristicLab.Services.Hive.DataAccess { 33 32 public class HiveDao : IHiveDao { 34 33 35 34 public HiveDataContext CreateContext(bool longRunning = false) { 36 35 //var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString); … … 38 37 if (ConfigurationManager.ConnectionStrings[Settings.Default.HiveConnectionStringName] == null) { 39 38 context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString); 40 } 41 else { 39 } else { 42 40 context = new HiveDataContext(provider.GetOpenConnection(Settings.Default.HiveConnectionStringName)); 43 41 } … … 93 91 } 94 92 93 public IEnumerable<DT.LightweightTask> GetLightweightTasks(Expression<Func<Task, bool>> predicate) { 94 List<DT.LightweightTask> tasks = new List<DT.LightweightTask>(); 95 96 using (var db = CreateContext()) { 97 var tasksQuery = db.Tasks.Where(predicate).Select(task => new { task.TaskId, task.ExecutionTimeMs, task.ParentTaskId, task.StateLogs, task.State, task.Command }); 98 var taskDatasQuery = db.Tasks.Where(predicate).Where(task => task.JobData != null).Select(task => new { task.TaskId, task.JobData.LastUpdate }); 99 100 foreach (var task in tasksQuery) { 101 DT.LightweightTask t = new DT.LightweightTask(); 102 t.Id = task.TaskId; 103 t.ExecutionTime = TimeSpan.FromMilliseconds(task.ExecutionTimeMs); 104 t.ParentTaskId = task.ParentTaskId; 105 t.StateLog = task.StateLogs == null ? new List<DT.StateLog>() : task.StateLogs.Select(x => DataTransfer.Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList(); 106 t.State = DataTransfer.Convert.ToDto(task.State); 107 t.Command = DataTransfer.Convert.ToDto(task.Command); 108 t.LastTaskDataUpdate = taskDatasQuery.Where(x => x.TaskId == task.TaskId).Count() > 0 ? taskDatasQuery.Select(x => x.LastUpdate).First() : DateTime.MinValue; 109 tasks.Add(t); 110 } 111 } 112 return tasks; 113 } 114 115 public IEnumerable<DT.LightweightTask> GetLightweightTasksWithoutStateLog(Expression<Func<Task, bool>> predicate) { 116 List<DT.LightweightTask> tasks = new List<DT.LightweightTask>(); 117 118 using (var db = CreateContext()) { 119 var tasksQuery = db.Tasks.Where(predicate).Select(task => new { task.TaskId, task.ExecutionTimeMs, task.ParentTaskId, task.State, task.Command }); 120 var taskDatasQuery = db.Tasks.Where(predicate).Where(task => task.JobData != null).Select(task => new { task.TaskId, task.JobData.LastUpdate }); 121 122 foreach (var task in tasksQuery) { 123 DT.LightweightTask t = new DT.LightweightTask(); 124 t.Id = task.TaskId; 125 t.ExecutionTime = TimeSpan.FromMilliseconds(task.ExecutionTimeMs); 126 t.ParentTaskId = task.ParentTaskId; 127 t.StateLog = new List<DT.StateLog>(); 128 t.State = DataTransfer.Convert.ToDto(task.State); 129 t.Command = DataTransfer.Convert.ToDto(task.Command); 130 t.LastTaskDataUpdate = taskDatasQuery.Where(x => x.TaskId == task.TaskId).Count() > 0 ? taskDatasQuery.Select(x => x.LastUpdate).First() : DateTime.MinValue; 131 tasks.Add(t); 132 } 133 } 134 return tasks; 135 } 136 95 137 public Guid AddTask(DT.Task dto) { 96 138 return ExecuteWithContext<Guid>((db) => { … … 106 148 } 107 149 108 public void UpdateTask (DT.Task dto) {150 public void UpdateTaskAndPlugins(DT.Task dto) { 109 151 ExecuteWithContext((db) => { 110 152 var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id); … … 118 160 db.SubmitChanges(); 119 161 }); 162 } 163 164 public void UpdateTaskAndStateLogs(DT.Task dto) { 165 using (var db = CreateContext()) { 166 var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id); 167 if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto)); 168 else DT.Convert.ToEntity(dto, entity); 169 db.SubmitChanges(); 170 } 171 } 172 173 public void UpdateTask(DT.Task dto) { 174 using (var db = CreateContext()) { 175 db.DeferredLoadingEnabled = false; 176 177 var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id); 178 if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto)); 179 else DT.Convert.ToEntityTaskOnly(dto, entity); 180 db.SubmitChanges(); 181 } 120 182 } 121 183 … … 156 218 } 157 219 158 public IEnumerable< DT.Task> GetWaitingTasks(DT.Slave slave, int count) {159 return ExecuteWithContext<IEnumerable< DT.Task>>((db) => {220 public IEnumerable<TaskInfoForScheduler> GetWaitingTasks(DT.Slave slave) { 221 return ExecuteWithContext<IEnumerable<TaskInfoForScheduler>>((db) => { 160 222 var resourceIds = GetParentResources(slave.Id).Select(r => r.Id); 161 223 //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);). … … 169 231 && ar.Task.CoresNeeded <= slave.FreeCores 170 232 && ar.Task.MemoryNeeded <= slave.FreeMemory 171 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) 172 select DT.Convert.ToDto(ar.Task); 173 var waitingTasks = (count == 0 ? query : query.Take(count)).ToArray(); 233 select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority }; 234 var waitingTasks = query.ToArray(); 174 235 return waitingTasks; 175 236 }); … … 178 239 public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) { 179 240 return ExecuteWithContext<DT.Task>((db) => { 180 var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId); 181 job.State = taskState; 241 db.DeferredLoadingEnabled = false; 242 var task = db.Tasks.SingleOrDefault(x => x.TaskId == taskId); 243 task.State = taskState; 182 244 db.StateLogs.InsertOnSubmit(new StateLog { 183 245 TaskId = taskId, … … 189 251 }); 190 252 db.SubmitChanges(); 191 job= db.Tasks.SingleOrDefault(x => x.TaskId == taskId);192 return DT.Convert.ToDto( job);253 task = db.Tasks.SingleOrDefault(x => x.TaskId == taskId); 254 return DT.Convert.ToDto(task); 193 255 }); 194 256 } … … 299 361 } 300 362 363 public IEnumerable<JobInfoForScheduler> GetJobInfoForScheduler(Expression<Func<Job, bool>> predicate) { 364 using (var db = CreateContext()) { 365 return db.Jobs.Where(predicate).Select(x => new JobInfoForScheduler() { Id = x.JobId, DateCreated = x.DateCreated, OwnerUserId = x.OwnerUserId }).ToArray(); 366 } 367 } 368 301 369 public Guid AddJob(DT.Job dto) { 302 370 return ExecuteWithContext<Guid>((db) => { 303 371 var entity = DT.Convert.ToEntity(dto); 304 372 db.Jobs.InsertOnSubmit(entity); 373 if (!db.UserPriorities.Any(x => x.UserId == dto.OwnerUserId)) 374 EnqueueUserPriority(new DT.UserPriority { Id = dto.OwnerUserId, DateEnqueued = dto.DateCreated }); 305 375 db.SubmitChanges(); 306 376 return entity.JobId; … … 374 444 // not allowed, delete 375 445 db.JobPermissions.DeleteOnSubmit(jobPermission); 376 } 377 else { 446 } else { 378 447 // update 379 448 jobPermission.Permission = permission; 380 449 jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored 381 450 } 382 } 383 else { 451 } else { 384 452 // insert 385 453 if (permission != Permission.NotAllowed) { … … 441 509 442 510 public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) { 443 return ExecuteWithContext 511 return ExecuteWithContext<IEnumerable<DT.PluginData>>((db) => { 444 512 return db.PluginDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray(); 445 513 }); … … 599 667 } 600 668 601 public void AssignJobToResource(Guid jobId, Guid resourceId) { 602 ExecuteWithContext((db) => { 603 var job = db.Tasks.Where(x => x.TaskId == jobId).Single(); 604 job.AssignedResources.Add(new AssignedResource() { TaskId = jobId, ResourceId = resourceId }); 669 public void AssignJobToResource(Guid taskId, IEnumerable<Guid> resourceIds) { 670 ExecuteWithContext((db) => { 671 db.DeferredLoadingEnabled = false; 672 673 List<AssignedResource> assignedResources = new List<AssignedResource>(); 674 foreach (Guid rId in resourceIds) { 675 assignedResources.Add(new AssignedResource() { TaskId = taskId, ResourceId = rId }); 676 } 677 db.AssignedResources.InsertAllOnSubmit(assignedResources); 605 678 db.SubmitChanges(); 606 679 }); … … 608 681 609 682 public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) { 610 return ExecuteWithContext 683 return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => { 611 684 var job = db.Tasks.Where(x => x.TaskId == jobId).Single(); 612 685 return job.AssignedResources.Select(x => DT.Convert.ToDto(x.Resource)).ToArray(); … … 636 709 public IEnumerable<DT.Resource> GetChildResources(Guid resourceId) { 637 710 return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => { 638 var childs = new List<DT.Resource>(); 639 foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) { 640 childs.Add(DT.Convert.ToDto(child)); 641 childs.AddRange(GetChildResources(child.ResourceId)); 642 } 643 return childs; 644 }); 711 return CollectChildResources(resourceId, db); 712 }); 713 } 714 715 public IEnumerable<DT.Resource> CollectChildResources(Guid resourceId, HiveDataContext db) { 716 var childs = new List<DT.Resource>(); 717 foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) { 718 childs.Add(DT.Convert.ToDto(child)); 719 childs.AddRange(CollectChildResources(child.ResourceId, db)); 720 } 721 return childs; 645 722 } 646 723 … … 661 738 #region ResourcePermission Methods 662 739 public DT.ResourcePermission GetResourcePermission(Guid resourceId, Guid grantedUserId) { 663 return ExecuteWithContext <DT.ResourcePermission>((db) => {740 return ExecuteWithContext<DT.ResourcePermission>((db) => { 664 741 return DT.Convert.ToDto(db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId)); 665 742 }); … … 734 811 if (entity != null) { 735 812 entity.LastCleanup = datetime; 736 } 737 else { 813 } else { 738 814 entity = new Lifecycle(); 739 815 entity.LifecycleId = 0; // always only one entry with ID:0 … … 825 901 db.SubmitChanges(); 826 902 }); 903 } 904 905 public Dictionary<Guid, int> GetWaitingTasksByUser() { 906 using (var db = CreateContext()) { 907 var waitingTasksByUser = from task in db.Tasks 908 where task.State == TaskState.Waiting 909 group task by task.Job.OwnerUserId into g 910 select new { UserId = g.Key, UsedCores = g.Count() }; 911 return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores); 912 } 913 } 914 915 public Dictionary<Guid, int> GetWaitingTasksByUserForResources(List<Guid> resourceIds) { 916 using (var db = CreateContext()) { 917 var waitingTasksByUser = from task in db.Tasks 918 where task.State == TaskState.Waiting && task.AssignedResources.Any(x => resourceIds.Contains(x.ResourceId)) 919 group task by task.Job.OwnerUserId into g 920 select new { UserId = g.Key, UsedCores = g.Count() }; 921 return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores); 922 } 923 } 924 925 public Dictionary<Guid, int> GetCalculatingTasksByUser() { 926 using (var db = CreateContext()) { 927 var calculatingTasksByUser = from task in db.Tasks 928 where task.State == TaskState.Calculating 929 group task by task.Job.OwnerUserId into g 930 select new { UserId = g.Key, UsedCores = g.Count() }; 931 return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores); 932 } 933 } 934 935 public Dictionary<Guid, int> GetCalculatingTasksByUserForResources(List<Guid> resourceIds) { 936 using (var db = CreateContext()) { 937 var calculatingTasksByUser = from task in db.Tasks 938 where task.State == TaskState.Calculating && task.AssignedResources.Any(x => resourceIds.Contains(x.ResourceId)) 939 group task by task.Job.OwnerUserId into g 940 select new { UserId = g.Key, UsedCores = g.Count() }; 941 return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores); 942 } 827 943 } 828 944 … … 904 1020 #endregion 905 1021 1022 #region UserPriority Methods 1023 public IEnumerable<DT.UserPriority> GetUserPriorities(Expression<Func<UserPriority, bool>> predicate) { 1024 using (var db = CreateContext()) { 1025 return db.UserPriorities.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray(); 1026 } 1027 } 1028 1029 public void EnqueueUserPriority(DT.UserPriority dto) { 1030 using (var db = CreateContext()) { 1031 var entity = db.UserPriorities.FirstOrDefault(x => x.UserId == dto.Id); 1032 if (entity == null) db.UserPriorities.InsertOnSubmit(DT.Convert.ToEntity(dto)); 1033 else DT.Convert.ToEntity(dto, entity); 1034 db.SubmitChanges(); 1035 } 1036 } 1037 #endregion 1038 906 1039 #region Helpers 907 1040 private void CollectChildTasks(HiveDataContext db, Guid parentTaskId, List<Task> collection) {
Note: See TracChangeset
for help on using the changeset viewer.