-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathchunksize.go
275 lines (232 loc) · 7.11 KB
/
chunksize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package strategies
import (
"context"
"fmt"
"math"
"sort"
"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/grafana/loki/v3/pkg/bloombuild/protos"
iter "github.com/grafana/loki/v3/pkg/iter/v2"
v1 "github.com/grafana/loki/v3/pkg/storage/bloom/v1"
"github.com/grafana/loki/v3/pkg/storage/config"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb/index"
)
type ChunkSizeStrategyLimits interface {
BloomTaskTargetSeriesChunksSizeBytes(tenantID string) uint64
}
type ChunkSizeStrategy struct {
limits ChunkSizeStrategyLimits
logger log.Logger
}
func NewChunkSizeStrategy(
limits ChunkSizeStrategyLimits,
logger log.Logger,
) (*ChunkSizeStrategy, error) {
return &ChunkSizeStrategy{
limits: limits,
logger: logger,
}, nil
}
func (s *ChunkSizeStrategy) Name() string {
return SplitBySeriesChunkSizeStrategyName
}
func (s *ChunkSizeStrategy) Plan(
ctx context.Context,
table config.DayTable,
tenant string,
tsdbs TSDBSet,
metas []bloomshipper.Meta,
) ([]*protos.Task, error) {
targetTaskSize := s.limits.BloomTaskTargetSeriesChunksSizeBytes(tenant)
logger := log.With(s.logger, "table", table.Addr(), "tenant", tenant)
level.Debug(s.logger).Log("msg", "loading work for tenant", "target task size", humanize.Bytes(targetTaskSize))
// Determine which TSDBs have gaps and need to be processed.
tsdbsWithGaps, err := gapsBetweenTSDBsAndMetas(v1.NewBounds(0, math.MaxUint64), tsdbs, metas)
if err != nil {
level.Error(logger).Log("msg", "failed to find gaps", "err", err)
return nil, fmt.Errorf("failed to find gaps: %w", err)
}
if len(tsdbsWithGaps) == 0 {
level.Debug(logger).Log("msg", "blooms exist for all tsdbs")
return nil, nil
}
sizedIter, iterSize, err := s.sizedSeriesIter(ctx, tenant, tsdbsWithGaps, targetTaskSize)
if err != nil {
return nil, fmt.Errorf("failed to get sized series iter: %w", err)
}
tasks := make([]*protos.Task, 0, iterSize)
for sizedIter.Next() {
series := sizedIter.At()
if series.Len() == 0 {
// This should never happen, but just in case.
level.Warn(logger).Log("msg", "got empty series batch", "tsdb", series.TSDB().Name())
continue
}
bounds := series.Bounds()
blocks, err := getBlocksMatchingBounds(metas, bounds)
if err != nil {
return nil, fmt.Errorf("failed to get blocks matching bounds: %w", err)
}
planGap := protos.Gap{
Bounds: bounds,
Series: series.V1Series(),
Blocks: blocks,
}
tasks = append(tasks, protos.NewTask(table, tenant, bounds, series.TSDB(), []protos.Gap{planGap}))
}
if err := sizedIter.Err(); err != nil {
return nil, fmt.Errorf("failed to iterate over sized series: %w", err)
}
return tasks, nil
}
func getBlocksMatchingBounds(metas []bloomshipper.Meta, bounds v1.FingerprintBounds) ([]bloomshipper.BlockRef, error) {
blocks := make([]bloomshipper.BlockRef, 0, 10)
for _, meta := range metas {
if meta.Bounds.Intersection(bounds) == nil {
// this meta doesn't overlap the gap, skip
continue
}
for _, block := range meta.Blocks {
if block.Bounds.Intersection(bounds) == nil {
// this block doesn't overlap the gap, skip
continue
}
// this block overlaps the gap, add it to the plan
// for this gap
blocks = append(blocks, block)
}
}
// ensure we sort blocks so deduping iterator works as expected
sort.Slice(blocks, func(i, j int) bool {
return blocks[i].Bounds.Less(blocks[j].Bounds)
})
peekingBlocks := iter.NewPeekIter(
iter.NewSliceIter(
blocks,
),
)
// dedupe blocks which could be in multiple metas
itr := iter.NewDedupingIter(
func(a, b bloomshipper.BlockRef) bool {
return a == b
},
iter.Identity[bloomshipper.BlockRef],
func(a, _ bloomshipper.BlockRef) bloomshipper.BlockRef {
return a
},
peekingBlocks,
)
deduped, err := iter.Collect(itr)
if err != nil {
return nil, fmt.Errorf("failed to dedupe blocks: %w", err)
}
return deduped, nil
}
type seriesBatch struct {
tsdb tsdb.SingleTenantTSDBIdentifier
series []*v1.Series
size uint64
}
func newSeriesBatch(tsdb tsdb.SingleTenantTSDBIdentifier) seriesBatch {
return seriesBatch{
tsdb: tsdb,
series: make([]*v1.Series, 0, 100),
}
}
func (b *seriesBatch) Bounds() v1.FingerprintBounds {
if len(b.series) == 0 {
return v1.NewBounds(0, 0)
}
// We assume that the series are sorted by fingerprint.
// This is guaranteed since series are iterated in order by the TSDB.
return v1.NewBounds(b.series[0].Fingerprint, b.series[len(b.series)-1].Fingerprint)
}
func (b *seriesBatch) V1Series() []*v1.Series {
return b.series
}
func (b *seriesBatch) Append(s *v1.Series, size uint64) {
b.series = append(b.series, s)
b.size += size
}
func (b *seriesBatch) Len() int {
return len(b.series)
}
func (b *seriesBatch) Size() uint64 {
return b.size
}
func (b *seriesBatch) TSDB() tsdb.SingleTenantTSDBIdentifier {
return b.tsdb
}
func (s *ChunkSizeStrategy) sizedSeriesIter(
ctx context.Context,
tenant string,
tsdbsWithGaps []tsdbGaps,
targetTaskSizeBytes uint64,
) (iter.Iterator[seriesBatch], int, error) {
batches := make([]seriesBatch, 0, 100)
var currentBatch seriesBatch
for _, idx := range tsdbsWithGaps {
if currentBatch.Len() > 0 {
batches = append(batches, currentBatch)
}
currentBatch = newSeriesBatch(idx.tsdbIdentifier)
for _, gap := range idx.gaps {
if err := idx.tsdb.ForSeries(
ctx,
tenant,
gap,
0, math.MaxInt64,
func(_ labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) (stop bool) {
select {
case <-ctx.Done():
return true
default:
var seriesSize uint64
for _, chk := range chks {
seriesSize += uint64(chk.KB * 1024)
}
// Cut a new batch IF the current batch is not empty (so we add at least one series to the batch)
// AND Adding this series to the batch would exceed the target task size.
if currentBatch.Len() > 0 && currentBatch.Size()+seriesSize > targetTaskSizeBytes {
batches = append(batches, currentBatch)
currentBatch = newSeriesBatch(idx.tsdbIdentifier)
}
res := &v1.Series{
Fingerprint: fp,
Chunks: make(v1.ChunkRefs, 0, len(chks)),
}
for _, chk := range chks {
res.Chunks = append(res.Chunks, v1.ChunkRef{
From: model.Time(chk.MinTime),
Through: model.Time(chk.MaxTime),
Checksum: chk.Checksum,
})
}
currentBatch.Append(res, seriesSize)
return false
}
},
labels.MustNewMatcher(labels.MatchEqual, "", ""),
); err != nil {
return nil, 0, err
}
// Add the last batch for this gap if it's not empty.
if currentBatch.Len() > 0 {
batches = append(batches, currentBatch)
currentBatch = newSeriesBatch(idx.tsdbIdentifier)
}
}
}
select {
case <-ctx.Done():
return iter.NewEmptyIter[seriesBatch](), 0, ctx.Err()
default:
return iter.NewCancelableIter[seriesBatch](ctx, iter.NewSliceIter[seriesBatch](batches)), len(batches), nil
}
}