Skip to content

Commit 621c317

Browse files
committed
[core] Apricot host-detector inventory cache
1 parent 4c87d4b commit 621c317

3 files changed

Lines changed: 167 additions & 2 deletions

File tree

apricot/cacheproxy/service.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package cacheproxy
2+
3+
import (
4+
"github.com/AliceO2Group/Control/configuration"
5+
"github.com/AliceO2Group/Control/configuration/componentcfg"
6+
)
7+
8+
// Implements a cache proxy Service for the configuration system.
9+
// Only DetectorForHost/DetectorsForHosts are cached, all other calls are passed through.
10+
type Service struct{
11+
base configuration.Service
12+
cache svcCache
13+
}
14+
15+
type svcCache struct {
16+
detectorsInventory map[string][]string
17+
detectorForHost map[string]string
18+
}
19+
20+
func NewService(base configuration.Service) (*Service, error) {
21+
svc := &Service{
22+
base: base,
23+
cache: svcCache{
24+
detectorsInventory: make(map[string][]string),
25+
detectorForHost: make(map[string]string),
26+
},
27+
}
28+
29+
var err error
30+
svc.cache.detectorsInventory, err = svc.base.GetDetectorsInventory()
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
for det, hosts := range svc.cache.detectorsInventory {
36+
for _, host := range hosts {
37+
svc.cache.detectorForHost[host] = det
38+
}
39+
}
40+
41+
return svc, nil
42+
}
43+
44+
func (s Service) GetRuntimeEntry(component string, key string) (string, error) {
45+
return s.base.GetRuntimeEntry(component, key)
46+
}
47+
48+
func (s Service) SetRuntimeEntry(component string, key string, value string) error {
49+
return s.base.SetRuntimeEntry(component, key, value)
50+
}
51+
52+
func (s Service) ListRuntimeEntries(component string) ([]string, error) {
53+
return s.base.ListRuntimeEntries(component)
54+
}
55+
56+
func (s Service) NewRunNumber() (runNumber uint32, err error) {
57+
return s.base.NewRunNumber()
58+
}
59+
60+
func (s Service) GetDefaults() map[string]string {
61+
return s.base.GetDefaults()
62+
}
63+
64+
func (s Service) GetVars() map[string]string {
65+
return s.base.GetVars()
66+
}
67+
68+
func (s Service) GetComponentConfiguration(query *componentcfg.Query) (payload string, err error) {
69+
return s.base.GetComponentConfiguration(query)
70+
}
71+
72+
func (s Service) GetComponentConfigurationWithLastIndex(query *componentcfg.Query) (payload string, lastIndex uint64, err error) {
73+
return s.base.GetComponentConfigurationWithLastIndex(query)
74+
}
75+
76+
func (s Service) GetAndProcessComponentConfiguration(query *componentcfg.Query, varStack map[string]string) (payload string, err error) {
77+
return s.base.GetAndProcessComponentConfiguration(query, varStack)
78+
}
79+
80+
func (s Service) ListDetectors(getAll bool) (detectors []string, err error) {
81+
return s.base.ListDetectors(getAll)
82+
}
83+
84+
func (s Service) GetHostInventory(detector string) (hosts []string, err error) {
85+
return s.base.GetHostInventory(detector)
86+
}
87+
88+
func (s Service) GetDetectorsInventory() (inventory map[string][]string, err error) {
89+
return s.base.GetDetectorsInventory()
90+
}
91+
92+
func (s Service) ListComponents() (components []string, err error) {
93+
return s.base.ListComponents()
94+
}
95+
96+
func (s Service) ListComponentEntries(query *componentcfg.EntriesQuery, showLatestTimestamp bool) (entries []string, err error) {
97+
return s.base.ListComponentEntries(query, showLatestTimestamp)
98+
}
99+
100+
func (s Service) ListComponentEntryHistory(query *componentcfg.Query) (entries []string, err error) {
101+
return s.base.ListComponentEntryHistory(query)
102+
}
103+
104+
func (s Service) ResolveComponentQuery(query *componentcfg.Query) (resolved *componentcfg.Query, err error) {
105+
return s.base.ResolveComponentQuery(query)
106+
}
107+
108+
func (s Service) ImportComponentConfiguration(query *componentcfg.Query, payload string, newComponent bool, useVersioning bool) (existingComponentUpdated bool, existingEntryUpdated bool, newTimestamp int64, err error) {
109+
return s.base.ImportComponentConfiguration(query, payload, newComponent, useVersioning)
110+
}
111+
112+
func (s Service) GetDetectorForHost(hostname string) (string, error) {
113+
det, ok := s.cache.detectorForHost[hostname]
114+
if !ok {
115+
return s.base.GetDetectorForHost(hostname)
116+
}
117+
return det, nil
118+
}
119+
120+
func (s Service) GetDetectorsForHosts(hosts []string) ([]string, error) {
121+
detectors := make(map[string]struct{}, 0)
122+
for _, host := range hosts {
123+
det, ok := s.cache.detectorForHost[host]
124+
if !ok {
125+
return s.base.GetDetectorsForHosts(hosts)
126+
}
127+
detectors[det] = struct{}{}
128+
}
129+
detList := make([]string, 0, len(detectors))
130+
for det, _ := range detectors {
131+
detList = append(detList, det)
132+
}
133+
return detList, nil
134+
}
135+
136+
func (s Service) GetCRUCardsForHost(hostname string) (string, error) {
137+
return s.base.GetCRUCardsForHost(hostname)
138+
}
139+
140+
func (s Service) GetEndpointsForCRUCard(hostname, cardSerial string) (string, error) {
141+
return s.base.GetEndpointsForCRUCard(hostname, cardSerial)
142+
}
143+
144+
func (s Service) RawGetRecursive(path string) (string, error) {
145+
return s.base.RawGetRecursive(path)
146+
}

apricot/instance.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"net/url"
3030
"sync"
3131

32+
"github.com/AliceO2Group/Control/apricot/cacheproxy"
3233
"github.com/AliceO2Group/Control/apricot/local"
3334
"github.com/AliceO2Group/Control/apricot/remote"
3435
"github.com/AliceO2Group/Control/common/logger/infologger"
@@ -47,6 +48,8 @@ func newService(configUri string) (configuration.Service, error) {
4748
return nil, err
4849
}
4950

51+
var svc configuration.Service
52+
5053
switch parsedUri.Scheme {
5154
case "consul":
5255
fallthrough
@@ -62,7 +65,14 @@ func newService(configUri string) (configuration.Service, error) {
6265
WithField("level", infologger.IL_Support).
6366
Info("new embedded apricot instance")
6467
}
65-
return local.NewService(configUri)
68+
svc, err = local.NewService(configUri)
69+
if err != nil {
70+
return svc, err
71+
}
72+
if viper.GetBool("configCache") {
73+
svc, err = cacheproxy.NewService(svc)
74+
}
75+
return svc, err
6676
case "apricot":
6777
if viper.GetString("component") == "apricot" {
6878
log.WithField("configUri", configUri).
@@ -75,7 +85,14 @@ func newService(configUri string) (configuration.Service, error) {
7585
WithField("level", infologger.IL_Support).
7686
Info("new apricot client")
7787
}
78-
return remote.NewService(configUri)
88+
svc, err = remote.NewService(configUri)
89+
if err != nil {
90+
return svc, err
91+
}
92+
if viper.GetBool("configCache") {
93+
svc, err = cacheproxy.NewService(svc)
94+
}
95+
return svc, err
7996
default:
8097
return nil, fmt.Errorf("invalid configuration URI scheme %s", parsedUri.Scheme)
8198
}

core/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func setDefaults() error {
118118
viper.SetDefault("concurrentWorkflowTemplateProcessing", false)
119119
viper.SetDefault("concurrentIteratorRoleExpansion", false)
120120
viper.SetDefault("reuseUnlockedTasks", false)
121+
viper.SetDefault("configCache", true)
121122
return nil
122123
}
123124

@@ -179,6 +180,7 @@ func setFlags() error {
179180
pflag.Bool("concurrentWorkflowTemplateProcessing", viper.GetBool("concurrentWorkflowTemplateProcessing"), "Process workflow templates concurrently")
180181
pflag.Bool("concurrentIteratorRoleExpansion", viper.GetBool("concurrentIteratorRoleExpansion"), "Expand iterator roles concurrently during workflow template processing")
181182
pflag.Bool("reuseUnlockedTasks", viper.GetBool("reuseUnlockedTasks"), "Reuse unlocked active tasks when satisfying environment deployment requests")
183+
pflag.Bool("configCache", viper.GetBool("configCache"), "Enable cache layer between AliECS core and Apricot")
182184

183185
pflag.Parse()
184186
return viper.BindPFlags(pflag.CommandLine)

0 commit comments

Comments
 (0)