Skip to content

Commit c5e2d78

Browse files
cyriltovenabboreham
authored andcommitted
Fixes an issue in the index chunks/series intersect code. (#2796)
* Fixes an issue in the index chunks/series intersect code. This was introduce in #2700, more specifically this line https://github.com/cortexproject/cortex/pull/2700/files#diff-10bca0f4f31a2ca1edc507d0289b143dR537 This causes any query with the first label matcher not matching anything to return all matches of all other labels. This is a nasty one since, the code was relying on empty slice, and so it would skip nil values instead of returning no matches. I've added a regression test proving this is fixed everywhere. I think in cortex it can probably affect performance (since you have to download all chunk not required) but not read integrity. I have found this with @slim-bean while deploying Loki, all queriers where OOMing because of this. Signed-off-by: Cyril Tovena <[email protected]> * Update changelog. Signed-off-by: Cyril Tovena <[email protected]>
1 parent c9d9fdf commit c5e2d78

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [FEATURE] Introduced `ruler.for-grace-period`, Minimum duration between alert and restored "for" state. This is maintained only for alerts with configured "for" time greater than grace period. #2783
88
* [FEATURE] Introduced `ruler.for-resend-delay`, Minimum amount of time to wait before resending an alert to Alertmanager. #2783
99
* [ENHANCEMENT] Experimental: Querier can now optionally query secondary store. This is specified by using `-querier.second-store-engine` option, with values `chunks` or `tsdb`. Standard configuration options for this store are used. Additionally, this querying can be configured to happen only for queries that need data older than `-querier.use-second-store-before-time`. Default value of zero will always query secondary store. #2747
10+
* [BUGFIX] Fixed a bug in the index intersect code causing storage to return more chunks/series than required. #2796
1011
* [BUGFIX] Fixed the number of reported keys in the background cache queue. #2764
1112
* [BUGFIX] Fix race in processing of headers in sharded queries. #2762
1213

pkg/chunk/chunk_store.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,13 @@ func (c *store) lookupChunksByMetricName(ctx context.Context, userID string, fro
424424
// Receive chunkSets from all matchers
425425
var chunkIDs []string
426426
var lastErr error
427+
var initialized bool
427428
for i := 0; i < len(matchers); i++ {
428429
select {
429430
case incoming := <-incomingChunkIDs:
430-
if chunkIDs == nil {
431+
if !initialized {
431432
chunkIDs = incoming
433+
initialized = true
432434
} else {
433435
chunkIDs = intersectStrings(chunkIDs, incoming)
434436
}

pkg/chunk/chunk_store_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func TestChunkStore_Get(t *testing.T) {
177177
query: `foo{toms="code", bar="baz"}`,
178178
expect: []Chunk{fooChunk1},
179179
},
180+
{
181+
query: `foo{a="b", bar="baz"}`,
182+
expect: nil,
183+
},
180184
{
181185
query: `{__name__=~"foo"}`,
182186
err: "query must contain metric name",

pkg/chunk/series_store.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,14 @@ func (c *seriesStore) lookupSeriesByMetricNameMatchers(ctx context.Context, from
305305
var lastErr error
306306
var cardinalityExceededErrors int
307307
var cardinalityExceededError CardinalityExceededError
308+
var initialized bool
308309
for i := 0; i < len(matchers); i++ {
309310
select {
310311
case incoming := <-incomingIDs:
311312
preIntersectionCount += len(incoming)
312-
if ids == nil {
313+
if !initialized {
313314
ids = incoming
315+
initialized = true
314316
} else {
315317
ids = intersectStrings(ids, incoming)
316318
}

0 commit comments

Comments
 (0)