Enable caching for job metric data

This commit is contained in:
Jan Eitzinger 2022-09-13 15:21:50 +02:00
parent 520c814e3b
commit 732801548f

View File

@ -77,16 +77,18 @@ func LoadData(job *schema.Job,
metrics []string, metrics []string,
scopes []schema.MetricScope, scopes []schema.MetricScope,
ctx context.Context) (schema.JobData, error) { ctx context.Context) (schema.JobData, error) {
data := cache.Get(cacheKey(job, metrics, scopes), func() (_ interface{}, ttl time.Duration, size int) {
var jd schema.JobData var jd schema.JobData
var err error var err error
if job.State == schema.JobStateRunning || if job.State == schema.JobStateRunning ||
job.MonitoringStatus == schema.MonitoringStatusRunningOrArchiving || job.MonitoringStatus == schema.MonitoringStatusRunningOrArchiving ||
!useArchive { !useArchive {
repo, ok := metricDataRepos[job.Cluster] repo, ok := metricDataRepos[job.Cluster]
if !ok { if !ok {
return nil, fmt.Errorf("no metric data repository configured for '%s'", job.Cluster) return fmt.Errorf("no metric data repository configured for '%s'", job.Cluster), 0, 0
} }
if scopes == nil { if scopes == nil {
@ -105,13 +107,14 @@ func LoadData(job *schema.Job,
if len(jd) != 0 { if len(jd) != 0 {
log.Errorf("partial error: %s", err.Error()) log.Errorf("partial error: %s", err.Error())
} else { } else {
return nil, err return err, 0, 0
} }
} }
size = jd.Size()
} else { } else {
jd, err = archive.GetHandle().LoadJobData(job) jd, err = archive.GetHandle().LoadJobData(job)
if err != nil { if err != nil {
return nil, err return err, 0, 0
} }
// Avoid sending unrequested data to the client: // Avoid sending unrequested data to the client:
@ -144,11 +147,23 @@ func LoadData(job *schema.Job,
} }
jd = res jd = res
} }
size = jd.Size()
}
ttl = 5 * time.Hour
if job.State == schema.JobStateRunning {
ttl = 2 * time.Minute
} }
prepareJobData(job, jd, scopes) prepareJobData(job, jd, scopes)
return jd, ttl, size
})
return jd, nil if err, ok := data.(error); ok {
return nil, err
}
return data.(schema.JobData), nil
} }
// Used for the jobsFootprint GraphQL-Query. TODO: Rename/Generalize. // Used for the jobsFootprint GraphQL-Query. TODO: Rename/Generalize.
@ -323,5 +338,5 @@ func ArchiveJob(job *schema.Job, ctx context.Context) (*schema.JobMeta, error) {
return jobMeta, nil return jobMeta, nil
} }
return jobMeta, archive.Import(jobMeta, &jobData) return jobMeta, archive.GetHandle().ImportJob(jobMeta, &jobData)
} }