-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathflush.go
553 lines (475 loc) · 16.8 KB
/
flush.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
package ingester
import (
"bytes"
"errors"
"fmt"
"net/http"
"sync"
"time"
"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/backoff"
"github.com/grafana/dskit/ring"
"github.com/grafana/dskit/tenant"
"github.com/grafana/dskit/user"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"golang.org/x/net/context"
"golang.org/x/time/rate"
"github.com/grafana/loki/v3/pkg/chunkenc"
"github.com/grafana/loki/v3/pkg/storage/chunk"
"github.com/grafana/loki/v3/pkg/util"
util_log "github.com/grafana/loki/v3/pkg/util/log"
)
const (
// Backoff for retrying 'immediate' flushes. Only counts for queue
// position, not wallclock time.
flushBackoff = 1 * time.Second
// Lower bound on flushes per check period for rate-limiter
minFlushes = 100
nameLabel = "__name__"
logsValue = "logs"
flushReasonIdle = "idle"
flushReasonMaxAge = "max_age"
flushReasonForced = "forced"
flushReasonNotOwned = "not_owned"
flushReasonFull = "full"
flushReasonSynced = "synced"
)
// I don't know if this needs to be private but I only needed it in this package.
type flushReasonCounter struct {
flushReasonIdle int
flushReasonMaxAge int
flushReasonForced int
flushReasonNotOwned int
flushReasonFull int
flushReasonSynced int
}
func (f *flushReasonCounter) Log() []interface{} {
// return counters only if they are non zero
var log []interface{}
if f.flushReasonIdle > 0 {
log = append(log, "idle", f.flushReasonIdle)
}
if f.flushReasonMaxAge > 0 {
log = append(log, "max_age", f.flushReasonMaxAge)
}
if f.flushReasonForced > 0 {
log = append(log, "forced", f.flushReasonForced)
}
if f.flushReasonNotOwned > 0 {
log = append(log, "not_owned", f.flushReasonNotOwned)
}
if f.flushReasonFull > 0 {
log = append(log, "full", f.flushReasonFull)
}
if f.flushReasonSynced > 0 {
log = append(log, "synced", f.flushReasonSynced)
}
return log
}
func (f *flushReasonCounter) IncrementForReason(reason string) error {
switch reason {
case flushReasonIdle:
f.flushReasonIdle++
case flushReasonMaxAge:
f.flushReasonMaxAge++
case flushReasonForced:
f.flushReasonForced++
case flushReasonNotOwned:
f.flushReasonNotOwned++
case flushReasonFull:
f.flushReasonFull++
case flushReasonSynced:
f.flushReasonSynced++
default:
return fmt.Errorf("unknown reason: %s", reason)
}
return nil
}
// Note: this is called both during the WAL replay (zero or more times)
// and then after replay as well.
func (i *Ingester) InitFlushQueues() {
i.flushQueuesDone.Add(i.cfg.ConcurrentFlushes)
for j := 0; j < i.cfg.ConcurrentFlushes; j++ {
i.flushQueues[j] = util.NewPriorityQueue(i.metrics.flushQueueLength)
go i.flushLoop(j)
}
}
// Flush implements ring.FlushTransferer
// Flush triggers a flush of all the chunks and closes the flush queues.
// Called from the Lifecycler as part of the ingester shutdown.
func (i *Ingester) Flush() {
i.flush(true)
}
// TransferOut implements ring.FlushTransferer
// Noop implementation because ingesters have a WAL now that does not require transferring chunks any more.
// We return ErrTransferDisabled to indicate that we don't support transfers, and therefore we may flush on shutdown if configured to do so.
func (i *Ingester) TransferOut(_ context.Context) error {
return ring.ErrTransferDisabled
}
func (i *Ingester) flush(mayRemoveStreams bool) {
i.sweepUsers(true, mayRemoveStreams)
// Close the flush queues, to unblock waiting workers.
for _, flushQueue := range i.flushQueues {
flushQueue.Close()
}
i.flushQueuesDone.Wait()
level.Debug(i.logger).Log("msg", "flush queues have drained")
}
// FlushHandler triggers a flush of all in memory chunks. Mainly used for
// local testing.
func (i *Ingester) FlushHandler(w http.ResponseWriter, _ *http.Request) {
i.sweepUsers(true, true)
w.WriteHeader(http.StatusNoContent)
}
type flushOp struct {
from model.Time
userID string
fp model.Fingerprint
immediate bool
}
func (o *flushOp) Key() string {
return fmt.Sprintf("%s-%s-%v", o.userID, o.fp, o.immediate)
}
func (o *flushOp) Priority() int64 {
return -int64(o.from)
}
// sweepUsers periodically schedules series for flushing and garbage collects users with no streams
func (i *Ingester) sweepUsers(immediate, mayRemoveStreams bool) {
instances := i.getInstances()
for _, instance := range instances {
i.sweepInstance(instance, immediate, mayRemoveStreams)
}
i.setFlushRate()
}
func (i *Ingester) sweepInstance(instance *instance, immediate, mayRemoveStreams bool) {
_ = instance.streams.ForEach(func(s *stream) (bool, error) {
i.sweepStream(instance, s, immediate)
i.removeFlushedChunks(instance, s, mayRemoveStreams)
return true, nil
})
}
func (i *Ingester) sweepStream(instance *instance, stream *stream, immediate bool) {
stream.chunkMtx.RLock()
defer stream.chunkMtx.RUnlock()
if len(stream.chunks) == 0 {
return
}
lastChunk := stream.chunks[len(stream.chunks)-1]
shouldFlush, _ := i.shouldFlushChunk(&lastChunk)
if len(stream.chunks) == 1 && !immediate && !shouldFlush && !instance.ownedStreamsSvc.isStreamNotOwned(stream.fp) {
return
}
flushQueueIndex := int(uint64(stream.fp) % uint64(i.cfg.ConcurrentFlushes))
firstTime, _ := stream.chunks[0].chunk.Bounds()
i.flushQueues[flushQueueIndex].Enqueue(&flushOp{
model.TimeFromUnixNano(firstTime.UnixNano()), instance.instanceID,
stream.fp, immediate,
})
}
// Compute a rate such to spread calls to the store over nearly all of the flush period,
// for example if we have 600 items in the queue and period 1 min we will send 10.5 per second.
// Note if the store can't keep up with this rate then it doesn't make any difference.
func (i *Ingester) setFlushRate() {
totalQueueLength := 0
for _, q := range i.flushQueues {
totalQueueLength += q.Length()
}
const jitter = 1.05 // aim to finish a little bit before the end of the period
flushesPerSecond := float64(totalQueueLength) / i.cfg.FlushCheckPeriod.Seconds() * jitter
// Avoid going very slowly with tiny queues
if flushesPerSecond*i.cfg.FlushCheckPeriod.Seconds() < minFlushes {
flushesPerSecond = minFlushes / i.cfg.FlushCheckPeriod.Seconds()
}
level.Debug(util_log.Logger).Log("msg", "computed flush rate", "rate", flushesPerSecond)
i.flushRateLimiter.SetLimit(rate.Limit(flushesPerSecond))
}
func (i *Ingester) flushLoop(j int) {
l := log.With(i.logger, "loop", j)
defer func() {
level.Debug(l).Log("msg", "Ingester.flushLoop() exited")
i.flushQueuesDone.Done()
}()
for {
o := i.flushQueues[j].Dequeue()
if o == nil {
return
}
op := o.(*flushOp)
if !op.immediate {
_ = i.flushRateLimiter.Wait(context.Background())
}
m := util_log.WithUserID(op.userID, l)
err := i.flushOp(m, op)
if err != nil {
level.Error(m).Log("msg", "failed to flush", "err", err)
}
// If we're exiting & we failed to flush, put the failed operation
// back in the queue at a later point.
if op.immediate && err != nil {
op.from = op.from.Add(flushBackoff)
i.flushQueues[j].Enqueue(op)
}
}
}
func (i *Ingester) flushOp(l log.Logger, op *flushOp) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
b := backoff.New(ctx, i.cfg.FlushOpBackoff)
for b.Ongoing() {
err := i.flushUserSeries(ctx, op.userID, op.fp, op.immediate)
if err == nil {
break
}
level.Error(l).Log("msg", "failed to flush", "retries", b.NumRetries(), "err", err)
b.Wait()
}
return b.Err()
}
func (i *Ingester) flushUserSeries(ctx context.Context, userID string, fp model.Fingerprint, immediate bool) error {
instance, ok := i.getInstanceByID(userID)
if !ok {
return nil
}
chunks, labels, chunkMtx := i.collectChunksToFlush(instance, fp, immediate)
if len(chunks) < 1 {
return nil
}
totalCompressedSize := 0
totalUncompressedSize := 0
frc := flushReasonCounter{}
for _, c := range chunks {
totalCompressedSize += c.chunk.CompressedSize()
totalUncompressedSize += c.chunk.UncompressedSize()
err := frc.IncrementForReason(c.reason)
if err != nil {
level.Error(i.logger).Log("msg", "error incrementing flush reason", "err", err)
}
}
lbs := labels.String()
logValues := make([]interface{}, 0, 35)
logValues = append(logValues,
"msg", "flushing stream",
"user", userID,
"fp", fp,
"immediate", immediate,
"num_chunks", len(chunks),
"total_comp", humanize.Bytes(uint64(totalCompressedSize)),
"avg_comp", humanize.Bytes(uint64(totalCompressedSize/len(chunks))),
"total_uncomp", humanize.Bytes(uint64(totalUncompressedSize)),
"avg_uncomp", humanize.Bytes(uint64(totalUncompressedSize/len(chunks))))
logValues = append(logValues, frc.Log()...)
logValues = append(logValues, "labels", lbs)
level.Info(i.logger).Log(logValues...)
ctx = user.InjectOrgID(ctx, userID)
ctx, cancelFunc := context.WithTimeout(ctx, i.cfg.FlushOpTimeout)
defer cancelFunc()
err := i.flushChunks(ctx, fp, labels, chunks, chunkMtx)
if err != nil {
return fmt.Errorf("failed to flush chunks: %w, num_chunks: %d, labels: %s", err, len(chunks), lbs)
}
return nil
}
func (i *Ingester) collectChunksToFlush(instance *instance, fp model.Fingerprint, immediate bool) ([]*chunkDesc, labels.Labels, *sync.RWMutex) {
var stream *stream
var ok bool
stream, ok = instance.streams.LoadByFP(fp)
if !ok {
return nil, nil, nil
}
stream.chunkMtx.Lock()
defer stream.chunkMtx.Unlock()
notOwnedStream := instance.ownedStreamsSvc.isStreamNotOwned(fp)
var result []*chunkDesc
for j := range stream.chunks {
shouldFlush, reason := i.shouldFlushChunk(&stream.chunks[j])
if !shouldFlush && notOwnedStream {
shouldFlush, reason = true, flushReasonNotOwned
}
if immediate || shouldFlush {
// Ensure no more writes happen to this chunk.
if !stream.chunks[j].closed {
stream.chunks[j].closed = true
}
// Flush this chunk if it hasn't already been successfully flushed.
if stream.chunks[j].flushed.IsZero() {
if immediate {
reason = flushReasonForced
}
stream.chunks[j].reason = reason
result = append(result, &stream.chunks[j])
}
}
}
return result, stream.labels, &stream.chunkMtx
}
func (i *Ingester) shouldFlushChunk(chunk *chunkDesc) (bool, string) {
// Append should close the chunk when the a new one is added.
if chunk.closed {
if chunk.synced {
return true, flushReasonSynced
}
return true, flushReasonFull
}
if time.Since(chunk.lastUpdated) > i.cfg.MaxChunkIdle {
return true, flushReasonIdle
}
if from, to := chunk.chunk.Bounds(); to.Sub(from) > i.cfg.MaxChunkAge {
return true, flushReasonMaxAge
}
return false, ""
}
func (i *Ingester) removeFlushedChunks(instance *instance, stream *stream, mayRemoveStream bool) {
now := time.Now()
stream.chunkMtx.Lock()
defer stream.chunkMtx.Unlock()
prevNumChunks := len(stream.chunks)
var subtracted int
for len(stream.chunks) > 0 {
if stream.chunks[0].flushed.IsZero() || now.Sub(stream.chunks[0].flushed) < i.cfg.RetainPeriod {
break
}
subtracted += stream.chunks[0].chunk.UncompressedSize()
stream.chunks[0].chunk = nil // erase reference so the chunk can be garbage-collected
stream.chunks = stream.chunks[1:]
}
i.metrics.memoryChunks.Sub(float64(prevNumChunks - len(stream.chunks)))
// Signal how much data has been flushed to lessen any WAL replay pressure.
i.replayController.Sub(int64(subtracted))
if mayRemoveStream && len(stream.chunks) == 0 {
// Unlock first, then lock inside streams' lock to prevent deadlock
stream.chunkMtx.Unlock()
// Only lock streamsMap when it's needed to remove a stream
instance.streams.WithLock(func() {
stream.chunkMtx.Lock()
// Double check length
if len(stream.chunks) == 0 {
instance.removeStream(stream)
}
})
}
}
// flushChunks iterates over given chunkDescs, derives chunk.Chunk from them and flush them to the store, one at a time.
//
// If a chunk fails to be flushed, this operation is reinserted in the queue. Since previously flushed chunks
// are marked as flushed, they shouldn't be flushed again.
// It has to close given chunks to have have the head block included.
func (i *Ingester) flushChunks(ctx context.Context, fp model.Fingerprint, labelPairs labels.Labels, cs []*chunkDesc, chunkMtx sync.Locker) error {
userID, err := tenant.TenantID(ctx)
if err != nil {
return err
}
// NB(owen-d): No longer needed in TSDB (and is removed in that code path)
// It's required by historical index stores so we keep it for now.
labelsBuilder := labels.NewBuilder(labelPairs)
labelsBuilder.Set(nameLabel, logsValue)
metric := labelsBuilder.Labels()
sizePerTenant := i.metrics.chunkSizePerTenant.WithLabelValues(userID)
countPerTenant := i.metrics.chunksPerTenant.WithLabelValues(userID)
for j, c := range cs {
if err := i.closeChunk(c, chunkMtx); err != nil {
return fmt.Errorf("chunk close for flushing: %w", err)
}
firstTime, lastTime := util.RoundToMilliseconds(c.chunk.Bounds())
ch := chunk.NewChunk(
userID, fp, metric,
chunkenc.NewFacade(c.chunk, i.cfg.BlockSize, i.cfg.TargetChunkSize),
firstTime,
lastTime,
)
// encodeChunk mutates the chunk so we must pass by reference
if err := i.encodeChunk(ctx, &ch, c); err != nil {
return err
}
if err := i.flushChunk(ctx, &ch); err != nil {
return err
}
reason := func() string {
chunkMtx.Lock()
defer chunkMtx.Unlock()
return c.reason
}()
i.reportFlushedChunkStatistics(&ch, c, sizePerTenant, countPerTenant, reason)
i.markChunkAsFlushed(cs[j], chunkMtx)
}
return nil
}
// markChunkAsFlushed mark a chunk to make sure it won't be flushed if this operation fails.
func (i *Ingester) markChunkAsFlushed(desc *chunkDesc, chunkMtx sync.Locker) {
chunkMtx.Lock()
defer chunkMtx.Unlock()
desc.flushed = time.Now()
}
// closeChunk closes the given chunk while locking it to ensure that new blocks are cut before flushing.
//
// If the chunk isn't closed, data in the head block isn't included.
func (i *Ingester) closeChunk(desc *chunkDesc, chunkMtx sync.Locker) error {
chunkMtx.Lock()
defer chunkMtx.Unlock()
return desc.chunk.Close()
}
// encodeChunk encodes a chunk.Chunk based on the given chunkDesc.
//
// If the encoding is unsuccessful the flush operation is reinserted in the queue which will cause
// the encoding for a given chunk to be evaluated again.
func (i *Ingester) encodeChunk(ctx context.Context, ch *chunk.Chunk, desc *chunkDesc) error {
if err := ctx.Err(); err != nil {
return err
}
start := time.Now()
chunkBytesSize := desc.chunk.BytesSize() + 4*1024 // size + 4kB should be enough room for cortex header
if err := ch.EncodeTo(bytes.NewBuffer(make([]byte, 0, chunkBytesSize)), i.logger); err != nil {
if !errors.Is(err, chunk.ErrChunkDecode) {
return fmt.Errorf("chunk encoding: %w", err)
}
i.metrics.chunkDecodeFailures.WithLabelValues(ch.UserID).Inc()
}
i.metrics.chunkEncodeTime.Observe(time.Since(start).Seconds())
i.metrics.chunksEncoded.WithLabelValues(ch.UserID).Inc()
return nil
}
// flushChunk flushes the given chunk to the store.
//
// If the flush is successful, metrics for this flush are to be reported.
// If the flush isn't successful, the operation for this userID is requeued allowing this and all other unflushed
// chunk to have another opportunity to be flushed.
func (i *Ingester) flushChunk(ctx context.Context, ch *chunk.Chunk) error {
if err := i.store.Put(ctx, []chunk.Chunk{*ch}); err != nil {
i.metrics.chunksFlushFailures.Inc()
return fmt.Errorf("store put chunk: %w", err)
}
i.metrics.flushedChunksStats.Inc(1)
return nil
}
// reportFlushedChunkStatistics calculate overall statistics of flushed chunks without compromising the flush process.
func (i *Ingester) reportFlushedChunkStatistics(ch *chunk.Chunk, desc *chunkDesc, sizePerTenant prometheus.Counter, countPerTenant prometheus.Counter, reason string) {
byt, err := ch.Encoded()
if err != nil {
level.Error(i.logger).Log("msg", "failed to encode flushed wire chunk", "err", err)
return
}
i.metrics.chunksFlushedPerReason.WithLabelValues(reason).Add(1)
compressedSize := float64(len(byt))
uncompressedSize, ok := chunkenc.UncompressedSize(ch.Data)
if ok && compressedSize > 0 {
i.metrics.chunkCompressionRatio.Observe(float64(uncompressedSize) / compressedSize)
}
utilization := ch.Data.Utilization()
i.metrics.chunkUtilization.Observe(utilization)
numEntries := desc.chunk.Size()
i.metrics.chunkEntries.Observe(float64(numEntries))
i.metrics.chunkSize.Observe(compressedSize)
sizePerTenant.Add(compressedSize)
countPerTenant.Inc()
boundsFrom, boundsTo := desc.chunk.Bounds()
i.metrics.chunkAge.Observe(time.Since(boundsFrom).Seconds())
i.metrics.chunkLifespan.Observe(boundsTo.Sub(boundsFrom).Hours())
i.metrics.flushedChunksBytesStats.Record(compressedSize)
i.metrics.flushedChunksLinesStats.Record(float64(numEntries))
i.metrics.flushedChunksUtilizationStats.Record(utilization)
i.metrics.flushedChunksAgeStats.Record(time.Since(boundsFrom).Seconds())
i.metrics.flushedChunksLifespanStats.Record(boundsTo.Sub(boundsFrom).Seconds())
}