Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/18 12:07:16 (6 years ago)
Author:
gkronber
Message:

#2925 merged changes r15972:16382 from trunk to branch

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Services.Hive

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs

    r15583 r16386  
    3737    public void GenerateStatistics() {
    3838      using (var pm = new PersistenceManager()) {
     39
     40        pm.UseTransaction(() => {
     41          UpdateDimProjectTable(pm);
     42          pm.SubmitChanges();
     43        });
     44
    3945        pm.UseTransaction(() => {
    4046          UpdateDimUserTable(pm);
     47         
    4148          UpdateDimJobTable(pm);
    4249          UpdateDimClientsTable(pm);
     
    5461            UpdateFactClientInfoTable(time, pm);
    5562            pm.SubmitChanges();
     63            UpdateFactProjectInfoTable(time, pm);
     64            pm.SubmitChanges();
    5665          });
    5766
    5867          pm.UseTransaction(() => {
    59             UpdateTaskFactsTable(pm);
     68            UpdateFactTaskTable(pm);
    6069            try {
    6170              pm.SubmitChanges();
     
    7281          });
    7382        }
     83
     84        pm.UseTransaction(() => {
     85          FlagJobsForDeletion(pm);
     86          pm.SubmitChanges();
     87        });
    7488      }
    7589    }
     
    106120    }
    107121
     122    // add new projects
     123    // delete expired projects
     124    // update information of existing projects
     125    private void UpdateDimProjectTable(PersistenceManager pm) {
     126      var projectDao = pm.ProjectDao;
     127      var dimProjectDao = pm.DimProjectDao;
     128
     129      var projects = projectDao.GetAll().ToList();
     130      var dimProjects = dimProjectDao.GetAllOnlineProjects().ToList();
     131
     132      var onlineProjects = dimProjects.Where(x => projects.Select(y => y.ProjectId).Contains(x.ProjectId));
     133      var addedProjects = projects.Where(x => !dimProjects.Select(y => y.ProjectId).Contains(x.ProjectId));
     134      var removedProjects = dimProjects.Where(x => !projects.Select(y => y.ProjectId).Contains(x.ProjectId));
     135
     136      // set expiration time of removed projects
     137      foreach (var p in removedProjects) {
     138        p.DateExpired = DateTime.Now;
     139      }
     140
     141      // add new projects
     142      dimProjectDao.Save(addedProjects.Select(x => new DimProject {
     143        ProjectId = x.ProjectId,
     144        ParentProjectId = x.ParentProjectId,
     145        Name = x.Name,
     146        Description = x.Description,
     147        OwnerUserId = x.OwnerUserId,
     148        StartDate = x.StartDate,
     149        EndDate = x.EndDate,
     150        DateCreated = x.DateCreated,
     151        DateExpired = null
     152      }));
     153
     154      // expire project if its parent has changed and create a new entry
     155      // otherwise perform "normal" update
     156      foreach (var dimP in onlineProjects) {
     157        var p = projects.Where(x => x.ProjectId == dimP.ProjectId).SingleOrDefault();
     158        if (p != null) {
     159          if (dimP.ParentProjectId == null ? p.ParentProjectId != null : dimP.ParentProjectId != p.ParentProjectId) { // or: (!object.Equals(dimP.ParentProjectId, p.ParentProjectId))
     160            dimP.DateExpired = DateTime.Now;
     161            dimProjectDao.Save(new DimProject {
     162              ProjectId = p.ProjectId,
     163              ParentProjectId = p.ParentProjectId,
     164              Name = p.Name,
     165              Description = p.Description,
     166              OwnerUserId = p.OwnerUserId,
     167              StartDate = p.StartDate,
     168              EndDate = p.EndDate,
     169              DateCreated = p.DateCreated,
     170              DateExpired = null
     171            });
     172          } else {
     173            dimP.Name = p.Name;
     174            dimP.Description = p.Description;
     175            dimP.OwnerUserId = p.OwnerUserId;
     176            dimP.StartDate = p.StartDate;
     177            dimP.EndDate = p.EndDate;
     178          }
     179        }
     180      }
     181    }
     182
    108183    private void UpdateDimJobTable(PersistenceManager pm) {
     184      var dimProjectDao = pm.DimProjectDao;
    109185      var dimJobDao = pm.DimJobDao;
    110186      var jobDao = pm.JobDao;
     
    118194          JobName = x.Name ?? string.Empty,
    119195          DateCreated = x.DateCreated,
     196          ProjectId = dimProjectDao.GetLastValidIdByProjectId(x.ProjectId),
    120197          TotalTasks = taskDao.GetAll().Count(y => y.JobId == x.JobId)
    121198        })
     
    127204        UserName = GetUserName(x.UserId),
    128205        DateCreated = x.DateCreated,
     206        ProjectId = x.ProjectId,
    129207        TotalTasks = x.TotalTasks,
    130208        CompletedTasks = 0,
     
    134212
    135213    private void UpdateExistingDimJobs(PersistenceManager pm) {
     214      var dimProjectDao = pm.DimProjectDao;
    136215      var jobDao = pm.JobDao;
    137216      var dimJobDao = pm.DimJobDao;
     
    151230          }
    152231        }
     232        var job = jobDao.GetById(dimJob.JobId);
    153233        if (totalTasks == completedTasks) {
    154234          var completeDate = factTaskDao.GetLastCompletedTaskFromJob(dimJob.JobId);
    155235          if (completeDate == null) {
    156             if (jobDao.GetById(dimJob.JobId) == null) {
     236            if (job == null) {
    157237              completeDate = DateTime.Now;
    158238            }
     
    160240          dimJob.DateCompleted = completeDate;
    161241        }
     242        if(job != null) {
     243          dimJob.JobName = job.Name;
     244          dimJob.ProjectId = dimProjectDao.GetLastValidIdByProjectId(job.ProjectId);
     245        }
     246
    162247        dimJob.TotalTasks = totalTasks;
    163248        dimJob.CompletedTasks = completedTasks;
     
    165250    }
    166251
     252    private void FlagJobsForDeletion(PersistenceManager pm) {
     253      var jobDao = pm.JobDao;
     254      var jobs = jobDao.GetJobsReadyForDeletion();
     255      foreach(var job in jobs) {
     256        job.State = JobState.DeletionPending;
     257      }
     258    }
     259
    167260    private void UpdateDimClientsTable(PersistenceManager pm) {
    168261      var dimClientDao = pm.DimClientDao;
    169       var slaveDao = pm.SlaveDao;
    170       var slaves = slaveDao.GetAll();
    171       var recentlyAddedClients = dimClientDao.GetActiveClients();
    172       var slaveIds = slaves.Select(x => x.ResourceId);
    173 
    174       var removedClientIds = recentlyAddedClients
    175         .Where(x => !slaveIds.Contains(x.ResourceId))
    176         .Select(x => x.Id);
    177       var modifiedClients =
    178         from slave in slaves
    179         join client in recentlyAddedClients on slave.ResourceId equals client.ResourceId
    180         where (slave.Name != client.Name
    181                || slave.ParentResourceId == null && client.ResourceGroupId != null // because both can be null and null comparison
    182                || slave.ParentResourceId != null && client.ResourceGroupId == null // does return no entry on the sql server
    183                || slave.ParentResourceId != client.ResourceGroupId
    184                || ((slave.ParentResource != null) && slave.ParentResource.ParentResourceId != client.ResourceGroup2Id))
    185         select new {
    186           SlaveId = slave.ResourceId,
    187           ClientId = client.Id
    188         };
    189       var clientIds = dimClientDao.GetActiveClients().Select(x => x.ResourceId);
    190       var modifiedClientIds = modifiedClients.Select(x => x.SlaveId);
    191       var newClients = slaves
    192         .Where(x => !clientIds.Contains(x.ResourceId)
    193                     || modifiedClientIds.Contains(x.ResourceId))
    194         .Select(x => new {
    195           x.ResourceId,
    196           x.Name,
    197           ResourceGroupId = x.ParentResourceId,
    198           GroupName = x.ParentResource != null ? x.ParentResource.Name : null,
    199           ResourceGroup2Id = x.ParentResource != null ? x.ParentResource.ParentResourceId : null,
    200           GroupName2 = x.ParentResource != null ? x.ParentResource.ParentResource != null ? x.ParentResource.ParentResource.Name : null : null
    201         })
    202         .ToList();
    203 
    204       var clientsToUpdate = removedClientIds.Union(modifiedClients.Select(x => x.ClientId));
    205       dimClientDao.UpdateExpirationTime(clientsToUpdate, DateTime.Now);
    206       dimClientDao.Save(newClients.Select(x => new DimClient {
     262      var resourceDao = pm.ResourceDao;
     263
     264      var resources = resourceDao.GetAll().ToList();
     265      var dimClients = dimClientDao.GetAllOnlineClients().ToList();
     266
     267      var onlineClients = dimClients.Where(x => resources.Select(y => y.ResourceId).Contains(x.ResourceId));
     268      var addedResources = resources.Where(x => !dimClients.Select(y => y.ResourceId).Contains(x.ResourceId));
     269      var removedResources = dimClients.Where(x => !resources.Select(y => y.ResourceId).Contains(x.ResourceId));
     270
     271      // set expiration time of removed resources
     272      foreach(var r in removedResources) {
     273        r.DateExpired = DateTime.Now;
     274      }
     275
     276      // add new resources
     277      dimClientDao.Save(addedResources.Select(x => new DimClient {
    207278        ResourceId = x.ResourceId,
     279        ParentResourceId = x.ParentResourceId,
    208280        Name = x.Name,
    209         ExpirationTime = null,
    210         ResourceGroupId = x.ResourceGroupId,
    211         GroupName = x.GroupName,
    212         ResourceGroup2Id = x.ResourceGroup2Id,
    213         GroupName2 = x.GroupName2
     281        ResourceType = x.ResourceType,
     282        DateCreated = DateTime.Now,
     283        DateExpired = null
    214284      }));
    215     }
     285
     286      // expire client if its parent has changed and create a new entry
     287      // otherwise perform "normal" update
     288      foreach(var dimc in onlineClients) {
     289        var r = resources.Where(x => x.ResourceId == dimc.ResourceId).SingleOrDefault();
     290        if(r != null) {
     291          if(dimc.ParentResourceId == null ? r.ParentResourceId != null : dimc.ParentResourceId != r.ParentResourceId) {
     292            var now = DateTime.Now;
     293            dimc.DateExpired = now;
     294            dimClientDao.Save(new DimClient {
     295              ResourceId = r.ResourceId,
     296              ParentResourceId = r.ParentResourceId,
     297              Name = r.Name,
     298              ResourceType = r.ResourceType,
     299              DateCreated = now,
     300              DateExpired = null
     301            });
     302          } else {
     303            dimc.Name = r.Name;
     304          }
     305        }
     306      }
     307    }
     308
     309    //// (1) for new slaves (not yet reported in Table DimClients) ...
     310    //// and modified slaves (name or parent resource changed) a new DimClient-entry is created
     311    //// (2) for already reported removed and modifid clients the expiration date is set
     312    //private void UpdateDimClientsTableOld(PersistenceManager pm) {
     313    //  var dimClientDao = pm.DimClientDao;
     314    //  var slaveDao = pm.SlaveDao;
     315    //  var slaves = slaveDao.GetAll();
     316    //  var recentlyAddedClients = dimClientDao.GetAllOnlineClients();
     317    //  var slaveIds = slaves.Select(x => x.ResourceId);
     318
     319    //  var removedClientIds = recentlyAddedClients
     320    //    .Where(x => !slaveIds.Contains(x.ResourceId))
     321    //    .Select(x => x.Id);
     322    //  var modifiedClients =
     323    //    from slave in slaves
     324    //    join client in recentlyAddedClients on slave.ResourceId equals client.ResourceId
     325    //    where (slave.Name != client.Name
     326    //           || slave.ParentResourceId == null && client.ResourceGroupId != null // because both can be null and null comparison
     327    //           || slave.ParentResourceId != null && client.ResourceGroupId == null // does return no entry on the sql server
     328    //           || slave.ParentResourceId != client.ResourceGroupId
     329    //           || ((slave.ParentResource != null) && slave.ParentResource.ParentResourceId != client.ResourceGroup2Id))
     330    //    select new {
     331    //      SlaveId = slave.ResourceId,
     332    //      ClientId = client.Id
     333    //    };
     334    //  var clientIds = dimClientDao.GetAllOnlineClients().Select(x => x.ResourceId);
     335    //  var modifiedClientIds = modifiedClients.Select(x => x.SlaveId);
     336    //  var newClients = slaves
     337    //    .Where(x => !clientIds.Contains(x.ResourceId)
     338    //                || modifiedClientIds.Contains(x.ResourceId))
     339    //    .Select(x => new {
     340    //      x.ResourceId,
     341    //      x.Name,
     342    //      ResourceGroupId = x.ParentResourceId,
     343    //      GroupName = x.ParentResource != null ? x.ParentResource.Name : null,
     344    //      ResourceGroup2Id = x.ParentResource != null ? x.ParentResource.ParentResourceId : null,
     345    //      GroupName2 = x.ParentResource != null ? x.ParentResource.ParentResource != null ? x.ParentResource.ParentResource.Name : null : null
     346    //    })
     347    //    .ToList();
     348
     349    //  var clientsToUpdate = removedClientIds.Union(modifiedClients.Select(x => x.ClientId));
     350    //  dimClientDao.UpdateExpirationTime(clientsToUpdate, DateTime.Now);
     351    //  dimClientDao.Save(newClients.Select(x => new DimClient {
     352    //    ResourceId = x.ResourceId,
     353    //    Name = x.Name,
     354    //    ExpirationTime = null,
     355    //    ResourceGroupId = x.ResourceGroupId,
     356    //    GroupName = x.GroupName,
     357    //    ResourceGroup2Id = x.ResourceGroup2Id,
     358    //    GroupName2 = x.GroupName2
     359    //  }));
     360    //}
     361
    216362
    217363    private void UpdateFactClientInfoTable(DimTime newTime, PersistenceManager pm) {
     
    222368      var newRawFactInfos =
    223369        from s in slaveDao.GetAll()
    224         join c in dimClientDao.GetActiveClients() on s.ResourceId equals c.ResourceId
     370        join c in dimClientDao.GetAllOnlineSlaves() on s.ResourceId equals c.ResourceId
    225371        join lcf in factClientInfoDao.GetLastUpdateTimestamps() on c.ResourceId equals lcf.ResourceId into joinCf
    226372        from cf in joinCf.DefaultIfEmpty()
     
    261407    }
    262408
    263     private void UpdateTaskFactsTable(PersistenceManager pm) {
     409    private void UpdateFactProjectInfoTable(DimTime newTime, PersistenceManager pm) {
     410      var factProjectInfoDao = pm.FactProjectInfoDao;
     411      var dimProjectDao = pm.DimProjectDao;
     412      var projectDao = pm.ProjectDao;
     413
     414      var projectAvailabilityStats = projectDao.GetAvailabilityStatsPerProject();
     415      var projectUsageStats = projectDao.GetUsageStatsPerProject();
     416      var dimProjects = dimProjectDao.GetAllOnlineProjects().ToList();
     417
     418      factProjectInfoDao.Save(
     419        from dimp in dimProjects
     420        let aStats = projectAvailabilityStats.Where(x => x.ProjectId == dimp.ProjectId).SingleOrDefault()
     421        let uStats = projectUsageStats.Where(x => x.ProjectId == dimp.ProjectId).SingleOrDefault()
     422        select new FactProjectInfo {
     423            ProjectId = dimp.Id,
     424            DimTime = newTime,
     425            NumTotalCores = aStats != null ? aStats.Cores : 0,
     426            TotalMemory = aStats != null ? aStats.Memory : 0,
     427            NumUsedCores = uStats != null ? uStats.Cores : 0,
     428            UsedMemory = uStats != null ? uStats.Memory : 0
     429          }
     430        );
     431    }
     432
     433    private void UpdateFactTaskTable(PersistenceManager pm) {
    264434      var factTaskDao = pm.FactTaskDao;
    265435      var taskDao = pm.TaskDao;
     
    267437
    268438      var factTaskIds = factTaskDao.GetAll().Select(x => x.TaskId);
    269       var notFinishedFactTasks = factTaskDao.GetNotFinishedTasks().Select(x => new {
    270         x.TaskId,
    271         x.LastClientId
    272       });
    273 
    274       var newTasks =
     439      var notFinishedFactTasks = factTaskDao.GetNotFinishedTasks();
     440      //var notFinishedFactTasks = factTaskDao.GetNotFinishedTasks().Select(x => new {
     441      //  x.TaskId,
     442      //  x.LastClientId
     443      //});
     444
     445      // query several properties for all new and not finished tasks
     446      // in order to use them later either...
     447      // (1) to update the fact task entry of not finished tasks
     448      // (2) to insert a new fact task entry for new tasks
     449      var newAndNotFinishedTasks =
    275450        (from task in taskDao.GetAllChildTasks()
    276451         let stateLogs = task.StateLogs.OrderByDescending(x => x.DateTime)
     
    280455         join lastFactTask in notFinishedFactTasks on task.TaskId equals lastFactTask.TaskId into lastFactPerTask
    281456         from lastFact in lastFactPerTask.DefaultIfEmpty()
    282          join client in dimClientDao.GetActiveClients() on lastSlaveId equals client.ResourceId into clientsPerSlaveId
     457         join client in dimClientDao.GetAllOnlineClients() on lastSlaveId equals client.ResourceId into clientsPerSlaveId
    283458         from client in clientsPerSlaveId.DefaultIfEmpty()
    284459         select new {
     
    296471         }).ToList();
    297472
    298       //insert facts for new tasks
     473      // (1) update data of already existing facts
     474      // i.e. for all in newAndNotFinishedTasks where NotFinishedTask = true
     475      foreach (var notFinishedFactTask in notFinishedFactTasks) {
     476        var nfftUpdate = newAndNotFinishedTasks.Where(x => x.TaskId == notFinishedFactTask.TaskId).SingleOrDefault();
     477        if(nfftUpdate != null) {
     478          var taskData = CalculateFactTaskData(nfftUpdate.StateLogs);
     479
     480          notFinishedFactTask.StartTime = taskData.StartTime;
     481          notFinishedFactTask.EndTime = taskData.EndTime;
     482          notFinishedFactTask.LastClientId = nfftUpdate.LastClientId;
     483          notFinishedFactTask.Priority = nfftUpdate.Priority;
     484          notFinishedFactTask.CoresRequired = nfftUpdate.CoresRequired;
     485          notFinishedFactTask.MemoryRequired = nfftUpdate.MemoryRequired;
     486          notFinishedFactTask.NumCalculationRuns = taskData.CalculationRuns;
     487          notFinishedFactTask.NumRetries = taskData.Retries;
     488          notFinishedFactTask.WaitingTime = taskData.WaitingTime;
     489          notFinishedFactTask.CalculatingTime = taskData.CalculatingTime;
     490          notFinishedFactTask.TransferTime = taskData.TransferTime;
     491          notFinishedFactTask.TaskState = nfftUpdate.State;
     492          notFinishedFactTask.Exception = taskData.Exception;
     493          notFinishedFactTask.InitialWaitingTime = taskData.InitialWaitingTime;
     494        }
     495      }
     496
     497      // (2) insert facts for new tasks
     498      // i.e. for all in newAndNotFinishedTasks where NotFinishedTask = false
    299499      factTaskDao.Save(
    300         from x in newTasks
     500        from x in newAndNotFinishedTasks
    301501        where !x.NotFinishedTask
    302502        let taskData = CalculateFactTaskData(x.StateLogs)
     
    320520        });
    321521
    322       //update data of already existing facts
    323       foreach (var notFinishedTask in factTaskDao.GetNotFinishedTasks()) {
    324         var ntc = newTasks.Where(x => x.TaskId == notFinishedTask.TaskId);
    325         if (ntc.Any()) {
    326           var x = ntc.Single();
    327           var taskData = CalculateFactTaskData(x.StateLogs);
    328 
    329           notFinishedTask.StartTime = taskData.StartTime;
    330           notFinishedTask.EndTime = taskData.EndTime;
    331           notFinishedTask.LastClientId = x.LastClientId;
    332           notFinishedTask.Priority = x.Priority;
    333           notFinishedTask.CoresRequired = x.CoresRequired;
    334           notFinishedTask.MemoryRequired = x.MemoryRequired;
    335           notFinishedTask.NumCalculationRuns = taskData.CalculationRuns;
    336           notFinishedTask.NumRetries = taskData.Retries;
    337           notFinishedTask.WaitingTime = taskData.WaitingTime;
    338           notFinishedTask.CalculatingTime = taskData.CalculatingTime;
    339           notFinishedTask.TransferTime = taskData.TransferTime;
    340           notFinishedTask.TaskState = x.State;
    341           notFinishedTask.Exception = taskData.Exception;
    342           notFinishedTask.InitialWaitingTime = taskData.InitialWaitingTime;
    343         }
    344       }
     522
     523      ////update data of already existing facts
     524      //foreach (var notFinishedTask in factTaskDao.GetNotFinishedTasks()) {
     525      //  var ntc = newTasks.Where(x => x.TaskId == notFinishedTask.TaskId);
     526      //  if (ntc.Any()) {
     527      //    var x = ntc.Single();
     528      //    var taskData = CalculateFactTaskData(x.StateLogs);
     529
     530      //    notFinishedTask.StartTime = taskData.StartTime;
     531      //    notFinishedTask.EndTime = taskData.EndTime;
     532      //    notFinishedTask.LastClientId = x.LastClientId;
     533      //    notFinishedTask.Priority = x.Priority;
     534      //    notFinishedTask.CoresRequired = x.CoresRequired;
     535      //    notFinishedTask.MemoryRequired = x.MemoryRequired;
     536      //    notFinishedTask.NumCalculationRuns = taskData.CalculationRuns;
     537      //    notFinishedTask.NumRetries = taskData.Retries;
     538      //    notFinishedTask.WaitingTime = taskData.WaitingTime;
     539      //    notFinishedTask.CalculatingTime = taskData.CalculatingTime;
     540      //    notFinishedTask.TransferTime = taskData.TransferTime;
     541      //    notFinishedTask.TaskState = x.State;
     542      //    notFinishedTask.Exception = taskData.Exception;
     543      //    notFinishedTask.InitialWaitingTime = taskData.InitialWaitingTime;
     544      //  }
     545      //}
    345546    }
    346547
Note: See TracChangeset for help on using the changeset viewer.