forked from grafana/loki
/
head_manager.go
668 lines (557 loc) · 17.3 KB
/
head_manager.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package tsdb
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"sync"
"time"
"github.com/cespare/xxhash"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/record"
"go.uber.org/atomic"
"github.com/grafana/loki/pkg/storage/chunk"
"github.com/grafana/loki/pkg/storage/chunk/client/util"
"github.com/grafana/loki/pkg/storage/stores/index/stats"
"github.com/grafana/loki/pkg/storage/stores/tsdb/index"
"github.com/grafana/loki/pkg/util/wal"
)
/*
period is a duration which the ingesters use to group index writes into a (WAL,TenantHeads) pair.
After each period elapses, a set of zero or more multitenant TSDB indices are built (one per
index bucket, generally 24h).
It's important to note that this cycle occurs in real time as opposed to the timestamps of
chunk entries. Index writes during the `period` may span multiple index buckets. Periods
also expose some helper functions to get the remainder-less offset integer for that period,
which we use in file creation/etc.
*/
type period time.Duration
const defaultRotationPeriod = period(15 * time.Minute)
func (p period) PeriodFor(t time.Time) int {
return int(t.UnixNano() / int64(p))
}
func (p period) TimeForPeriod(n int) time.Time {
return time.Unix(0, int64(p)*int64(n))
}
// Do not specify without bit shifting. This allows us to
// do shard index calcuations via bitwise & rather than modulos.
const defaultHeadManagerStripeSize = 1 << 7
/*
HeadManager both accepts flushed chunk writes
and exposes the index interface for multiple tenants.
It also handles updating an underlying WAL and periodically
rotates both the tenant Heads and the underlying WAL, using
the old versions to build + upload TSDB files.
On disk, it looks like:
tsdb/
v1/
# scratch directory used for temp tsdb files during build stage
scratch/
# wal directory used to store WALs being written on the ingester.
# These are eventually shipped to storage as multi-tenant TSDB files
# and compacted into per tenant indices
wal/
<timestamp>
# multitenant tsdb files which are created on the ingesters/shipped
multitenant/
<timestamp>-<ingester-name>.tsdb
per_tenant/
# post-compaction tenant tsdbs which are grouped per
# period bucket
<tenant>/
<bucket>/
index-<from>-<through>-<checksum>.tsdb
*/
type HeadManager struct {
log log.Logger
dir string
metrics *Metrics
// RLocked for all writes/reads,
// Locked before rotating heads/wal
mtx sync.RWMutex
// how often WALs should be rotated and TSDBs cut
period period
tsdbManager TSDBManager
active, prev *headWAL
shards int
activeHeads, prevHeads *tenantHeads
Index
}
func NewHeadManager(logger log.Logger, dir string, metrics *Metrics, tsdbManager TSDBManager) *HeadManager {
shards := defaultHeadManagerStripeSize
m := &HeadManager{
log: log.With(logger, "component", "tsdb-head-manager"),
dir: dir,
metrics: metrics,
tsdbManager: tsdbManager,
period: defaultRotationPeriod,
shards: shards,
}
m.Index = LazyIndex(func() (Index, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
var indices []Index
if m.prevHeads != nil {
indices = append(indices, m.prevHeads)
}
if m.activeHeads != nil {
indices = append(indices, m.activeHeads)
}
return NewMultiIndex(indices...)
})
return m
}
func (m *HeadManager) Stop() error {
m.mtx.Lock()
defer m.mtx.Unlock()
if err := m.active.Stop(); err != nil {
return err
}
return m.buildTSDBFromWAL(m.active.initialized)
}
func (m *HeadManager) Append(userID string, ls labels.Labels, chks index.ChunkMetas) error {
// TSDB doesnt need the __name__="log" convention the old chunk store index used.
// We must create a copy of the labels here to avoid mutating the existing
// labels when writing across index buckets.
b := labels.NewBuilder(ls)
b.Del(labels.MetricName)
ls = b.Labels()
m.mtx.RLock()
now := time.Now()
if m.period.PeriodFor(now) > m.period.PeriodFor(m.activeHeads.start) {
m.mtx.RUnlock()
if err := m.Rotate(now); err != nil {
return errors.Wrap(err, "rotating TSDB Head")
}
m.mtx.RLock()
}
defer m.mtx.RUnlock()
rec := m.activeHeads.Append(userID, ls, chks)
return m.active.Log(rec)
}
func (m *HeadManager) Start() error {
if err := os.RemoveAll(filepath.Join(m.dir, "scratch")); err != nil {
return errors.Wrap(err, "removing tsdb scratch dir")
}
for _, d := range managerRequiredDirs(m.dir) {
if err := util.EnsureDirectory(d); err != nil {
return errors.Wrapf(err, "ensuring required directory exists: %s", d)
}
}
walsByPeriod, err := walsByPeriod(m.dir, m.period)
if err != nil {
return err
}
level.Info(m.log).Log("msg", "loaded wals by period", "groups", len(walsByPeriod))
// Load the shipper with any previously built TSDBs
if err := m.tsdbManager.Start(); err != nil {
return errors.Wrap(err, "failed to start tsdb manager")
}
// Build any old WALs into a TSDB for the shipper
var allWALs []WALIdentifier
for _, group := range walsByPeriod {
allWALs = append(allWALs, group.wals...)
}
now := time.Now()
if err := m.tsdbManager.BuildFromWALs(
now,
allWALs,
); err != nil {
return errors.Wrap(err, "building tsdb")
}
if err := os.RemoveAll(managerWalDir(m.dir)); err != nil {
return errors.New("cleaning (removing) wal dir")
}
return m.Rotate(now)
}
func managerRequiredDirs(parent string) []string {
return []string{
managerScratchDir(parent),
managerWalDir(parent),
managerMultitenantDir(parent),
managerPerTenantDir(parent),
}
}
func managerScratchDir(parent string) string {
return filepath.Join(parent, "scratch")
}
func managerWalDir(parent string) string {
return filepath.Join(parent, "wal")
}
func managerMultitenantDir(parent string) string {
return filepath.Join(parent, "multitenant")
}
func managerPerTenantDir(parent string) string {
return filepath.Join(parent, "per_tenant")
}
func (m *HeadManager) Rotate(t time.Time) error {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.activeHeads != nil && m.period.PeriodFor(t) == m.period.PeriodFor(m.activeHeads.start) {
// no-op, we've already rotated to the desired period
return nil
}
// create new wal
nextWALPath := walPath(m.dir, t)
nextWAL, err := newHeadWAL(m.log, nextWALPath, t)
if err != nil {
return errors.Wrapf(err, "creating tsdb wal: %s during rotation", nextWALPath)
}
// create new tenant heads
nextHeads := newTenantHeads(t, m.shards, m.metrics, m.log)
stopPrev := func(s string) {
if m.prev != nil {
if err := m.prev.Stop(); err != nil {
level.Error(m.log).Log(
"msg", "failed stopping wal",
"period", m.period.PeriodFor(m.prev.initialized),
"err", err,
"wal", s,
)
}
}
}
stopPrev("previous cycle") // stop the previous wal if it hasn't been cleaned up yet
m.prev = m.active
m.prevHeads = m.activeHeads
m.active = nextWAL
m.activeHeads = nextHeads
stopPrev("freshly rotated") // stop the newly rotated-out wal
// build tsdb from rotated-out period
// TODO(owen-d): don't block Append() waiting for tsdb building. Use a work channel/etc
if m.prev != nil {
if err := m.buildTSDBFromWAL(m.prev.initialized); err != nil {
return errors.Wrap(err, "building tsdb from rotated out period")
}
}
// Now that the tsdbManager has the updated TSDBs, we can remove our references
m.prevHeads = nil
m.prev = nil
return nil
}
func (m *HeadManager) buildTSDBFromWAL(t time.Time) error {
level.Debug(m.log).Log("msg", "combining tsdb WALs")
grp, _, err := walsForPeriod(m.dir, m.period, m.period.PeriodFor(t))
if err != nil {
return errors.Wrap(err, "listing wals")
}
level.Debug(m.log).Log("msg", "listed WALs", "pd", grp.period, "n", len(grp.wals))
// TODO(owen-d): It's probably faster to build this from the *tenantHeads instead,
// but we already need to impl BuildFromWALs to ensure we can correctly build/ship
// TSDBs from orphaned WALs of previous periods during startup.
// we use the same timestamp as wal here for the filename to ensure it can't clobber
// an existing file from a previous cycle. I don't think this is possible, but
// perhaps in some unusual crashlooping it could be, so let's be safe and protect ourselves.
if err := m.tsdbManager.BuildFromWALs(t, grp.wals); err != nil {
return errors.Wrapf(err, "building TSDB from prevHeads WALs for period %d", grp.period)
}
// Now that a TSDB has been created from this group, it's safe to remove them
if err := m.removeWALGroup(grp); err != nil {
return errors.Wrapf(err, "removing prev TSDB WALs for period %d", grp.period)
}
level.Debug(m.log).Log("msg", "removing wals", "pd", grp.period, "n", len(grp.wals))
return nil
}
type WalGroup struct {
period int
wals []WALIdentifier
}
func walsByPeriod(dir string, period period) ([]WalGroup, error) {
groupsMap, err := walGroups(dir, period)
if err != nil {
return nil, err
}
res := make([]WalGroup, 0, len(groupsMap))
for _, grp := range groupsMap {
res = append(res, *grp)
}
// Ensure the earliers periods are seen first
sort.Slice(res, func(i, j int) bool {
return res[i].period < res[j].period
})
return res, nil
}
func walGroups(dir string, period period) (map[int]*WalGroup, error) {
files, err := ioutil.ReadDir(managerWalDir(dir))
if err != nil {
return nil, err
}
groupsMap := map[int]*WalGroup{}
for _, f := range files {
if id, ok := parseWALPath(f.Name()); ok {
pd := period.PeriodFor(id.ts)
grp, ok := groupsMap[pd]
if !ok {
grp = &WalGroup{
period: pd,
}
groupsMap[pd] = grp
}
grp.wals = append(grp.wals, id)
}
}
for _, grp := range groupsMap {
// Ensure the earliest wals are seen first
sort.Slice(grp.wals, func(i, j int) bool {
return grp.wals[i].ts.Before(grp.wals[j].ts)
})
}
return groupsMap, nil
}
func walsForPeriod(dir string, period period, offset int) (WalGroup, bool, error) {
groupsMap, err := walGroups(dir, period)
if err != nil {
return WalGroup{}, false, err
}
grp, ok := groupsMap[offset]
if !ok {
return WalGroup{}, false, nil
}
return *grp, true, nil
}
func (m *HeadManager) removeWALGroup(grp WalGroup) error {
for _, wal := range grp.wals {
if err := os.RemoveAll(walPath(m.dir, wal.ts)); err != nil {
return errors.Wrapf(err, "removing tsdb wal: %s", walPath(m.dir, wal.ts))
}
}
return nil
}
func walPath(parent string, t time.Time) string {
return filepath.Join(
managerWalDir(parent),
fmt.Sprintf("%d", t.Unix()),
)
}
// recoverHead recovers from all WALs belonging to some period
// and inserts it into the active *tenantHeads
func recoverHead(dir string, heads *tenantHeads, wals []WALIdentifier) error {
for _, id := range wals {
// use anonymous function for ease of cleanup
if err := func(id WALIdentifier) error {
reader, closer, err := wal.NewWalReader(walPath(dir, id.ts), -1)
if err != nil {
return err
}
defer closer.Close()
// map of users -> ref -> series.
// Keep track of which ref corresponds to which series
// for each WAL so we replay into the correct series
seriesMap := make(map[string]map[uint64]labels.Labels)
for reader.Next() {
rec := &WALRecord{}
if err := decodeWALRecord(reader.Record(), rec); err != nil {
return err
}
// labels are always written to the WAL before corresponding chunks
if len(rec.Series.Labels) > 0 {
tenant, ok := seriesMap[rec.UserID]
if !ok {
tenant = make(map[uint64]labels.Labels)
seriesMap[rec.UserID] = tenant
}
tenant[uint64(rec.Series.Ref)] = rec.Series.Labels
}
if len(rec.Chks.Chks) > 0 {
tenant, ok := seriesMap[rec.UserID]
if !ok {
return errors.New("found tsdb chunk metas without user in WAL replay")
}
ls, ok := tenant[rec.Chks.Ref]
if !ok {
return errors.New("found tsdb chunk metas without series in WAL replay")
}
_ = heads.Append(rec.UserID, ls, rec.Chks.Chks)
}
}
return reader.Err()
}(id); err != nil {
return errors.Wrap(
err,
"error recovering from TSDB WAL",
)
}
}
return nil
}
type WALIdentifier struct {
ts time.Time
}
func parseWALPath(p string) (id WALIdentifier, ok bool) {
ts, err := strconv.Atoi(p)
if err != nil {
return
}
return WALIdentifier{
ts: time.Unix(int64(ts), 0),
}, true
}
type tenantHeads struct {
mint, maxt atomic.Int64 // easy lookup for Bounds() impl
start time.Time
shards int
locks []sync.RWMutex
tenants []map[string]*Head
log log.Logger
chunkFilter chunk.RequestChunkFilterer
metrics *Metrics
}
func newTenantHeads(start time.Time, shards int, metrics *Metrics, logger log.Logger) *tenantHeads {
res := &tenantHeads{
start: start,
shards: shards,
locks: make([]sync.RWMutex, shards),
tenants: make([]map[string]*Head, shards),
log: log.With(logger, "component", "tenant-heads"),
metrics: metrics,
}
for i := range res.tenants {
res.tenants[i] = make(map[string]*Head)
}
return res
}
func (t *tenantHeads) Append(userID string, ls labels.Labels, chks index.ChunkMetas) *WALRecord {
var mint, maxt int64
for _, chk := range chks {
if chk.MinTime < mint || mint == 0 {
mint = chk.MinTime
}
if chk.MaxTime > maxt {
maxt = chk.MaxTime
}
}
updateMintMaxt(mint, maxt, &t.mint, &t.maxt)
head := t.getOrCreateTenantHead(userID)
newStream, refID := head.Append(ls, chks)
rec := &WALRecord{
UserID: userID,
Chks: ChunkMetasRecord{
Ref: refID,
Chks: chks,
},
}
if newStream {
rec.Series = record.RefSeries{
Ref: chunks.HeadSeriesRef(refID),
Labels: ls,
}
}
return rec
}
func (t *tenantHeads) getOrCreateTenantHead(userID string) *Head {
idx := t.shardForTenant(userID)
mtx := &t.locks[idx]
// return existing tenant head if it exists
mtx.RLock()
head, ok := t.tenants[idx][userID]
mtx.RUnlock()
if ok {
return head
}
mtx.Lock()
defer mtx.Unlock()
// tenant head was not found before.
// Check again if a competing request created the head already, don't create it again if so.
head, ok = t.tenants[idx][userID]
if !ok {
head = NewHead(userID, t.metrics, t.log)
t.tenants[idx][userID] = head
}
return head
}
func (t *tenantHeads) shardForTenant(userID string) uint64 {
return xxhash.Sum64String(userID) & uint64(t.shards-1)
}
func (t *tenantHeads) Close() error { return nil }
func (t *tenantHeads) SetChunkFilterer(chunkFilter chunk.RequestChunkFilterer) {
t.chunkFilter = chunkFilter
}
func (t *tenantHeads) Bounds() (model.Time, model.Time) {
return model.Time(t.mint.Load()), model.Time(t.maxt.Load())
}
func (t *tenantHeads) tenantIndex(userID string, from, through model.Time) (idx Index, ok bool) {
i := t.shardForTenant(userID)
t.locks[i].RLock()
defer t.locks[i].RUnlock()
tenant, ok := t.tenants[i][userID]
if !ok {
return
}
idx = NewTSDBIndex(tenant.indexRange(int64(from), int64(through)))
if t.chunkFilter != nil {
idx.SetChunkFilterer(t.chunkFilter)
}
return idx, true
}
func (t *tenantHeads) GetChunkRefs(ctx context.Context, userID string, from, through model.Time, res []ChunkRef, shard *index.ShardAnnotation, matchers ...*labels.Matcher) ([]ChunkRef, error) {
idx, ok := t.tenantIndex(userID, from, through)
if !ok {
return nil, nil
}
return idx.GetChunkRefs(ctx, userID, from, through, nil, shard, matchers...)
}
// Series follows the same semantics regarding the passed slice and shard as GetChunkRefs.
func (t *tenantHeads) Series(ctx context.Context, userID string, from, through model.Time, res []Series, shard *index.ShardAnnotation, matchers ...*labels.Matcher) ([]Series, error) {
idx, ok := t.tenantIndex(userID, from, through)
if !ok {
return nil, nil
}
return idx.Series(ctx, userID, from, through, nil, shard, matchers...)
}
func (t *tenantHeads) LabelNames(ctx context.Context, userID string, from, through model.Time, matchers ...*labels.Matcher) ([]string, error) {
idx, ok := t.tenantIndex(userID, from, through)
if !ok {
return nil, nil
}
return idx.LabelNames(ctx, userID, from, through, matchers...)
}
func (t *tenantHeads) LabelValues(ctx context.Context, userID string, from, through model.Time, name string, matchers ...*labels.Matcher) ([]string, error) {
idx, ok := t.tenantIndex(userID, from, through)
if !ok {
return nil, nil
}
return idx.LabelValues(ctx, userID, from, through, name, matchers...)
}
func (t *tenantHeads) Stats(ctx context.Context, userID string, from, through model.Time, blooms *stats.Blooms, shard *index.ShardAnnotation, matchers ...*labels.Matcher) (*stats.Blooms, error) {
idx, ok := t.tenantIndex(userID, from, through)
if !ok {
return blooms, nil
}
return idx.Stats(ctx, userID, from, through, blooms, shard, matchers...)
}
// helper only used in building TSDBs
func (t *tenantHeads) forAll(fn func(user string, ls labels.Labels, chks index.ChunkMetas) error) error {
for i, shard := range t.tenants {
t.locks[i].RLock()
defer t.locks[i].RUnlock()
for user, tenant := range shard {
idx := tenant.Index()
ps, err := postingsForMatcher(idx, nil, labels.MustNewMatcher(labels.MatchEqual, "", ""))
if err != nil {
return err
}
for ps.Next() {
var (
ls labels.Labels
chks []index.ChunkMeta
)
_, err := idx.Series(ps.At(), &ls, &chks)
if err != nil {
return errors.Wrapf(err, "iterating postings for tenant: %s", user)
}
if err := fn(user, ls, chks); err != nil {
return err
}
}
}
}
return nil
}