Skip to content

Commit

Permalink
Implementation of sessionBrowserName and a fix for maxSessions (#2709
Browse files Browse the repository at this point in the history
…and #3061 (#3062)
  • Loading branch information
Wolfe1 committed May 25, 2022
1 parent 1eeef4f commit d5f857c
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ To learn more about our roadmap, we recommend reading [this document](ROADMAP.md
### Improvements

- **General:** Use more readable timestamps in KEDA Operator logs ([#3066](https://github.com/kedacore/keda/issue/3066))
- **Selenium Grid Scaler:** Edge active sessions not being properly counted ([#2709](https://github.com/kedacore/keda/issues/2709))
- **Selenium Grid Scaler:** Max Sessions implementation issue ([#3061](https://github.com/kedacore/keda/issues/3061))

### Deprecations

Expand Down
39 changes: 25 additions & 14 deletions pkg/scalers/selenium_grid_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"strconv"
"strings"
Expand All @@ -28,12 +29,13 @@ type seleniumGridScaler struct {
}

type seleniumGridScalerMetadata struct {
url string
browserName string
targetValue int64
browserVersion string
unsafeSsl bool
scalerIndex int
url string
browserName string
sessionBrowserName string
targetValue int64
browserVersion string
unsafeSsl bool
scalerIndex int
}

type seleniumResponse struct {
Expand All @@ -47,6 +49,7 @@ type data struct {

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

type sessionsInfo struct {
Expand Down Expand Up @@ -109,6 +112,12 @@ func parseSeleniumGridScalerMetadata(config *ScalerConfig) (*seleniumGridScalerM
return nil, fmt.Errorf("no browser name given in metadata")
}

if val, ok := config.TriggerMetadata["sessionBrowserName"]; ok {
meta.sessionBrowserName = val
} else {
meta.sessionBrowserName = meta.browserName
}

if val, ok := config.TriggerMetadata["browserVersion"]; ok && val != "" {
meta.browserVersion = val
} else {
Expand Down Expand Up @@ -172,7 +181,7 @@ func (s *seleniumGridScaler) IsActive(ctx context.Context) (bool, error) {

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

if err != nil {
Expand All @@ -199,14 +208,14 @@ func (s *seleniumGridScaler) getSessionsCount(ctx context.Context) (int64, error
if err != nil {
return -1, err
}
v, err := getCountFromSeleniumResponse(b, s.metadata.browserName, s.metadata.browserVersion)
v, err := getCountFromSeleniumResponse(b, s.metadata.browserName, s.metadata.browserVersion, s.metadata.sessionBrowserName)
if err != nil {
return -1, err
}
return v, nil
}

func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion string) (int64, error) {
func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion string, sessionBrowserName string) (int64, error) {
var count int64
var seleniumResponse = seleniumResponse{}

Expand All @@ -221,7 +230,7 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
if capability.BrowserName == browserName {
if strings.HasPrefix(capability.BrowserVersion, browserVersion) {
count++
} else if capability.BrowserVersion == "" && browserVersion == DefaultBrowserVersion {
} else if browserVersion == DefaultBrowserVersion {
count++
}
}
Expand All @@ -234,7 +243,7 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
for _, session := range sessions {
var capability = capability{}
if err := json.Unmarshal([]byte(session.Capabilities), &capability); err == nil {
if capability.BrowserName == browserName {
if capability.BrowserName == sessionBrowserName {
if strings.HasPrefix(capability.BrowserVersion, browserVersion) {
count++
} else if browserVersion == DefaultBrowserVersion {
Expand All @@ -247,10 +256,12 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
}

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

if gridMaxSession > 0 {
count = (count + gridMaxSession - 1) / gridMaxSession
if gridMaxSession > 0 && gridNodeCount > 0 {
// Get count, convert count to next highest int64
var floatCount = float64(count) / (float64(gridMaxSession) / float64(gridNodeCount))
count = int64(math.Ceil(floatCount))
}

return count, nil
}
Loading

0 comments on commit d5f857c

Please sign in to comment.