cc-backend/internal/taskManager/updateFootprintService.go

147 lines
4.7 KiB
Go
Raw Permalink Normal View History

// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package taskManager
2024-08-29 08:45:04 +02:00
import (
"context"
"math"
2024-08-29 08:45:04 +02:00
"time"
"github.com/ClusterCockpit/cc-backend/internal/config"
"github.com/ClusterCockpit/cc-backend/internal/metricdata"
"github.com/ClusterCockpit/cc-backend/pkg/archive"
2024-08-29 08:45:04 +02:00
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/ClusterCockpit/cc-backend/pkg/schema"
2024-09-03 13:41:00 +02:00
sq "github.com/Masterminds/squirrel"
2024-08-29 08:45:04 +02:00
"github.com/go-co-op/gocron/v2"
)
2024-09-02 12:07:44 +02:00
func RegisterFootprintWorker() {
var frequency string
2024-10-28 11:56:34 +01:00
if config.Keys.CronFrequency != nil && config.Keys.CronFrequency.FootprintWorker != "" {
frequency = config.Keys.CronFrequency.FootprintWorker
} else {
frequency = "10m"
}
d, _ := time.ParseDuration(frequency)
log.Infof("Register Footprint Update service with %s interval", frequency)
2024-08-29 08:45:04 +02:00
s.NewJob(gocron.DurationJob(d),
gocron.NewTask(
func() {
2024-09-03 13:41:00 +02:00
s := time.Now()
c := 0
ce := 0
cl := 0
log.Printf("Update Footprints started at %s", s.Format(time.RFC3339))
for _, cluster := range archive.Clusters {
s_cluster := time.Now()
jobs, err := jobRepo.FindRunningJobs(cluster.Name)
if err != nil {
continue
}
// NOTE: Additional Subcluster Loop Could Allow For Limited List Of Footprint-Metrics Only.
// - Chunk-Size Would Then Be 'SubCluster' (Running Jobs, Transactions) as Lists Can Change Within SCs
// - Would Require Review of 'updateFootprint' Usage (Logic Could Possibly Be Included Here Completely)
allMetrics := make([]string, 0)
metricConfigs := archive.GetCluster(cluster.Name).MetricConfig
for _, mc := range metricConfigs {
allMetrics = append(allMetrics, mc.Name)
}
2024-08-29 08:45:04 +02:00
repo, err := metricdata.GetMetricDataRepo(cluster.Name)
if err != nil {
2024-11-22 15:08:53 +01:00
log.Errorf("no metric data repository configured for '%s'", cluster.Name)
continue
}
2024-11-22 13:39:59 +01:00
pendingStatements := []sq.UpdateBuilder{}
for _, job := range jobs {
2024-11-22 15:08:53 +01:00
log.Debugf("Prepare job %d", job.JobID)
cl++
s_job := time.Now()
jobStats, err := repo.LoadStats(job, allMetrics, context.Background())
if err != nil {
2024-11-22 13:36:26 +01:00
log.Errorf("error wile loading job data stats for footprint update: %v", err)
ce++
continue
}
jobMeta := &schema.JobMeta{
BaseJob: job.BaseJob,
StartTime: job.StartTime.Unix(),
Statistics: make(map[string]schema.JobStatistics),
}
2024-11-22 16:59:18 +01:00
for _, metric := range allMetrics {
2024-11-22 17:56:55 +01:00
avg, min, max := 0.0, 0.0, 0.0
data, ok := jobStats[metric] // JobStats[Metric1:[Hostname1:[Stats], Hostname2:[Stats], ...], Metric2[...] ...]
2024-11-22 16:31:35 +01:00
if ok {
2024-11-22 16:59:18 +01:00
for _, res := range job.Resources {
hostStats, ok := data[res.Hostname]
2024-11-22 16:31:35 +01:00
if ok {
avg += hostStats.Avg
min = math.Min(min, hostStats.Min)
max = math.Max(max, hostStats.Max)
}
2024-11-22 15:57:28 +01:00
}
}
// Add values rounded to 2 digits
jobMeta.Statistics[metric] = schema.JobStatistics{
Unit: schema.Unit{
Prefix: archive.GetMetricConfig(job.Cluster, metric).Unit.Prefix,
Base: archive.GetMetricConfig(job.Cluster, metric).Unit.Base,
},
Avg: (math.Round((avg/float64(job.NumNodes))*100) / 100),
Min: (math.Round(min*100) / 100),
Max: (math.Round(max*100) / 100),
}
}
// Build Statement per Job, Add to Pending Array
stmt := sq.Update("job")
2024-09-05 14:58:08 +02:00
stmt, err = jobRepo.UpdateFootprint(stmt, jobMeta)
if err != nil {
2024-11-22 13:36:26 +01:00
log.Errorf("update job (dbid: %d) statement build failed at footprint step: %s", job.ID, err.Error())
ce++
continue
}
stmt = stmt.Where("job.id = ?", job.ID)
pendingStatements = append(pendingStatements, stmt)
2024-11-22 13:36:26 +01:00
log.Debugf("Job %d took %s", job.JobID, time.Since(s_job))
}
t, err := jobRepo.TransactionInit()
if err != nil {
2024-11-22 13:36:26 +01:00
log.Errorf("failed TransactionInit %v", err)
log.Errorf("skipped %d transactions for cluster %s", len(pendingStatements), cluster.Name)
ce += len(pendingStatements)
} else {
for _, ps := range pendingStatements {
query, args, err := ps.ToSql()
if err != nil {
log.Errorf("failed in ToSQL conversion: %v", err)
ce++
} else {
// args...: Footprint-JSON, Energyfootprint-JSON, TotalEnergy, JobID
jobRepo.TransactionAdd(t, query, args...)
c++
}
}
jobRepo.TransactionEnd(t)
}
log.Debugf("Finish Cluster %s, took %s", cluster.Name, time.Since(s_cluster))
}
log.Printf("Updating %d (of %d; Skipped %d) Footprints is done and took %s", c, cl, ce, time.Since(s))
2024-08-29 08:45:04 +02:00
}))
}