-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix race conditions in the memory alerts store #3648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3598efa
e10a1f1
57572f4
9c21204
6f686b3
1d72d10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -34,10 +34,11 @@ const alertChannelLength = 200 | |||||
type Alerts struct { | ||||||
cancel context.CancelFunc | ||||||
|
||||||
mtx sync.Mutex | ||||||
|
||||||
alerts *store.Alerts | ||||||
marker types.AlertMarker | ||||||
|
||||||
mtx sync.Mutex | ||||||
listeners map[int]listeningAlerts | ||||||
next int | ||||||
|
||||||
|
@@ -100,37 +101,53 @@ func NewAlerts(ctx context.Context, m types.AlertMarker, intervalGC time.Duratio | |||||
logger: log.With(l, "component", "provider"), | ||||||
callback: alertCallback, | ||||||
} | ||||||
a.alerts.SetGCCallback(func(alerts []types.Alert) { | ||||||
for _, alert := range alerts { | ||||||
// As we don't persist alerts, we no longer consider them after | ||||||
// they are resolved. Alerts waiting for resolved notifications are | ||||||
// held in memory in aggregation groups redundantly. | ||||||
m.Delete(alert.Fingerprint()) | ||||||
a.callback.PostDelete(&alert) | ||||||
} | ||||||
|
||||||
a.mtx.Lock() | ||||||
for i, l := range a.listeners { | ||||||
select { | ||||||
case <-l.done: | ||||||
delete(a.listeners, i) | ||||||
close(l.alerts) | ||||||
default: | ||||||
// listener is not closed yet, hence proceed. | ||||||
} | ||||||
} | ||||||
a.mtx.Unlock() | ||||||
}) | ||||||
|
||||||
if r != nil { | ||||||
a.registerMetrics(r) | ||||||
} | ||||||
|
||||||
go a.alerts.Run(ctx, intervalGC) | ||||||
go a.gcLoop(ctx, intervalGC) | ||||||
|
||||||
return a, nil | ||||||
} | ||||||
|
||||||
func (a *Alerts) gcLoop(ctx context.Context, interval time.Duration) { | ||||||
t := time.NewTicker(interval) | ||||||
defer t.Stop() | ||||||
for { | ||||||
select { | ||||||
case <-ctx.Done(): | ||||||
return | ||||||
case <-t.C: | ||||||
a.gc() | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
func (a *Alerts) gc() { | ||||||
damnever marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
a.mtx.Lock() | ||||||
defer a.mtx.Unlock() | ||||||
|
||||||
deleted := a.alerts.GC() | ||||||
for _, alert := range deleted { | ||||||
// As we don't persist alerts, we no longer consider them after | ||||||
// they are resolved. Alerts waiting for resolved notifications are | ||||||
// held in memory in aggregation groups redundantly. | ||||||
a.marker.Delete(alert.Fingerprint()) | ||||||
a.callback.PostDelete(&alert) | ||||||
} | ||||||
|
||||||
for i, l := range a.listeners { | ||||||
select { | ||||||
case <-l.done: | ||||||
delete(a.listeners, i) | ||||||
close(l.alerts) | ||||||
default: | ||||||
// listener is not closed yet, hence proceed. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You can address it in the next PR. |
||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// Close the alert provider. | ||||||
func (a *Alerts) Close() { | ||||||
if a.cancel != nil { | ||||||
|
@@ -174,11 +191,13 @@ func (a *Alerts) GetPending() provider.AlertIterator { | |||||
ch = make(chan *types.Alert, alertChannelLength) | ||||||
done = make(chan struct{}) | ||||||
) | ||||||
a.mtx.Lock() | ||||||
defer a.mtx.Unlock() | ||||||
alerts := a.alerts.List() | ||||||
damnever marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
go func() { | ||||||
defer close(ch) | ||||||
|
||||||
for _, a := range a.alerts.List() { | ||||||
for _, a := range alerts { | ||||||
select { | ||||||
case ch <- a: | ||||||
case <-done: | ||||||
|
@@ -192,11 +211,16 @@ func (a *Alerts) GetPending() provider.AlertIterator { | |||||
|
||||||
// Get returns the alert for a given fingerprint. | ||||||
func (a *Alerts) Get(fp model.Fingerprint) (*types.Alert, error) { | ||||||
a.mtx.Lock() | ||||||
damnever marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
defer a.mtx.Unlock() | ||||||
return a.alerts.Get(fp) | ||||||
} | ||||||
|
||||||
// Put adds the given alert to the set. | ||||||
func (a *Alerts) Put(alerts ...*types.Alert) error { | ||||||
a.mtx.Lock() | ||||||
defer a.mtx.Unlock() | ||||||
|
||||||
for _, alert := range alerts { | ||||||
fp := alert.Fingerprint() | ||||||
|
||||||
|
@@ -226,21 +250,22 @@ func (a *Alerts) Put(alerts ...*types.Alert) error { | |||||
|
||||||
a.callback.PostStore(alert, existing) | ||||||
|
||||||
a.mtx.Lock() | ||||||
for _, l := range a.listeners { | ||||||
select { | ||||||
case l.alerts <- alert: | ||||||
case <-l.done: | ||||||
} | ||||||
} | ||||||
a.mtx.Unlock() | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
// count returns the number of non-resolved alerts we currently have stored filtered by the provided state. | ||||||
func (a *Alerts) count(state types.AlertState) int { | ||||||
a.mtx.Lock() | ||||||
damnever marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
defer a.mtx.Unlock() | ||||||
|
||||||
var count int | ||||||
for _, alert := range a.alerts.List() { | ||||||
if alert.Resolved() { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,12 +64,13 @@ func (a *Alerts) Run(ctx context.Context, interval time.Duration) { | |
case <-ctx.Done(): | ||
return | ||
case <-t.C: | ||
a.gc() | ||
a.GC() | ||
} | ||
} | ||
} | ||
|
||
func (a *Alerts) gc() { | ||
// GC deletes resolved alerts and returns them. | ||
func (a *Alerts) GC() []types.Alert { | ||
a.Lock() | ||
var resolved []types.Alert | ||
for fp, alert := range a.c { | ||
|
@@ -90,6 +91,7 @@ func (a *Alerts) gc() { | |
} | ||
a.Unlock() | ||
a.cb(resolved) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gotjosh I want to remove the callback in a future PR, so I'm not too worried about both returning |
||
return resolved | ||
} | ||
|
||
// Get returns the Alert with the matching fingerprint, or an error if it is | ||
|
Uh oh!
There was an error while loading. Please reload this page.