-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathstore.go
640 lines (543 loc) · 17 KB
/
store.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package bloomshipper
import (
"context"
"fmt"
"path"
"sort"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/storage"
v1 "github.com/grafana/loki/v3/pkg/storage/bloom/v1"
"github.com/grafana/loki/v3/pkg/storage/chunk/cache"
"github.com/grafana/loki/v3/pkg/storage/chunk/client"
"github.com/grafana/loki/v3/pkg/storage/chunk/client/util"
"github.com/grafana/loki/v3/pkg/storage/config"
"github.com/grafana/loki/v3/pkg/util/constants"
"github.com/grafana/loki/v3/pkg/util/spanlogger"
)
var (
errNoStore = errors.New("no store found for time")
)
type Store interface {
ResolveMetas(ctx context.Context, params MetaSearchParams) ([][]MetaRef, []*Fetcher, error)
FetchMetas(ctx context.Context, params MetaSearchParams) ([]Meta, error)
FetchBlocks(ctx context.Context, refs []BlockRef, opts ...FetchOption) ([]*CloseableBlockQuerier, error)
TenantFilesForInterval(
ctx context.Context, interval Interval,
filter func(tenant string, object client.StorageObject) bool,
) (map[string][]client.StorageObject, error)
Fetcher(ts model.Time) (*Fetcher, error)
Client(ts model.Time) (Client, error)
Stop()
}
type StoreWithMetrics interface {
Store
BloomMetrics() *v1.Metrics
}
type bloomStoreConfig struct {
workingDirs []string
numWorkers int
maxBloomPageSize int
}
// Compiler check to ensure bloomStoreEntry implements the Store interface
var _ Store = &bloomStoreEntry{}
type bloomStoreEntry struct {
start model.Time
cfg config.PeriodConfig
objectClient client.ObjectClient
bloomClient Client
fetcher *Fetcher
defaultKeyResolver // TODO(owen-d): impl schema aware resolvers
}
// ResolveMetas implements store.
func (b *bloomStoreEntry) ResolveMetas(ctx context.Context, params MetaSearchParams) ([][]MetaRef, []*Fetcher, error) {
var refs []MetaRef
tables := tablesForRange(b.cfg, params.Interval)
for _, table := range tables {
prefix := path.Join(rootFolder, table, params.TenantID, metasFolder)
level.Debug(b.fetcher.logger).Log(
"msg", "listing metas",
"store", b.cfg.From,
"table", table,
"tenant", params.TenantID,
"prefix", prefix,
)
list, _, err := b.objectClient.List(ctx, prefix, "")
if err != nil {
return nil, nil, fmt.Errorf("error listing metas under prefix [%s]: %w", prefix, err)
}
for _, object := range list {
metaRef, err := b.ParseMetaKey(key(object.Key))
if err != nil {
return nil, nil, err
}
// LIST calls return keys in lexicographic order.
// Since fingerprints are the first part of the path,
// we can stop iterating once we find an item greater
// than the keyspace we're looking for
if params.Keyspace.Cmp(metaRef.Bounds.Min) == v1.After {
break
}
// Only check keyspace for now, because we don't have start/end timestamps in the refs
if !params.Keyspace.Overlaps(metaRef.Bounds) {
continue
}
refs = append(refs, metaRef)
}
}
// return empty metaRefs/fetchers if there are no refs
if len(refs) == 0 {
return [][]MetaRef{}, []*Fetcher{}, nil
}
return [][]MetaRef{refs}, []*Fetcher{b.fetcher}, nil
}
// FilterMetasOverlappingBounds filters metas that are within the given bounds.
// the input metas are expected to be sorted by fingerprint.
func FilterMetasOverlappingBounds(metas []Meta, bounds v1.FingerprintBounds) []Meta {
withinBounds := make([]Meta, 0, len(metas))
for _, meta := range metas {
// We can stop iterating once we find an item greater
// than the keyspace we're looking for
if bounds.Cmp(meta.Bounds.Min) == v1.After {
break
}
// Only check keyspace for now, because we don't have start/end timestamps in the refs
if !bounds.Overlaps(meta.Bounds) {
continue
}
withinBounds = append(withinBounds, meta)
}
return withinBounds
}
// FetchMetas implements store.
func (b *bloomStoreEntry) FetchMetas(ctx context.Context, params MetaSearchParams) ([]Meta, error) {
logger := spanlogger.FromContext(ctx)
resolverStart := time.Now()
metaRefs, fetchers, err := b.ResolveMetas(ctx, params)
resolverDuration := time.Since(resolverStart)
if err != nil {
return nil, err
}
if len(metaRefs) != len(fetchers) {
return nil, errors.New("metaRefs and fetchers have unequal length")
}
var metaCt int
for i := range metaRefs {
metaCt += len(metaRefs[i])
}
logger.LogKV(
"msg", "resolved metas",
"metas", metaCt,
"duration", resolverDuration,
)
var metas []Meta
for i := range fetchers {
res, err := fetchers[i].FetchMetas(ctx, metaRefs[i])
if err != nil {
return nil, err
}
metas = append(metas, res...)
}
return metas, nil
}
// FetchBlocks implements Store.
func (b *bloomStoreEntry) FetchBlocks(ctx context.Context, refs []BlockRef, opts ...FetchOption) ([]*CloseableBlockQuerier, error) {
return b.fetcher.FetchBlocks(ctx, refs, opts...)
}
func (b *bloomStoreEntry) TenantFilesForInterval(
ctx context.Context,
interval Interval,
filter func(tenant string, object client.StorageObject) bool,
) (map[string][]client.StorageObject, error) {
tables := tablesForRange(b.cfg, interval)
if len(tables) == 0 {
return nil, nil
}
tenants := make(map[string][]client.StorageObject, 100)
for _, table := range tables {
prefix := path.Join(rootFolder, table)
level.Debug(b.fetcher.logger).Log(
"msg", "listing tenants",
"store", b.cfg.From,
"table", table,
"prefix", prefix,
)
objects, _, err := b.objectClient.List(ctx, prefix, "")
if err != nil {
if b.objectClient.IsObjectNotFoundErr(err) {
continue
}
return nil, fmt.Errorf("error listing tenants under prefix [%s]: %w", prefix, err)
}
if len(objects) == 0 {
continue
}
// Sort objects by the key to ensure keys are sorted by tenant.
cmpObj := func(a, b client.StorageObject) int {
if a.Key < b.Key {
return -1
}
if a.Key > b.Key {
return 1
}
return 0
}
if !slices.IsSortedFunc(objects, cmpObj) {
slices.SortFunc(objects, cmpObj)
}
for i := 0; i < len(objects); i++ {
tenant, err := b.TenantPrefix(key(objects[i].Key))
if err != nil {
return nil, fmt.Errorf("error parsing tenant key [%s]: %w", objects[i].Key, err)
}
// Search next object with different tenant
var j int
for j = i + 1; j < len(objects); j++ {
nextTenant, err := b.TenantPrefix(key(objects[j].Key))
if err != nil {
return nil, fmt.Errorf("error parsing tenant key [%s]: %w", objects[i].Key, err)
}
if nextTenant != tenant {
break
}
}
if _, ok := tenants[tenant]; !ok {
tenants[tenant] = nil // Initialize tenant with empty slice
}
if filter != nil && !filter(tenant, objects[i]) {
continue
}
// Add all objects for this tenant
tenants[tenant] = append(tenants[tenant], objects[i:j]...)
i = j - 1 // -1 because the loop will increment i by 1
}
}
return tenants, nil
}
// Fetcher implements Store.
func (b *bloomStoreEntry) Fetcher(_ model.Time) (*Fetcher, error) {
return b.fetcher, nil
}
// Client implements Store.
func (b *bloomStoreEntry) Client(_ model.Time) (Client, error) {
return b.bloomClient, nil
}
// Stop implements Store.
func (b bloomStoreEntry) Stop() {
b.bloomClient.Stop()
b.fetcher.Close()
}
// Compiler check to ensure BloomStore implements the Store interface
var _ StoreWithMetrics = &BloomStore{}
type BloomStore struct {
stores []*bloomStoreEntry
storageConfig storage.Config
metrics *storeMetrics
bloomMetrics *v1.Metrics
logger log.Logger
defaultKeyResolver // TODO(owen-d): impl schema aware resolvers
}
func NewBloomStore(
periodicConfigs []config.PeriodConfig,
storageConfig storage.Config,
clientMetrics storage.ClientMetrics,
metasCache cache.Cache,
blocksCache Cache,
reg prometheus.Registerer,
logger log.Logger,
) (*BloomStore, error) {
store := &BloomStore{
storageConfig: storageConfig,
metrics: newStoreMetrics(reg, constants.Loki, "bloom_store"),
bloomMetrics: v1.NewMetrics(reg),
logger: logger,
}
if metasCache == nil {
metasCache = cache.NewNoopCache()
}
if blocksCache == nil {
return nil, errors.New("no blocks cache")
}
// sort by From time
sort.Slice(periodicConfigs, func(i, j int) bool {
return periodicConfigs[i].From.Time.Before(periodicConfigs[i].From.Time)
})
// TODO(chaudum): Remove wrapper
cfg := bloomStoreConfig{
workingDirs: storageConfig.BloomShipperConfig.WorkingDirectory,
numWorkers: storageConfig.BloomShipperConfig.DownloadParallelism,
maxBloomPageSize: int(storageConfig.BloomShipperConfig.MaxQueryPageSize),
}
for _, wd := range cfg.workingDirs {
if err := util.EnsureDirectory(wd); err != nil {
return nil, errors.Wrapf(err, "failed to create working directory for bloom store: '%s'", wd)
}
if err := util.RequirePermissions(wd, 0o700); err != nil {
return nil, errors.Wrapf(err, "insufficient permissions on working directory for bloom store: '%s'", wd)
}
}
for _, periodicConfig := range periodicConfigs {
objectClient, err := storage.NewObjectClient(periodicConfig.ObjectType, storageConfig, clientMetrics)
if err != nil {
return nil, errors.Wrapf(err, "creating object client for period %s", periodicConfig.From)
}
if storageConfig.BloomShipperConfig.CacheListOps {
objectClient = newCachedListOpObjectClient(objectClient, 5*time.Minute, 10*time.Second)
}
bloomClient, err := NewBloomClient(cfg, objectClient, logger)
if err != nil {
return nil, errors.Wrapf(err, "creating bloom client for period %s", periodicConfig.From)
}
regWithLabels := prometheus.WrapRegistererWith(prometheus.Labels{"store": periodicConfig.From.String()}, reg)
fetcher, err := NewFetcher(cfg, bloomClient, metasCache, blocksCache, regWithLabels, logger, store.bloomMetrics)
if err != nil {
return nil, errors.Wrapf(err, "creating fetcher for period %s", periodicConfig.From)
}
store.stores = append(store.stores, &bloomStoreEntry{
start: periodicConfig.From.Time,
cfg: periodicConfig,
objectClient: objectClient,
bloomClient: bloomClient,
fetcher: fetcher,
})
}
return store, nil
}
func (b *BloomStore) BloomMetrics() *v1.Metrics {
return b.bloomMetrics
}
// Impements KeyResolver
func (b *BloomStore) Meta(ref MetaRef) (loc Location) {
_ = b.storeDo(ref.StartTimestamp, func(s *bloomStoreEntry) error {
loc = s.Meta(ref)
return nil
})
// NB(owen-d): should not happen unless a ref is requested outside the store's accepted range.
// This should be prevented during query validation
if loc == nil {
loc = defaultKeyResolver{}.Meta(ref)
}
return
}
// Impements KeyResolver
func (b *BloomStore) Block(ref BlockRef) (loc Location) {
_ = b.storeDo(ref.StartTimestamp, func(s *bloomStoreEntry) error {
loc = s.Block(ref)
return nil
})
// NB(owen-d): should not happen unless a ref is requested outside the store's accepted range.
// This should be prevented during query validation
if loc == nil {
loc = defaultKeyResolver{}.Block(ref)
}
return
}
func (b *BloomStore) TenantFilesForInterval(
ctx context.Context,
interval Interval,
filter func(tenant string, object client.StorageObject) bool,
) (map[string][]client.StorageObject, error) {
var allTenants map[string][]client.StorageObject
err := b.forStores(ctx, interval, func(innerCtx context.Context, interval Interval, store Store) error {
tenants, err := store.TenantFilesForInterval(innerCtx, interval, filter)
if err != nil {
return err
}
if allTenants == nil {
allTenants = tenants
return nil
}
for tenant, files := range tenants {
allTenants[tenant] = append(allTenants[tenant], files...)
}
return nil
})
return allTenants, err
}
// Fetcher implements Store.
func (b *BloomStore) Fetcher(ts model.Time) (*Fetcher, error) {
if store := b.getStore(ts); store != nil {
return store.Fetcher(ts)
}
return nil, errNoStore
}
// Client implements Store.
func (b *BloomStore) Client(ts model.Time) (Client, error) {
if store := b.getStore(ts); store != nil {
return store.Client(ts)
}
return nil, errNoStore
}
// ResolveMetas implements Store.
func (b *BloomStore) ResolveMetas(ctx context.Context, params MetaSearchParams) ([][]MetaRef, []*Fetcher, error) {
refs := make([][]MetaRef, 0, len(b.stores))
fetchers := make([]*Fetcher, 0, len(b.stores))
err := b.forStores(ctx, params.Interval, func(innerCtx context.Context, interval Interval, store Store) error {
newParams := params
newParams.Interval = interval
metas, fetcher, err := store.ResolveMetas(innerCtx, newParams)
if err != nil {
return err
}
if len(metas) > 0 {
// only append if there are any results
refs = append(refs, metas...)
fetchers = append(fetchers, fetcher...)
}
return nil
})
return refs, fetchers, err
}
// FetchMetas implements Store.
func (b *BloomStore) FetchMetas(ctx context.Context, params MetaSearchParams) ([]Meta, error) {
metaRefs, fetchers, err := b.ResolveMetas(ctx, params)
if err != nil {
return nil, err
}
if len(metaRefs) != len(fetchers) {
return nil, errors.New("metaRefs and fetchers have unequal length")
}
metas := []Meta{}
for i := range fetchers {
res, err := fetchers[i].FetchMetas(ctx, metaRefs[i])
if err != nil {
return nil, err
}
if len(res) > 0 {
metas = append(metas, res...)
}
}
return metas, nil
}
// partitionBlocksByFetcher returns a slice of BlockRefs for each fetcher
func (b *BloomStore) partitionBlocksByFetcher(blocks []BlockRef) (refs [][]BlockRef, fetchers []*Fetcher) {
for i := len(b.stores) - 1; i >= 0; i-- {
s := b.stores[i]
from, through := s.start, model.Latest
if i < len(b.stores)-1 {
through = b.stores[i+1].start
}
var res []BlockRef
for _, ref := range blocks {
if ref.StartTimestamp >= from && ref.StartTimestamp < through {
res = append(res, ref)
}
}
if len(res) > 0 {
refs = append(refs, res)
fetchers = append(fetchers, s.fetcher)
}
}
return refs, fetchers
}
// FetchBlocks implements Store.
func (b *BloomStore) FetchBlocks(ctx context.Context, blocks []BlockRef, opts ...FetchOption) ([]*CloseableBlockQuerier, error) {
refs, fetchers := b.partitionBlocksByFetcher(blocks)
results := make([]*CloseableBlockQuerier, 0, len(blocks))
for i := range fetchers {
res, err := fetchers[i].FetchBlocks(ctx, refs[i], opts...)
if err != nil {
return results, err
}
results = append(results, res...)
}
// sort responses (results []*CloseableBlockQuerier) based on requests (blocks []BlockRef)
sortBlocks(results, blocks)
return results, nil
}
func sortBlocks(bqs []*CloseableBlockQuerier, refs []BlockRef) {
tmp := make([]*CloseableBlockQuerier, len(refs))
for _, bq := range bqs {
if bq == nil {
// ignore responses with nil values
continue
}
idx := slices.Index(refs, bq.BlockRef)
if idx < 0 {
// not found
// should not happen in the context of sorting responses based on requests
continue
}
tmp[idx] = bq
}
copy(bqs, tmp)
}
// Stop implements Store.
func (b *BloomStore) Stop() {
for _, s := range b.stores {
s.Stop()
}
}
func (b *BloomStore) getStore(ts model.Time) *bloomStoreEntry {
// find the schema with the lowest start _after_ tm
j := sort.Search(len(b.stores), func(j int) bool {
return b.stores[j].start > ts
})
// reduce it by 1 because we want a schema with start <= tm
j--
if 0 <= j && j < len(b.stores) {
return b.stores[j]
}
// should in theory never happen
return nil
}
func (b *BloomStore) storeDo(ts model.Time, f func(s *bloomStoreEntry) error) error {
if store := b.getStore(ts); store != nil {
return f(store)
}
return fmt.Errorf("no store found for timestamp %s", ts.Time())
}
func (b *BloomStore) forStores(ctx context.Context, interval Interval, f func(innerCtx context.Context, interval Interval, store Store) error) error {
if len(b.stores) == 0 {
return nil
}
from, through := interval.Start, interval.End
// first, find the schema with the highest start _before or at_ from
i := sort.Search(len(b.stores), func(i int) bool {
return b.stores[i].start > from
})
if i > 0 {
i--
} else {
// This could happen if we get passed a sample from before 1970.
i = 0
from = b.stores[0].start
}
// next, find the schema with the lowest start _after_ through
j := sort.Search(len(b.stores), func(j int) bool {
return b.stores[j].start > through
})
start := from
for ; i < j; i++ {
nextSchemaStarts := model.Latest
if i+1 < len(b.stores) {
nextSchemaStarts = b.stores[i+1].start
}
end := min(through, nextSchemaStarts-1)
err := f(ctx, NewInterval(start, end), b.stores[i])
if err != nil {
return err
}
start = nextSchemaStarts
}
return nil
}
func tablesForRange(periodConfig config.PeriodConfig, interval Interval) []string {
step := int64(periodConfig.IndexTables.Period.Seconds())
lower := interval.Start.Unix() / step
upper := interval.End.Unix() / step
// end is exclusive, so if it's on a step boundary, we don't want to include it
if interval.End.Unix()%step == 0 {
upper--
}
tables := make([]string, 0, 1+upper-lower)
for i := lower; i <= upper; i++ {
tables = append(tables, fmt.Sprintf("%s%d", periodConfig.IndexTables.Prefix, i))
}
return tables
}