|
| 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 | +} |
0 commit comments