Skip to content

Commit

Permalink
fix(checker): fix checker settings (#972) (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf authored Dec 18, 2023
1 parent 27662e7 commit da69459
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
16 changes: 3 additions & 13 deletions checker/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package worker

import (
"errors"
"runtime"
"sync/atomic"
"time"

Expand Down Expand Up @@ -127,17 +126,8 @@ func (check *Checker) startCheckerWorker(w checkerWorker) error {
return nil
}

maxParallelChecks := w.MaxParallelChecks()
if maxParallelChecks == 0 {
maxParallelChecks = runtime.NumCPU()

check.Logger.Info().
Int("number_of_cpu", maxParallelChecks).
Msg("MaxParallel" + w.Name() + "Checks is not configured, set it to the number of CPU")
}

const maxParallelChecksMaxValue = 1024 * 8
if maxParallelChecks > maxParallelChecksMaxValue {
if w.MaxParallelChecks() > maxParallelChecksMaxValue {
return errors.New("MaxParallel" + w.Name() + "Checks value is too large")
}

Expand All @@ -146,10 +136,10 @@ func (check *Checker) startCheckerWorker(w checkerWorker) error {

triggerIdsToCheckChan := check.startTriggerToCheckGetter(
w.GetTriggersToCheck,
maxParallelChecks,
w.MaxParallelChecks(),
)

for i := 0; i < maxParallelChecks; i++ {
for i := 0; i < w.MaxParallelChecks(); i++ {
check.tomb.Go(func() error {
return check.startTriggerHandler(
triggerIdsToCheckChan,
Expand Down
32 changes: 32 additions & 0 deletions cmd/checker/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"runtime"

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/checker"
"github.com/moira-alert/moira/cmd"
Expand Down Expand Up @@ -39,6 +41,8 @@ type checkerConfig struct {
MaxParallelChecks int `yaml:"max_parallel_checks"`
// Max concurrent remote checkers to run. Equals to the number of processor cores found on Moira host by default or when variable is defined as 0.
MaxParallelRemoteChecks int `yaml:"max_parallel_remote_checks"`
// Max concurrent prometheus checkers to run. Equals to the number of processor cores found on Moira host by default or when variable is defined as 0.
MaxParallelPrometheusChecks int `yaml:"max_parallel_prometheus_checks"`
// Specify log level by entities
SetLogLevel triggersLogConfig `yaml:"set_log_level"`
// Metric event pop operation batch size
Expand All @@ -47,6 +51,15 @@ type checkerConfig struct {
MetricEventPopDelay string `yaml:"metric_event_pop_delay"`
}

func handleParallelChecks(parallelChecks *int) bool {
if parallelChecks != nil && *parallelChecks == 0 {
*parallelChecks = runtime.NumCPU()
return true
}

return false
}

func (config *checkerConfig) getSettings(logger moira.Logger) *checker.Config {
logTriggersToLevel := make(map[string]string)
for _, v := range config.SetLogLevel.TriggersToLevel {
Expand All @@ -56,13 +69,32 @@ func (config *checkerConfig) getSettings(logger moira.Logger) *checker.Config {
Int("number_of_triggers", len(logTriggersToLevel)).
Msg("Found dynamic log rules in config for some triggers")

if handleParallelChecks(&config.MaxParallelChecks) {
logger.Info().
Int("number_of_cpu", config.MaxParallelChecks).
Msg("MaxParallelChecks is not configured, set it to the number of CPU")
}

if handleParallelChecks(&config.MaxParallelRemoteChecks) {
logger.Info().
Int("number_of_cpu", config.MaxParallelRemoteChecks).
Msg("MaxParallelRemoteChecks is not configured, set it to the number of CPU")
}

if handleParallelChecks(&config.MaxParallelPrometheusChecks) {
logger.Info().
Int("number_of_cpu", config.MaxParallelPrometheusChecks).
Msg("MaxParallelPrometheusChecks is not configured, set it to the number of CPU")
}

return &checker.Config{
CheckInterval: to.Duration(config.CheckInterval),
LazyTriggersCheckInterval: to.Duration(config.LazyTriggersCheckInterval),
NoDataCheckInterval: to.Duration(config.NoDataCheckInterval),
StopCheckingIntervalSeconds: int64(to.Duration(config.StopCheckingInterval).Seconds()),
MaxParallelLocalChecks: config.MaxParallelChecks,
MaxParallelRemoteChecks: config.MaxParallelRemoteChecks,
MaxParallelPrometheusChecks: config.MaxParallelPrometheusChecks,
LogTriggersToLevel: logTriggersToLevel,
MetricEventPopBatchSize: int64(config.MetricEventPopBatchSize),
MetricEventPopDelay: to.Duration(config.MetricEventPopDelay),
Expand Down
2 changes: 1 addition & 1 deletion database/redis/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (connector *DbConnector) GetTriggerIDsStartWith(prefix string) ([]string, e
return matchedTriggers, nil
}

func (connector *DbConnector) updateTrigger(triggerID string, newTrigger *moira.Trigger, oldTrigger *moira.Trigger) error {
func (connector *DbConnector) updateTrigger(triggerID string, newTrigger *moira.Trigger, oldTrigger *moira.Trigger) error { // nolint:gocyclo
bytes, err := reply.GetTriggerBytes(triggerID, newTrigger)
if err != nil {
return err
Expand Down

0 comments on commit da69459

Please sign in to comment.