Skip to content

Commit

Permalink
feat: Introduce activationThreshold/minMetricValue for Redis Scalers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Turrado Ferrero committed Aug 3, 2022
1 parent 98e937e commit 9b3dc2a
Show file tree
Hide file tree
Showing 15 changed files with 1,679 additions and 2,451 deletions.
35 changes: 23 additions & 12 deletions pkg/scalers/redis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
)

const (
defaultTargetListLength = 5
defaultDBIdx = 0
defaultEnableTLS = false
defaultListLength = 5
defaultActivationListLength = 0
defaultDBIdx = 0
defaultEnableTLS = false
)

type redisAddressParser func(metadata, resolvedEnv, authParams map[string]string) (redisConnectionInfo, error)
Expand All @@ -45,11 +46,12 @@ type redisConnectionInfo struct {
}

type redisMetadata struct {
targetListLength int64
listName string
databaseIndex int
connectionInfo redisConnectionInfo
scalerIndex int
listLength int64
activationListLength int64
listName string
databaseIndex int
connectionInfo redisConnectionInfo
scalerIndex int
}

// NewRedisScaler creates a new redisScaler
Expand Down Expand Up @@ -180,14 +182,23 @@ func parseRedisMetadata(config *ScalerConfig, parserFn redisAddressParser) (*red
meta := redisMetadata{
connectionInfo: connInfo,
}
meta.targetListLength = defaultTargetListLength

meta.listLength = defaultListLength
if val, ok := config.TriggerMetadata["listLength"]; ok {
listLength, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, fmt.Errorf("list length parsing error %s", err.Error())
}
meta.targetListLength = listLength
meta.listLength = listLength
}

meta.activationListLength = defaultActivationListLength
if val, ok := config.TriggerMetadata["activationListLength"]; ok {
activationListLength, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, fmt.Errorf("activationListLength parsing error %s", err.Error())
}
meta.activationListLength = activationListLength
}

if val, ok := config.TriggerMetadata["listName"]; ok {
Expand Down Expand Up @@ -217,7 +228,7 @@ func (s *redisScaler) IsActive(ctx context.Context) (bool, error) {
return false, err
}

return length > 0, nil
return length > s.metadata.activationListLength, nil
}

func (s *redisScaler) Close(context.Context) error {
Expand All @@ -231,7 +242,7 @@ func (s *redisScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricS
Metric: v2beta2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, metricName),
},
Target: GetMetricTarget(s.metricType, s.metadata.targetListLength),
Target: GetMetricTarget(s.metricType, s.metadata.listLength),
}
metricSpec := v2beta2.MetricSpec{
External: externalMetric, Type: externalMetricType,
Expand Down
94 changes: 48 additions & 46 deletions pkg/scalers/redis_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var testRedisMetadata = []parseRedisMetadataTestData{
{map[string]string{"listName": "mylist", "listLength": "10", "address": "", "password": ""}, true, map[string]string{}},
// improperly formed listLength
{map[string]string{"listName": "mylist", "listLength": "AA", "addressFromEnv": "REDIS_HOST", "password": ""}, true, map[string]string{}},
// improperly formed activationListLength
{map[string]string{"listName": "mylist", "listLength": "1", "activationListLength": "AA", "addressFromEnv": "REDIS_HOST", "password": ""}, true, map[string]string{}},
// address does not resolve
{map[string]string{"listName": "mylist", "listLength": "0", "addressFromEnv": "REDIS_WRONG", "password": ""}, true, map[string]string{}},
// password is defined in the authParams
Expand Down Expand Up @@ -153,8 +155,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
"addresses": ":7001, :7002",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{":7001", ":7002"},
},
Expand All @@ -171,8 +173,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
"ports": "1, 2, 3",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -192,8 +194,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
"username": "username",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -213,8 +215,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
},
authParams: map[string]string{},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -235,8 +237,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -257,8 +259,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
"password": "password",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -279,8 +281,8 @@ func TestParseRedisClusterMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand Down Expand Up @@ -364,8 +366,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"addresses": ":7001, :7002",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{":7001", ":7002"},
},
Expand All @@ -382,8 +384,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"ports": "1, 2, 3",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -402,8 +404,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"ports": "1, 2, 3",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -423,8 +425,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"username": "username",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -444,8 +446,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
},
authParams: map[string]string{},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -466,8 +468,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -488,8 +490,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"password": "password",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -510,8 +512,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -532,8 +534,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"sentinelUsername": "sentinelUsername",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -553,8 +555,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
},
authParams: map[string]string{},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -575,8 +577,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -597,8 +599,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"sentinelPassword": "sentinelPassword",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -619,8 +621,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -641,8 +643,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
"sentinelMaster": "sentinelMaster",
},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -662,8 +664,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
},
authParams: map[string]string{},
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand All @@ -684,8 +686,8 @@ func TestParseRedisSentinelMetadata(t *testing.T) {
authParams: map[string]string{},
resolvedEnv: testRedisResolvedEnv,
wantMeta: &redisMetadata{
targetListLength: 5,
listName: "mylist",
listLength: 5,
listName: "mylist",
connectionInfo: redisConnectionInfo{
addresses: []string{"a:1", "b:2", "c:3"},
hosts: []string{"a", "b", "c"},
Expand Down
Loading

0 comments on commit 9b3dc2a

Please sign in to comment.