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

[weekly-r317] PostingsForLabelMatching: check for waiting writers #760

Closed
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
19 changes: 19 additions & 0 deletions tsdb/index/postings.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,17 @@ func (p *MemPostings) PostingsForLabelMatching(ctx context.Context, name string,
its := make([]Postings, 0, len(vals))
p.mtx.RLock()
e := p.m[name]
iter := 0
for _, v := range vals {
iter++
if iter%1024 == 0 && p.writersWaiting() {
// There are writers waiting, so unlock the mutex, and use this opportunity to check the context.
p.mtx.RUnlock()
if ctx.Err() != nil {
return ErrPostings(ctx.Err())
}
p.mtx.RLock()
}
if refs, ok := e[v]; ok {
// Some of the values may have been garbage-collected in the meantime this is fine, we'll just skip them.
// If we didn't let the mutex go, we'd have these postings here, but they would be pointing nowhere
Expand All @@ -486,6 +496,15 @@ func (p *MemPostings) PostingsForLabelMatching(ctx context.Context, name string,
return Merge(ctx, its...)
}

// writersWaiting can be called from an RLock()ed-goroutine to check whether there are other goroutines waiting on Lock().
func (p *MemPostings) writersWaiting() bool {
if !p.mtx.TryRLock() {
return true
}
p.mtx.RUnlock()
return false
}

// ExpandPostings returns the postings expanded as a slice.
func ExpandPostings(p Postings) (res []storage.SeriesRef, err error) {
for p.Next() {
Expand Down
Loading