-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathchunk_store_utils.go
252 lines (217 loc) · 6.16 KB
/
chunk_store_utils.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
package chunk
import (
"context"
"sync"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/promql"
"github.com/cortexproject/cortex/pkg/chunk/cache"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/spanlogger"
)
const chunkDecodeParallelism = 16
func filterChunksByTime(from, through model.Time, chunks []Chunk) []Chunk {
filtered := make([]Chunk, 0, len(chunks))
for _, chunk := range chunks {
if chunk.Through < from || through < chunk.From {
continue
}
filtered = append(filtered, chunk)
}
return filtered
}
func keysFromChunks(chunks []Chunk) []string {
keys := make([]string, 0, len(chunks))
for _, chk := range chunks {
keys = append(keys, chk.ExternalKey())
}
return keys
}
func labelNamesFromChunks(chunks []Chunk) []string {
var result UniqueStrings
for _, c := range chunks {
for _, l := range c.Metric {
result.Add(l.Name)
}
}
return result.Strings()
}
func filterChunksByUniqueFingerprint(chunks []Chunk) ([]Chunk, []string) {
filtered := make([]Chunk, 0, len(chunks))
keys := make([]string, 0, len(chunks))
uniqueFp := map[model.Fingerprint]struct{}{}
for _, chunk := range chunks {
if _, ok := uniqueFp[chunk.Fingerprint]; ok {
continue
}
filtered = append(filtered, chunk)
keys = append(keys, chunk.ExternalKey())
uniqueFp[chunk.Fingerprint] = struct{}{}
}
return filtered, keys
}
func filterChunksByMatchers(chunks []Chunk, filters []*labels.Matcher) []Chunk {
filteredChunks := make([]Chunk, 0, len(chunks))
outer:
for _, chunk := range chunks {
for _, filter := range filters {
if !filter.Matches(chunk.Metric.Get(filter.Name)) {
continue outer
}
}
filteredChunks = append(filteredChunks, chunk)
}
return filteredChunks
}
// Fetcher deals with fetching chunk contents from the cache/store,
// and writing back any misses to the cache. Also responsible for decoding
// chunks from the cache, in parallel.
type Fetcher struct {
storage Client
cache cache.Cache
cacheStubs bool
wait sync.WaitGroup
decodeRequests chan decodeRequest
}
type decodeRequest struct {
chunk Chunk
buf []byte
responses chan decodeResponse
}
type decodeResponse struct {
chunk Chunk
err error
}
// NewChunkFetcher makes a new ChunkFetcher.
func NewChunkFetcher(cacher cache.Cache, cacheStubs bool, storage Client) (*Fetcher, error) {
c := &Fetcher{
storage: storage,
cache: cacher,
cacheStubs: cacheStubs,
decodeRequests: make(chan decodeRequest),
}
c.wait.Add(chunkDecodeParallelism)
for i := 0; i < chunkDecodeParallelism; i++ {
go c.worker()
}
return c, nil
}
// Stop the ChunkFetcher.
func (c *Fetcher) Stop() {
close(c.decodeRequests)
c.wait.Wait()
c.cache.Stop()
}
func (c *Fetcher) worker() {
defer c.wait.Done()
decodeContext := NewDecodeContext()
for req := range c.decodeRequests {
err := req.chunk.Decode(decodeContext, req.buf)
if err != nil {
cacheCorrupt.Inc()
}
req.responses <- decodeResponse{
chunk: req.chunk,
err: err,
}
}
}
// FetchChunks fetches a set of chunks from cache and store. Note that the keys passed in must be
// lexicographically sorted, while the returned chunks are not in the same order as the passed in chunks.
func (c *Fetcher) FetchChunks(ctx context.Context, chunks []Chunk, keys []string) ([]Chunk, error) {
log, ctx := spanlogger.New(ctx, "ChunkStore.FetchChunks")
defer log.Span.Finish()
// Now fetch the actual chunk data from Memcache / S3
cacheHits, cacheBufs, _ := c.cache.Fetch(ctx, keys)
fromCache, missing, err := c.processCacheResponse(ctx, chunks, cacheHits, cacheBufs)
if err != nil {
level.Warn(log).Log("msg", "error fetching from cache", "err", err)
}
var fromStorage []Chunk
if len(missing) > 0 {
fromStorage, err = c.storage.GetChunks(ctx, missing)
}
// Always cache any chunks we did get
if cacheErr := c.writeBackCache(ctx, fromStorage); cacheErr != nil {
level.Warn(log).Log("msg", "could not store chunks in chunk cache", "err", cacheErr)
}
if err != nil {
// Don't rely on Cortex error translation here.
return nil, promql.ErrStorage{Err: err}
}
allChunks := append(fromCache, fromStorage...)
return allChunks, nil
}
func (c *Fetcher) writeBackCache(ctx context.Context, chunks []Chunk) error {
keys := make([]string, 0, len(chunks))
bufs := make([][]byte, 0, len(chunks))
for i := range chunks {
var encoded []byte
var err error
if !c.cacheStubs {
encoded, err = chunks[i].Encoded()
// TODO don't fail, just log and continue?
if err != nil {
return err
}
}
keys = append(keys, chunks[i].ExternalKey())
bufs = append(bufs, encoded)
}
c.cache.Store(ctx, keys, bufs)
return nil
}
// ProcessCacheResponse decodes the chunks coming back from the cache, separating
// hits and misses.
func (c *Fetcher) processCacheResponse(ctx context.Context, chunks []Chunk, keys []string, bufs [][]byte) ([]Chunk, []Chunk, error) {
var (
requests = make([]decodeRequest, 0, len(keys))
responses = make(chan decodeResponse)
missing []Chunk
)
log, _ := spanlogger.New(ctx, "Fetcher.processCacheResponse")
defer log.Span.Finish()
i, j := 0, 0
for i < len(chunks) && j < len(keys) {
chunkKey := chunks[i].ExternalKey()
if chunkKey < keys[j] {
missing = append(missing, chunks[i])
i++
} else if chunkKey > keys[j] {
level.Warn(util.Logger).Log("msg", "got chunk from cache we didn't ask for")
j++
} else {
requests = append(requests, decodeRequest{
chunk: chunks[i],
buf: bufs[j],
responses: responses,
})
i++
j++
}
}
for ; i < len(chunks); i++ {
missing = append(missing, chunks[i])
}
level.Debug(log).Log("chunks", len(chunks), "decodeRequests", len(requests), "missing", len(missing))
go func() {
for _, request := range requests {
c.decodeRequests <- request
}
}()
var (
err error
found []Chunk
)
for i := 0; i < len(requests); i++ {
response := <-responses
// Don't exit early, as we don't want to block the workers.
if response.err != nil {
err = response.err
} else {
found = append(found, response.chunk)
}
}
return found, missing, err
}