Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2023-11-09 16:26:35 +01:00
parent e3b8667fb1
commit 83b0ed7bdb

View file

@ -46,16 +46,17 @@ func NewPostingsForMatchersCache(ttl time.Duration, maxItems int, maxBytes int64
calls: &sync.Map{}, calls: &sync.Map{},
cached: list.New(), cached: list.New(),
ttl: ttl, ttl: ttl,
ttlAttrib: attribute.Stringer("ttl", ttl), maxItems: maxItems,
maxItems: maxItems, maxBytes: maxBytes,
maxBytes: maxBytes, force: force,
force: force,
timeNow: time.Now, timeNow: time.Now,
postingsForMatchers: PostingsForMatchers, postingsForMatchers: PostingsForMatchers,
tracer: otel.Tracer(""), tracer: otel.Tracer(""),
ttlAttrib: attribute.Stringer("ttl", ttl),
forceAttrib: attribute.Bool("force", force),
} }
return b return b
@ -69,11 +70,10 @@ type PostingsForMatchersCache struct {
cached *list.List cached *list.List
cachedBytes int64 cachedBytes int64
ttl time.Duration ttl time.Duration
ttlAttrib attribute.KeyValue maxItems int
maxItems int maxBytes int64
maxBytes int64 force bool
force bool
// timeNow is the time.Now that can be replaced for testing purposes // timeNow is the time.Now that can be replaced for testing purposes
timeNow func() time.Time timeNow func() time.Time
@ -81,12 +81,16 @@ type PostingsForMatchersCache struct {
postingsForMatchers func(ctx context.Context, ix IndexPostingsReader, ms ...*labels.Matcher) (index.Postings, error) postingsForMatchers func(ctx context.Context, ix IndexPostingsReader, ms ...*labels.Matcher) (index.Postings, error)
tracer trace.Tracer tracer trace.Tracer
// Preallocated for performance
ttlAttrib attribute.KeyValue
forceAttrib attribute.KeyValue
} }
func (c *PostingsForMatchersCache) PostingsForMatchers(ctx context.Context, ix IndexPostingsReader, concurrent bool, ms ...*labels.Matcher) (index.Postings, error) { func (c *PostingsForMatchersCache) PostingsForMatchers(ctx context.Context, ix IndexPostingsReader, concurrent bool, ms ...*labels.Matcher) (index.Postings, error) {
ctx, span := c.tracer.Start(ctx, "PostingsForMatchersCache.PostingsForMatchers", trace.WithAttributes( ctx, span := c.tracer.Start(ctx, "PostingsForMatchersCache.PostingsForMatchers", trace.WithAttributes(
attribute.Bool("concurrent", concurrent), attribute.Bool("concurrent", concurrent),
attribute.Bool("force", c.force), c.ttlAttrib,
c.forceAttrib,
)) ))
defer span.End() defer span.End()
@ -115,7 +119,6 @@ type postingsForMatcherPromise struct {
cloner *index.PostingsCloner cloner *index.PostingsCloner
err error err error
tracer trace.Tracer
} }
func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings, error) { func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings, error) {
@ -153,8 +156,7 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
defer span.End() defer span.End()
promise := &postingsForMatcherPromise{ promise := &postingsForMatcherPromise{
done: make(chan struct{}), done: make(chan struct{}),
tracer: c.tracer,
} }
oldPromise, loaded := c.calls.LoadOrStore(key, promise) oldPromise, loaded := c.calls.LoadOrStore(key, promise)
@ -254,9 +256,7 @@ func (c *PostingsForMatchersCache) created(ctx context.Context, key string, ts t
span := trace.SpanFromContext(ctx) span := trace.SpanFromContext(ctx)
if c.ttl <= 0 { if c.ttl <= 0 {
span.AddEvent("deleting cached promise since c.ttl <= 0", trace.WithAttributes( span.AddEvent("deleting cached promise since c.ttl <= 0")
c.ttlAttrib,
))
c.calls.Delete(key) c.calls.Delete(key)
return return
} }
@ -271,7 +271,6 @@ func (c *PostingsForMatchersCache) created(ctx context.Context, key string, ts t
}) })
c.cachedBytes += sizeBytes c.cachedBytes += sizeBytes
span.AddEvent("added cached value to expiry queue", trace.WithAttributes( span.AddEvent("added cached value to expiry queue", trace.WithAttributes(
c.ttlAttrib,
attribute.Stringer("timestamp", ts), attribute.Stringer("timestamp", ts),
attribute.Int64("size in bytes", sizeBytes), attribute.Int64("size in bytes", sizeBytes),
attribute.Int64("cached bytes", c.cachedBytes), attribute.Int64("cached bytes", c.cachedBytes),