Skip to content

Commit

Permalink
feat: Introduce activationThreshold/minMetricValue for MySQL Scaler (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Turrado Ferrero authored Jul 26, 2022
1 parent 9e017c2 commit 0b607be
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
30 changes: 20 additions & 10 deletions pkg/scalers/mysql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ type mySQLScaler struct {
}

type mySQLMetadata struct {
connectionString string // Database connection string
username string
password string
host string
port string
dbName string
query string
queryValue float64
metricName string
connectionString string // Database connection string
username string
password string
host string
port string
dbName string
query string
queryValue float64
activationQueryValue float64
metricName string
}

var mySQLLog = logf.Log.WithName("mysql_scaler")
Expand Down Expand Up @@ -78,6 +79,15 @@ func parseMySQLMetadata(config *ScalerConfig) (*mySQLMetadata, error) {
return nil, fmt.Errorf("no queryValue given")
}

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

switch {
case config.AuthParams["connectionString"] != "":
meta.connectionString = config.AuthParams["connectionString"]
Expand Down Expand Up @@ -188,7 +198,7 @@ func (s *mySQLScaler) IsActive(ctx context.Context) (bool, error) {
mySQLLog.Error(err, fmt.Sprintf("Error inspecting MySQL: %s", err))
return false, err
}
return messages > 0, nil
return messages > s.metadata.activationQueryValue, nil
}

// getQueryResult returns result of the scaler query
Expand Down
7 changes: 7 additions & 0 deletions pkg/scalers/mysql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ var testMySQLMetadata = []parseMySQLMetadataTestData{
resolvedEnv: testMySQLResolvedEnv,
raisesError: false,
},
// Invalid activationQueryValue
{
metadata: map[string]string{"query": "query", "queryValue": "12", "activationQueryValue": "AA"},
authParams: map[string]string{"host": "test_host", "port": "test_port", "username": "test_username", "password": "MYSQL_PASSWORD", "dbName": "test_dbname"},
resolvedEnv: testMySQLResolvedEnv,
raisesError: true,
},
}

var mySQLMetricIdentifiers = []mySQLMetricIdentifier{
Expand Down
17 changes: 16 additions & 1 deletion tests/scalers_go/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type templateData struct {
MySQLDatabase string
MySQLRootPassword string
MySQLConnectionString string
ItemsToWrite int
}

type templateValues map[string]string
Expand Down Expand Up @@ -126,6 +127,7 @@ spec:
- type: mysql
metadata:
queryValue: "4"
activationQueryValue: "100"
query: "SELECT CEIL(COUNT(*) / 5) FROM task_instance WHERE state='running' OR state='queued'"
authenticationRef:
name: keda-trigger-auth-mysql-secret
Expand All @@ -140,6 +142,7 @@ metadata:
name: mysql-insert-job
namespace: {{.TestNamespace}}
spec:
ttlSecondsAfterFinished: 0
template:
metadata:
labels:
Expand All @@ -154,7 +157,7 @@ spec:
- insert
env:
- name: TASK_INSTANCES_COUNT
value: "4000"
value: "{{.ItemsToWrite}}"
- name: CONNECTION_STRING
valueFrom:
secretKeyRef:
Expand Down Expand Up @@ -235,6 +238,7 @@ func TestMySQLScaler(t *testing.T) {
setupMySQL(t, kc, data, templates)

// test scaling
testActivation(t, kc, data)
testScaleUp(t, kc, data)
testScaleDown(t, kc)

Expand Down Expand Up @@ -266,9 +270,19 @@ func setupMySQL(t *testing.T, kc *kubernetes.Clientset, data templateData, templ
"replica count should start out as 0")
}

func testActivation(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing activation ---")
t.Log("--- applying job ---")
data.ItemsToWrite = 50
KubectlApplyWithTemplate(t, data, "insertRecordsJobTemplate", insertRecordsJobTemplate)

AssertReplicaCountNotChangeDuringTimePeriod(t, kc, deploymentName, testNamespace, 0, 60)
}

func testScaleUp(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing scale up ---")
t.Log("--- applying job ---")
data.ItemsToWrite = 4000
KubectlApplyWithTemplate(t, data, "insertRecordsJobTemplate", insertRecordsJobTemplate)
// Check if deployment scale to 2 (the max)
maxReplicaCount := 2
Expand All @@ -295,6 +309,7 @@ func getTemplateData() (templateData, templateValues) {
MySQLDatabase: mySQLDatabase,
MySQLRootPassword: mySQLRootPassword,
MySQLConnectionString: base64MySQLConnectionString,
ItemsToWrite: 0,
}, templateValues{
"deploymentTemplate": deploymentTemplate,
"secretTemplate": secretTemplate,
Expand Down

0 comments on commit 0b607be

Please sign in to comment.