Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* [BUGFIX] Ring: Fix an instance becoming permanently unable to acquire tokens. #7901
* [BUGFIX] Ingester: Stop `QueryStream` from reading the TSDB index after the request is canceled or times out. The series loop noticed cancellation only when it sent a batch, so a query shard whose series all failed the shard matcher, or the lazy matchers of `-ingester.enable-matcher-optimization`, kept decoding every series in its postings after the querier had given up. The loop now checks the request context every 128 series and returns the context error. #7931
* [BUGFIX] Ingester: Fix the expanded postings cache byte accounting drifting under concurrent expiry and eviction, which made the cache count bytes for entries it no longer held. Failed fetches are no longer cached until the TTL expires. #7944
* [BUGFIX] Ingester: Fix the expanded postings cache keeping the whole unfiltered postings list in memory for queries resolved with lazy matchers, while charging only the bytes of the matching series against `max_bytes`. #7992
* [BUGFIX] Querier: Increment `cortex_query_evictions_total` and deregister the victim before cancelling it in the query evictor, fixing double-counted evictions of still-unwinding queries. #7616
* [BUGFIX] Ring: Ignore READONLY ingesters only when renewing tokens, so active series rebalance during scale-down without skewing initial token assignment for instances warm-started in READONLY. #7946 #7982
* [BUGFIX] Distributor: Fix a `panic: send on closed channel` crash during shutdown when `-distributor.num-push-workers` or `-distributor.num-query-workers` is greater than 0. #7964
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/tsdb/expanded_postings_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ func (c *blocksPostingsForMatchersCache) fetchWithLazyMatchers(ctx context.Conte
}
}

// filtered shares ids' backing array; copy it so the cache entry does not keep
// every selected ref reachable while being charged only for the kept ones.
if len(filtered) < len(ids) {
filtered = append(make([]storage.SeriesRef, 0, len(filtered)), filtered...)
}
return filtered, int64(len(filtered) * 8), nil
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/storage/tsdb/lazy_matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,39 @@ func TestFetchWithLazyMatchers_FiltersCorrectly(t *testing.T) {
assert.Equal(t, []storage.SeriesRef{1, 3}, refs)
}

// The cache charges len(refs)*8 bytes, so the result must not keep the
// unfiltered postings array reachable.
func TestFetchWithLazyMatchers_ResultDoesNotRetainUnfilteredRefs(t *testing.T) {
const selected = 1000

ids := make([]storage.SeriesRef, 0, selected)
series := make(map[storage.SeriesRef]labels.Labels, selected)
for i := range selected {
ref := storage.SeriesRef(i + 1)
pod := "worker"
if i < 2 {
pod = "web"
}
ids = append(ids, ref)
series[ref] = labels.FromStrings("pod", pod)
}
ir := &mockIndexReaderWithSeries{series: series}
cache := &blocksPostingsForMatchersCache{
postingsForMatchersFunc: func(_ context.Context, _ prom_tsdb.IndexReader, _ ...*labels.Matcher) (index.Postings, error) {
return index.NewListPostings(ids), nil
},
}

selectMs := []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "__name__", "cpu")}
lazyMs := []*labels.Matcher{labels.MustNewMatcher(labels.MatchRegexp, "pod", "web")}

refs, size, err := cache.fetchWithLazyMatchers(context.Background(), ir, selectMs, lazyMs)
assert.NoError(t, err)
assert.Equal(t, []storage.SeriesRef{1, 2}, refs)
assert.Equal(t, len(refs), cap(refs))
assert.Equal(t, int64(cap(refs)*8), size)
}

// TestFetchWithLazyMatchers_BufferReuseSafety guards the per-series label
// buffer reuse in fetchWithLazyMatchers.
func TestFetchWithLazyMatchers_BufferReuseSafety(t *testing.T) {
Expand Down