Skip to content
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

alert: fix alert queue capacity #149

Merged
merged 1 commit into from
Dec 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pkg/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (a *Alert) ResolvedAt(ts time.Time) bool {
type Queue struct {
logger log.Logger
maxBatchSize int
capacity int
labels labels.Labels

mtx sync.Mutex
Expand All @@ -97,7 +98,7 @@ func NewQueue(logger log.Logger, reg prometheus.Registerer, capacity, maxBatchSi
}
q := &Queue{
logger: logger,
queue: make([]*Alert, 0, capacity),
capacity: capacity,
morec: make(chan struct{}, 1),
maxBatchSize: maxBatchSize,
labels: lset,
Expand Down Expand Up @@ -142,9 +143,7 @@ func (q *Queue) Len() int {

// Cap returns the fixed capacity of the queue.
func (q *Queue) Cap() int {
q.mtx.Lock()
defer q.mtx.Unlock()
return cap(q.queue)
return q.capacity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not concurrent safe getter is fine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it is kind of static right? We don't want to change it or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it's a constant passed into the constructor. We never modify it.
Not sure whether it's a global constant though. It could be fine... OTOH though in really big setups, especially if there's one global rule layer, a really high amount of alerts are very possible and we might want to make add a flag for it.

}

// Pop takes a batch of alerts from the front of the queue. The batch size is limited
Expand Down Expand Up @@ -190,7 +189,7 @@ func (q *Queue) Push(alerts []*Alert) {

// Queue capacity should be significantly larger than a single alert
// batch could be.
if d := len(alerts) - cap(q.queue); d > 0 {
if d := len(alerts) - q.capacity; d > 0 {
alerts = alerts[d:]

level.Warn(q.logger).Log(
Expand All @@ -201,7 +200,7 @@ func (q *Queue) Push(alerts []*Alert) {

// If the queue is full, remove the oldest alerts in favor
// of newer ones.
if d := (len(q.queue) + len(alerts)) - cap(q.queue); d > 0 {
if d := (len(q.queue) + len(alerts)) - q.capacity; d > 0 {
q.queue = q.queue[d:]

level.Warn(q.logger).Log(
Expand Down