From da69459daaa79556ad1c70c22f4e0fb4af7171b7 Mon Sep 17 00:00:00 2001 From: almostinf <87192879+almostinf@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:28:42 +0300 Subject: [PATCH] fix(checker): fix checker settings (#972) (#976) --- checker/worker/worker.go | 16 +++------------- cmd/checker/config.go | 32 ++++++++++++++++++++++++++++++++ database/redis/trigger.go | 2 +- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/checker/worker/worker.go b/checker/worker/worker.go index c072de4b5..fc9f58a3f 100644 --- a/checker/worker/worker.go +++ b/checker/worker/worker.go @@ -2,7 +2,6 @@ package worker import ( "errors" - "runtime" "sync/atomic" "time" @@ -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") } @@ -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, diff --git a/cmd/checker/config.go b/cmd/checker/config.go index 149de43a3..73904b9a6 100644 --- a/cmd/checker/config.go +++ b/cmd/checker/config.go @@ -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" @@ -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 @@ -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 { @@ -56,6 +69,24 @@ 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), @@ -63,6 +94,7 @@ func (config *checkerConfig) getSettings(logger moira.Logger) *checker.Config { 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), diff --git a/database/redis/trigger.go b/database/redis/trigger.go index b8868dbdd..131efd232 100644 --- a/database/redis/trigger.go +++ b/database/redis/trigger.go @@ -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