Skip to content

Commit 6fc6d14

Browse files
chaudummveitas
authored andcommitted
fix(blooms): Match series to newest block only (grafana#15481)
**What this PR does / why we need it**: While running bloom filters in production we noticed some Loki clusters that showed a very high percentage of missing chunks when querying blooms, thus resulting in lower filter rate. The reason is that old, superseded blocks are still considered up-to-date, because they cover a keyspace that is not covered by newer blocks with smaller keyspaces (due to larger individual series). ``` | series fingerprint keyspace ------------+---------------------------------------------------------------- | o o o o o o o o o o o o o ------------+---------------------------------------------------------------- iteration 1 | 111111111111111111111111111111111111111111111111111111111 iteration 2 | 22222222222222 3333333333333333 444444 iteration 3 | 5555555 6666666 77777777 888888888 9999999999 ... up-to-date | 555555522266666661111177777777333388888888811119999999999 ------------+---------------------------------------------------------------- | x ``` The chart shows the different blocks marked with the numbers 1 to 9 for a subset of the full series fingerprint keyspace. The blocks are generated in multiple successive bloom building iterations. The first block covers a larger keyspace (more series), because the individual blooms in the blocks are smaller in the beginning of the day. Later, the blooms get larger and therefore the block fingerprint ranges gets smaller. However, since we are dealing with fingerprint ranges, not individual fingerprints, the newer blocks cause "gaps" in the range of the previously larger keyspace. In the case above, every block except block 4, are considered up-to-date, since each of them covers a keyspace that is otherwise not covered. When resolving blocks for a series at query time, we consider looking at all up-to-date blocks, which are referenced by the meta files. The series `x` in the chart shows, that it is within the range of 3 up-to-date blocks: 1, 2, 5. However, only the newest block (5) may contain the requested series. This PR changes the block resolver on the index-gateway to only match the newest block to a series, based on the timestamp of the TSDB from with the blocks were generated. --- Signed-off-by: Christian Haudum <[email protected]>
1 parent 9a8f5bf commit 6fc6d14

File tree

3 files changed

+60
-30
lines changed

3 files changed

+60
-30
lines changed

pkg/bloombuild/builder/builder.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/grafana/loki/v3/pkg/storage/config"
3232
"github.com/grafana/loki/v3/pkg/storage/stores"
3333
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper"
34-
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb"
3534
utillog "github.com/grafana/loki/v3/pkg/util/log"
3635
"github.com/grafana/loki/v3/pkg/util/ring"
3736
)
@@ -415,7 +414,6 @@ func (b *Builder) processTask(
415414
Bounds: gap.Bounds,
416415
},
417416
},
418-
Sources: []tsdb.SingleTenantTSDBIdentifier{task.TSDB},
419417
}
420418

421419
// Fetch blocks that aren't up to date but are in the desired fingerprint range
@@ -492,6 +490,7 @@ func (b *Builder) processTask(
492490
level.Debug(logger).Log("msg", "uploaded block", "progress_pct", fmt.Sprintf("%.2f", pct))
493491

494492
meta.Blocks = append(meta.Blocks, built.BlockRef)
493+
meta.Sources = append(meta.Sources, task.TSDB)
495494
}
496495

497496
if err := newBlocks.Err(); err != nil {

pkg/bloomgateway/resolver.go

+45-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bloomgateway
22

33
import (
44
"context"
5+
"slices"
56
"sort"
67
"time"
78

@@ -61,36 +62,55 @@ func (r *defaultBlockResolver) Resolve(ctx context.Context, tenant string, inter
6162
}
6263

6364
func blocksMatchingSeries(metas []bloomshipper.Meta, interval bloomshipper.Interval, series []*logproto.GroupedChunkRefs) []blockWithSeries {
64-
result := make([]blockWithSeries, 0, len(metas))
65-
66-
for _, meta := range metas {
67-
for _, block := range meta.Blocks {
65+
slices.SortFunc(series, func(a, b *logproto.GroupedChunkRefs) int { return int(a.Fingerprint - b.Fingerprint) })
6866

69-
// skip blocks that are not within time interval
70-
if !interval.Overlaps(block.Interval()) {
71-
continue
67+
result := make([]blockWithSeries, 0, len(metas))
68+
cache := make(map[bloomshipper.BlockRef]int)
69+
70+
// find the newest block for each series
71+
for _, s := range series {
72+
var b *bloomshipper.BlockRef
73+
var newestTs time.Time
74+
75+
for i := range metas {
76+
for j := range metas[i].Blocks {
77+
block := metas[i].Blocks[j]
78+
// To keep backwards compatibility, we can only look at the source at index 0
79+
// because in the past the slice had always length 1, see
80+
// https://github.com/grafana/loki/blob/b4060154d198e17bef8ba0fbb1c99bb5c93a412d/pkg/bloombuild/builder/builder.go#L418
81+
sourceTs := metas[i].Sources[0].TS
82+
// Newer metas have len(Sources) == len(Blocks)
83+
if len(metas[i].Sources) > j {
84+
sourceTs = metas[i].Sources[j].TS
85+
}
86+
// skip blocks that are not within time interval
87+
if !interval.Overlaps(block.Interval()) {
88+
continue
89+
}
90+
// skip blocks that do not contain the series
91+
if block.Cmp(s.Fingerprint) != v1.Overlap {
92+
continue
93+
}
94+
// only use the block if it is newer than the previous
95+
if sourceTs.After(newestTs) {
96+
b = &block
97+
newestTs = sourceTs
98+
}
7299
}
100+
}
73101

74-
min := sort.Search(len(series), func(i int) bool {
75-
return block.Cmp(series[i].Fingerprint) > v1.Before
76-
})
77-
78-
max := sort.Search(len(series), func(i int) bool {
79-
return block.Cmp(series[i].Fingerprint) == v1.After
80-
})
81-
82-
// All fingerprints fall outside of the consumer's range
83-
if min == len(series) || max == 0 || min == max {
84-
continue
85-
}
102+
if b == nil {
103+
continue
104+
}
86105

87-
// At least one fingerprint is within bounds of the blocks
88-
// so append to results
89-
dst := make([]*logproto.GroupedChunkRefs, max-min)
90-
_ = copy(dst, series[min:max])
106+
idx, ok := cache[*b]
107+
if ok {
108+
result[idx].series = append(result[idx].series, s)
109+
} else {
110+
cache[*b] = len(result)
91111
result = append(result, blockWithSeries{
92-
block: block,
93-
series: dst,
112+
block: *b,
113+
series: []*logproto.GroupedChunkRefs{s},
94114
})
95115
}
96116
}

pkg/bloomgateway/resolver_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/grafana/loki/v3/pkg/logproto"
1010
v1 "github.com/grafana/loki/v3/pkg/storage/bloom/v1"
1111
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper"
12+
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb"
1213
)
1314

1415
func makeBlockRef(minFp, maxFp model.Fingerprint, from, through model.Time) bloomshipper.BlockRef {
@@ -28,6 +29,9 @@ func makeMeta(minFp, maxFp model.Fingerprint, from, through model.Time) bloomshi
2829
Blocks: []bloomshipper.BlockRef{
2930
makeBlockRef(minFp, maxFp, from, through),
3031
},
32+
Sources: []tsdb.SingleTenantTSDBIdentifier{
33+
{TS: through.Time()},
34+
},
3135
}
3236
}
3337

@@ -100,14 +104,21 @@ func TestBlockResolver_BlocksMatchingSeries(t *testing.T) {
100104

101105
t.Run("multiple overlapping blocks within time range covering full keyspace", func(t *testing.T) {
102106
metas := []bloomshipper.Meta{
103-
makeMeta(0x00, 0xdf, 1000, 1999),
104-
makeMeta(0xc0, 0xff, 1000, 1999),
107+
// 2 series overlap
108+
makeMeta(0x00, 0xdf, 1000, 1499), // "old" meta covers first 4 series
109+
makeMeta(0xc0, 0xff, 1500, 1999), // "new" meta covers last 4 series
105110
}
106111
res := blocksMatchingSeries(metas, interval, series)
112+
for i := range res {
113+
t.Logf("%s", res[i].block)
114+
for j := range res[i].series {
115+
t.Logf(" %016x", res[i].series[j].Fingerprint)
116+
}
117+
}
107118
expected := []blockWithSeries{
108119
{
109120
block: metas[0].Blocks[0],
110-
series: series[0:4],
121+
series: series[0:2], // series 0x00c0 and 0x00d0 are covered in the newer block
111122
},
112123
{
113124
block: metas[1].Blocks[0],

0 commit comments

Comments
 (0)