-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
single_file_index.go
437 lines (370 loc) · 12.2 KB
/
single_file_index.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package tsdb
import (
"context"
"errors"
"fmt"
"io"
"math"
"path/filepath"
"time"
"github.com/opentracing/opentracing-go"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/grafana/loki/v3/pkg/storage/chunk"
"github.com/grafana/loki/v3/pkg/storage/stores/index/seriesvolume"
shipperindex "github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/index"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb/index"
"github.com/grafana/loki/v3/pkg/util"
util_log "github.com/grafana/loki/v3/pkg/util/log"
)
var ErrAlreadyOnDesiredVersion = errors.New("tsdb file already on desired version")
// GetRawFileReaderFunc returns an io.ReadSeeker for reading raw tsdb file from disk
type GetRawFileReaderFunc func() (io.ReadSeeker, error)
func OpenShippableTSDB(p string) (shipperindex.Index, error) {
id, err := identifierFromPath(p)
if err != nil {
return nil, err
}
return NewShippableTSDBFile(id)
}
func RebuildWithVersion(ctx context.Context, path string, desiredVer int) (shipperindex.Index, error) {
indexFile, err := OpenShippableTSDB(path)
if err != nil {
return nil, err
}
defer func() {
if err := indexFile.Close(); err != nil {
level.Error(util_log.Logger).Log("msg", "failed to close index file", "err", err)
}
}()
currVer := indexFile.(*TSDBFile).Index.(*TSDBIndex).reader.(*index.Reader).Version()
if currVer == desiredVer {
return nil, ErrAlreadyOnDesiredVersion
}
builder := NewBuilder(desiredVer)
err = indexFile.(*TSDBFile).Index.(*TSDBIndex).ForSeries(ctx, "", nil, 0, math.MaxInt64, func(lbls labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) (stop bool) {
builder.AddSeries(lbls.Copy(), fp, chks)
return false
}, labels.MustNewMatcher(labels.MatchEqual, "", ""))
if err != nil {
return nil, err
}
parentDir := filepath.Dir(path)
id, err := builder.Build(ctx, parentDir, func(from, through model.Time, checksum uint32) Identifier {
id := SingleTenantTSDBIdentifier{
TS: time.Now(),
From: from,
Through: through,
Checksum: checksum,
}
return NewPrefixedIdentifier(id, parentDir, "")
})
if err != nil {
return nil, err
}
return NewShippableTSDBFile(id)
}
// nolint
// TSDBFile is backed by an actual file and implements the indexshipper/index.Index interface
type TSDBFile struct {
// reuse Identifier for resolving locations
Identifier
// reuse TSDBIndex for reading
Index
// to sastisfy Reader() and Close() methods
getRawFileReader GetRawFileReaderFunc
}
func NewShippableTSDBFile(id Identifier) (*TSDBFile, error) {
idx, getRawFileReader, err := NewTSDBIndexFromFile(id.Path())
if err != nil {
return nil, err
}
return &TSDBFile{
Identifier: id,
Index: idx,
getRawFileReader: getRawFileReader,
}, err
}
func (f *TSDBFile) Close() error {
return f.Index.Close()
}
func (f *TSDBFile) Reader() (io.ReadSeeker, error) {
return f.getRawFileReader()
}
// nolint
// TSDBIndex is backed by an IndexReader
// and translates the IndexReader to an Index implementation
// It loads the file into memory and doesn't keep a file descriptor open
type TSDBIndex struct {
reader IndexReader
chunkFilter chunk.RequestChunkFilterer
}
// Return the index as well as the underlying raw file reader which isn't exposed as an index
// method but is helpful for building an io.reader for the index shipper
func NewTSDBIndexFromFile(location string) (*TSDBIndex, GetRawFileReaderFunc, error) {
reader, err := index.NewFileReader(location)
if err != nil {
return nil, nil, err
}
return NewTSDBIndex(reader), func() (io.ReadSeeker, error) {
return reader.RawFileReader()
}, nil
}
func NewTSDBIndex(reader IndexReader) *TSDBIndex {
return &TSDBIndex{
reader: reader,
}
}
func (i *TSDBIndex) Close() error {
return i.reader.Close()
}
func (i *TSDBIndex) Bounds() (model.Time, model.Time) {
from, through := i.reader.Bounds()
return model.Time(from), model.Time(through)
}
func (i *TSDBIndex) SetChunkFilterer(chunkFilter chunk.RequestChunkFilterer) {
i.chunkFilter = chunkFilter
}
// fn must NOT capture it's arguments. They're reused across series iterations and returned to
// a pool after completion.
// Iteration will stop if the callback returns true.
// Accepts a userID argument in order to implement `Index` interface, but since this is a single tenant index,
// it is ignored (it's enforced elsewhere in index selection)
func (i *TSDBIndex) ForSeries(ctx context.Context, _ string, fpFilter index.FingerprintFilter, from model.Time, through model.Time, fn func(labels.Labels, model.Fingerprint, []index.ChunkMeta) (stop bool), matchers ...*labels.Matcher) error {
// TODO(owen-d): use pool
var ls labels.Labels
chks := ChunkMetasPool.Get()
defer ChunkMetasPool.Put(chks)
var filterer chunk.Filterer
if i.chunkFilter != nil {
filterer = i.chunkFilter.ForRequest(ctx)
}
return i.forPostings(ctx, fpFilter, from, through, matchers, func(p index.Postings) error {
for p.Next() {
hash, err := i.reader.Series(p.At(), int64(from), int64(through), &ls, &chks)
if err != nil {
return err
}
// skip series that belong to different shards
if fpFilter != nil && !fpFilter.Match(model.Fingerprint(hash)) {
continue
}
if filterer != nil && filterer.ShouldFilter(ls) {
continue
}
if stop := fn(ls, model.Fingerprint(hash), chks); stop {
break
}
}
return p.Err()
})
}
func (i *TSDBIndex) forPostings(
_ context.Context,
fpFilter index.FingerprintFilter,
_, _ model.Time,
matchers []*labels.Matcher,
fn func(index.Postings) error,
) error {
p, err := PostingsForMatchers(i.reader, fpFilter, matchers...)
if err != nil {
return err
}
return fn(p)
}
func (i *TSDBIndex) GetChunkRefs(ctx context.Context, userID string, from, through model.Time, res []ChunkRef, fpFilter index.FingerprintFilter, matchers ...*labels.Matcher) ([]ChunkRef, error) {
if res == nil {
res = ChunkRefsPool.Get()
}
res = res[:0]
if err := i.ForSeries(ctx, "", fpFilter, from, through, func(ls labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) (stop bool) {
for _, chk := range chks {
res = append(res, ChunkRef{
User: userID, // assumed to be the same, will be enforced by caller.
Fingerprint: fp,
Start: chk.From(),
End: chk.Through(),
Checksum: chk.Checksum,
})
}
return false
}, matchers...); err != nil {
return nil, err
}
return res, nil
}
func (i *TSDBIndex) Series(ctx context.Context, _ string, from, through model.Time, res []Series, fpFilter index.FingerprintFilter, matchers ...*labels.Matcher) ([]Series, error) {
if res == nil {
res = SeriesPool.Get()
}
res = res[:0]
if err := i.ForSeries(ctx, "", fpFilter, from, through, func(ls labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) (stop bool) {
if len(chks) == 0 {
return
}
res = append(res, Series{
Labels: ls.Copy(),
Fingerprint: fp,
})
return false
}, matchers...); err != nil {
return nil, err
}
return res, nil
}
func (i *TSDBIndex) LabelNames(_ context.Context, _ string, _, _ model.Time, matchers ...*labels.Matcher) ([]string, error) {
if len(matchers) == 0 {
return i.reader.LabelNames()
}
return labelNamesWithMatchers(i.reader, matchers...)
}
func (i *TSDBIndex) LabelValues(_ context.Context, _ string, _, _ model.Time, name string, matchers ...*labels.Matcher) ([]string, error) {
if len(matchers) == 0 {
return i.reader.LabelValues(name)
}
return labelValuesWithMatchers(i.reader, name, matchers...)
}
func (i *TSDBIndex) Checksum() uint32 {
return i.reader.Checksum()
}
func (i *TSDBIndex) Identifier(string) SingleTenantTSDBIdentifier {
lower, upper := i.Bounds()
return SingleTenantTSDBIdentifier{
TS: time.Now(),
From: lower,
Through: upper,
Checksum: i.Checksum(),
}
}
func (i *TSDBIndex) Stats(ctx context.Context, _ string, from, through model.Time, acc IndexStatsAccumulator, fpFilter index.FingerprintFilter, _ shouldIncludeChunk, matchers ...*labels.Matcher) error {
return i.forPostings(ctx, fpFilter, from, through, matchers, func(p index.Postings) error {
// TODO(owen-d): use pool
var ls labels.Labels
var filterer chunk.Filterer
if i.chunkFilter != nil {
filterer = i.chunkFilter.ForRequest(ctx)
}
for p.Next() {
fp, stats, err := i.reader.ChunkStats(p.At(), int64(from), int64(through), &ls)
if err != nil {
return err
}
// skip series that belong to different shards
if fpFilter != nil && !fpFilter.Match(model.Fingerprint(fp)) {
continue
}
if filterer != nil && filterer.ShouldFilter(ls) {
continue
}
if stats.Entries > 0 {
// need to add stream
acc.AddStream(model.Fingerprint(fp))
acc.AddChunkStats(stats)
}
}
return p.Err()
})
}
// Volume returns the volumes of the series described by the passed
// matchers by the names of the passed matchers. All non-requested labels are
// aggregated into the requested series.
//
// ex: Imagine we have two labels: 'foo' and 'fizz' each with two values 'a'
// and 'b'. Called with the matcher `{foo="a"}`, Volume returns the
// aggregated size of the series `{foo="a"}`. If Volume with
// `{foo=~".+", fizz=~".+"}, it returns the volumes aggregated as follows:
//
// {foo="a", fizz="a"}
// {foo="a", fizz="b"}
// {foo="b", fizz="a"}
// {foo="b", fizz="b"}
//
// Volume optionally accepts a slice of target labels. If provided, volumes are aggregated
// into those labels only. For example, given the matcher {fizz=~".+"} and target labels of []string{"foo"},
// volumes would be aggregated as follows:
//
// {foo="a"} which would be the sum of {foo="a", fizz="a"} and {foo="a", fizz="b"}
// {foo="b"} which would be the sum of {foo="b", fizz="a"} and {foo="b", fizz="b"}
func (i *TSDBIndex) Volume(
ctx context.Context,
_ string,
from, through model.Time,
acc VolumeAccumulator,
fpFilter index.FingerprintFilter,
_ shouldIncludeChunk,
targetLabels []string,
aggregateBy string,
matchers ...*labels.Matcher,
) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "Index.Volume")
defer sp.Finish()
labelsToMatch, matchers, includeAll := util.PrepareLabelsAndMatchers(targetLabels, matchers, TenantLabel)
seriesNames := make(map[uint64]string)
seriesLabels := labels.Labels(make([]labels.Label, 0, len(labelsToMatch)))
aggregateBySeries := seriesvolume.AggregateBySeries(aggregateBy) || aggregateBy == ""
return i.forPostings(ctx, fpFilter, from, through, matchers, func(p index.Postings) error {
var ls labels.Labels
var filterer chunk.Filterer
if i.chunkFilter != nil {
filterer = i.chunkFilter.ForRequest(ctx)
}
for p.Next() {
fp, stats, err := i.reader.ChunkStats(p.At(), int64(from), int64(through), &ls)
if err != nil {
return fmt.Errorf("series volume: %w", err)
}
// skip series that belong to different shards
if fpFilter != nil && !fpFilter.Match(model.Fingerprint(fp)) {
continue
}
if filterer != nil && filterer.ShouldFilter(ls) {
continue
}
if stats.Entries > 0 {
var labelVolumes map[string]uint64
if aggregateBySeries {
seriesLabels = seriesLabels[:0]
for _, l := range ls {
if _, ok := labelsToMatch[l.Name]; l.Name != TenantLabel && includeAll || ok {
seriesLabels = append(seriesLabels, l)
}
}
} else {
// when aggregating by labels, capture sizes for target labels if provided,
// otherwise for all intersecting labels
labelVolumes = make(map[string]uint64, len(ls))
for _, l := range ls {
if len(targetLabels) > 0 {
if _, ok := labelsToMatch[l.Name]; l.Name != TenantLabel && includeAll || ok {
labelVolumes[l.Name] += stats.KB << 10
}
} else {
if l.Name != TenantLabel {
labelVolumes[l.Name] += stats.KB << 10
}
}
}
}
// If the labels are < 1k, this does not alloc
// https://github.com/prometheus/prometheus/pull/8025
hash := seriesLabels.Hash()
if _, ok := seriesNames[hash]; !ok {
seriesNames[hash] = seriesLabels.String()
}
if aggregateBySeries {
if err = acc.AddVolume(seriesNames[hash], stats.KB<<10); err != nil {
return err
}
} else {
for label, volume := range labelVolumes {
if err = acc.AddVolume(label, volume); err != nil {
return err
}
}
}
}
}
return p.Err()
})
}