-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache rendered centrals #1433
Cache rendered centrals #1433
Conversation
12cf1ff
to
5001774
Compare
5001774
to
80fa426
Compare
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: SimonBaeumer The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
fix unit test Add unit test
366001a
to
267e957
Compare
centralCRYAMLCache[key] = centralYAML | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example of keyed mutex for locking cache keys
var km = keyedMutex{locks: map[string]*sync.Mutex{}} | |
type keyedMutex struct { | |
locks map[string]*sync.Mutex | |
mLock sync.Mutex | |
} | |
func (k keyedMutex) getLockBy(key string) *sync.Mutex { | |
k.mLock.Lock() | |
defer k.mLock.Unlock() | |
if ret, ok := k.locks[key]; ok { | |
return ret | |
} | |
lock := &sync.Mutex{} | |
k.locks[key] = lock | |
return lock | |
} | |
func (k keyedMutex) Lock(key string) { | |
k.getLockBy(key).Lock() | |
} | |
func (k keyedMutex) Unlock(key string) { | |
k.getLockBy(key).Unlock() | |
} |
result, err := readFromCache(params, cfg) | ||
if err != nil { | ||
return "", err | ||
} | ||
if result != "" { | ||
return result, nil | ||
} | ||
|
||
centralCR, err := renderCentral(params, cfg) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
centralYaml, err := yaml.Marshal(centralCR) | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to marshal Central CR") | ||
} | ||
|
||
centralYAMLString := string(centralYaml) | ||
err = addCache(params, cfg, centralYAMLString) | ||
if err != nil { | ||
return "", err | ||
} | ||
return centralYAMLString, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to lock the cache key
result, err := readFromCache(params, cfg) | |
if err != nil { | |
return "", err | |
} | |
if result != "" { | |
return result, nil | |
} | |
centralCR, err := renderCentral(params, cfg) | |
if err != nil { | |
return "", err | |
} | |
centralYaml, err := yaml.Marshal(centralCR) | |
if err != nil { | |
return "", errors.Wrap(err, "failed to marshal Central CR") | |
} | |
centralYAMLString := string(centralYaml) | |
err = addCache(params, cfg, centralYAMLString) | |
if err != nil { | |
return "", err | |
} | |
return centralYAMLString, nil | |
cacheKey, err := getCacheKey(params, cfg) | |
if err != nil { | |
return "", err | |
} | |
km.Lock(cacheKey) | |
defer km.Unlock(cacheKey) | |
if result := readFromCache(cacheKey); result != "" { | |
return result, nil | |
} | |
centralCR, err := renderCentral(params, cfg) | |
if err != nil { | |
return "", err | |
} | |
centralYaml, err := yaml.Marshal(centralCR) | |
if err != nil { | |
return "", errors.Wrap(err, "failed to marshal Central CR") | |
} | |
centralYAMLString := string(centralYaml) | |
addCache(cacheKey, centralYAMLString) | |
return centralYAMLString, nil |
func readFromCache(params CentralParams, config Config) (string, error) { | ||
cacheKey, err := getCacheKey(params, config) | ||
if err != nil { | ||
return "", fmt.Errorf("Could not get cache key: %w", err) | ||
} | ||
|
||
if val, ok := centralCRYAMLCache[cacheKey]; ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you need to RLock the cache + passing the cache key directly
func readFromCache(params CentralParams, config Config) (string, error) { | |
cacheKey, err := getCacheKey(params, config) | |
if err != nil { | |
return "", fmt.Errorf("Could not get cache key: %w", err) | |
} | |
if val, ok := centralCRYAMLCache[cacheKey]; ok { | |
func readFromCache(cacheKey string) (string) { | |
centralCRYAMLCache.RLock() | |
defer centralCRYamlCache.RUnlock() | |
if val, ok := centralCRYAMLCache[cacheKey]; ok { |
func addCache(params CentralParams, config Config, centralYAML string) error { | ||
centralCacheMutex.Lock() | ||
defer centralCacheMutex.Unlock() | ||
key, err := getCacheKey(params, config) | ||
if err != nil { | ||
return fmt.Errorf("could not add to cache: %w", err) | ||
} | ||
|
||
centralCRYAMLCache[key] = centralYAML | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passing the cache key directly
func addCache(params CentralParams, config Config, centralYAML string) error { | |
centralCacheMutex.Lock() | |
defer centralCacheMutex.Unlock() | |
key, err := getCacheKey(params, config) | |
if err != nil { | |
return fmt.Errorf("could not add to cache: %w", err) | |
} | |
centralCRYAMLCache[key] = centralYAML | |
return nil | |
func addCache(cacheKey string, centralYAML string) { | |
centralCacheMutex.Lock() | |
defer centralCacheMutex.Unlock() | |
centralCRYAMLCache[key] = centralYAML |
Description
This PR adds a cache for the rendered Centrals in the GitOps service.
Checklist (Definition of Done)
Test manual
ROX-12345: ...
Test manual
for i in {1..1500}; do ./scripts/create-central.sh "test-central-${i}"; done
export OCM_TOKEN=$(ocm token)
./scripts/fmcurl rhacs/v1/agent-clusters/1234567890abcdef1234567890abcdef/centrals
./scripts/fmcurl rhacs/v1/agent-clusters/1234567890abcdef1234567890abcdef/centrals