Skip to content

Commit

Permalink
fix: Consider maxSessions in Selenium Grid Scaler (kedacore#2774)
Browse files Browse the repository at this point in the history
Signed-off-by: Ram Cohen <ram.cohen@gmail.com>
  • Loading branch information
adborroto authored and RamCohen committed Mar 23, 2022
1 parent 579b091 commit d549bb8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- **Metric API Scaler:** Improve error handling on not-ok response ([#2317](https://github.com/kedacore/keda/issues/2317))
- **Prometheus Scaler:** Support for `X-Scope-OrgID` header in Prometheus scaler ([#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))
- **Selenium Grid Scaler** Consider `maxSession` grid info when scaling. ([#2618](https://github.com/kedacore/keda/issues/2618))

### Breaking Changes

Expand Down
13 changes: 12 additions & 1 deletion pkg/scalers/selenium_grid_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ type seleniumResponse struct {
}

type data struct {
Grid grid `json:"grid"`
SessionsInfo sessionsInfo `json:"sessionsInfo"`
}

type grid struct {
MaxSession int `json:"maxSession"`
}

type sessionsInfo struct {
SessionQueueRequests []string `json:"sessionQueueRequests"`
Sessions []seleniumSession `json:"sessions"`
Expand Down Expand Up @@ -164,7 +169,7 @@ func (s *seleniumGridScaler) IsActive(ctx context.Context) (bool, error) {

func (s *seleniumGridScaler) getSessionsCount(ctx context.Context) (*resource.Quantity, error) {
body, err := json.Marshal(map[string]string{
"query": "{ sessionsInfo { sessionQueueRequests, sessions { id, capabilities, nodeId } } }",
"query": "{ grid { maxSession }, sessionsInfo { sessionQueueRequests, sessions { id, capabilities, nodeId } } }",
})

if err != nil {
Expand Down Expand Up @@ -238,5 +243,11 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
}
}

var gridMaxSession = int64(seleniumResponse.Data.Grid.MaxSession)

if gridMaxSession > 0 {
count = (count + gridMaxSession - 1) / gridMaxSession
}

return resource.NewQuantity(count, resource.DecimalSI), nil
}
35 changes: 35 additions & 0 deletions pkg/scalers/selenium_grid_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
args: args{
b: []byte(`{
"data": {
"grid":{
"maxSession": 1
},
"sessionsInfo": {
"sessionQueueRequests": [],
"sessions": []
Expand All @@ -58,6 +61,9 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
args: args{
b: []byte(`{
"data": {
"grid":{
"maxSession": 1
},
"sessionsInfo": {
"sessionQueueRequests": ["{\n \"browserName\": \"chrome\"\n}","{\n \"browserName\": \"chrome\"\n}"],
"sessions": [
Expand All @@ -81,6 +87,9 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
args: args{
b: []byte(`{
"data": {
"grid":{
"maxSession": 1
},
"sessionsInfo": {
"sessionQueueRequests": ["{\n \"browserName\": \"chrome\"\n}","{\n \"browserName\": \"chrome\"\n}"],
"sessions": []
Expand All @@ -93,12 +102,35 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
want: resource.NewQuantity(2, resource.DecimalSI),
wantErr: false,
},
{
name: "active sessions with matching browsername and maxSession=2 should return count as 1",
args: args{
b: []byte(`{
"data": {
"grid":{
"maxSession": 2
},
"sessionsInfo": {
"sessionQueueRequests": ["{\n \"browserName\": \"chrome\",\n \"browserVersion\": \"91.0\"\n}","{\n \"browserName\": \"chrome\"\n}"],
"sessions": []
}
}
}`),
browserName: "chrome",
browserVersion: "latest",
},
want: resource.NewQuantity(1, resource.DecimalSI),
wantErr: false,
},
{
name: "active sessions with matching browsername should return count as 3",
args: args{
b: []byte(`{
"data": {
"sessionsInfo": {
"grid":{
"maxSession": 1
},
"sessionQueueRequests": ["{\n \"browserName\": \"chrome\"\n}","{\n \"browserName\": \"chrome\"\n}"],
"sessions": [
{
Expand All @@ -121,6 +153,9 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
args: args{
b: []byte(`{
"data": {
"grid":{
"maxSession": 1
},
"sessionsInfo": {
"sessionQueueRequests": ["{\n \"browserName\": \"chrome\",\n \"browserVersion\": \"91.0\"\n}","{\n \"browserName\": \"chrome\"\n}"],
"sessions": [
Expand Down

0 comments on commit d549bb8

Please sign in to comment.