remove dicovery race for the context field

This commit is contained in:
Krasi Georgiev 2018-01-26 00:32:36 +01:00
parent 47538cf6ce
commit acc4197098
2 changed files with 20 additions and 18 deletions

View file

@ -238,12 +238,17 @@ func main() {
ctxWeb, cancelWeb = context.WithCancel(context.Background()) ctxWeb, cancelWeb = context.WithCancel(context.Background())
ctxRule = context.Background() ctxRule = context.Background()
notifier = notifier.New(&cfg.notifier, log.With(logger, "component", "notifier")) notifier = notifier.New(&cfg.notifier, log.With(logger, "component", "notifier"))
discoveryManagerScrape = discovery.NewManager(log.With(logger, "component", "discovery manager scrape"))
discoveryManagerNotify = discovery.NewManager(log.With(logger, "component", "discovery manager notify")) ctxScrape, cancelScrape = context.WithCancel(context.Background())
scrapeManager = retrieval.NewScrapeManager(log.With(logger, "component", "scrape manager"), fanoutStorage) discoveryManagerScrape = discovery.NewManager(ctxScrape, log.With(logger, "component", "discovery manager scrape"))
queryEngine = promql.NewEngine(fanoutStorage, &cfg.queryEngine)
ruleManager = rules.NewManager(&rules.ManagerOptions{ ctxNotify, cancelNotify = context.WithCancel(context.Background())
discoveryManagerNotify = discovery.NewManager(ctxNotify, log.With(logger, "component", "discovery manager notify"))
scrapeManager = retrieval.NewScrapeManager(log.With(logger, "component", "scrape manager"), fanoutStorage)
queryEngine = promql.NewEngine(fanoutStorage, &cfg.queryEngine)
ruleManager = rules.NewManager(&rules.ManagerOptions{
Appendable: fanoutStorage, Appendable: fanoutStorage,
QueryFunc: rules.EngineQueryFunc(queryEngine), QueryFunc: rules.EngineQueryFunc(queryEngine),
NotifyFunc: sendAlerts(notifier, cfg.web.ExternalURL.String()), NotifyFunc: sendAlerts(notifier, cfg.web.ExternalURL.String()),
@ -375,30 +380,28 @@ func main() {
) )
} }
{ {
ctx, cancel := context.WithCancel(context.Background())
g.Add( g.Add(
func() error { func() error {
err := discoveryManagerScrape.Run(ctx) err := discoveryManagerScrape.Run()
level.Info(logger).Log("msg", "Scrape discovery manager stopped") level.Info(logger).Log("msg", "Scrape discovery manager stopped")
return err return err
}, },
func(err error) { func(err error) {
level.Info(logger).Log("msg", "Stopping scrape discovery manager...") level.Info(logger).Log("msg", "Stopping scrape discovery manager...")
cancel() cancelScrape()
}, },
) )
} }
{ {
ctx, cancel := context.WithCancel(context.Background())
g.Add( g.Add(
func() error { func() error {
err := discoveryManagerNotify.Run(ctx) err := discoveryManagerNotify.Run()
level.Info(logger).Log("msg", "Notify discovery manager stopped") level.Info(logger).Log("msg", "Notify discovery manager stopped")
return err return err
}, },
func(err error) { func(err error) {
level.Info(logger).Log("msg", "Stopping notify discovery manager...") level.Info(logger).Log("msg", "Stopping notify discovery manager...")
cancel() cancelNotify()
}, },
) )
} }

View file

@ -60,13 +60,13 @@ type poolKey struct {
} }
// NewManager is the Discovery Manager constructor // NewManager is the Discovery Manager constructor
func NewManager(logger log.Logger) *Manager { func NewManager(ctx context.Context, logger log.Logger) *Manager {
return &Manager{ return &Manager{
logger: logger, logger: logger,
syncCh: make(chan map[string][]*targetgroup.Group), syncCh: make(chan map[string][]*targetgroup.Group),
targets: make(map[poolKey]map[string]*targetgroup.Group), targets: make(map[poolKey]map[string]*targetgroup.Group),
discoverCancel: []context.CancelFunc{}, discoverCancel: []context.CancelFunc{},
ctx: context.Background(), ctx: ctx,
} }
} }
@ -89,13 +89,12 @@ type Manager struct {
} }
// Run starts the background processing // Run starts the background processing
func (m *Manager) Run(ctx context.Context) error { func (m *Manager) Run() error {
m.ctx = ctx
for { for {
select { select {
case <-ctx.Done(): case <-m.ctx.Done():
m.cancelDiscoverers() m.cancelDiscoverers()
return ctx.Err() return m.ctx.Err()
} }
} }
} }