Skip to content

perf(tsdb): add targeted label cardinality scan - #4

Open
yuchen-db wants to merge 1 commit into
db_mainfrom
top-metric-cardinality-scan
Open

perf(tsdb): add targeted label cardinality scan#4
yuchen-db wants to merge 1 commit into
db_mainfrom
top-metric-cardinality-scan

Conversation

@yuchen-db

Copy link
Copy Markdown
Owner

Summary

MemPostings.Stats scans every label name and value while holding RLock, even when a caller only needs the highest-cardinality values for one label such as __name__. Periodic callers can therefore block new-series insertion on large postings indexes.

This adds TopLabelValuesBySeriesCount on MemPostings and Head. The method snapshots only the requested label's values and postings lengths under RLock, then performs top-K selection after releasing the lock. Existing Stats and /api/v1/status/tsdb behavior remain unchanged; Thanos must switch its periodic top-metrics collector to the new API in a follow-up.

Benchmark

The fixture contains approximately 1,000 __name__ values and 200,000 unrelated label values.

                         │   all-labels   │            single-label            │
                         │     sec/op     │   sec/op     vs base               │
PostingsCardinalityStats   18905.62µ ± 4%   30.03µ ± 3%  -99.84% (p=0.002 n=6)

                         │   all-labels   │            single-label             │
                         │      B/op      │     B/op      vs base               │
PostingsCardinalityStats   6326.86Ki ± 0%   24.23Ki ± 0%  -99.62% (p=0.002 n=6)

                         │   all-labels    │            single-label            │
                         │    allocs/op    │ allocs/op   vs base                │
PostingsCardinalityStats   200804.000 ± 0%   2.000 ± 0%  -100.00% (p=0.002 n=6)

Command:

go test ./tsdb/index -run '^$' -bench '^BenchmarkPostingsCardinalityStats$' -benchmem -count=6

Tests

  • go test ./tsdb/index ./tsdb
  • go test -race ./tsdb/index ./tsdb -run '^(TestMemPostingsTopLabelValuesBySeriesCount|TestHeadTopLabelValuesBySeriesCount)$' -count=1
  • make lint with the repository-selected Go 1.23 toolchain
[PERF] TSDB: Add an API for computing top label values by series count without scanning unrelated labels.

Signed-off-by: Yuchen Wang <yuchen.wang@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant