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

feat: Introduce activationThreshold/minMetricValue for MySQL Scaler #3422

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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