Skip to content

Commit

Permalink
feat: Introduce activationThreshold/minMetricValue for Redis Scalers
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
  • Loading branch information
JorTurFer committed Jul 23, 2022
1 parent 6609084 commit d6542f2
Show file tree
Hide file tree
Showing 15 changed files with 1,671 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 @@ -44,11 +45,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
}

var redisLog = logf.Log.WithName("redis_scaler")
Expand Down Expand Up @@ -178,14 +180,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 @@ -215,7 +226,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 @@ -229,7 +240,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 @@ -45,6 +45,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 @@ -151,8 +153,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 @@ -169,8 +171,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 @@ -190,8 +192,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 @@ -211,8 +213,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 @@ -233,8 +235,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 @@ -255,8 +257,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 @@ -277,8 +279,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 @@ -362,8 +364,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 @@ -380,8 +382,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 @@ -400,8 +402,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 @@ -421,8 +423,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 @@ -442,8 +444,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 @@ -464,8 +466,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 @@ -486,8 +488,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 @@ -508,8 +510,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 @@ -530,8 +532,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 @@ -551,8 +553,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 @@ -573,8 +575,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 @@ -595,8 +597,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 @@ -617,8 +619,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 @@ -639,8 +641,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 @@ -660,8 +662,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 @@ -682,8 +684,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 d6542f2

Please sign in to comment.