Skip to content

Commit 6239464

Browse files
Fix postings cache size estimation and add postings count to spans (#980)
* Fix postings cache size estimation and add postings count to spans The size.Of() already returns recursive size including postings cloner, so we don't need to add EstimateSize(). Instead, track the actual number of postings separately and add it to spans for better observability. Previously we incorrectly estimated postings count as size/8, but the size includes more than just postings data. * Remove unnecessary casting * Use plain int * Fix tests
1 parent 14d2a30 commit 6239464

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

tsdb/index/postings.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,9 +941,8 @@ func (c *PostingsCloner) Clone() Postings {
941941
return newListPostings(c.ids...)
942942
}
943943

944-
// EstimateSize returns an estimate of the size of the PostingsCloner.
945-
func (c *PostingsCloner) EstimateSize() int64 {
946-
return int64(len(c.ids) * 8)
944+
func (c *PostingsCloner) NumPostings() int {
945+
return len(c.ids)
947946
}
948947

949948
// FindIntersectingPostings checks the intersection of p and candidates[i] for each i in candidates,

tsdb/postings_for_matchers_cache.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,12 @@ func (c *PostingsForMatchersCache) postingsForMatchersPromise(ctx context.Contex
451451
promise.callersCtxTracker.Close()
452452

453453
sizeBytes := int64(len(key) + size.Of(promise))
454+
numPostings := 0
454455
if promise.cloner != nil {
455-
sizeBytes += promise.cloner.EstimateSize()
456+
numPostings = promise.cloner.NumPostings()
456457
}
457458

458-
c.onPromiseExecutionDone(ctx, key, promise.evaluationCompletedAt, sizeBytes, promise.err)
459+
c.onPromiseExecutionDone(ctx, key, promise.evaluationCompletedAt, sizeBytes, numPostings, promise.err)
459460
return promise.result
460461
}
461462

@@ -563,7 +564,7 @@ func (c *PostingsForMatchersCache) evictHead(now time.Time) (reason int) {
563564
// onPromiseExecutionDone must be called once the execution of PostingsForMatchers promise has done.
564565
// The input err contains details about any error that could have occurred when executing it.
565566
// The input ts is the function call time.
566-
func (c *PostingsForMatchersCache) onPromiseExecutionDone(ctx context.Context, key string, completedAt time.Time, sizeBytes int64, err error) {
567+
func (c *PostingsForMatchersCache) onPromiseExecutionDone(ctx context.Context, key string, completedAt time.Time, sizeBytes int64, numPostings int, err error) {
567568
span := trace.SpanFromContext(ctx)
568569

569570
// Call the registered hook, if any. It's used only for testing purposes.
@@ -608,6 +609,7 @@ func (c *PostingsForMatchersCache) onPromiseExecutionDone(ctx context.Context, k
608609
trace.WithAttributes(
609610
attribute.Stringer("evaluation completed at", completedAt),
610611
attribute.Int64("size in bytes", sizeBytes),
612+
attribute.Int("num postings", numPostings),
611613
attribute.Int64("cached bytes", lastCachedBytes),
612614
),
613615
trace.WithAttributes(c.additionalAttributes...),

tsdb/postings_for_matchers_cache_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func TestPostingsForMatchersCache(t *testing.T) {
601601
t.Run(fmt.Sprintf("shared=%t, invalidation=%t", shared, invalidation), func(t *testing.T) {
602602
const (
603603
maxItems = 100 // Never hit it.
604-
maxBytes = 2400
604+
maxBytes = 1400
605605
numMatchers = 5
606606
postingsListSize = 30 // 8 bytes per posting ref, so 30 x 8 = 240 bytes.
607607
)
@@ -676,9 +676,9 @@ func TestPostingsForMatchersCache(t *testing.T) {
676676
expectedBytes := 0
677677
expectedEntries := 4
678678
if shared {
679-
expectedBytes = 2808
679+
expectedBytes = 1848
680680
} else {
681-
expectedBytes = 2692
681+
expectedBytes = 1732
682682
}
683683

684684
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(fmt.Sprintf(`
@@ -855,7 +855,7 @@ func TestPostingsForMatchersCache(t *testing.T) {
855855
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
856856
# HELP postings_for_matchers_cache_bytes_total Total number of bytes in the PostingsForMatchers cache.
857857
# TYPE postings_for_matchers_cache_bytes_total gauge
858-
postings_for_matchers_cache_bytes_total 230
858+
postings_for_matchers_cache_bytes_total 206
859859
860860
# HELP postings_for_matchers_cache_entries_total Total number of entries in the PostingsForMatchers cache.
861861
# TYPE postings_for_matchers_cache_entries_total gauge
@@ -965,7 +965,7 @@ func TestPostingsForMatchersCache(t *testing.T) {
965965
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
966966
# HELP postings_for_matchers_cache_bytes_total Total number of bytes in the PostingsForMatchers cache.
967967
# TYPE postings_for_matchers_cache_bytes_total gauge
968-
postings_for_matchers_cache_bytes_total 230
968+
postings_for_matchers_cache_bytes_total 206
969969
970970
# HELP postings_for_matchers_cache_entries_total Total number of entries in the PostingsForMatchers cache.
971971
# TYPE postings_for_matchers_cache_entries_total gauge

0 commit comments

Comments
 (0)