forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flush.go
304 lines (256 loc) · 8.47 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
package ingester
import (
"fmt"
"net/http"
"time"
"golang.org/x/net/context"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/weaveworks/common/user"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/util"
"github.com/grafana/loki/pkg/chunkenc"
loki_util "github.com/grafana/loki/pkg/util"
)
var (
chunkEntries = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loki_ingester_chunk_entries",
Help: "Distribution of stored chunk entries (when stored).",
Buckets: prometheus.ExponentialBuckets(200, 2, 9), // biggest bucket is 200*2^(9-1) = 51200
})
chunkSize = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loki_ingester_chunk_size_bytes",
Help: "Distribution of stored chunk sizes (when stored).",
Buckets: prometheus.ExponentialBuckets(10000, 2, 7), // biggest bucket is 10000*2^(7-1) = 640000 (~640KB)
})
chunksPerTenant = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "loki_ingester_chunks_stored_total",
Help: "Total stored chunks per tenant.",
}, []string{"tenant"})
chunkSizePerTenant = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "loki_ingester_chunk_stored_bytes_total",
Help: "Total bytes stored in chunks per tenant.",
}, []string{"tenant"})
chunkAge = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loki_ingester_chunk_age_seconds",
Help: "Distribution of chunk ages (when stored).",
// with default settings chunks should flush between 5 min and 12 hours
// so buckets at 1min, 5min, 10min, 30min, 1hr, 2hr, 4hr, 10hr, 12hr, 16hr
Buckets: []float64{60, 300, 600, 1800, 3600, 7200, 14400, 36000, 43200, 57600},
})
chunkEncodeTime = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loki_ingester_chunk_encode_time_seconds",
Help: "Distribution of chunk encode times.",
// 10ms to 10s.
Buckets: prometheus.ExponentialBuckets(0.01, 4, 6),
})
)
const (
// Backoff for retrying 'immediate' flushes. Only counts for queue
// position, not wallclock time.
flushBackoff = 1 * time.Second
nameLabel = "__name__"
logsValue = "logs"
)
// 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.sweepUsers(true)
// Close the flush queues, to unblock waiting workers.
for _, flushQueue := range i.flushQueues {
flushQueue.Close()
}
i.flushQueuesDone.Wait()
}
// 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)
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 series
func (i *Ingester) sweepUsers(immediate bool) {
instances := i.getInstances()
for _, instance := range instances {
i.sweepInstance(instance, immediate)
}
}
func (i *Ingester) sweepInstance(instance *instance, immediate bool) {
instance.streamsMtx.Lock()
defer instance.streamsMtx.Unlock()
for _, stream := range instance.streams {
i.sweepStream(instance, stream, immediate)
i.removeFlushedChunks(instance, stream)
}
}
func (i *Ingester) sweepStream(instance *instance, stream *stream, immediate bool) {
if len(stream.chunks) == 0 {
return
}
lastChunk := stream.chunks[len(stream.chunks)-1]
if len(stream.chunks) == 1 && time.Since(lastChunk.lastUpdated) < i.cfg.MaxChunkIdle && !immediate {
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,
})
}
func (i *Ingester) flushLoop(j int) {
defer func() {
level.Debug(util.Logger).Log("msg", "Ingester.flushLoop() exited")
i.flushQueuesDone.Done()
}()
for {
o := i.flushQueues[j].Dequeue()
if o == nil {
return
}
op := o.(*flushOp)
level.Debug(util.Logger).Log("msg", "flushing stream", "userid", op.userID, "fp", op.fp, "immediate", op.immediate)
err := i.flushUserSeries(op.userID, op.fp, op.immediate)
if err != nil {
level.Error(util.WithUserID(op.userID, util.Logger)).Log("msg", "failed to flush user", "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) flushUserSeries(userID string, fp model.Fingerprint, immediate bool) error {
instance, ok := i.getInstanceByID(userID)
if !ok {
return nil
}
chunks, labels := i.collectChunksToFlush(instance, fp, immediate)
if len(chunks) < 1 {
return nil
}
ctx := user.InjectOrgID(context.Background(), userID)
ctx, cancel := context.WithTimeout(ctx, i.cfg.FlushOpTimeout)
defer cancel()
err := i.flushChunks(ctx, fp, labels, chunks)
if err != nil {
return err
}
instance.streamsMtx.Lock()
for _, chunk := range chunks {
chunk.flushed = time.Now()
}
instance.streamsMtx.Unlock()
return nil
}
func (i *Ingester) collectChunksToFlush(instance *instance, fp model.Fingerprint, immediate bool) ([]*chunkDesc, []client.LabelAdapter) {
instance.streamsMtx.Lock()
defer instance.streamsMtx.Unlock()
stream, ok := instance.streams[fp]
if !ok {
return nil, nil
}
var result []*chunkDesc
for j := range stream.chunks {
if immediate || i.shouldFlushChunk(&stream.chunks[j]) {
// 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() {
result = append(result, &stream.chunks[j])
}
}
}
return result, stream.labels
}
func (i *Ingester) shouldFlushChunk(chunk *chunkDesc) bool {
// Append should close the chunk when the a new one is added.
if chunk.closed {
return true
}
if time.Since(chunk.lastUpdated) > i.cfg.MaxChunkIdle {
chunk.closed = true
return true
}
return false
}
func (i *Ingester) removeFlushedChunks(instance *instance, stream *stream) {
now := time.Now()
for len(stream.chunks) > 0 {
if stream.chunks[0].flushed.IsZero() || now.Sub(stream.chunks[0].flushed) < i.cfg.RetainPeriod {
break
}
stream.chunks[0].chunk = nil // erase reference so the chunk can be garbage-collected
stream.chunks = stream.chunks[1:]
}
if len(stream.chunks) == 0 {
delete(instance.streams, stream.fp)
instance.index.Delete(client.FromLabelAdaptersToLabels(stream.labels), stream.fp)
instance.streamsRemovedTotal.Inc()
}
}
func (i *Ingester) flushChunks(ctx context.Context, fp model.Fingerprint, labelPairs []client.LabelAdapter, cs []*chunkDesc) error {
userID, err := user.ExtractOrgID(ctx)
if err != nil {
return err
}
labelsBuilder := labels.NewBuilder(client.FromLabelAdaptersToLabels(labelPairs))
labelsBuilder.Set(nameLabel, logsValue)
metric := labelsBuilder.Labels()
wireChunks := make([]chunk.Chunk, 0, len(cs))
for _, c := range cs {
firstTime, lastTime := loki_util.RoundToMilliseconds(c.chunk.Bounds())
c := chunk.NewChunk(
userID, fp, metric,
chunkenc.NewFacade(c.chunk),
firstTime,
lastTime,
)
start := time.Now()
if err := c.Encode(); err != nil {
return err
}
chunkEncodeTime.Observe(time.Since(start).Seconds())
wireChunks = append(wireChunks, c)
}
if err := i.store.Put(ctx, wireChunks); err != nil {
return err
}
// Record statistsics only when actual put request did not return error.
sizePerTenant := chunkSizePerTenant.WithLabelValues(userID)
countPerTenant := chunksPerTenant.WithLabelValues(userID)
for i, wc := range wireChunks {
numEntries := cs[i].chunk.Size()
byt, err := wc.Encoded()
if err != nil {
continue
}
chunkEntries.Observe(float64(numEntries))
chunkSize.Observe(float64(len(byt)))
sizePerTenant.Add(float64(len(byt)))
countPerTenant.Inc()
firstTime, _ := cs[i].chunk.Bounds()
chunkAge.Observe(time.Since(firstTime).Seconds())
}
return nil
}