Skip to content

Commit

Permalink
[Heartbeat] Limit browser jobs to 2 by default (#32564) (#32566)
Browse files Browse the repository at this point in the history
Fixes #32082 by limiting browser jobs to 2. Users can still override this with the global heartbeat.jobs.browser.limit: 42.

This also fixes a previously unknown bug, where changing the heartbeat.jobs.*.limit values would cause heartbeat to crash with a panic. This appears to be a bug ingo-ucfg. The workaround this PR adds is moving from map[string]JobLimit to map[string]*JobLimit, which doesn't trigger the same reflection issues.

(cherry picked from commit cdd37ca)

Co-authored-by: Andrew Cholakian <[email protected]>
  • Loading branch information
mergify[bot] and andrewvc authored Aug 2, 2022
1 parent 8910f8f commit 9dffb09
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
*Heartbeat*
- Browser monitors (beta) no write to the `synthetics-*` index prefix. {pull}32064[32064]
- Setting a custom index for a given monitor is now deprecated. Streams are preferred. {pull}32064[32064]
- Browserc monitors now default to a max concurrency of two. {pull}32564[32564]


*Metricbeat*
Expand Down
10 changes: 8 additions & 2 deletions heartbeat/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Config struct {
ConfigMonitors *conf.C `config:"config.monitors"`
Scheduler Scheduler `config:"scheduler"`
Autodiscover *autodiscover.Config `config:"autodiscover"`
Jobs map[string]JobLimit `config:"jobs"`
Jobs map[string]*JobLimit `config:"jobs"`
}

type JobLimit struct {
Expand All @@ -46,4 +46,10 @@ type Scheduler struct {
}

// DefaultConfig is the canonical instantiation of Config.
var DefaultConfig = Config{}
var DefaultConfig = Config{
Jobs: map[string]*JobLimit{
"browser": {
Limit: 2,
},
},
}
2 changes: 1 addition & 1 deletion heartbeat/scheduler/schedjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestSchedJobRun(t *testing.T) {

// testRecursiveForkingJob tests that a schedJob that splits into multiple parallel pieces executes without error
func TestRecursiveForkingJob(t *testing.T) {
s := Create(1000, monitoring.NewRegistry(), tarawaTime(), map[string]config.JobLimit{
s := Create(1000, monitoring.NewRegistry(), tarawaTime(), map[string]*config.JobLimit{
"atype": {Limit: 1},
}, false)
ran := batomic.NewInt(0)
Expand Down
5 changes: 3 additions & 2 deletions heartbeat/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,19 @@ type Schedule interface {
RunOnInit() bool
}

func getJobLimitSem(jobLimitByType map[string]config.JobLimit) map[string]*semaphore.Weighted {
func getJobLimitSem(jobLimitByType map[string]*config.JobLimit) map[string]*semaphore.Weighted {
jobLimitSem := map[string]*semaphore.Weighted{}
for jobType, jobLimit := range jobLimitByType {
if jobLimit.Limit > 0 {
logp.L().Infof("limiting to %d concurrent jobs for '%s' type", jobLimit.Limit, jobType)
jobLimitSem[jobType] = semaphore.NewWeighted(jobLimit.Limit)
}
}
return jobLimitSem
}

// NewWithLocation creates a new Scheduler using the given runAt zone.
func Create(limit int64, registry *monitoring.Registry, location *time.Location, jobLimitByType map[string]config.JobLimit, runOnce bool) *Scheduler {
func Create(limit int64, registry *monitoring.Registry, location *time.Location, jobLimitByType map[string]*config.JobLimit, runOnce bool) *Scheduler {
ctx, cancelCtx := context.WithCancel(context.Background())

if limit < 1 {
Expand Down
4 changes: 2 additions & 2 deletions heartbeat/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ func TestSchedTaskLimits(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var jobConfigByType = map[string]config.JobLimit{}
var jobConfigByType = map[string]*config.JobLimit{}
jobType := "http"
if tt.limit > 0 {
jobConfigByType = map[string]config.JobLimit{
jobConfigByType = map[string]*config.JobLimit{
jobType: {Limit: tt.limit},
}
}
Expand Down

0 comments on commit 9dffb09

Please sign in to comment.