forked from grafana/loki
/
multi_file_index.go
249 lines (208 loc) · 6.23 KB
/
multi_file_index.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
package tsdb
import (
"context"
"errors"
"github.com/grafana/dskit/multierror"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"golang.org/x/sync/errgroup"
"github.com/grafana/loki/pkg/storage/chunk"
"github.com/grafana/loki/pkg/storage/stores/index/stats"
"github.com/grafana/loki/pkg/storage/stores/tsdb/index"
)
type MultiIndex struct {
indices []Index
}
func NewMultiIndex(indices ...Index) (*MultiIndex, error) {
if len(indices) == 0 {
return nil, errors.New("must supply at least one index")
}
return &MultiIndex{indices: indices}, nil
}
func (i *MultiIndex) Bounds() (model.Time, model.Time) {
var lowest, highest model.Time
for _, idx := range i.indices {
from, through := idx.Bounds()
if lowest == 0 || from < lowest {
lowest = from
}
if highest == 0 || through > highest {
highest = through
}
}
return lowest, highest
}
func (i *MultiIndex) SetChunkFilterer(chunkFilter chunk.RequestChunkFilterer) {
for _, x := range i.indices {
x.SetChunkFilterer(chunkFilter)
}
}
func (i *MultiIndex) Close() error {
var errs multierror.MultiError
for _, idx := range i.indices {
if err := idx.Close(); err != nil {
errs = append(errs, err)
}
}
return errs.Err()
}
func (i *MultiIndex) forIndices(ctx context.Context, from, through model.Time, fn func(context.Context, Index) (interface{}, error)) ([]interface{}, error) {
queryBounds := newBounds(from, through)
g, ctx := errgroup.WithContext(ctx)
// ensure we prebuffer the channel by the maximum possible
// return length so our goroutines don't block
ch := make(chan interface{}, len(i.indices))
for _, idx := range i.indices {
// ignore indices which can't match this query
if Overlap(queryBounds, idx) {
// run all queries in linked goroutines (cancel after first err),
// bounded by parallelism controls if applicable.
// must wrap g.Go in anonymous function to capture
// idx variable during iteration
func(idx Index) {
g.Go(func() error {
got, err := fn(ctx, idx)
if err != nil {
return err
}
ch <- got
return nil
})
}(idx)
}
}
// wait for work to finish or error|ctx expiration
if err := g.Wait(); err != nil {
return nil, err
}
close(ch)
results := make([]interface{}, 0, len(i.indices))
for x := range ch {
results = append(results, x)
}
return results, nil
}
func (i *MultiIndex) GetChunkRefs(ctx context.Context, userID string, from, through model.Time, res []ChunkRef, shard *index.ShardAnnotation, matchers ...*labels.Matcher) ([]ChunkRef, error) {
if res == nil {
res = ChunkRefsPool.Get()
}
res = res[:0]
groups, err := i.forIndices(ctx, from, through, func(ctx context.Context, idx Index) (interface{}, error) {
return idx.GetChunkRefs(ctx, userID, from, through, nil, shard, matchers...)
})
if err != nil {
return nil, err
}
// keep track of duplicates
seen := make(map[ChunkRef]struct{})
// TODO(owen-d): Do this more efficiently,
// not all indices overlap each other
for _, group := range groups {
g := group.([]ChunkRef)
for _, ref := range g {
_, ok := seen[ref]
if ok {
continue
}
seen[ref] = struct{}{}
res = append(res, ref)
}
ChunkRefsPool.Put(g)
}
return res, nil
}
func (i *MultiIndex) Series(ctx context.Context, userID string, from, through model.Time, res []Series, shard *index.ShardAnnotation, matchers ...*labels.Matcher) ([]Series, error) {
if res == nil {
res = SeriesPool.Get()
}
res = res[:0]
groups, err := i.forIndices(ctx, from, through, func(ctx context.Context, idx Index) (interface{}, error) {
return idx.Series(ctx, userID, from, through, nil, shard, matchers...)
})
if err != nil {
return nil, err
}
seen := make(map[model.Fingerprint]struct{})
for _, x := range groups {
seriesSet := x.([]Series)
for _, s := range seriesSet {
_, ok := seen[s.Fingerprint]
if ok {
continue
}
seen[s.Fingerprint] = struct{}{}
res = append(res, s)
}
SeriesPool.Put(seriesSet)
}
return res, nil
}
func (i *MultiIndex) LabelNames(ctx context.Context, userID string, from, through model.Time, matchers ...*labels.Matcher) ([]string, error) {
groups, err := i.forIndices(ctx, from, through, func(ctx context.Context, idx Index) (interface{}, error) {
return idx.LabelNames(ctx, userID, from, through, matchers...)
})
if err != nil {
return nil, err
}
var maxLn int // maximum number of chunk refs, assuming no duplicates
xs := make([][]string, 0, len(i.indices))
for _, group := range groups {
x := group.([]string)
maxLn += len(x)
xs = append(xs, x)
}
// optimistically allocate the maximum length slice
// to avoid growing incrementally
results := make([]string, 0, maxLn)
seen := make(map[string]struct{})
for _, ls := range xs {
for _, l := range ls {
_, ok := seen[l]
if ok {
continue
}
seen[l] = struct{}{}
results = append(results, l)
}
}
return results, nil
}
func (i *MultiIndex) LabelValues(ctx context.Context, userID string, from, through model.Time, name string, matchers ...*labels.Matcher) ([]string, error) {
groups, err := i.forIndices(ctx, from, through, func(ctx context.Context, idx Index) (interface{}, error) {
return idx.LabelValues(ctx, userID, from, through, name, matchers...)
})
if err != nil {
return nil, err
}
var maxLn int // maximum number of chunk refs, assuming no duplicates
xs := make([][]string, 0, len(i.indices))
for _, group := range groups {
x := group.([]string)
maxLn += len(x)
xs = append(xs, x)
}
// optimistically allocate the maximum length slice
// to avoid growing incrementally
results := make([]string, 0, maxLn)
seen := make(map[string]struct{})
for _, ls := range xs {
for _, l := range ls {
_, ok := seen[l]
if ok {
continue
}
seen[l] = struct{}{}
results = append(results, l)
}
}
return results, nil
}
func (i *MultiIndex) Stats(ctx context.Context, userID string, from, through model.Time, blooms *stats.Blooms, shard *index.ShardAnnotation, matchers ...*labels.Matcher) (*stats.Blooms, error) {
if blooms == nil {
blooms = stats.BloomPool.Get()
}
_, err := i.forIndices(ctx, from, through, func(ctx context.Context, idx Index) (interface{}, error) {
return idx.Stats(ctx, userID, from, through, blooms, shard, matchers...)
})
return blooms, err
}