- Timestamp:
- 06/19/15 18:06:19 (9 years ago)
- Location:
- branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveJanitor.cs
r12477 r12484 49 49 public HiveJanitor() { 50 50 stop = false; 51 cleanupWaitHandle = new AutoResetEvent( true);52 generateStatisticsWaitHandle = new AutoResetEvent( true);51 cleanupWaitHandle = new AutoResetEvent(false); 52 generateStatisticsWaitHandle = new AutoResetEvent(false); 53 53 } 54 54 -
branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs
r12477 r12484 38 38 UpdateDimUserTable(pm); 39 39 UpdateDimJobTable(pm); 40 UpdateDimClients (pm);40 UpdateDimClientsTable(pm); 41 41 pm.SubmitChanges(); 42 42 }); … … 47 47 pm.SubmitChanges(); 48 48 UpdateFactClientInfoTable(newTime, pm); 49 UpdateTaskFacts (newTime, pm);49 UpdateTaskFactsTable(newTime, pm); 50 50 try { 51 51 pm.SubmitChanges(); … … 67 67 var now = DateTime.Now; 68 68 var timeEntry = new DimTime { 69 Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0), 69 Time = now, 70 Minute = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0), 70 71 Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0), 71 72 Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0), … … 73 74 Year = new DateTime(now.Year, 1, 1, 0, 0, 0) 74 75 }; 75 // using SaveOrAttach instead of save because the table can only be updated once every minute 76 // (pk restriction) save would cause a duplicate key exception if the method is accidentally 77 // called more than once every minute (e.g. restarting the service within a minute) 78 return dimTimeDao.SaveOrAttach(timeEntry); 76 return dimTimeDao.Save(timeEntry); 79 77 } 80 78 … … 118 116 } 119 117 120 private void UpdateDimClients (PersistenceManager pm) {118 private void UpdateDimClientsTable(PersistenceManager pm) { 121 119 var dimClientDao = pm.DimClientDao; 122 120 var slaveDao = pm.SlaveDao; … … 208 206 } 209 207 210 private void UpdateTaskFacts (DimTime newTime, PersistenceManager pm) {208 private void UpdateTaskFactsTable(DimTime newTime, PersistenceManager pm) { 211 209 var factTaskDao = pm.FactTaskDao; 212 210 var taskDao = pm.TaskDao; … … 217 215 var notFinishedFactTasks = factTaskDao.GetNotFinishedTasks().Select(x => new { 218 216 x.TaskId, 219 x.DimTimeStart,220 217 x.LastClientId 221 218 }); … … 238 235 MemoryRequired = task.MemoryNeeded, 239 236 State = task.State, 240 StateLogs = stateLogs, 241 DimTimeStart = lastFact != null ? lastFact.DimTimeStart : newTime, 237 StateLogs = stateLogs.OrderBy(x => x.DateTime), 242 238 LastClientId = client != null 243 239 ? client.Id : lastFact != null 244 240 ? lastFact.LastClientId : (Guid?)null 245 241 }; 246 247 var endStates = new[] { TaskState.Finished, TaskState.Failed, TaskState.Aborted };248 242 factTaskDao.Save( 249 243 from x in newTasks.ToList() 250 let stateLogsLinkedList = new LinkedList<StateLog>(x.StateLogs.OrderBy(y => y.DateTime)) 251 let lastStateLog = stateLogsLinkedList.OrderByDescending(y => y.DateTime).FirstOrDefault(sl => sl.Exception != null) 244 let taskData = CalculateFactTaskData(x.StateLogs) 252 245 select new FactTask { 253 246 TaskId = x.TaskId, 254 247 JobId = x.JobId, 255 DimTimeStart = x.DimTimeStart,256 DimTimeEnd = endStates.Contains(x.State) ? newTime : null,248 StartTime = taskData.StartTime, 249 EndTime = taskData.EndTime, 257 250 LastClientId = x.LastClientId, 258 251 Priority = x.Priority, 259 252 CoresRequired = x.CoresRequired, 260 253 MemoryRequired = x.MemoryRequired, 261 NumCalculationRuns = stateLogsLinkedList.CountCalculationRuns(),262 NumRetries = stateLogsLinkedList.CountRetries(),263 TotalWaitingTime = stateLogsLinkedList.SumTotalTimeWhere(stateLog => stateLog.Value.State == TaskState.Waiting),264 TotalRuntime = stateLogsLinkedList.SumTotalTimeWhere(stateLog => stateLog.Value.State == TaskState.Calculating && stateLog.NextIs(y => y.State == TaskState.Transferring)),265 T otalTransferTime = stateLogsLinkedList.SumTotalTimeWhere(stateLog => stateLog.Value.State == TaskState.Transferring),254 NumCalculationRuns = taskData.CalculationRuns, 255 NumRetries = taskData.Retries, 256 WaitingTime = taskData.WaitingTime, 257 CalculatingTime = taskData.CalculatingTime, 258 TransferTime = taskData.TransferTime, 266 259 TaskState = x.State, 267 Exception = lastStateLog == null ? string.Empty : lastStateLog.Exception 260 Exception = taskData.Exception, 261 InitialWaitingTime = taskData.InitialWaitingTime 268 262 }); 269 263 factTaskDao.Delete(notFinishedFactTasks.Select(x => x.TaskId)); … … 280 274 } 281 275 } 282 } 283 284 public static class StateLogLinkedListExtensions { 285 public static int CountCalculationRuns(this LinkedList<StateLog> stateLogs) { 286 return stateLogs.EnumerateNodes() 287 .Count(sl => sl.Value.State == TaskState.Calculating && sl.NextIs(nsl => nsl.State == TaskState.Transferring)); 288 } 289 290 public static int CountRetries(this LinkedList<StateLog> stateLogs) { 291 return stateLogs.EnumerateNodes() 292 .Count(sl => sl.Value.State == TaskState.Calculating && sl.Next != null && sl.NextIs(nsl => nsl.State != TaskState.Transferring)); 293 } 294 295 public static double SumTotalTimeWhere(this LinkedList<StateLog> stateLogs, Predicate<LinkedListNode<StateLog>> predicate) { 296 return stateLogs.EnumerateNodes() 297 .Where(stateLog => predicate(stateLog)) 298 .Sum(stateLog => stateLog.Next != null ? (stateLog.Next.Value.DateTime - stateLog.Value.DateTime).TotalMinutes : 0.0); 299 } 300 } 301 302 public static class LinkedListExtensions { 303 public static IEnumerable<LinkedListNode<T>> EnumerateNodes<T>(this LinkedList<T> list) { 304 var node = list.First; 305 while (node != null) { 306 yield return node; 307 node = node.Next; 276 277 private class FactTaskData { 278 public int CalculationRuns { get; set; } 279 public int Retries { get; set; } 280 public double CalculatingTime { get; set; } 281 public double WaitingTime { get; set; } 282 public double TransferTime { get; set; } 283 public double InitialWaitingTime { get; set; } 284 public string Exception { get; set; } 285 public DateTime? StartTime { get; set; } 286 public DateTime? EndTime { get; set; } 287 } 288 289 private FactTaskData CalculateFactTaskData(IEnumerable<StateLog> stateLogs) { 290 var factTaskData = new FactTaskData(); 291 var enumerator = stateLogs.GetEnumerator(); 292 if (enumerator.MoveNext()) { 293 StateLog current = enumerator.Current, first = current, prev = null; 294 while (current != null) { 295 var next = enumerator.MoveNext() ? enumerator.Current : null; 296 int timeSpanInSeconds; 297 if (next != null) { 298 timeSpanInSeconds = (int)(next.DateTime - current.DateTime).TotalSeconds; 299 } else { 300 timeSpanInSeconds = (int)(DateTime.Now - current.DateTime).TotalSeconds; 301 factTaskData.Exception = current.Exception; 302 } 303 switch (current.State) { 304 case TaskState.Calculating: 305 factTaskData.CalculatingTime += timeSpanInSeconds; 306 factTaskData.CalculationRuns++; 307 if (factTaskData.CalculationRuns == 1) { 308 factTaskData.StartTime = current.DateTime; 309 factTaskData.InitialWaitingTime = (int)(current.DateTime - first.DateTime).TotalSeconds; 310 } 311 if (prev != null && prev.State != TaskState.Transferring) { 312 factTaskData.Retries++; 313 } 314 break; 315 316 case TaskState.Waiting: 317 factTaskData.WaitingTime += timeSpanInSeconds; 318 break; 319 320 case TaskState.Transferring: 321 factTaskData.TransferTime += timeSpanInSeconds; 322 break; 323 324 case TaskState.Finished: 325 case TaskState.Failed: 326 case TaskState.Aborted: 327 factTaskData.EndTime = current.DateTime; 328 break; 329 } 330 prev = current; 331 current = next; 332 } 308 333 } 309 } 310 311 public static bool NextIs<T>(this LinkedListNode<T> node, Predicate<T> predicate) { 312 return node.Next != null && predicate(node.Next.Value); 334 return factTaskData; 313 335 } 314 336 }
Note: See TracChangeset
for help on using the changeset viewer.