Skip to content
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

Activation threshold (activationValue) for Rabbitmq #2831

Merged
merged 18 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ To learn more about our roadmap, we recommend reading [this document](ROADMAP.md
- **Prometheus Scaler:** Check and properly inform user that `threshold` is not set ([#2793](https://github.com/kedacore/keda/issues/2793))
- **Prometheus Scaler:** Support for `X-Scope-OrgID` header ([#2667](https://github.com/kedacore/keda/issues/2667))
- **RabbitMQ Scaler:** Include `vhost` for RabbitMQ when retrieving queue info with `useRegex` ([#2498](https://github.com/kedacore/keda/issues/2498))
- **RabbitMQ Scaler:** Add activation threshold `activationValue` ([#2800](https://github.com/kedacore/keda/issues/2800))
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
- **Selenium Grid Scaler:** Consider `maxSession` grid info when scaling. ([#2618](https://github.com/kedacore/keda/issues/2618))

### Deprecations
Expand Down
40 changes: 30 additions & 10 deletions pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ func init() {
}

const (
rabbitQueueLengthMetricName = "queueLength"
rabbitModeTriggerConfigName = "mode"
rabbitValueTriggerConfigName = "value"
rabbitModeQueueLength = "QueueLength"
rabbitModeMessageRate = "MessageRate"
defaultRabbitMQQueueLength = 20
rabbitMetricType = "External"
rabbitRootVhostPath = "/%2F"
rabbitQueueLengthMetricName = "queueLength"
rabbitModeTriggerConfigName = "mode"
rabbitValueTriggerConfigName = "value"
rabbitActivationValueTriggerConfigName = "activationValue"
rabbitModeQueueLength = "QueueLength"
rabbitModeMessageRate = "MessageRate"
defaultRabbitMQQueueLength = 20
rabbitMetricType = "External"
rabbitRootVhostPath = "/%2F"
)

const (
Expand Down Expand Up @@ -63,6 +64,7 @@ type rabbitMQMetadata struct {
queueName string
mode string // QueueLength or MessageRate
value float64 // trigger value (queue length or publish/sec. rate)
activationValue float64 // activation value
host string // connection string for either HTTP or AMQP protocol
protocol string // either http or amqp protocol
vhostName *string // override the vhost from the connection info
Expand Down Expand Up @@ -286,6 +288,7 @@ func parseTrigger(meta *rabbitMQMetadata, config *ScalerConfig) (*rabbitMQMetada
deprecatedQueueLengthValue, deprecatedQueueLengthPresent := config.TriggerMetadata[rabbitQueueLengthMetricName]
mode, modePresent := config.TriggerMetadata[rabbitModeTriggerConfigName]
value, valuePresent := config.TriggerMetadata[rabbitValueTriggerConfigName]
activationValue, activationValuePresent := config.TriggerMetadata[rabbitActivationValueTriggerConfigName]

// Initialize to default trigger settings
meta.mode = rabbitModeQueueLength
Expand All @@ -301,6 +304,15 @@ func parseTrigger(meta *rabbitMQMetadata, config *ScalerConfig) (*rabbitMQMetada
return nil, fmt.Errorf("queueLength is deprecated; configure only %s and %s", rabbitModeTriggerConfigName, rabbitValueTriggerConfigName)
}

// Parse activation value
if activationValuePresent {
activation, err := strconv.ParseFloat(activationValue, 64)
if err != nil {
return nil, fmt.Errorf("can't parse %s: %s", rabbitActivationValueTriggerConfigName, err)
}
meta.activationValue = activation
}

// Parse deprecated `queueLength` value
if deprecatedQueueLengthPresent {
queueLength, err := strconv.ParseFloat(deprecatedQueueLengthValue, 64)
Expand All @@ -310,6 +322,10 @@ func parseTrigger(meta *rabbitMQMetadata, config *ScalerConfig) (*rabbitMQMetada
meta.mode = rabbitModeQueueLength
meta.value = queueLength

if meta.activationValue > queueLength {
return nil, fmt.Errorf("activationValue is greater than value")
}

JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
return meta, nil
}

Expand Down Expand Up @@ -339,6 +355,10 @@ func parseTrigger(meta *rabbitMQMetadata, config *ScalerConfig) (*rabbitMQMetada
return nil, fmt.Errorf("protocol %s not supported; must be http to use mode %s", meta.protocol, rabbitModeMessageRate)
}

if meta.activationValue > meta.value {
return nil, fmt.Errorf("activationValue is greater than value")
}
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved

return meta, nil
}

Expand Down Expand Up @@ -376,9 +396,9 @@ func (s *rabbitMQScaler) IsActive(ctx context.Context) (bool, error) {
}

if s.metadata.mode == rabbitModeQueueLength {
return messages > 0, nil
return float64(messages) > s.metadata.activationValue, nil
}
return publishRate > 0 || messages > 0, nil
return publishRate > s.metadata.activationValue || float64(messages) > s.metadata.activationValue, nil
}

func (s *rabbitMQScaler) getQueueStatus() (int64, float64, error) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/scalers/rabbitmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{
// missing queueName
{map[string]string{"queueLength": "10", "hostFromEnv": host}, true, map[string]string{}},
// host defined in authParams
{map[string]string{"queueLength": "10"}, true, map[string]string{"host": host}},
{map[string]string{"queueLength": "10", "hostFromEnv": host}, true, map[string]string{"host": host}},
// properly formed metadata with http protocol
{map[string]string{"queueLength": "10", "queueName": "sample", "host": host, "protocol": "http"}, false, map[string]string{}},
// queue name with slashes
Expand Down Expand Up @@ -111,6 +111,12 @@ var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{
{map[string]string{"mode": "MessageRate", "value": "1000", "queueName": "sample", "host": "http://", "useRegex": "true", "pageSize": "-1"}, true, map[string]string{}},
// invalid pageSize
{map[string]string{"mode": "MessageRate", "value": "1000", "queueName": "sample", "host": "http://", "useRegex": "true", "pageSize": "a"}, true, map[string]string{}},
// activationValue passed
{map[string]string{"activationValue": "10", "queueLength": "20", "queueName": "sample", "hostFromEnv": host}, false, map[string]string{}},
// invalid activationValue > queueLength
{map[string]string{"activationValue": "20", "queueLength": "10", "queueName": "sample", "hostFromEnv": host}, true, map[string]string{}},
// malformed activationValue
{map[string]string{"activationValue": "AA", "queueLength": "10", "queueName": "sample", "hostFromEnv": host}, true, map[string]string{}},
// http and excludeUnacknowledged
{map[string]string{"mode": "QueueLength", "value": "1000", "queueName": "sample", "host": "http://", "useRegex": "true", "excludeUnacknowledged": "true"}, false, map[string]string{}},
// amqp and excludeUnacknowledged
Expand Down
4 changes: 4 additions & 0 deletions tests/scalers_go/rabbitmq/rabbitmq_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package rabbitmq

import (
"fmt"
"testing"

"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -198,6 +199,9 @@ func RMQPublishMessages(t *testing.T, namespace, connectionString, queueName str
MessageCount: messageCount,
}

// Before push messages remove previous jobs if any
_, _ = helper.ExecuteCommand(fmt.Sprintf("kubectl delete jobs/rabbitmq-publish-%s --namespace %s", queueName, namespace))

helper.KubectlApplyWithTemplate(t, data, "rmqPublishTemplate", publishTemplate)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/base64"
"fmt"
"testing"
"time"

"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,6 +59,7 @@ spec:
hostFromEnv: RabbitApiHost
mode: QueueLength
value: '10'
activationValue: '5'
`
)

Expand Down Expand Up @@ -87,6 +89,8 @@ func TestScaler(t *testing.T) {

testScaling(t, kc)

testActivationValue(t, kc)

// cleanup
t.Log("--- cleaning up ---")
DeleteKubernetesResources(t, kc, testNamespace, data, templates)
Expand Down Expand Up @@ -117,3 +121,15 @@ func testScaling(t *testing.T, kc *kubernetes.Clientset) {
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 1),
"replica count should be 0 after 1 minute")
}

func testActivationValue(t *testing.T, kc *kubernetes.Clientset) {
t.Log("--- testing activation value ---")
messagesToQueue := 3
RMQPublishMessages(t, rmqNamespace, connectionString, queueName, messagesToQueue)

// Wait for replicas
time.Sleep(60 * time.Second)

assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 1),
"replica count should be 0 after 1 minute")
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
}