Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/08/15 15:51:32 (9 years ago)
Author:
dglaser
Message:

#2388:

HeuristicLab.Services.Access:
HeuristicLab.Services.Access.DataAccess:

  • Changed connection strings and certificates for local usage

HeuristicLab.Services.Hive.DataAccess:

  • Added compiled queries for frequently used queries
  • Integrated string queries from OptimizedHiveDao

HeuristicLab.Services.Hive:

  • Added NewHeartbeatManager.cs
  • Added NewRoundRobinTaskScheduler.cs
  • Added PerformanceLogger
  • Updated AuthoriziationManager
  • Updated NewHiveService
    • Added Regions
    • Implemented missing methods
    • Improved performance of several queries

HeuristicLab.Services.WebApp.Status:

  • Fixed a bug which caused an error when calculating the average waiting time.
Location:
branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedResourceDao.cs

    r12584 r12691  
    3535      return Table.Where(x => x.TaskId == taskId);
    3636    }
     37
     38    public bool TaskIsAllowedToBeCalculatedBySlave(Guid taskId, Guid slaveId) {
     39      return DataContext.ExecuteQuery<int>(TaskIsAllowedToBeCalculatedBySlaveQueryString, slaveId, taskId).First() > 0;
     40    }
     41
     42    #region String queries
     43    private const string TaskIsAllowedToBeCalculatedBySlaveQueryString = @"
     44      WITH pr AS (
     45        SELECT ResourceId, ParentResourceId
     46        FROM [Resource]
     47        WHERE ResourceId = {0}
     48        UNION ALL
     49        SELECT r.ResourceId, r.ParentResourceId
     50        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
     51      )
     52      SELECT COUNT(ar.TaskId)
     53      FROM pr JOIN AssignedResources ar ON pr.ResourceId = ar.ResourceId
     54      WHERE ar.TaskId = {1}
     55    ";
     56    #endregion
    3757  }
    3858}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/DowntimeDao.cs

    r12468 r12691  
    2929
    3030    public override Downtime GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.DowntimeId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    public IQueryable<Downtime> GetByResourceId(Guid id) {
     35      return Table.Where(x => x.ResourceId == id);
     36    }
     37
     38    #region Compiled queries
     39    private static readonly Func<DataContext, Guid, Downtime> GetByIdQuery =
     40      CompiledQuery.Compile((DataContext db, Guid downtimeId) =>
     41        (from downtime in db.GetTable<Downtime>()
     42         where downtime.DowntimeId == downtimeId
     43         select downtime).SingleOrDefault());
     44    #endregion
    3345  }
    3446}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/GenericDao.cs

    r12484 r12691  
    109109
    110110    public bool Exists(T entity) {
    111       return Table.Contains(entity);
     111      return ExistsQuery(DataContext, entity);
    112112    }
    113113
     
    116116    }
    117117    #endregion
     118
     119    #region Compiled queries
     120    private static readonly Func<DataContext, T, bool> ExistsQuery =
     121      CompiledQuery.Compile((DataContext db, T entity) =>
     122        db.GetTable<T>().Contains(entity));
     123    #endregion
    118124  }
    119125}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimClientDao.cs

    r12516 r12691  
    2727namespace HeuristicLab.Services.Hive.DataAccess.Daos.HiveStatistics {
    2828  public class DimClientDao : GenericDao<Guid, DimClient> {
    29     private const string UpdateExpirationTimeQuery =
    30       @"UPDATE [statistics].[DimClient]
    31            SET ExpirationTime = {0}
    32          WHERE Id IN ({1});";
    33 
    3429    public DimClientDao(DataContext dataContext) : base(dataContext) { }
    3530
    3631    public override DimClient GetById(Guid id) {
    37       return Table.SingleOrDefault(x => x.Id == id);
     32      return GetByIdQuery(DataContext, id);
    3833    }
     34
    3935    public IQueryable<DimClient> GetActiveClients() {
    4036      return Table.Where(x => x.ExpirationTime == null);
     
    5349      return 0;
    5450    }
     51
     52    #region Compiled queries
     53    private static readonly Func<DataContext, Guid, DimClient> GetByIdQuery =
     54      CompiledQuery.Compile((DataContext db, Guid id) =>
     55        (from dimClient in db.GetTable<DimClient>()
     56         where dimClient.Id == id
     57         select dimClient).SingleOrDefault());
     58    #endregion
     59
     60    #region String queries
     61    private const string UpdateExpirationTimeQuery =
     62      @"UPDATE [statistics].[DimClient]
     63           SET ExpirationTime = {0}
     64         WHERE Id IN ({1});";
     65    #endregion
    5566  }
    5667}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimJobDao.cs

    r12516 r12691  
    2929
    3030    public override DimJob GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.JobId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
    3333
     
    4343      return Table.Where(x => x.DateCompleted != null);
    4444    }
     45
     46    #region Compiled queries
     47    private static readonly Func<DataContext, Guid, DimJob> GetByIdQuery =
     48      CompiledQuery.Compile((DataContext db, Guid id) =>
     49        (from dimJob in db.GetTable<DimJob>()
     50         where dimJob.JobId == id
     51         select dimJob).SingleOrDefault());
     52    #endregion
    4553  }
    4654}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimTimeDao.cs

    r12468 r12691  
    2929
    3030    public override DimTime GetById(DateTime id) {
    31       return Table.SingleOrDefault(x => x.Time == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
    3333    public DimTime GetLastEntry() {
    34       return Table.OrderByDescending(x => x.Time).FirstOrDefault();
     34      return GetLastEntryQuery(DataContext);
    3535    }
     36
     37    #region Compiled queries
     38    private static readonly Func<DataContext, DateTime, DimTime> GetByIdQuery =
     39      CompiledQuery.Compile((DataContext db, DateTime id) =>
     40        (from dimTime in db.GetTable<DimTime>()
     41         where dimTime.Time == id
     42         select dimTime).SingleOrDefault());
     43
     44    private static readonly Func<DataContext, DimTime> GetLastEntryQuery =
     45      CompiledQuery.Compile((DataContext db) =>
     46        (from dimTime in db.GetTable<DimTime>()
     47         orderby dimTime.Time descending
     48         select dimTime).FirstOrDefault());
     49    #endregion
    3650  }
    3751}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimUserDao.cs

    r12468 r12691  
    2929
    3030    public override DimUser GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.UserId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    #region Compiled queries
     35    private static readonly Func<DataContext, Guid, DimUser> GetByIdQuery =
     36      CompiledQuery.Compile((DataContext db, Guid id) =>
     37        (from dimUser in db.GetTable<DimUser>()
     38         where dimUser.UserId == id
     39         select dimUser).SingleOrDefault());
     40    #endregion
    3341  }
    3442}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactClientInfoDao.cs

    r12516 r12691  
    5353      return from cf in Table
    5454             join r in DimClientTable on cf.ClientId equals r.Id
    55              group cf by r.ResourceId into grpFacts
    56              select new LastUpdateTimestamp {
    57                ResourceId = grpFacts.Key,
    58                Timestamp = grpFacts.Max(x => x.Time),
    59              };
     55             group cf by r.ResourceId
     56               into grpFacts
     57               select new LastUpdateTimestamp {
     58                 ResourceId = grpFacts.Key,
     59                 Timestamp = grpFacts.Max(x => x.Time),
     60               };
    6061    }
    6162
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactTaskDao.cs

    r12551 r12691  
    3737    }
    3838
    39     private const string BatchDeleteQuery =
    40       @"DELETE FROM [statistics].[FactTask]
    41          WHERE TaskId IN ({0});";
    4239    public FactTaskDao(DataContext dataContext) : base(dataContext) { }
    4340
    4441    public override FactTask GetById(Guid id) {
    45       return Table.SingleOrDefault(x => x.TaskId == id);
     42      return GetByIdQuery(DataContext, id);
    4643    }
    4744
     
    5552
    5653    public DateTime? GetLastCompletedTaskFromJob(Guid id) {
    57       return Table.Where(x => x.JobId == id && x.EndTime != null).Max(x => x.EndTime);
     54      return GetLastCompletedTaskFromJobQuery(DataContext, id);
    5855    }
    5956
     
    9289    }
    9390
     91    #region Compiled queries
     92    private static readonly Func<DataContext, Guid, FactTask> GetByIdQuery =
     93      CompiledQuery.Compile((DataContext db, Guid id) =>
     94        (from factTask in db.GetTable<FactTask>()
     95         where factTask.TaskId == id
     96         select factTask).SingleOrDefault());
     97
     98
     99    private static readonly Func<DataContext, Guid, DateTime?> GetLastCompletedTaskFromJobQuery =
     100      CompiledQuery.Compile((DataContext db, Guid id) =>
     101        (from factTask in db.GetTable<FactTask>()
     102         where factTask.JobId == id && factTask.EndTime != null
     103         select factTask.EndTime).Max());
     104    #endregion
     105
     106    #region String queries
     107    private const string BatchDeleteQuery =
     108      @"DELETE FROM [statistics].[FactTask]
     109         WHERE TaskId IN ({0});";
     110    #endregion
    94111  }
    95112}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs

    r12468 r12691  
    2929
    3030    public override Job GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.JobId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    #region Compiled queries
     35    private static readonly Func<DataContext, Guid, Job> GetByIdQuery =
     36      CompiledQuery.Compile((DataContext db, Guid jobId) =>
     37        (from job in db.GetTable<Job>()
     38         where job.JobId == jobId
     39         select job).SingleOrDefault());
     40    #endregion
    3341  }
    3442}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobPermissionDao.cs

    r12584 r12691  
    3333
    3434    public JobPermission GetByJobAndUserId(Guid jobId, Guid userId) {
    35       return Table.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == userId);
     35      return GetByJobAndUserIdQuery(DataContext, jobId, userId);
    3636    }
     37
     38    public IQueryable<JobPermission> GetByJobId(Guid jobId) {
     39      return Table.Where(x => x.JobId == jobId);
     40    }
     41
     42    public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
     43      var jobPermission = GetByJobAndUserId(jobId, grantedUserId);
     44      if (jobPermission != null) {
     45        if (permission == Permission.NotAllowed) {
     46          Delete(jobPermission);
     47        } else {
     48          jobPermission.Permission = permission;
     49          jobPermission.GrantedByUserId = grantedByUserId;
     50        }
     51      } else {
     52        if (permission != Permission.NotAllowed) {
     53          Save(new JobPermission {
     54            JobId = jobId,
     55            GrantedByUserId = grantedByUserId,
     56            GrantedUserId = grantedUserId,
     57            Permission = permission
     58          });
     59        }
     60      }
     61    }
     62
     63    #region Compiled queries
     64    private static readonly Func<DataContext, Guid, Guid, JobPermission> GetByJobAndUserIdQuery =
     65      CompiledQuery.Compile((DataContext db, Guid jobId, Guid userId) =>
     66        (from jobPermission in db.GetTable<JobPermission>()
     67         where jobPermission.JobId == jobId && jobPermission.GrantedUserId == userId
     68         select jobPermission).SingleOrDefault());
     69    #endregion
    3770  }
    3871}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/LifecycleDao.cs

    r12468 r12691  
    2020#endregion
    2121
     22using System;
    2223using System.Data.Linq;
    2324using System.Linq;
     
    2829
    2930    public override Lifecycle GetById(int id) {
    30       return Table.SingleOrDefault(x => x.LifecycleId == id);
     31      return GetByIdQuery(DataContext, id);
    3132    }
     33
     34    public Lifecycle GetLastLifecycle() {
     35      return GetLastLifecycleQuery(DataContext);
     36    }
     37
     38    public void UpdateLifecycle() {
     39      var entity = GetLastLifecycle();
     40      if (entity != null) {
     41        entity.LastCleanup = DateTime.Now;
     42      } else {
     43        Save(new Lifecycle {
     44          LifecycleId = 0,
     45          LastCleanup = DateTime.Now
     46        });
     47      }
     48    }
     49
     50    #region Compiled queries
     51    private static readonly Func<DataContext, int, Lifecycle> GetByIdQuery =
     52     CompiledQuery.Compile((DataContext db, int lifecycleId) =>
     53       (from lifecycle in db.GetTable<Lifecycle>()
     54        where lifecycle.LifecycleId == lifecycleId
     55        select lifecycle).SingleOrDefault());
     56
     57    private static readonly Func<DataContext, Lifecycle> GetLastLifecycleQuery =
     58      CompiledQuery.Compile((DataContext db) =>
     59        (from lifecycle in db.GetTable<Lifecycle>()
     60         select lifecycle).SingleOrDefault());
     61    #endregion
    3262  }
    3363}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDao.cs

    r12468 r12691  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Data.Linq;
    2425using System.Linq;
     
    2930
    3031    public override Plugin GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.PluginId == id);
     32      return GetByIdQuery(DataContext, id);
    3233    }
     34
     35    public IEnumerable<Plugin> GetByHash(byte[] hash) {
     36      return GetByHashQuery(DataContext).Where(x => x.Hash.SequenceEqual(hash));
     37    }
     38
     39    #region Compiled queries
     40    private static readonly Func<DataContext, Guid, Plugin> GetByIdQuery =
     41      CompiledQuery.Compile((DataContext db, Guid pluginId) =>
     42        (from plugin in db.GetTable<Plugin>()
     43         where plugin.PluginId == pluginId
     44         select plugin).SingleOrDefault());
     45
     46    private static readonly Func<DataContext, IEnumerable<Plugin>> GetByHashQuery =
     47      CompiledQuery.Compile((DataContext db) =>
     48        (from plugin in db.GetTable<Plugin>()
     49         where plugin.Hash != null
     50         select plugin));
     51    #endregion
    3352  }
    3453}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDataDao.cs

    r12468 r12691  
    2929
    3030    public override PluginData GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.PluginDataId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    #region Compiled queries
     35    private static readonly Func<DataContext, Guid, PluginData> GetByIdQuery =
     36      CompiledQuery.Compile((DataContext db, Guid pluginDataId) =>
     37        (from pluginData in db.GetTable<PluginData>()
     38         where pluginData.PluginDataId == pluginDataId
     39         select pluginData).SingleOrDefault());
     40    #endregion
    3341  }
    3442}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/RequiredPluginDao.cs

    r12468 r12691  
    2929
    3030    public override RequiredPlugin GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.RequiredPluginId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    #region Compiled queries
     35    private static readonly Func<DataContext, Guid, RequiredPlugin> GetByIdQuery =
     36      CompiledQuery.Compile((DataContext db, Guid requiredPluginId) =>
     37        (from requiredPlugin in db.GetTable<RequiredPlugin>()
     38         where requiredPlugin.RequiredPluginId == requiredPluginId
     39         select requiredPlugin).SingleOrDefault());
     40    #endregion
    3341  }
    3442}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourceDao.cs

    r12468 r12691  
    2929
    3030    public override Resource GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.ResourceId == id);
     31      return GetByIdQuery(DataContext, id);
     32    }
     33
     34    public Resource GetByName(string name) {
     35      return GetByNameQuery(DataContext, name);
    3236    }
    3337
     
    3539      return Table.Where(x => x.OwnerUserId != null);
    3640    }
     41
     42    #region Compiled queries
     43    private static readonly Func<DataContext, Guid, Resource> GetByIdQuery =
     44      CompiledQuery.Compile((DataContext db, Guid resourceId) =>
     45        (from resource in db.GetTable<Resource>()
     46         where resource.ResourceId == resourceId
     47         select resource).SingleOrDefault());
     48
     49    private static readonly Func<DataContext, string, Resource> GetByNameQuery =
     50     CompiledQuery.Compile((DataContext db, string name) =>
     51       (from resource in db.GetTable<Resource>()
     52        where resource.Name == name
     53        select resource).FirstOrDefault());
     54    #endregion
    3755  }
    3856}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourcePermissionDao.cs

    r12468 r12691  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Data.Linq;
     25using System.Linq;
    2426
    2527namespace HeuristicLab.Services.Hive.DataAccess.Daos {
     
    3032      throw new NotImplementedException();
    3133    }
     34
     35    public IEnumerable<ResourcePermission> GetByResourceId(Guid id) {
     36      return GetByResourceIdGetByIdQuery(DataContext, id);
     37    }
     38
     39    public void DeleteByResourceAndGrantedUserId(Guid resourceId, IEnumerable<Guid> grantedUserId) {
     40      string paramIds = string.Join(",", grantedUserId.Select(x => string.Format("'{0}'", x)));
     41      if (!string.IsNullOrWhiteSpace(paramIds)) {
     42        string query = string.Format(DeleteByGrantedUserQuery, resourceId, paramIds);
     43        DataContext.ExecuteCommand(query);
     44      }
     45    }
     46
     47    #region Compiled queries
     48    private static readonly Func<DataContext, Guid, IEnumerable<ResourcePermission>> GetByResourceIdGetByIdQuery =
     49      CompiledQuery.Compile((DataContext db, Guid resourceId) =>
     50        from resourcePermission in db.GetTable<ResourcePermission>()
     51        where resourcePermission.ResourceId == resourceId
     52        select resourcePermission);
     53    #endregion
     54
     55    #region String queries
     56    private const string DeleteByGrantedUserQuery =
     57      @"DELETE FROM [ResourcePermission]
     58         WHERE ResourceId = '{0}'
     59           AND GrantedUserId IN ({1});";
     60    #endregion
    3261  }
    3362}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveDao.cs

    r12551 r12691  
    4646    #region IGenericDao<Guid,Slave> Members
    4747    public Slave GetById(Guid id) {
    48       return Entities.SingleOrDefault(x => x.ResourceId == id);
     48      return GetByIdQuery(dataContext, id);
    4949    }
    5050
     
    117117
    118118    public bool Exists(Slave entity) {
    119       return Table.Contains(entity);
     119      return ExistsQuery(dataContext, entity);
    120120    }
    121121
     
    124124    }
    125125    #endregion
     126
     127    public bool SlaveHasToShutdownComputer(Guid slaveId) {
     128      return dataContext.ExecuteQuery<int>(DowntimeQueryString, slaveId, DateTime.Now, DowntimeType.Shutdown.ToString()).FirstOrDefault() > 0;
     129    }
     130
     131    public bool SlaveIsAllowedToCalculate(Guid slaveId) {
     132      return dataContext.ExecuteQuery<int>(DowntimeQueryString, slaveId, DateTime.Now, DowntimeType.Offline.ToString()).FirstOrDefault() == 0;
     133    }
     134
     135    #region Compiled queries
     136    private static readonly Func<DataContext, Guid, Slave> GetByIdQuery =
     137      CompiledQuery.Compile((DataContext db, Guid slaveId) =>
     138        (from slave in db.GetTable<Resource>().OfType<Slave>()
     139         where slave.ResourceId == slaveId
     140         select slave).SingleOrDefault());
     141
     142    private static readonly Func<DataContext, Slave, bool> ExistsQuery =
     143      CompiledQuery.Compile((DataContext db, Slave entity) =>
     144        db.GetTable<Resource>().OfType<Slave>().Contains(entity));
     145    #endregion
     146
     147    #region String queries
     148    private const string DowntimeQueryString = @"
     149      WITH pr AS (
     150        SELECT ResourceId, ParentResourceId
     151        FROM [Resource]
     152        WHERE ResourceId = {0}
     153        UNION ALL
     154        SELECT r.ResourceId, r.ParentResourceId
     155        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
     156      )
     157      SELECT COUNT(dt.DowntimeId)
     158      FROM pr JOIN [Downtime] dt ON pr.ResourceId = dt.ResourceId
     159      WHERE {1} BETWEEN dt.StartDate AND dt.EndDate
     160        AND dt.DowntimeType = {2}
     161    ";
     162    #endregion
    126163  }
    127164}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveGroupDao.cs

    r12484 r12691  
    4545    #region IGenericDao<Guid,SlaveGroup> Members
    4646    public SlaveGroup GetById(Guid id) {
    47       return Entities.SingleOrDefault(x => x.ResourceId == id);
     47      return GetByIdQuery(dataContext, id);
    4848    }
    4949
     
    112112
    113113    public bool Exists(SlaveGroup entity) {
    114       return Table.Contains(entity);
     114      return ExistsQuery(dataContext, entity);
    115115    }
    116116
     
    119119    }
    120120    #endregion
     121
     122    #region Compiled queries
     123    private static readonly Func<DataContext, Guid, SlaveGroup> GetByIdQuery =
     124      CompiledQuery.Compile((DataContext db, Guid slaveGroupId) =>
     125        (from slaveGroup in db.GetTable<Resource>().OfType<SlaveGroup>()
     126         where slaveGroup.ResourceId == slaveGroupId
     127         select slaveGroup).SingleOrDefault());
     128
     129    private static readonly Func<DataContext, SlaveGroup, bool> ExistsQuery =
     130      CompiledQuery.Compile((DataContext db, SlaveGroup slaveGroup) =>
     131        db.GetTable<Resource>().OfType<SlaveGroup>().Contains(slaveGroup));
     132    #endregion
    121133  }
    122134}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/StateLogDao.cs

    r12584 r12691  
    2626namespace HeuristicLab.Services.Hive.DataAccess.Daos {
    2727  public class StateLogDao : GenericDao<Guid, StateLog> {
    28 
    2928    private Table<Task> TaskTable {
    3029      get { return DataContext.GetTable<Task>(); }
     
    3433
    3534    public override StateLog GetById(Guid id) {
    36       return Table.SingleOrDefault(x => x.StateLogId == id);
     35      return GetByIdQuery(DataContext, id);
    3736    }
    3837
    3938    public StateLog GetLastStateLogFromTask(Task task) {
    40       return Table
    41         .Where(x => x.TaskId == task.TaskId)
    42         .OrderByDescending(x => x.DateTime)
    43         .First(x => x.SlaveId != null);
     39      return GetLastStateLogFromTaskQuery(DataContext, task);
    4440    }
     41
     42    #region Compiled queries
     43    private static readonly Func<DataContext, Guid, StateLog> GetByIdQuery =
     44      CompiledQuery.Compile((DataContext db, Guid stateLogId) =>
     45        (from stateLog in db.GetTable<StateLog>()
     46         where stateLog.StateLogId == stateLogId
     47         select stateLog).SingleOrDefault());
     48
     49    private static readonly Func<DataContext, Task, StateLog> GetLastStateLogFromTaskQuery =
     50      CompiledQuery.Compile((DataContext db, Task task) =>
     51        (from stateLog in db.GetTable<StateLog>()
     52         where stateLog.TaskId == task.TaskId
     53         orderby stateLog.DateTime descending
     54         select stateLog).First(x => x.SlaveId != null));
     55    #endregion
    4556  }
    4657}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs

    r12584 r12691  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Data.Linq;
    2425using System.Linq;
     
    2930
    3031    public override Task GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.TaskId == id);
     32      return GetByIdQuery(DataContext, id);
    3233    }
    3334
     
    3940      return Table.Where(x => x.JobId == id);
    4041    }
     42    public class TaskPriorityInfo {
     43      public Guid JobId { get; set; }
     44      public Guid TaskId { get; set; }
     45      public int Priority { get; set; }
     46    }
     47
     48    public IEnumerable<TaskPriorityInfo> GetWaitingTasks(Slave slave) {
     49      //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
     50      //Because there is at the moment no case where this makes sense (there don't exist parent tasks which need to be calculated),
     51      //we skip this step because it's wasted runtime
     52      return DataContext.ExecuteQuery<TaskPriorityInfo>(GetWaitingTasksQueryString, slave.ResourceId, Enum.GetName(typeof(TaskState), TaskState.Waiting), slave.FreeCores, slave.FreeMemory).ToList();
     53    }
     54
     55    public void UpdateExecutionTime(Guid taskId, double executionTime) {
     56      DataContext.ExecuteCommand(UpdateExecutionTimeQuery, executionTime, DateTime.Now, taskId);
     57    }
     58
     59    #region Compiled queries
     60    private static readonly Func<DataContext, Guid, Task> GetByIdQuery =
     61      CompiledQuery.Compile((DataContext db, Guid taskId) =>
     62        (from task in db.GetTable<Task>()
     63         where task.TaskId == taskId
     64         select task).SingleOrDefault());
     65    #endregion
     66
     67    #region String queries
     68    private const string GetWaitingTasksQueryString = @"
     69      WITH pr AS (
     70        SELECT ResourceId, ParentResourceId
     71        FROM [Resource]
     72        WHERE ResourceId = {0}
     73        UNION ALL
     74        SELECT r.ResourceId, r.ParentResourceId
     75        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
     76      )
     77      SELECT DISTINCT t.TaskId, t.JobId, t.Priority
     78      FROM pr JOIN AssignedResources ar ON ar.ResourceId = pr.ResourceId
     79          JOIN Task t ON t.TaskId = ar.TaskId
     80      WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
     81          AND t.TaskState = {1}
     82          AND t.CoresNeeded <= {2}
     83          AND t.MemoryNeeded <= {3}
     84    ";
     85
     86    private const string UpdateExecutionTimeQuery = @"
     87      UPDATE [Task]
     88         SET ExecutionTimeMs = {0},
     89             LastHeartbeat = {1}
     90       WHERE TaskId = {2}
     91    ";
     92    #endregion
    4193  }
    4294}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDataDao.cs

    r12468 r12691  
    2929
    3030    public override TaskData GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.TaskId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    #region Compiled queries
     35    private static readonly Func<DataContext, Guid, TaskData> GetByIdQuery =
     36      CompiledQuery.Compile((DataContext db, Guid taskId) =>
     37        (from taskData in db.GetTable<TaskData>()
     38         where taskData.TaskId == taskId
     39         select taskData).SingleOrDefault());
     40    #endregion
    3341  }
    3442}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/UserPriorityDao.cs

    r12468 r12691  
    2929
    3030    public override UserPriority GetById(Guid id) {
    31       return Table.SingleOrDefault(x => x.UserId == id);
     31      return GetByIdQuery(DataContext, id);
    3232    }
     33
     34    #region Compiled queries
     35    private static readonly Func<DataContext, Guid, UserPriority> GetByIdQuery =
     36      CompiledQuery.Compile((DataContext db, Guid userId) =>
     37        (from userPriority in db.GetTable<UserPriority>()
     38         where userPriority.UserId == userId
     39         select userPriority).SingleOrDefault());
     40    #endregion
    3341  }
    3442}
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs

    r12468 r12691  
    3535
    3636    #region Hive daos
     37
     38    private AssignedResourceDao assignedResourceDao;
    3739    public AssignedResourceDao AssignedResourceDao {
    38       get { return new AssignedResourceDao(dataContext); }
    39     }
    40 
     40      get { return assignedResourceDao ?? (assignedResourceDao = new AssignedResourceDao(dataContext)); }
     41    }
     42
     43    private DowntimeDao downtimeDao;
    4144    public DowntimeDao DowntimeDao {
    42       get { return new DowntimeDao(dataContext); }
    43     }
    44 
     45      get { return downtimeDao ?? (downtimeDao = new DowntimeDao(dataContext)); }
     46    }
     47
     48    private JobDao jobDao;
    4549    public JobDao JobDao {
    46       get { return new JobDao(dataContext); }
    47     }
    48 
     50      get { return jobDao ?? (jobDao = new JobDao(dataContext)); }
     51    }
     52
     53    private JobPermissionDao jobPermissionDao;
    4954    public JobPermissionDao JobPermissionDao {
    50       get { return new JobPermissionDao(dataContext); }
    51     }
    52 
     55      get { return jobPermissionDao ?? (jobPermissionDao = new JobPermissionDao(dataContext)); }
     56    }
     57
     58    private LifecycleDao lifecycleDao;
    5359    public LifecycleDao LifecycleDao {
    54       get { return new LifecycleDao(dataContext); }
    55     }
    56 
     60      get { return lifecycleDao ?? (lifecycleDao = new LifecycleDao(dataContext)); }
     61    }
     62
     63    private PluginDao pluginDao;
    5764    public PluginDao PluginDao {
    58       get { return new PluginDao(dataContext); }
    59     }
    60 
     65      get { return pluginDao ?? (pluginDao = new PluginDao(dataContext)); }
     66    }
     67
     68    private PluginDataDao pluginDataDao;
    6169    public PluginDataDao PluginDataDao {
    62       get { return new PluginDataDao(dataContext); }
    63     }
    64 
     70      get { return pluginDataDao ?? (pluginDataDao = new PluginDataDao(dataContext)); }
     71    }
     72
     73    private RequiredPluginDao requiredPluginDao;
    6574    public RequiredPluginDao RequiredPluginDao {
    66       get { return new RequiredPluginDao(dataContext); }
    67     }
    68 
     75      get { return requiredPluginDao ?? (requiredPluginDao = new RequiredPluginDao(dataContext)); }
     76    }
     77
     78    private ResourceDao resourceDao;
    6979    public ResourceDao ResourceDao {
    70       get { return new ResourceDao(dataContext); }
    71     }
    72 
     80      get { return resourceDao ?? (resourceDao = new ResourceDao(dataContext)); }
     81    }
     82
     83    private ResourcePermissionDao resourcePermissionDao;
    7384    public ResourcePermissionDao ResourcePermissionDao {
    74       get { return new ResourcePermissionDao(dataContext); }
    75     }
    76 
     85      get { return resourcePermissionDao ?? (resourcePermissionDao = new ResourcePermissionDao(dataContext)); }
     86    }
     87
     88    private SlaveDao slaveDao;
    7789    public SlaveDao SlaveDao {
    78       get { return new SlaveDao(dataContext); }
    79     }
    80 
     90      get { return slaveDao ?? (slaveDao = new SlaveDao(dataContext)); }
     91    }
     92
     93    private SlaveGroupDao slaveGroupDao;
    8194    public SlaveGroupDao SlaveGroupDao {
    82       get { return new SlaveGroupDao(dataContext); }
    83     }
    84 
     95      get { return slaveGroupDao ?? (slaveGroupDao = new SlaveGroupDao(dataContext)); }
     96    }
     97
     98    private StateLogDao stateLogDao;
    8599    public StateLogDao StateLogDao {
    86       get { return new StateLogDao(dataContext); }
    87     }
    88 
     100      get { return stateLogDao ?? (stateLogDao = new StateLogDao(dataContext)); }
     101    }
     102
     103    private TaskDao taskDao;
    89104    public TaskDao TaskDao {
    90       get { return new TaskDao(dataContext); }
    91     }
    92 
     105      get { return taskDao ?? (taskDao = new TaskDao(dataContext)); }
     106    }
     107
     108    private TaskDataDao taskDataDao;
    93109    public TaskDataDao TaskDataDao {
    94       get { return new TaskDataDao(dataContext); }
    95     }
    96 
     110      get { return taskDataDao ?? (taskDataDao = new TaskDataDao(dataContext)); }
     111    }
     112
     113    private UserPriorityDao userPriorityDao;
    97114    public UserPriorityDao UserPriorityDao {
    98       get { return new UserPriorityDao(dataContext); }
     115      get { return userPriorityDao ?? (userPriorityDao = new UserPriorityDao(dataContext)); }
    99116    }
    100117    #endregion
    101118
    102119    #region HiveStatistics daos
     120
     121    private DimClientDao dimClientDao;
    103122    public DimClientDao DimClientDao {
    104       get { return new DimClientDao(dataContext); }
    105     }
    106 
     123      get { return dimClientDao ?? (dimClientDao = new DimClientDao(dataContext)); }
     124    }
     125
     126    private DimJobDao dimJobDao;
    107127    public DimJobDao DimJobDao {
    108       get { return new DimJobDao(dataContext); }
    109     }
    110 
     128      get { return dimJobDao ?? (dimJobDao = new DimJobDao(dataContext)); }
     129    }
     130
     131    private DimTimeDao dimTimeDao;
    111132    public DimTimeDao DimTimeDao {
    112       get { return new DimTimeDao(dataContext); }
    113     }
    114 
     133      get { return dimTimeDao ?? (dimTimeDao = new DimTimeDao(dataContext)); }
     134    }
     135
     136    private DimUserDao dimUserDao;
    115137    public DimUserDao DimUserDao {
    116       get { return new DimUserDao(dataContext); }
    117     }
    118 
     138      get { return dimUserDao ?? (dimUserDao = new DimUserDao(dataContext)); }
     139    }
     140
     141    private FactClientInfoDao factClientInfoDao;
    119142    public FactClientInfoDao FactClientInfoDao {
    120       get { return new FactClientInfoDao(dataContext); }
    121     }
    122 
     143      get { return factClientInfoDao ?? (factClientInfoDao = new FactClientInfoDao(dataContext)); }
     144    }
     145
     146    private FactTaskDao factTaskDao;
    123147    public FactTaskDao FactTaskDao {
    124       get { return new FactTaskDao(dataContext); }
     148      get { return factTaskDao ?? (factTaskDao = new FactTaskDao(dataContext)); }
    125149    }
    126150    #endregion
     
    130154      if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
    131155      dataContext = context;
     156    }
     157
     158    public PersistenceManager(DataContext dataContext) {
     159      this.dataContext = dataContext;
    132160    }
    133161
     
    179207
    180208    public void Dispose() {
    181       if (dataContext != null) {
    182         dataContext.Dispose();
    183       }
     209      //if (dataContext != null) {
     210      //  dataContext.Dispose();
     211      //}
    184212    }
    185213  }
Note: See TracChangeset for help on using the changeset viewer.