Skip to content

Commit

Permalink
[core] 7 day eviction policy for task class cache
Browse files Browse the repository at this point in the history
  • Loading branch information
teo committed Aug 17, 2023
1 parent 6bec4ef commit 0f979bc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
3 changes: 3 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"os/user"
"path/filepath"
"strings"
"time"

"github.com/AliceO2Group/Control/apricot"
apricotpb "github.com/AliceO2Group/Control/apricot/protos"
Expand Down Expand Up @@ -120,6 +121,7 @@ func setDefaults() error {
viper.SetDefault("concurrentIteratorRoleExpansion", true)
viper.SetDefault("reuseUnlockedTasks", false)
viper.SetDefault("configCache", true)
viper.SetDefault("taskClassCacheTTL", 7*24*time.Hour)
return nil
}

Expand Down Expand Up @@ -183,6 +185,7 @@ func setFlags() error {
pflag.Bool("concurrentIteratorRoleExpansion", viper.GetBool("concurrentIteratorRoleExpansion"), "Expand iterator roles concurrently during workflow template processing")
pflag.Bool("reuseUnlockedTasks", viper.GetBool("reuseUnlockedTasks"), "Reuse unlocked active tasks when satisfying environment deployment requests")
pflag.Bool("configCache", viper.GetBool("configCache"), "Enable cache layer between AliECS core and Apricot")
pflag.Duration("taskClassCacheTTL", viper.GetDuration("taskClassCacheTTL"), "TTL for task class cache entries")

pflag.Parse()
return viper.BindPFlags(pflag.CommandLine)
Expand Down
12 changes: 11 additions & 1 deletion core/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,18 @@ func (m *Manager) removeInactiveClasses() {
_ = m.classes.Do(func(classMap *map[string]*taskclass.Class) error {
keys := make([]string, 0)

taskClassCacheTTL := viper.GetDuration("taskClassCacheTTL")

// push keys of classes that don't appear in roster any more into a slice
for taskClassIdentifier := range *classMap {
for taskClassIdentifier, class := range *classMap {
if class == nil {
// don't really know what to do with a valid TCI but nil class
continue
}
if time.Since(class.UpdatedTimestamp) < taskClassCacheTTL {
// class is still fresh, skip
continue
}
if len(m.roster.filteredForClass(taskClassIdentifier)) == 0 {
keys = append(keys, taskClassIdentifier)
}
Expand Down
39 changes: 21 additions & 18 deletions core/task/taskclass/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package taskclass

import (
"fmt"
"time"

"github.com/AliceO2Group/Control/common"
"github.com/AliceO2Group/Control/common/controlmode"
Expand Down Expand Up @@ -64,13 +65,14 @@ type Class struct {
Control struct {
Mode controlmode.ControlMode `yaml:"mode"`
} `yaml:"control"`
Command *common.CommandInfo `yaml:"command"`
Wants ResourceWants `yaml:"wants"`
Limits *ResourceLimits `yaml:"limits"`
Bind []channel.Inbound `yaml:"bind"`
Properties gera.StringMap `yaml:"properties"`
Constraints []constraint.Constraint `yaml:"constraints"`
Connect []channel.Outbound `yaml:"connect"`
Command *common.CommandInfo `yaml:"command"`
Wants ResourceWants `yaml:"wants"`
Limits *ResourceLimits `yaml:"limits"`
Bind []channel.Inbound `yaml:"bind"`
Properties gera.StringMap `yaml:"properties"`
Constraints []constraint.Constraint `yaml:"constraints"`
Connect []channel.Outbound `yaml:"connect"`
UpdatedTimestamp time.Time `yaml:"-"`
}

func (c *Class) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
Expand Down Expand Up @@ -106,17 +108,18 @@ func (c *Class) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
}
}
*c = Class{
Identifier: aux.Identifier,
Defaults: gera.MakeStringMapWithMap(aux.Defaults),
Vars: gera.MakeStringMapWithMap(aux.Vars),
Control: aux.Control,
Command: aux.Command,
Wants: aux.Wants,
Limits: aux.Limits,
Bind: aux.Bind,
Properties: gera.MakeStringMapWithMap(aux.Properties),
Constraints: aux.Constraints,
Connect: aux.Connect,
Identifier: aux.Identifier,
Defaults: gera.MakeStringMapWithMap(aux.Defaults),
Vars: gera.MakeStringMapWithMap(aux.Vars),
Control: aux.Control,
Command: aux.Command,
Wants: aux.Wants,
Limits: aux.Limits,
Bind: aux.Bind,
Properties: gera.MakeStringMapWithMap(aux.Properties),
Constraints: aux.Constraints,
Connect: aux.Connect,
UpdatedTimestamp: time.Now(),
}
}
return
Expand Down
9 changes: 7 additions & 2 deletions core/task/taskclass/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

package taskclass

import "sync"
import (
"sync"
"time"
)

type Classes struct {
mu sync.RWMutex
Expand Down Expand Up @@ -75,7 +78,9 @@ func (c *Classes) DeleteKeys(keys []string) {
func (c *Classes) UpdateClass(key string, class *Class) {
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.classMap[key]; ok { //contains

class.UpdatedTimestamp = time.Now() // used for invalidating stale classcache entries
if _, ok := c.classMap[key]; ok { //contains
*c.classMap[key] = *class // update
} else {
c.classMap[key] = class // else add class as new entry
Expand Down

0 comments on commit 0f979bc

Please sign in to comment.