Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/13 17:16:49 (12 years ago)
Author:
pfleck
Message:

#2030
Replaced lazy loading with specialized queries.
Compiled queries used for Heardbeat queries.
Changed result types to IQueryable<T> for later query modification.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs

    r9381 r9385  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Data.Linq;
    2425using System.Linq;
    2526using System.Linq.Expressions;
     
    4546    public Task GetTaskDA(Guid id) {
    4647      var db = HiveOperationContext.Current.DataContext;
    47       return db.Tasks.SingleOrDefault(x => x.TaskId == id);
    48     }
     48      return GetTaskByIdQuery(db, id).SingleOrDefault();
     49    }
     50
     51    private static Func<HiveDataContext, Guid, IQueryable<Task>> GetTaskByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid id) =>
     52      from t in db.Tasks
     53      where t.TaskId == id
     54      select t
     55    );
    4956
    5057    public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) {
     
    205212    }
    206213
    207     public IEnumerable<TaskInfoForScheduler> GetWaitingTasksDA(Slave slave) {
     214    public IQueryable<TaskInfoForScheduler> GetWaitingTasksDA(Slave slave) {
    208215      var db = HiveOperationContext.Current.DataContext;
     216
    209217      var parentResources = GetParentResourcesDA(slave.ResourceId);
    210218      var resourceIds = parentResources.Select(x => x.ResourceId);
     
    213221      //we skip this step because it's wasted runtime
    214222
    215       var query = from ar in db.AssignedResources
    216                   where resourceIds.Contains(ar.ResourceId)
    217                      && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
    218                      && ar.Task.State == TaskState.Waiting
    219                      && ar.Task.CoresNeeded <= slave.FreeCores
    220                      && ar.Task.MemoryNeeded <= slave.FreeMemory
    221                   select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority };
    222       var waitingTasks = query.ToArray();
    223       return waitingTasks;
    224     }
     223      return from ar in db.AssignedResources
     224             where resourceIds.Contains(ar.ResourceId)
     225                 && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
     226                 && ar.Task.State == TaskState.Waiting
     227                 && ar.Task.CoresNeeded <= slave.FreeCores
     228                 && ar.Task.MemoryNeeded <= slave.FreeMemory
     229             select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority };
     230    }
     231
     232    /*private static Func<HiveDataContext, Guid, Slave, IQueryable<TaskInfoForScheduler>> GetWaitingTasksQuery = CompiledQuery.Compile((HiveDataContext db, Guid id, Slave slave) =>
     233      from ar in db.AssignedResources
     234      where ar.ResourceId == id
     235          && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
     236          && ar.Task.State == TaskState.Waiting
     237          && ar.Task.CoresNeeded <= slave.FreeCores
     238          && ar.Task.MemoryNeeded <= slave.FreeMemory
     239      select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority }
     240    );*/
    225241
    226242    public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
     
    265281      });
    266282
    267       var task = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
     283      var task = GetTaskDA(taskId);
    268284      task.State = taskState;
    269285
     
    567583    public Slave GetSlaveDA(Guid id) {
    568584      var db = HiveOperationContext.Current.DataContext;
    569       return db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id);
    570     }
     585      return GetSlaveByIdQuery(db, id).SingleOrDefault();
     586    }
     587
     588    private static Func<HiveDataContext, Guid, IQueryable<Slave>> GetSlaveByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid slaveId) =>
     589      from s in db.Resources.OfType<Slave>()
     590      where s.ResourceId == slaveId
     591      select s
     592    );
    571593
    572594    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
     
    714736    }
    715737
     738    public IQueryable<Guid> GetAssignedResourcesIdsDA(Guid taskId) {
     739      var db = HiveOperationContext.Current.DataContext;
     740      return GetAssignedResourcesIdQuery(db, taskId);
     741    }
     742
     743    private static Func<HiveDataContext, Guid, IQueryable<Guid>> GetAssignedResourcesIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid taskId) =>
     744     from ar in db.AssignedResources
     745     where ar.TaskId == taskId
     746     select ar.ResourceId
     747   );
     748
    716749    /// <summary>
    717750    /// Returns all parent resources of a resource (the given resource is also added)
     
    727760    public IEnumerable<Resource> GetParentResourcesDA(Guid resourceId) {
    728761      var db = HiveOperationContext.Current.DataContext;
    729       var resources = new List<Resource>();
    730       CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
    731       return resources;
    732     }
    733 
    734     private void CollectParentResources(List<Resource> resources, Resource resource) {
     762      var child = db.Resources.Single(r => r.ResourceId == resourceId);
     763
     764      yield return child;
     765      while (child.ParentResource != null) {
     766        child = child.ParentResource;
     767        yield return child;
     768      }
     769    }
     770
     771    public IEnumerable<Guid> GetParentResourcesIDsDA(Guid resourceId) {
     772      var db = HiveOperationContext.Current.DataContext;
     773      var child = db.Resources.Single(r => r.ResourceId == resourceId);
     774
     775      yield return resourceId;
     776      while (child.ParentResource != null) {
     777        child = child.ParentResource;
     778        yield return child.ResourceId;
     779      }
     780    }
     781
     782    public IQueryable<int> GetNumberOfDowntimesFromParentResourcesAtCurrentTimeDA(Guid resourceId, DowntimeType type) {
     783      var db = HiveOperationContext.Current.DataContext;
     784
     785      var ids = GetParentResourcesIDsDA(resourceId).ToArray();
     786
     787      return from r in db.Resources
     788             where ids.Contains(r.ResourceId)
     789             select (from d in db.Downtimes
     790                     where d.ResourceId == r.ResourceId && d.DowntimeType == type && (DateTime.Now >= d.StartDate) && (DateTime.Now <= d.EndDate)
     791                     select d).Count();
     792    }
     793
     794    /*private static Func<HiveDataContext, Guid, DowntimeType, int> GetNumberOfDowntimesAtCurrentTimeQuery =
     795      CompiledQuery.Compile((HiveDataContext db, Guid ids, DowntimeType type) =>
     796        (from d in db.Downtimes
     797         where d.ResourceId == ids && d.DowntimeType == type && (DateTime.Now >= d.StartDate) && (DateTime.Now <= d.EndDate)
     798         select d).Count()
     799    );*/
     800
     801    private static void CollectParentResources(ICollection<Resource> resources, Resource resource) {
    735802      if (resource == null) return;
    736803      resources.Add(resource);
Note: See TracChangeset for help on using the changeset viewer.