-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathbloomgateway.go
471 lines (394 loc) · 13.2 KB
/
bloomgateway.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
/*
The bloom gateway is a component that can be run as a standalone microserivce
target and provides capabilities for filtering ChunkRefs based on a given list
of line filter expressions.
*/
package bloomgateway
import (
"context"
"fmt"
"slices"
"sort"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/services"
"github.com/grafana/dskit/tenant"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/atomic"
iter "github.com/grafana/loki/v3/pkg/iter/v2"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/queue"
v1 "github.com/grafana/loki/v3/pkg/storage/bloom/v1"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper"
"github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/util/constants"
utillog "github.com/grafana/loki/v3/pkg/util/log"
"github.com/grafana/loki/v3/pkg/util/spanlogger"
)
const (
metricsSubsystem = "bloom_gateway"
querierMetricsSubsystem = "bloom_gateway_querier"
)
var (
// responsesPool pooling array of v1.Output [64, 128, 256, ..., 65536]
responsesPool = queue.NewSlicePool[v1.Output](1<<6, 1<<16, 2)
)
type Gateway struct {
services.Service
cfg Config
logger log.Logger
metrics *metrics
queue *queue.RequestQueue
activeUsers *util.ActiveUsersCleanupService
bloomStore bloomshipper.Store
pendingTasks *atomic.Int64
serviceMngr *services.Manager
serviceWatcher *services.FailureWatcher
workerConfig workerConfig
}
// fixedQueueLimits is a queue.Limits implementation that returns a fixed value for MaxConsumers.
// Notably this lets us run with "disabled" max consumers (0) for the bloom gateway meaning it will
// distribute any request to any receiver.
type fixedQueueLimits struct {
maxConsumers int
}
func (l *fixedQueueLimits) MaxConsumers(_ string, _ int) int {
return l.maxConsumers
}
// New returns a new instance of the Bloom Gateway.
func New(cfg Config, store bloomshipper.Store, logger log.Logger, reg prometheus.Registerer) (*Gateway, error) {
utillog.WarnExperimentalUse("Bloom Gateway", logger)
g := &Gateway{
cfg: cfg,
logger: logger,
metrics: newMetrics(reg, constants.Loki, metricsSubsystem),
workerConfig: workerConfig{
maxItems: cfg.NumMultiplexItems,
queryConcurrency: cfg.BlockQueryConcurrency,
},
pendingTasks: &atomic.Int64{},
bloomStore: store,
}
queueMetrics := queue.NewMetrics(reg, constants.Loki, metricsSubsystem)
g.queue = queue.NewRequestQueue(cfg.MaxOutstandingPerTenant, time.Minute, &fixedQueueLimits{0}, queueMetrics)
g.activeUsers = util.NewActiveUsersCleanupWithDefaultValues(queueMetrics.Cleanup)
if err := g.initServices(); err != nil {
return nil, err
}
g.Service = services.NewBasicService(g.starting, g.running, g.stopping).WithName("bloom-gateway")
return g, nil
}
func (g *Gateway) initServices() error {
var err error
svcs := []services.Service{g.queue, g.activeUsers}
for i := 0; i < g.cfg.WorkerConcurrency; i++ {
id := fmt.Sprintf("bloom-query-worker-%d", i)
w := newWorker(id, g.workerConfig, g.queue, g.bloomStore, g.pendingTasks, g.logger, g.metrics.workerMetrics)
svcs = append(svcs, w)
}
g.serviceMngr, err = services.NewManager(svcs...)
if err != nil {
return err
}
g.serviceWatcher = services.NewFailureWatcher()
g.serviceWatcher.WatchManager(g.serviceMngr)
return nil
}
func (g *Gateway) starting(ctx context.Context) error {
var err error
defer func() {
if err == nil || g.serviceMngr == nil {
return
}
if err := services.StopManagerAndAwaitStopped(context.Background(), g.serviceMngr); err != nil {
level.Error(g.logger).Log("msg", "failed to gracefully stop bloom gateway dependencies", "err", err)
}
}()
if err := services.StartManagerAndAwaitHealthy(ctx, g.serviceMngr); err != nil {
return errors.Wrap(err, "unable to start bloom gateway subservices")
}
return nil
}
func (g *Gateway) running(ctx context.Context) error {
// We observe inflight tasks frequently and at regular intervals, to have a good
// approximation of max inflight tasks over percentiles of time. We also do it with
// a ticker so that we keep tracking it even if we have no new requests but stuck inflight
// tasks (eg. worker are all exhausted).
inflightTasksTicker := time.NewTicker(250 * time.Millisecond)
defer inflightTasksTicker.Stop()
for {
select {
case <-ctx.Done():
return nil
case err := <-g.serviceWatcher.Chan():
return errors.Wrap(err, "bloom gateway subservice failed")
case <-inflightTasksTicker.C:
inflight := g.pendingTasks.Load()
g.metrics.inflightRequests.Observe(float64(inflight))
}
}
}
func (g *Gateway) stopping(_ error) error {
return services.StopManagerAndAwaitStopped(context.Background(), g.serviceMngr)
}
// FilterChunkRefs implements BloomGatewayServer
func (g *Gateway) FilterChunkRefs(ctx context.Context, req *logproto.FilterChunkRefRequest) (*logproto.FilterChunkRefResponse, error) {
tenantID, err := tenant.TenantID(ctx)
if err != nil {
return nil, err
}
sp, ctx := opentracing.StartSpanFromContext(ctx, "bloomgateway.FilterChunkRefs")
stats, ctx := ContextWithEmptyStats(ctx)
logger := spanlogger.FromContextWithFallback(
ctx,
utillog.WithContext(ctx, g.logger),
)
defer func() {
level.Info(logger).Log(stats.KVArgs()...)
sp.Finish()
}()
// start time == end time --> empty response
if req.From.Equal(req.Through) {
stats.Status = labelSuccess
return &logproto.FilterChunkRefResponse{
ChunkRefs: []*logproto.GroupedChunkRefs{},
}, nil
}
// start time > end time --> error response
if req.Through.Before(req.From) {
stats.Status = labelFailure
return nil, errors.New("from time must not be after through time")
}
matchers := v1.ExtractTestableLabelMatchers(req.Plan.AST)
stats.NumMatchers = len(matchers)
g.metrics.receivedMatchers.Observe(float64(len(matchers)))
// Shortcut if request does not contain filters
if len(matchers) == 0 {
stats.Status = labelSuccess
return &logproto.FilterChunkRefResponse{ChunkRefs: req.Refs}, nil
}
blocks := make([]bloomshipper.BlockRef, 0, len(req.Blocks))
for _, key := range req.Blocks {
block, err := bloomshipper.BlockRefFromKey(key)
if err != nil {
stats.Status = labelFailure
return nil, errors.New("could not parse block key")
}
blocks = append(blocks, block)
}
// Shortcut if request does not contain blocks
if len(blocks) == 0 {
stats.Status = labelSuccess
return &logproto.FilterChunkRefResponse{ChunkRefs: req.Refs}, nil
}
seriesByDay := partitionRequest(req)
stats.NumTasks = len(seriesByDay)
sp.LogKV(
"matchers", len(matchers),
"days", len(seriesByDay),
"blocks", len(req.Blocks),
"series_requested", len(req.Refs),
)
// len(seriesByDay) should never be 0
// Not sure how this can happen, but there was a bug report
// https://github.com/grafana/loki/issues/14623
if len(seriesByDay) == 0 {
stats.Status = labelSuccess
return &logproto.FilterChunkRefResponse{ChunkRefs: req.Refs}, nil
}
if len(seriesByDay) > 1 {
stats.Status = labelFailure
return nil, errors.New("request time range must span exactly one day")
}
series := seriesByDay[0]
task := newTask(ctx, tenantID, series, matchers, blocks)
// TODO(owen-d): include capacity in constructor?
task.responses = responsesPool.Get(len(series.series))
// free up the responses
defer responsesPool.Put(task.responses)
g.activeUsers.UpdateUserTimestamp(tenantID, time.Now())
var preFilterSeries, preFilterChunks int
preFilterSeries = len(req.Refs)
for _, series := range req.Refs {
preFilterChunks += len(series.Refs)
}
tasksCh := make(chan Task, 1)
// TODO(owen-d): gracefully handle full queues
task.enqueueTime = time.Now()
if err := g.queue.Enqueue(tenantID, nil, task, func() {
// When enqueuing, we also add the task to the pending tasks
_ = g.pendingTasks.Inc()
}); err != nil {
stats.Status = labelFailure
return nil, errors.Wrap(err, "failed to enqueue task")
}
// TODO(owen-d): use `concurrency` lib, bound parallelism
go g.consumeTask(ctx, task, tasksCh)
combinedRecorder := v1.NewBloomRecorder(ctx, "combined")
select {
case <-ctx.Done():
stats.Status = "cancel"
return nil, errors.Wrap(ctx.Err(), "request failed")
case task = <-tasksCh:
if task.Err() != nil {
stats.Status = labelFailure
return nil, errors.Wrap(task.Err(), "request failed")
}
combinedRecorder.Merge(task.recorder)
}
combinedRecorder.Report(utillog.WithContext(ctx, g.logger), g.bloomStore.BloomMetrics())
start := time.Now()
filtered := filterChunkRefs(req, task.responses)
duration := time.Since(start)
stats.AddPostProcessingTime(duration)
var postFilterSeries, postFilterChunks int
postFilterSeries = len(filtered)
for _, group := range filtered {
postFilterChunks += len(group.Refs)
}
g.metrics.requestedSeries.Observe(float64(preFilterSeries))
g.metrics.filteredSeries.Observe(float64(preFilterSeries - postFilterSeries))
g.metrics.requestedChunks.Observe(float64(preFilterChunks))
g.metrics.filteredChunks.Observe(float64(preFilterChunks - postFilterChunks))
stats.Status = "success"
stats.SeriesRequested = preFilterSeries
stats.SeriesFiltered = preFilterSeries - postFilterSeries
stats.ChunksRequested = preFilterChunks
stats.ChunksFiltered = preFilterChunks - postFilterChunks
sp.LogKV("msg", "return filtered chunk refs")
return &logproto.FilterChunkRefResponse{ChunkRefs: filtered}, nil
}
// consumeTask receives v1.Output yielded from the block querier on the task's
// result channel and stores them on the task.
// In case the context task is done, it drains the remaining items until the
// task is closed by the worker.
// Once the tasks is closed, it will send the task with the results from the
// block querier to the supplied task channel.
func (g *Gateway) consumeTask(ctx context.Context, task Task, tasksCh chan<- Task) {
for res := range task.resCh {
select {
case <-ctx.Done():
// do nothing
default:
// chunks may not always be sorted
if !slices.IsSortedFunc(res.Removals, func(a, b v1.ChunkRef) int { return a.Cmp(b) }) {
slices.SortFunc(res.Removals, func(a, b v1.ChunkRef) int { return a.Cmp(b) })
}
task.responses = append(task.responses, res)
}
}
select {
case <-ctx.Done():
// do nothing
case <-task.Done():
// notify request handler about finished task
tasksCh <- task
}
}
// TODO(owen-d): improve perf. This can be faster with a more specialized impl
// NB(owen-d): `req` is mutated in place for performance, but `responses` is not
// Removals of the outputs must be sorted.
func filterChunkRefs(req *logproto.FilterChunkRefRequest, responses []v1.Output) []*logproto.GroupedChunkRefs {
// sort responses by fingerprint
sort.Slice(responses, func(i, j int) bool { return responses[i].Fp < responses[j].Fp })
res := make([]*logproto.GroupedChunkRefs, 0, len(req.Refs))
// dedupe outputs, merging the same series.
// This returns an Iterator[v1.Output]
dedupedResps := iter.NewDedupingIter(
// eq
func(o1, o2 v1.Output) bool {
return o1.Fp == o2.Fp
},
// from
iter.Identity[v1.Output],
// merge two removal sets for the same series, deduping
// requires that the removals of the outputs are sorted
func(o1, o2 v1.Output) v1.Output {
res := v1.Output{Fp: o1.Fp}
var chks v1.ChunkRefs
var i, j int
for i < len(o1.Removals) && j < len(o2.Removals) {
a, b := o1.Removals[i], o2.Removals[j]
if a == b {
chks = append(chks, a)
i++
j++
continue
}
if a.Less(b) {
chks = append(chks, a)
i++
continue
}
chks = append(chks, b)
j++
}
if i < len(o1.Removals) {
chks = append(chks, o1.Removals[i:]...)
}
if j < len(o2.Removals) {
chks = append(chks, o2.Removals[j:]...)
}
res.Removals = chks
return res
},
iter.NewPeekIter(iter.NewSliceIter(responses)),
)
// Iterate through the requested and filtered series/chunks,
// removing chunks that were filtered out.
var next bool
var at v1.Output
if next = dedupedResps.Next(); next {
at = dedupedResps.At()
}
for i := 0; i < len(req.Refs); i++ {
// we've hit the end of the removals -- append the rest of the
// requested series and return
if !next {
res = append(res, req.Refs[i:]...)
return res
}
// the current series had no removals
cur := req.Refs[i]
if cur.Fingerprint < uint64(at.Fp) {
res = append(res, cur)
continue
}
// the current series had removals. No need to check for equality
// b/c removals must be present in input
filterChunkRefsForSeries(cur, at.Removals)
if len(cur.Refs) > 0 {
res = append(res, cur)
}
// advance removals
if next = dedupedResps.Next(); next {
at = dedupedResps.At()
}
}
return res
}
// mutates cur
func filterChunkRefsForSeries(cur *logproto.GroupedChunkRefs, removals v1.ChunkRefs) {
// use same backing array to avoid allocations
res := cur.Refs[:0]
var i, j int
for i < len(cur.Refs) && j < len(removals) {
if (*v1.ChunkRef)(cur.Refs[i]).Less(removals[j]) {
// chunk was not removed
res = append(res, cur.Refs[i])
i++
} else {
// Since all removals must exist in the series, we can assume that if the removal
// is not less, it must be equal to the current chunk (a match). Skip this chunk.
i++
j++
}
}
if i < len(cur.Refs) {
res = append(res, cur.Refs[i:]...)
}
cur.Refs = cur.Refs[:len(res)]
}