mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Fix Map Race by Moving Locking closer to the Write (#2476)
This commit is contained in:
parent
182d7de9cd
commit
0f48d07f95
|
@ -217,11 +217,6 @@ func (ts *TargetSet) UpdateProviders(p map[string]TargetProvider) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TargetSet) updateProviders(ctx context.Context, providers map[string]TargetProvider) {
|
func (ts *TargetSet) updateProviders(ctx context.Context, providers map[string]TargetProvider) {
|
||||||
// Lock for the entire time. This may mean up to 5 seconds until the full initial set
|
|
||||||
// is retrieved and applied.
|
|
||||||
// We could release earlier with some tweaks, but this is easier to reason about.
|
|
||||||
ts.mtx.Lock()
|
|
||||||
defer ts.mtx.Unlock()
|
|
||||||
|
|
||||||
// Stop all previous target providers of the target set.
|
// Stop all previous target providers of the target set.
|
||||||
if ts.cancelProviders != nil {
|
if ts.cancelProviders != nil {
|
||||||
|
@ -233,7 +228,9 @@ func (ts *TargetSet) updateProviders(ctx context.Context, providers map[string]T
|
||||||
// (Re-)create a fresh tgroups map to not keep stale targets around. We
|
// (Re-)create a fresh tgroups map to not keep stale targets around. We
|
||||||
// will retrieve all targets below anyway, so cleaning up everything is
|
// will retrieve all targets below anyway, so cleaning up everything is
|
||||||
// safe and doesn't inflict any additional cost.
|
// safe and doesn't inflict any additional cost.
|
||||||
|
ts.mtx.Lock()
|
||||||
ts.tgroups = map[string]*config.TargetGroup{}
|
ts.tgroups = map[string]*config.TargetGroup{}
|
||||||
|
ts.mtx.Unlock()
|
||||||
|
|
||||||
for name, prov := range providers {
|
for name, prov := range providers {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -292,9 +289,6 @@ func (ts *TargetSet) updateProviders(ctx context.Context, providers map[string]T
|
||||||
|
|
||||||
// update handles a target group update from a target provider identified by the name.
|
// update handles a target group update from a target provider identified by the name.
|
||||||
func (ts *TargetSet) update(name string, tgroup *config.TargetGroup) {
|
func (ts *TargetSet) update(name string, tgroup *config.TargetGroup) {
|
||||||
ts.mtx.Lock()
|
|
||||||
defer ts.mtx.Unlock()
|
|
||||||
|
|
||||||
ts.setTargetGroup(name, tgroup)
|
ts.setTargetGroup(name, tgroup)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
@ -304,6 +298,9 @@ func (ts *TargetSet) update(name string, tgroup *config.TargetGroup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TargetSet) setTargetGroup(name string, tg *config.TargetGroup) {
|
func (ts *TargetSet) setTargetGroup(name string, tg *config.TargetGroup) {
|
||||||
|
ts.mtx.Lock()
|
||||||
|
defer ts.mtx.Unlock()
|
||||||
|
|
||||||
if tg == nil {
|
if tg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue