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

@ -239,8 +239,13 @@ func main() {
ctxRule = context.Background()
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())
discoveryManagerScrape = discovery.NewManager(ctxScrape, log.With(logger, "component", "discovery manager scrape"))
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{
@ -375,30 +380,28 @@ func main() {
)
}
{
ctx, cancel := context.WithCancel(context.Background())
g.Add(
func() error {
err := discoveryManagerScrape.Run(ctx)
err := discoveryManagerScrape.Run()
level.Info(logger).Log("msg", "Scrape discovery manager stopped")
return err
},
func(err error) {
level.Info(logger).Log("msg", "Stopping scrape discovery manager...")
cancel()
cancelScrape()
},
)
}
{
ctx, cancel := context.WithCancel(context.Background())
g.Add(
func() error {
err := discoveryManagerNotify.Run(ctx)
err := discoveryManagerNotify.Run()
level.Info(logger).Log("msg", "Notify discovery manager stopped")
return err
},
func(err error) {
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
func NewManager(logger log.Logger) *Manager {
func NewManager(ctx context.Context, logger log.Logger) *Manager {
return &Manager{
logger: logger,
syncCh: make(chan map[string][]*targetgroup.Group),
targets: make(map[poolKey]map[string]*targetgroup.Group),
discoverCancel: []context.CancelFunc{},
ctx: context.Background(),
ctx: ctx,
}
}
@ -89,13 +89,12 @@ type Manager struct {
}
// Run starts the background processing
func (m *Manager) Run(ctx context.Context) error {
m.ctx = ctx
func (m *Manager) Run() error {
for {
select {
case <-ctx.Done():
case <-m.ctx.Done():
m.cancelDiscoverers()
return ctx.Err()
return m.ctx.Err()
}
}
}