mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Don't allow cancelled contexts to poison the postings for matchers cache.
Signed-off-by: Charles Korn <charles.korn@grafana.com>
This commit is contained in:
parent
8df8db6c73
commit
6dcebc9e25
|
@ -78,27 +78,31 @@ func (c *PostingsForMatchersCache) PostingsForMatchers(ctx context.Context, ix I
|
||||||
return c.postingsForMatchers(ctx, ix, ms...)
|
return c.postingsForMatchers(ctx, ix, ms...)
|
||||||
}
|
}
|
||||||
c.expire()
|
c.expire()
|
||||||
return c.postingsForMatchersPromise(ctx, ix, ms)()
|
return c.postingsForMatchersPromise(ix, ms)(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
type postingsForMatcherPromise struct {
|
type postingsForMatcherPromise struct {
|
||||||
sync.WaitGroup
|
done chan struct{}
|
||||||
|
|
||||||
cloner *index.PostingsCloner
|
cloner *index.PostingsCloner
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postingsForMatcherPromise) result() (index.Postings, error) {
|
func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings, error) {
|
||||||
p.Wait()
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
case <-p.done:
|
||||||
if p.err != nil {
|
if p.err != nil {
|
||||||
return nil, p.err
|
return nil, p.err
|
||||||
}
|
}
|
||||||
return p.cloner.Clone(), nil
|
return p.cloner.Clone(), nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Context, ix IndexPostingsReader, ms []*labels.Matcher) func() (index.Postings, error) {
|
func (c *PostingsForMatchersCache) postingsForMatchersPromise(ix IndexPostingsReader, ms []*labels.Matcher) func(context.Context) (index.Postings, error) {
|
||||||
promise := new(postingsForMatcherPromise)
|
promise := new(postingsForMatcherPromise)
|
||||||
promise.Add(1)
|
promise.done = make(chan struct{})
|
||||||
|
|
||||||
key := matchersKey(ms)
|
key := matchersKey(ms)
|
||||||
oldPromise, loaded := c.calls.LoadOrStore(key, promise)
|
oldPromise, loaded := c.calls.LoadOrStore(key, promise)
|
||||||
|
@ -106,9 +110,10 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
|
||||||
promise = oldPromise.(*postingsForMatcherPromise)
|
promise = oldPromise.(*postingsForMatcherPromise)
|
||||||
return promise.result
|
return promise.result
|
||||||
}
|
}
|
||||||
defer promise.Done()
|
defer close(promise.done)
|
||||||
|
|
||||||
if postings, err := c.postingsForMatchers(ctx, ix, ms...); err != nil {
|
// FIXME: do we need to cancel the call to postingsForMatchers if all the callers waiting for the result have cancelled their context?
|
||||||
|
if postings, err := c.postingsForMatchers(context.Background(), ix, ms...); err != nil {
|
||||||
promise.err = err
|
promise.err = err
|
||||||
} else {
|
} else {
|
||||||
promise.cloner = index.NewPostingsCloner(postings)
|
promise.cloner = index.NewPostingsCloner(postings)
|
||||||
|
|
Loading…
Reference in a new issue