Skip to content

Commit

Permalink
Reduce regression
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Pracucci <[email protected]>
  • Loading branch information
pracucci committed Jan 28, 2025
1 parent 2d463bd commit 8982998
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions tsdb/postings_for_matchers_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ func (p *postingsForMatcherPromise) result(ctx context.Context) (index.Postings,
if p.err != nil {
return nil, fmt.Errorf("postingsForMatchers promise completed with error: %w", p.err)
}

trace.SpanFromContext(ctx).AddEvent("completed postingsForMatchers promise", trace.WithAttributes(
// Do not format the timestamp to introduce a performance regression.
attribute.Int64("evaluation completed at (epoch seconds)", p.evaluationCompletedAt.Unix()),
))

return p.cloner.Clone(), nil
}
}
Expand Down Expand Up @@ -220,7 +226,6 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
promise.callersCtxTracker.close()

oldPromise := oldPromiseValue.(*postingsForMatcherPromise)
oldPromiseEvaluationCompletedAt := time.Time{}

// Check if the promise already completed the execution and its TTL has not expired yet.
// If the TTL has expired, we don't want to return it, so we just recompute the postings
Expand All @@ -229,10 +234,6 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
if c.ttl > 0 {
select {
case <-oldPromise.done:
// Keep track of when the evaluation was completed. This field can be safely accessed only when
// the promise has done.
oldPromiseEvaluationCompletedAt = oldPromise.evaluationCompletedAt

if c.timeNow().Sub(oldPromise.evaluationCompletedAt) >= c.ttl {
// The cached promise already expired, but it has not been evicted.
span.AddEvent("skipping cached postingsForMatchers promise because its TTL already expired", trace.WithAttributes(
Expand Down Expand Up @@ -269,8 +270,7 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
}

c.metrics.hits.Inc()
span.AddEvent("using cached postingsForMatchers promise", trace.WithAttributes(
attribute.Stringer("cached promise evaluation completed at", evaluationCompletedAtTime(oldPromiseEvaluationCompletedAt)),
span.AddEvent("waiting cached postingsForMatchers promise", trace.WithAttributes(
attribute.String("cache_key", key),
))

Expand Down Expand Up @@ -308,16 +308,6 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
return promise.result
}

type evaluationCompletedAtTime time.Time

func (t evaluationCompletedAtTime) String() string {
if time.Time(t).IsZero() {
return "not completed (in-flight)"
}

return time.Time(t).String()
}

type postingsForMatchersCachedCall struct {
key string
ts time.Time
Expand Down

0 comments on commit 8982998

Please sign in to comment.