-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
composite_store.go
356 lines (309 loc) · 10.2 KB
/
composite_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
package stores
import (
"context"
"sort"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/storage/chunk"
"github.com/grafana/loki/v3/pkg/storage/chunk/fetcher"
"github.com/grafana/loki/v3/pkg/storage/stores/index"
"github.com/grafana/loki/v3/pkg/storage/stores/index/seriesvolume"
"github.com/grafana/loki/v3/pkg/storage/stores/index/stats"
tsdb_index "github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb/index"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb/sharding"
"github.com/grafana/loki/v3/pkg/util"
)
type ChunkWriter interface {
Put(ctx context.Context, chunks []chunk.Chunk) error
PutOne(ctx context.Context, from, through model.Time, chunk chunk.Chunk) error
}
type ChunkFetcherProvider interface {
GetChunkFetcher(tm model.Time) *fetcher.Fetcher
}
type ChunkFetcher interface {
GetChunks(ctx context.Context, userID string, from, through model.Time, predicate chunk.Predicate) ([][]chunk.Chunk, []*fetcher.Fetcher, error)
}
type Store interface {
index.BaseReader
index.StatsReader
index.Filterable
ChunkWriter
ChunkFetcher
ChunkFetcherProvider
Stop()
}
// CompositeStore is a Store which delegates to various stores depending
// on when they were activated.
type CompositeStore struct {
limits StoreLimits
stores []compositeStoreEntry
}
// Ensure interface implementation of CompositeStore
var _ Store = &CompositeStore{}
// NewCompositeStore creates a new Store which delegates to different stores depending
// on time.
func NewCompositeStore(limits StoreLimits) *CompositeStore {
return &CompositeStore{
stores: make([]compositeStoreEntry, 0),
limits: limits,
}
}
func (c *CompositeStore) AddStore(start model.Time, fetcher *fetcher.Fetcher, index index.Reader, writer ChunkWriter, stop func()) {
c.stores = append(c.stores, compositeStoreEntry{
start: start,
Store: &storeEntry{
fetcher: fetcher,
indexReader: index,
ChunkWriter: writer,
limits: c.limits,
stop: stop,
},
})
}
func (c *CompositeStore) Stores() []Store {
var stores []Store
for _, store := range c.stores {
stores = append(stores, store.Store)
}
return stores
}
func (c CompositeStore) Put(ctx context.Context, chunks []chunk.Chunk) error {
for _, chunk := range chunks {
err := c.forStores(ctx, chunk.From, chunk.Through, func(innerCtx context.Context, from, through model.Time, store Store) error {
return store.PutOne(innerCtx, from, through, chunk)
})
if err != nil {
return err
}
}
return nil
}
func (c CompositeStore) PutOne(ctx context.Context, from, through model.Time, chunk chunk.Chunk) error {
return c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
return store.PutOne(innerCtx, from, through, chunk)
})
}
func (c CompositeStore) SetChunkFilterer(chunkFilter chunk.RequestChunkFilterer) {
for _, store := range c.stores {
store.Store.SetChunkFilterer(chunkFilter)
}
}
func (c CompositeStore) GetSeries(ctx context.Context, userID string, from, through model.Time, matchers ...*labels.Matcher) ([]labels.Labels, error) {
var results []labels.Labels
found := map[uint64]struct{}{}
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
series, err := store.GetSeries(innerCtx, userID, from, through, matchers...)
if err != nil {
return err
}
for _, s := range series {
if _, ok := found[s.Hash()]; !ok {
results = append(results, s)
found[s.Hash()] = struct{}{}
}
}
return nil
})
sort.Slice(results, func(i, j int) bool {
return labels.Compare(results[i], results[j]) < 0
})
return results, err
}
// LabelValuesForMetricName retrieves all label values for a single label name and metric name.
func (c CompositeStore) LabelValuesForMetricName(ctx context.Context, userID string, from, through model.Time, metricName string, labelName string, matchers ...*labels.Matcher) ([]string, error) {
var result util.UniqueStrings
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
labelValues, err := store.LabelValuesForMetricName(innerCtx, userID, from, through, metricName, labelName, matchers...)
if err != nil {
return err
}
result.Add(labelValues...)
return nil
})
return result.Strings(), err
}
// LabelNamesForMetricName retrieves all label names for a metric name.
func (c CompositeStore) LabelNamesForMetricName(ctx context.Context, userID string, from, through model.Time, metricName string) ([]string, error) {
var result util.UniqueStrings
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
labelNames, err := store.LabelNamesForMetricName(innerCtx, userID, from, through, metricName)
if err != nil {
return err
}
result.Add(labelNames...)
return nil
})
return result.Strings(), err
}
func (c CompositeStore) GetChunks(ctx context.Context, userID string, from, through model.Time, predicate chunk.Predicate) ([][]chunk.Chunk, []*fetcher.Fetcher, error) {
chunkIDs := [][]chunk.Chunk{}
fetchers := []*fetcher.Fetcher{}
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
ids, fetcher, err := store.GetChunks(innerCtx, userID, from, through, predicate)
if err != nil {
return err
}
// Skip it if there are no chunks.
if len(ids) == 0 {
return nil
}
chunkIDs = append(chunkIDs, ids...)
fetchers = append(fetchers, fetcher...)
return nil
})
return chunkIDs, fetchers, err
}
func (c CompositeStore) Stats(ctx context.Context, userID string, from, through model.Time, matchers ...*labels.Matcher) (*stats.Stats, error) {
xs := make([]*stats.Stats, 0, len(c.stores))
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
x, err := store.Stats(innerCtx, userID, from, through, matchers...)
xs = append(xs, x)
return err
})
if err != nil {
return nil, err
}
res := stats.MergeStats(xs...)
return &res, err
}
func (c CompositeStore) Volume(ctx context.Context, userID string, from, through model.Time, limit int32, targetLabels []string, aggregateBy string, matchers ...*labels.Matcher) (*logproto.VolumeResponse, error) {
volumes := make([]*logproto.VolumeResponse, 0, len(c.stores))
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
volume, err := store.Volume(innerCtx, userID, from, through, limit, targetLabels, aggregateBy, matchers...)
volumes = append(volumes, volume)
return err
})
if err != nil {
return nil, err
}
res := seriesvolume.Merge(volumes, limit)
return res, err
}
func (c CompositeStore) GetShards(
ctx context.Context,
userID string,
from, through model.Time,
targetBytesPerShard uint64,
predicate chunk.Predicate,
) (*logproto.ShardsResponse, error) {
// TODO(owen-d): improve. Since shards aren't easily merge-able,
// we choose the store which returned the highest shard count.
// This is only used when a query crosses a schema boundary
var groups []*logproto.ShardsResponse
err := c.forStores(ctx, from, through, func(innerCtx context.Context, from, through model.Time, store Store) error {
shards, err := store.GetShards(innerCtx, userID, from, through, targetBytesPerShard, predicate)
if err != nil {
return err
}
groups = append(groups, shards)
return nil
})
if err != nil {
return nil, err
}
switch {
case len(groups) == 1:
return groups[0], nil
case len(groups) == 0:
return nil, nil
default:
sort.Slice(groups, func(i, j int) bool {
return len(groups[i].Shards) > len(groups[j].Shards)
})
return groups[0], nil
}
}
func (c CompositeStore) HasForSeries(from, through model.Time) (sharding.ForSeries, bool) {
var impls []sharding.ForSeries
_ = c.forStores(context.Background(), from, through, func(_ context.Context, from, through model.Time, store Store) error {
impl, ok := store.HasForSeries(from, through)
if ok {
impls = append(impls, impl)
}
return nil
})
if len(impls) == 0 {
return nil, false
}
wrapped := sharding.ForSeriesFunc(
func(
ctx context.Context,
userID string,
fpFilter tsdb_index.FingerprintFilter,
from model.Time,
through model.Time,
fn func(
labels.Labels,
model.Fingerprint,
[]tsdb_index.ChunkMeta,
) (stop bool),
matchers ...*labels.Matcher,
) error {
for _, impl := range impls {
if err := impl.ForSeries(ctx, userID, fpFilter, from, through, fn, matchers...); err != nil {
return err
}
}
return nil
},
)
return wrapped, true
}
func (c CompositeStore) GetChunkFetcher(tm model.Time) *fetcher.Fetcher {
// find the schema with the lowest start _after_ tm
j := sort.Search(len(c.stores), func(j int) bool {
return c.stores[j].start > tm
})
// reduce it by 1 because we want a schema with start <= tm
j--
if 0 <= j && j < len(c.stores) {
return c.stores[j].GetChunkFetcher(tm)
}
return nil
}
func (c CompositeStore) Stop() {
for _, store := range c.stores {
store.Stop()
}
}
func (c CompositeStore) forStores(ctx context.Context, from, through model.Time, callback func(innerCtx context.Context, from, through model.Time, store Store) error) error {
if len(c.stores) == 0 {
return nil
}
// first, find the schema with the highest start _before or at_ from
i := sort.Search(len(c.stores), func(i int) bool {
return c.stores[i].start > from
})
if i > 0 {
i--
} else {
// This could happen if we get passed a sample from before 1970.
i = 0
from = c.stores[0].start
}
// next, find the schema with the lowest start _after_ through
j := sort.Search(len(c.stores), func(j int) bool {
return c.stores[j].start > through
})
min := func(a, b model.Time) model.Time {
if a < b {
return a
}
return b
}
start := from
for ; i < j; i++ {
nextSchemaStarts := model.Latest
if i+1 < len(c.stores) {
nextSchemaStarts = c.stores[i+1].start
}
end := min(through, nextSchemaStarts-1)
err := callback(ctx, start, end, c.stores[i])
if err != nil {
return err
}
start = nextSchemaStarts
}
return nil
}