Enhancement: Reload all scrape pools concurrently (#16595)
buf.build / lint and publish (push) Waiting to run Details
CI / Go tests (push) Waiting to run Details
CI / More Go tests (push) Waiting to run Details
CI / Go tests with previous Go version (push) Waiting to run Details
CI / UI tests (push) Waiting to run Details
CI / Go tests on Windows (push) Waiting to run Details
CI / Mixins tests (push) Waiting to run Details
CI / Build Prometheus for common architectures (0) (push) Waiting to run Details
CI / Build Prometheus for common architectures (1) (push) Waiting to run Details
CI / Build Prometheus for common architectures (2) (push) Waiting to run Details
CI / Build Prometheus for all architectures (0) (push) Waiting to run Details
CI / Build Prometheus for all architectures (1) (push) Waiting to run Details
CI / Build Prometheus for all architectures (10) (push) Waiting to run Details
CI / Build Prometheus for all architectures (11) (push) Waiting to run Details
CI / Build Prometheus for all architectures (2) (push) Waiting to run Details
CI / Build Prometheus for all architectures (3) (push) Waiting to run Details
CI / Build Prometheus for all architectures (4) (push) Waiting to run Details
CI / Build Prometheus for all architectures (5) (push) Waiting to run Details
CI / Build Prometheus for all architectures (6) (push) Waiting to run Details
CI / Build Prometheus for all architectures (7) (push) Waiting to run Details
CI / Build Prometheus for all architectures (8) (push) Waiting to run Details
CI / Build Prometheus for all architectures (9) (push) Waiting to run Details
CI / Report status of build Prometheus for all architectures (push) Blocked by required conditions Details
CI / Check generated parser (push) Waiting to run Details
CI / golangci-lint (push) Waiting to run Details
CI / fuzzing (push) Waiting to run Details
CI / codeql (push) Waiting to run Details
CI / Publish main branch artifacts (push) Blocked by required conditions Details
CI / Publish release artefacts (push) Blocked by required conditions Details
CI / Publish UI on npm Registry (push) Blocked by required conditions Details
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run Details

* Reload all scrape pools concurrently

At the moment all scrape pools that need to be reloaded are reloaded one by one. While reloads are ongoing mtxScrape is locked.
For each pool that's being reloaded we need to wait until all targets are updated.
This whole process can take a while and the more scrape pools to reload the longer.
At the same time all pools are independent and there's no real reason to do them one-by-one.
Reload each pool in a seperate goroutine so we finish config reload as ASAP as possible and unlock the mtxScrape.

Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>

* Address PR review feedback

Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>

---------

Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>
This commit is contained in:
Łukasz Mierzwa 2025-06-09 15:01:35 +01:00 committed by GitHub
parent 8fc1750bcc
commit c528293376
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 20 deletions

View File

@ -26,6 +26,7 @@ import (
config_util "github.com/prometheus/common/config" config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/common/promslog" "github.com/prometheus/common/promslog"
"go.uber.org/atomic"
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
@ -287,17 +288,27 @@ func (m *Manager) ApplyConfig(cfg *config.Config) error {
} }
// Cleanup and reload pool if the configuration has changed. // Cleanup and reload pool if the configuration has changed.
var failed bool var (
for name, sp := range m.scrapePools { failed atomic.Bool
switch cfg, ok := m.scrapeConfigs[name]; { wg sync.WaitGroup
toDelete sync.Map // Stores the list of names of pools to delete.
)
for poolName, pool := range m.scrapePools {
wg.Add(1)
cfg, ok := m.scrapeConfigs[poolName]
// Reload each scrape pool in a dedicated goroutine so we don't have to wait a long time
// if we have a lot of scrape pools to update.
go func(name string, sp *scrapePool, cfg *config.ScrapeConfig, ok bool) {
defer wg.Done()
switch {
case !ok: case !ok:
sp.stop() sp.stop()
delete(m.scrapePools, name) toDelete.Store(name, struct{}{})
case !reflect.DeepEqual(sp.config, cfg): case !reflect.DeepEqual(sp.config, cfg):
err := sp.reload(cfg) err := sp.reload(cfg)
if err != nil { if err != nil {
m.logger.Error("error reloading scrape pool", "err", err, "scrape_pool", name) m.logger.Error("error reloading scrape pool", "err", err, "scrape_pool", name)
failed = true failed.Store(true)
} }
fallthrough fallthrough
case ok: case ok:
@ -307,9 +318,16 @@ func (m *Manager) ApplyConfig(cfg *config.Config) error {
sp.logger.Error("No logger found. This is a bug in Prometheus that should be reported upstream.", "scrape_pool", name) sp.logger.Error("No logger found. This is a bug in Prometheus that should be reported upstream.", "scrape_pool", name)
} }
} }
}(poolName, pool, cfg, ok)
} }
wg.Wait()
if failed { toDelete.Range(func(name, _ any) bool {
delete(m.scrapePools, name.(string))
return true
})
if failed.Load() {
return errors.New("failed to apply the new configuration") return errors.New("failed to apply the new configuration")
} }
return nil return nil