Skip to content

Commit

Permalink
fix: properly retrieve and close scalers cache (#4012)
Browse files Browse the repository at this point in the history
Signed-off-by: Zbynek Roubalik <[email protected]>

Signed-off-by: Zbynek Roubalik <[email protected]>
  • Loading branch information
zroubalik authored Dec 15, 2022
1 parent f4bee67 commit 06f6f7d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Here is an overview of all new **experimental** features:

### Fixes

- **General**: TODO ([#TODO](https://github.com/kedacore/keda/issues/TODO))
- **General**: Properly retrieve and close scalers cache ([#4011](https://github.com/kedacore/keda/issues/4011))

### Deprecations

Expand Down
7 changes: 4 additions & 3 deletions pkg/scaling/cache/scalers_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ import (
var log = logf.Log.WithName("scalers_cache")

type ScalersCache struct {
ScaledObject *kedav1alpha1.ScaledObject
Scalers []ScalerBuilder
Recorder record.EventRecorder
ScaledObject *kedav1alpha1.ScaledObject
Scalers []ScalerBuilder
ScalableObjectGeneration int64
Recorder record.EventRecorder
}

type ScalerBuilder struct {
Expand Down
22 changes: 13 additions & 9 deletions pkg/scaling/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,24 @@ func (h *scaleHandler) GetScalersCache(ctx context.Context, scalableObject inter
key := withTriggers.GenerateIdentifier()
generation := withTriggers.Generation

return h.performGetScalersCache(ctx, key, scalableObject, generation, "", "", "")
return h.performGetScalersCache(ctx, key, scalableObject, &generation, "", "", "")
}

// getScalersCacheForScaledObject returns cache for input ScaledObject, referenced by name and namespace
// we don't need to compare the Generation, because this method should be called only inside scale loop, where we have up to date object.
func (h *scaleHandler) getScalersCacheForScaledObject(ctx context.Context, scaledObjectName, scaledObjectNamespace string) (*cache.ScalersCache, error) {
key := kedav1alpha1.GenerateIdentifier("ScaledObject", scaledObjectNamespace, scaledObjectName)

return h.performGetScalersCache(ctx, key, nil, -1, "ScaledObject", scaledObjectNamespace, scaledObjectName)
return h.performGetScalersCache(ctx, key, nil, nil, "ScaledObject", scaledObjectNamespace, scaledObjectName)
}

// performGetScalersCache returns cache for input scalableObject, it is common code used by GetScalersCache() and getScalersCacheForScaledObject() methods
func (h *scaleHandler) performGetScalersCache(ctx context.Context, key string, scalableObject interface{}, scalableObjectGeneration int64, scalableObjectKind, scalableObjectNamespace, scalableObjectName string) (*cache.ScalersCache, error) {
func (h *scaleHandler) performGetScalersCache(ctx context.Context, key string, scalableObject interface{}, scalableObjectGeneration *int64, scalableObjectKind, scalableObjectNamespace, scalableObjectName string) (*cache.ScalersCache, error) {
h.scalerCachesLock.RLock()
if cache, ok := h.scalerCaches[key]; ok {
// generation was specified -> let's include it in the check as well
if scalableObjectGeneration != -1 {
if scalableObject != nil && cache.ScaledObject != nil && cache.ScaledObject.Generation == scalableObjectGeneration {
if scalableObjectGeneration != nil {
if cache.ScalableObjectGeneration == *scalableObjectGeneration {
h.scalerCachesLock.RUnlock()
return cache, nil
}
Expand All @@ -219,10 +219,13 @@ func (h *scaleHandler) performGetScalersCache(ctx context.Context, key string, s
defer h.scalerCachesLock.Unlock()
if cache, ok := h.scalerCaches[key]; ok {
// generation was specified -> let's include it in the check as well
if scalableObjectGeneration != -1 {
if scalableObject != nil && cache.ScaledObject != nil && cache.ScaledObject.Generation == scalableObjectGeneration {
if scalableObjectGeneration != nil {
if cache.ScalableObjectGeneration == *scalableObjectGeneration {
return cache, nil
}
// object was found in cache, but the generation is not correct,
// let's close scalers in the cache and proceed further to recreate the cache
cache.Close(ctx)
} else {
return cache, nil
}
Expand Down Expand Up @@ -269,8 +272,9 @@ func (h *scaleHandler) performGetScalersCache(ctx context.Context, key string, s
}

newCache := &cache.ScalersCache{
Scalers: scalers,
Recorder: h.recorder,
Scalers: scalers,
ScalableObjectGeneration: withTriggers.Generation,
Recorder: h.recorder,
}
switch obj := scalableObject.(type) {
case *kedav1alpha1.ScaledObject:
Expand Down

0 comments on commit 06f6f7d

Please sign in to comment.