[9533] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using System.Transactions;
|
---|
[9545] | 4 | using HeuristicLab.Services.Access;
|
---|
[9533] | 5 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Services.Hive {
|
---|
[9526] | 8 | public class HiveStatisticsGenerator : IStatisticsGenerator {
|
---|
[9545] | 9 |
|
---|
| 10 | private IUserManager userManager { get { return ServiceLocator.Instance.UserManager; } }
|
---|
| 11 |
|
---|
[9541] | 12 | private static readonly TimeSpan SmallestTimeSpan = new TimeSpan(0, 5, 0);
|
---|
[9533] | 13 |
|
---|
[9526] | 14 | public void GenerateStatistics() {
|
---|
[9533] | 15 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
| 16 | using (var transaction = new TransactionScope()) {
|
---|
[9541] | 17 | var newTime = UpdateDimensionTables(db);
|
---|
| 18 | db.SubmitChanges();
|
---|
[9533] | 19 |
|
---|
| 20 | if (newTime != null) {
|
---|
[9541] | 21 | UpdateFactTables(newTime, db);
|
---|
| 22 | db.SubmitChanges();
|
---|
[9533] | 23 | }
|
---|
| 24 |
|
---|
| 25 | transaction.Complete();
|
---|
| 26 | }
|
---|
[9526] | 27 | }
|
---|
| 28 |
|
---|
[9533] | 29 | private DimTime UpdateDimensionTables(HiveDataContext db) {
|
---|
| 30 | var newTime = UpdateTime(db);
|
---|
| 31 | // Update other tables out of sync with time dimension?
|
---|
| 32 | UpdateJobs(db);
|
---|
| 33 | UpdateUsers(db);
|
---|
[9538] | 34 | UpdateClients(db);
|
---|
[9526] | 35 |
|
---|
[9533] | 36 | return newTime;
|
---|
[9526] | 37 | }
|
---|
| 38 |
|
---|
[9533] | 39 | private DimTime UpdateTime(HiveDataContext db) {
|
---|
| 40 | var lastUpdate =
|
---|
| 41 | (from t in db.DimTimes
|
---|
| 42 | orderby t.Time descending
|
---|
| 43 | select t.Time)
|
---|
| 44 | .FirstOrDefault();
|
---|
[9526] | 45 |
|
---|
[9533] | 46 | var now = DateTime.Now;
|
---|
| 47 | DimTime newTime = null;
|
---|
[9541] | 48 | if (lastUpdate == default(DateTime) || lastUpdate + SmallestTimeSpan < now) {
|
---|
[9533] | 49 | newTime = new DimTime {
|
---|
[9541] | 50 | Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % SmallestTimeSpan.Minutes, 0),
|
---|
[9533] | 51 | Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0),
|
---|
| 52 | Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0),
|
---|
| 53 | Month = new DateTime(now.Year, now.Month, 1, 0, 0, 0),
|
---|
| 54 | Year = new DateTime(now.Year, 1, 1, 0, 0, 0)
|
---|
| 55 | };
|
---|
| 56 | db.DimTimes.InsertOnSubmit(newTime);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return newTime;
|
---|
[9526] | 60 | }
|
---|
[9533] | 61 |
|
---|
| 62 | private void UpdateJobs(HiveDataContext db) {
|
---|
| 63 | var newJobs =
|
---|
| 64 | from j in db.Jobs
|
---|
| 65 | where !db.DimJobs.Select(x => x.JobId).Contains(j.JobId)
|
---|
| 66 | select j;
|
---|
| 67 |
|
---|
| 68 | var newDimJobs =
|
---|
| 69 | from j in newJobs.ToList()
|
---|
| 70 | select new DimJob {
|
---|
| 71 | JobId = j.JobId,
|
---|
| 72 | JobName = j.Name,
|
---|
| 73 | UserId = j.OwnerUserId,
|
---|
[9545] | 74 | UserName = userManager.GetUserById(j.OwnerUserId).UserName
|
---|
[9533] | 75 | };
|
---|
| 76 |
|
---|
| 77 | db.DimJobs.InsertAllOnSubmit(newDimJobs);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private void UpdateUsers(HiveDataContext db) {
|
---|
| 81 | var newUsers =
|
---|
| 82 | from u in db.Resources.Where(x => x.OwnerUserId != null).Select(x => x.OwnerUserId.Value).Union(db.Jobs.Select(x => x.OwnerUserId))
|
---|
| 83 | where !db.DimUsers.Select(x => x.UserId).Contains(u)
|
---|
| 84 | select u;
|
---|
| 85 |
|
---|
| 86 | var newDimUsers =
|
---|
| 87 | from u in newUsers.ToList()
|
---|
| 88 | select new DimUser {
|
---|
| 89 | UserId = u,
|
---|
[9545] | 90 | Name = userManager.GetUserById(u).UserName
|
---|
[9533] | 91 | };
|
---|
| 92 |
|
---|
| 93 | db.DimUsers.InsertAllOnSubmit(newDimUsers);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[9538] | 96 | private void UpdateClients(HiveDataContext db) {
|
---|
| 97 | var removedClients =
|
---|
| 98 | from c in db.DimClients
|
---|
| 99 | where c.ExpirationTime == null &&
|
---|
| 100 | !db.Resources.OfType<Slave>().Select(x => x.ResourceId).Contains(c.ResourceId)
|
---|
| 101 | select c;
|
---|
| 102 |
|
---|
| 103 | var modifiedClients =
|
---|
| 104 | from s in db.Resources.OfType<Slave>()
|
---|
| 105 | join c in db.DimClients on s.ResourceId equals c.ResourceId
|
---|
| 106 | where c.ExpirationTime == null
|
---|
| 107 | && (s.Name != c.Name || s.ParentResourceId != c.ResourceGroupId ||
|
---|
| 108 | s.ParentResource.ParentResourceId != c.ResourceGroup2Id)
|
---|
| 109 | select new { Slave = s, Client = c };
|
---|
| 110 |
|
---|
| 111 | foreach (var client in removedClients.Union(modifiedClients.Select(x => x.Client))) {
|
---|
| 112 | client.ExpirationTime = DateTime.Now;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | var newClients =
|
---|
| 116 | from s in db.Resources.OfType<Slave>()
|
---|
| 117 | where !db.DimClients.Select(x => x.ResourceId).Contains(s.ResourceId)
|
---|
| 118 | || modifiedClients.Select(x => x.Slave.ResourceId).Contains(s.ResourceId)
|
---|
| 119 | select new {
|
---|
| 120 | Slave = s,
|
---|
| 121 | Group = s.ParentResourceId,
|
---|
| 122 | Group2 = s.ParentResource.ParentResourceId
|
---|
| 123 | };
|
---|
| 124 |
|
---|
| 125 | var newDimClients =
|
---|
| 126 | from s in newClients.ToList()
|
---|
| 127 | select new DimClient {
|
---|
| 128 | ResourceId = s.Slave.ResourceId,
|
---|
| 129 | Name = s.Slave.Name,
|
---|
| 130 | ExpirationTime = null,
|
---|
| 131 | ResourceGroupId = s.Group,
|
---|
| 132 | ResourceGroup2Id = s.Group2
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | db.DimClients.InsertAllOnSubmit(newDimClients);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[9541] | 138 | private void UpdateFactTables(DimTime newTime, HiveDataContext db) {
|
---|
| 139 | UpdateClientInfoFacts(newTime, db);
|
---|
| 140 | //UpdateTaskFacts(newTime, db);
|
---|
| 141 | }
|
---|
[9533] | 142 |
|
---|
[9541] | 143 | private void UpdateClientInfoFacts(DimTime newTime, HiveDataContext db) {
|
---|
| 144 | var lastFacts =
|
---|
| 145 | from cf in db.FactClientInfos
|
---|
[9545] | 146 | join r in db.DimClients on cf.ClientId equals r.Id
|
---|
| 147 | group cf by r.ResourceId into grpFacts
|
---|
| 148 | select new {
|
---|
| 149 | ResourceId = grpFacts.Key,
|
---|
| 150 | Fact = grpFacts.OrderByDescending(x => x.Time).First(),
|
---|
| 151 | };
|
---|
[9541] | 152 |
|
---|
| 153 | var slaves =
|
---|
| 154 | from s in db.Resources.OfType<Slave>()
|
---|
| 155 | join c in db.DimClients on s.ResourceId equals c.ResourceId
|
---|
[9545] | 156 | join lcf in lastFacts on c.ResourceId equals lcf.ResourceId into joinCf
|
---|
[9541] | 157 | from cf in joinCf.DefaultIfEmpty()
|
---|
[9545] | 158 | where c.ExpirationTime == null
|
---|
[9541] | 159 | select new {
|
---|
| 160 | Slave = s,
|
---|
| 161 | Client = c,
|
---|
[9545] | 162 | LastFact = cf != null ? cf.Fact : null
|
---|
[9541] | 163 | };
|
---|
| 164 |
|
---|
| 165 | var clientFacts =
|
---|
| 166 | from s in slaves.ToList()
|
---|
| 167 | select new FactClientInfo {
|
---|
| 168 | DimClient = s.Client,
|
---|
| 169 | DimTime = newTime,
|
---|
| 170 | UserId = s.Slave.OwnerUserId,
|
---|
| 171 | NumUsedCores =
|
---|
| 172 | s.Slave.Cores != null && s.Slave.FreeCores != null
|
---|
| 173 | ? s.Slave.Cores.Value - s.Slave.FreeCores.Value
|
---|
| 174 | : 0,
|
---|
| 175 | NumTotalCores = s.Slave.Cores ?? 0,
|
---|
| 176 | UsedMemory =
|
---|
| 177 | s.Slave.Memory != null && s.Slave.FreeMemory != null
|
---|
| 178 | ? s.Slave.Memory.Value - s.Slave.FreeMemory.Value
|
---|
| 179 | : 0,
|
---|
| 180 | TotalMemory = s.Slave.Memory ?? 0,
|
---|
| 181 | CpuUtilization = s.Slave.CpuUtilization,
|
---|
| 182 | TrafficIn = 0,
|
---|
| 183 | TrafficOut = 0,
|
---|
| 184 | TotalTimeIdle = CalcNewTotalTime(s.LastFact, newTime.Time,
|
---|
| 185 | x => x.TotalTimeIdle,
|
---|
| 186 | () => s.Slave.SlaveState == SlaveState.Idle && s.Slave.IsAllowedToCalculate),
|
---|
| 187 | TotalTimeCalculating = CalcNewTotalTime(s.LastFact, newTime.Time,
|
---|
| 188 | x => x.TotalTimeCalculating,
|
---|
| 189 | () => s.Slave.SlaveState == SlaveState.Calculating),
|
---|
| 190 | TotalTimeTransferring = 0.0,
|
---|
| 191 | TotalTimeUnavailable = CalcNewTotalTime(s.LastFact, newTime.Time,
|
---|
| 192 | x => x.TotalTimeUnavailable,
|
---|
| 193 | () => s.Slave.SlaveState == SlaveState.Idle && !s.Slave.IsAllowedToCalculate),
|
---|
| 194 | TotalTimeOffline = CalcNewTotalTime(s.LastFact, newTime.Time,
|
---|
| 195 | x => x.TotalTimeOffline,
|
---|
| 196 | () => s.Slave.SlaveState == SlaveState.Offline)
|
---|
| 197 | };
|
---|
| 198 |
|
---|
| 199 | db.FactClientInfos.InsertAllOnSubmit(clientFacts);
|
---|
[9533] | 200 | }
|
---|
[9541] | 201 |
|
---|
| 202 | private double CalcNewTotalTime(FactClientInfo lastFact, DateTime newTime, Func<FactClientInfo, double> selector, Func<bool> condition) {
|
---|
| 203 | if (lastFact == null) {
|
---|
| 204 | return 0.0;
|
---|
| 205 | }
|
---|
| 206 | return condition()
|
---|
| 207 | ? selector(lastFact) + (newTime - lastFact.Time).TotalMinutes
|
---|
| 208 | : selector(lastFact);
|
---|
| 209 | }
|
---|
[9526] | 210 | }
|
---|
| 211 | } |
---|