/
profiles.go
528 lines (442 loc) · 13.5 KB
/
profiles.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package phlaredb
import (
"context"
"fmt"
"sort"
"sync"
"github.com/gogo/status"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage"
"github.com/samber/lo"
"go.uber.org/atomic"
"google.golang.org/grpc/codes"
ingestv1 "github.com/grafana/pyroscope/api/gen/proto/go/ingester/v1"
"github.com/grafana/pyroscope/pkg/iter"
phlaremodel "github.com/grafana/pyroscope/pkg/model"
"github.com/grafana/pyroscope/pkg/phlaredb/query"
schemav1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
"github.com/grafana/pyroscope/pkg/phlaredb/tsdb"
"github.com/grafana/pyroscope/pkg/phlaredb/tsdb/index"
)
// delta encoding for ranges
type rowRange struct {
rowNum int64
length int
}
type rowRangeWithSeriesIndex struct {
*rowRange
seriesIndex uint32
}
// those need to be strictly ordered
type rowRangesWithSeriesIndex []rowRangeWithSeriesIndex
func (s rowRangesWithSeriesIndex) getSeriesIndex(rowNum int64) uint32 {
for _, rg := range s {
// it is possible that the series is not existing
if rg.rowRange == nil {
continue
}
if rg.rowNum <= rowNum && rg.rowNum+int64(rg.length) > rowNum {
return rg.seriesIndex
}
}
panic("series index not found")
}
type rowRanges map[rowRange]model.Fingerprint
func (rR rowRanges) iter() iter.Iterator[fingerprintWithRowNum] {
// ensure row ranges is sorted
rRSlice := lo.Keys(rR)
sort.Slice(rRSlice, func(i, j int) bool {
return rRSlice[i].rowNum < rRSlice[j].rowNum
})
fps := make([]model.Fingerprint, 0, len(rR))
for _, elem := range rRSlice {
fps = append(fps, rR[elem])
}
return &rowRangesIter{
r: rRSlice,
fps: fps,
pos: 0,
}
}
type fingerprintWithRowNum struct {
fp model.Fingerprint
rowNum int64
}
func (f fingerprintWithRowNum) RowNumber() int64 {
return f.rowNum
}
func (r rowRanges) fingerprintsWithRowNum() query.Iterator {
return query.NewRowNumberIterator(r.iter())
}
type rowRangesIter struct {
r []rowRange
fps []model.Fingerprint
pos int64
}
func (i *rowRangesIter) At() fingerprintWithRowNum {
return fingerprintWithRowNum{
rowNum: i.pos - 1,
fp: i.fps[0],
}
}
func (i *rowRangesIter) Next() bool {
if len(i.r) == 0 {
return false
}
if i.pos < i.r[0].rowNum {
i.pos = i.r[0].rowNum
}
if i.pos >= i.r[0].rowNum+int64(i.r[0].length) {
i.r = i.r[1:]
i.fps = i.fps[1:]
return i.Next()
}
i.pos++
return true
}
func (i *rowRangesIter) Close() error { return nil }
func (i *rowRangesIter) Err() error { return nil }
type profileSeries struct {
lbs phlaremodel.Labels
fp model.Fingerprint
minTime, maxTime int64
// profiles in memory
profiles []*schemav1.InMemoryProfile
// profiles temporary stored on disk in row group segements
// TODO: this information is crucial to recover segements to a full block later
profilesOnDisk []*rowRange
}
type profilesIndex struct {
ix *tsdb.BitPrefixInvertedIndex
// todo: like the inverted index we might want to shard fingerprint to avoid contentions.
profilesPerFP map[model.Fingerprint]*profileSeries
mutex sync.RWMutex
totalProfiles *atomic.Int64
totalSeries *atomic.Int64
rowGroupsOnDisk int
metrics *headMetrics
}
func newProfileIndex(totalShards uint32, metrics *headMetrics) (*profilesIndex, error) {
ix, err := tsdb.NewBitPrefixWithShards(totalShards)
if err != nil {
return nil, err
}
return &profilesIndex{
ix: ix,
profilesPerFP: make(map[model.Fingerprint]*profileSeries),
totalProfiles: atomic.NewInt64(0),
totalSeries: atomic.NewInt64(0),
metrics: metrics,
}, nil
}
// Add a new set of profile to the index.
// The seriesRef are expected to match the profile labels passed in.
func (pi *profilesIndex) Add(ps *schemav1.InMemoryProfile, lbs phlaremodel.Labels, profileName string) {
pi.mutex.Lock()
defer pi.mutex.Unlock()
profiles, ok := pi.profilesPerFP[ps.SeriesFingerprint]
if !ok {
lbs := pi.ix.Add(lbs, ps.SeriesFingerprint)
profiles = &profileSeries{
lbs: lbs,
fp: ps.SeriesFingerprint,
minTime: ps.TimeNanos,
maxTime: ps.TimeNanos,
profilesOnDisk: make([]*rowRange, pi.rowGroupsOnDisk),
}
pi.profilesPerFP[ps.SeriesFingerprint] = profiles
pi.metrics.series.Set(float64(pi.totalSeries.Inc()))
pi.metrics.seriesCreated.WithLabelValues(profileName).Inc()
}
// profile is latest in this series, use a shortcut
if ps.TimeNanos > profiles.maxTime {
// update max timeNanos
profiles.maxTime = ps.TimeNanos
// add profile to in memory slice
profiles.profiles = append(profiles.profiles, ps)
} else {
// use binary search to find position
i := sort.Search(len(profiles.profiles), func(i int) bool {
return profiles.profiles[i].TimeNanos > ps.TimeNanos
})
// insert into slice at correct position
profiles.profiles = append(profiles.profiles, &schemav1.InMemoryProfile{})
copy(profiles.profiles[i+1:], profiles.profiles[i:])
profiles.profiles[i] = ps
}
if ps.TimeNanos < profiles.minTime {
profiles.minTime = ps.TimeNanos
}
pi.metrics.profiles.Set(float64(pi.totalProfiles.Inc()))
pi.metrics.profilesCreated.WithLabelValues(profileName).Inc()
}
func (pi *profilesIndex) selectMatchingFPs(ctx context.Context, params *ingestv1.SelectProfilesRequest) ([]model.Fingerprint, error) {
sp, _ := opentracing.StartSpanFromContext(ctx, "selectMatchingFPs - Index")
defer sp.Finish()
selectors, err := parser.ParseMetricSelector(params.LabelSelector)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "failed to parse label selectors: "+err.Error())
}
if params.Type == nil {
return nil, errors.New("no profileType given")
}
selectors = append(selectors, phlaremodel.SelectorFromProfileType(params.Type))
filters, matchers := SplitFiltersAndMatchers(selectors)
ids, err := pi.ix.Lookup(matchers, nil)
if err != nil {
return nil, err
}
pi.mutex.RLock()
defer pi.mutex.RUnlock()
// filter fingerprints that no longer exist or don't match the filters
var idx int
outer:
for _, fp := range ids {
profile, ok := pi.profilesPerFP[fp]
if !ok {
// If a profile labels is missing here, it has already been flushed
// and is supposed to be picked up from storage by querier
continue
}
for _, filter := range filters {
if !filter.Matches(profile.lbs.Get(filter.Name)) {
continue outer
}
}
// keep this one
ids[idx] = fp
idx++
}
sp.SetTag("matchedSeries", idx)
return ids[:idx], nil
}
func (pi *profilesIndex) selectMatchingRowRanges(ctx context.Context, params *ingestv1.SelectProfilesRequest, rowGroupIdx int) (
query.Iterator,
map[model.Fingerprint]phlaremodel.Labels,
error,
) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "selectMatchingRowRanges - Index")
defer sp.Finish()
ids, err := pi.selectMatchingFPs(ctx, params)
if err != nil {
return nil, nil, err
}
// gather rowRanges and labels from matching series under read lock of the index
var (
rowRanges = make(rowRanges, len(ids))
labelsPerFP = make(map[model.Fingerprint]phlaremodel.Labels, len(ids))
)
pi.mutex.RLock()
defer pi.mutex.RUnlock()
for _, fp := range ids {
// skip if series no longer in index
profileSeries, ok := pi.profilesPerFP[fp]
if !ok {
continue
}
labelsPerFP[fp] = profileSeries.lbs
// skip if rowRange empty
rR := profileSeries.profilesOnDisk[rowGroupIdx]
if rR == nil {
continue
}
rowRanges[*rR] = fp
}
sp.SetTag("rowGroupSegment", rowGroupIdx)
sp.SetTag("matchedRowRangesCount", len(rowRanges))
return rowRanges.fingerprintsWithRowNum(), labelsPerFP, nil
}
type ProfileWithLabels struct {
profile *schemav1.InMemoryProfile
lbs phlaremodel.Labels
fp model.Fingerprint
}
func (p ProfileWithLabels) RowNumber() int64 { return 0 }
func (p ProfileWithLabels) StacktracePartition() uint64 {
return p.profile.StacktracePartition
}
func (p ProfileWithLabels) Timestamp() model.Time {
return model.TimeFromUnixNano(p.profile.TimeNanos)
}
func (p ProfileWithLabels) Fingerprint() model.Fingerprint {
return p.fp
}
func (p ProfileWithLabels) Labels() phlaremodel.Labels {
return p.lbs
}
func (p ProfileWithLabels) Samples() schemav1.Samples {
return p.profile.Samples
}
func (p ProfileWithLabels) Total() int64 {
return int64(p.profile.TotalValue)
}
type SeriesIterator struct {
iter.Iterator[*schemav1.InMemoryProfile]
curr ProfileWithLabels
fp model.Fingerprint
lbs phlaremodel.Labels
}
func NewSeriesIterator(labels phlaremodel.Labels, fingerprint model.Fingerprint, it iter.Iterator[*schemav1.InMemoryProfile]) *SeriesIterator {
return &SeriesIterator{
Iterator: it,
fp: fingerprint,
lbs: labels,
}
}
func (it *SeriesIterator) Next() bool {
if !it.Iterator.Next() {
return false
}
it.curr = ProfileWithLabels{
profile: it.Iterator.At(),
lbs: it.lbs,
fp: it.fp,
}
return true
}
func (it *SeriesIterator) At() Profile {
return it.curr
}
// forMatchingLabels iterates through all matching label sets and calls f for each labels set.
func (pi *profilesIndex) forMatchingLabels(matchers []*labels.Matcher,
fn func(lbs phlaremodel.Labels, fp model.Fingerprint) error,
) error {
filters, matchers := SplitFiltersAndMatchers(matchers)
ids, err := pi.ix.Lookup(matchers, nil)
if err != nil {
return err
}
pi.mutex.RLock()
defer pi.mutex.RUnlock()
outer:
for _, fp := range ids {
profile, ok := pi.profilesPerFP[fp]
if !ok {
// If a profile labels is missing here, it has already been flushed
// and is supposed to be picked up from storage by querier
continue
}
for _, filter := range filters {
if !filter.Matches(profile.lbs.Get(filter.Name)) {
continue outer
}
}
if err := fn(profile.lbs, fp); err != nil {
return err
}
}
return nil
}
// WriteTo writes the profiles tsdb index to the specified filepath.
func (pi *profilesIndex) writeTo(ctx context.Context, path string) ([][]rowRangeWithSeriesIndex, error) {
writer, err := index.NewWriter(ctx, path)
if err != nil {
return nil, err
}
pi.mutex.RLock()
defer pi.mutex.RUnlock()
pfs := make([]*profileSeries, 0, len(pi.profilesPerFP))
for _, p := range pi.profilesPerFP {
pfs = append(pfs, p)
}
// sort by fp
sort.Slice(pfs, func(i, j int) bool {
return phlaremodel.CompareLabelPairs(pfs[i].lbs, pfs[j].lbs) < 0
})
symbolsMap := make(map[string]struct{})
for _, s := range pfs {
for _, l := range s.lbs {
symbolsMap[l.Name] = struct{}{}
symbolsMap[l.Value] = struct{}{}
}
}
// Sort symbols
symbols := make([]string, 0, len(symbolsMap))
for s := range symbolsMap {
symbols = append(symbols, s)
}
sort.Strings(symbols)
// Add symbols
for _, symbol := range symbols {
if err := writer.AddSymbol(symbol); err != nil {
return nil, err
}
}
// ranges per row group
rangesPerRG := make([][]rowRangeWithSeriesIndex, len(pfs[0].profilesOnDisk))
// Add series
for i, s := range pfs {
if err := writer.AddSeries(storage.SeriesRef(i), s.lbs, s.fp, index.ChunkMeta{
MinTime: s.minTime,
MaxTime: s.maxTime,
// We store the series Index from the head with the series to use when retrieving data from parquet.
SeriesIndex: uint32(i),
}); err != nil {
return nil, err
}
// store series index
for idx, rg := range s.profilesOnDisk {
rangesPerRG[idx] = append(rangesPerRG[idx], rowRangeWithSeriesIndex{rowRange: rg, seriesIndex: uint32(i)})
}
}
return rangesPerRG, writer.Close()
}
func (pi *profilesIndex) cutRowGroup(rgProfiles []schemav1.InMemoryProfile) error {
pi.mutex.Lock()
defer pi.mutex.Unlock()
// adding rowGroup and rowNum information per fingerprint
rowRangePerFP := make(map[model.Fingerprint]*rowRange, len(pi.profilesPerFP))
countPerFP := make(map[model.Fingerprint]int, len(pi.profilesPerFP))
for rowNum, p := range rgProfiles {
countPerFP[p.SeriesFingerprint]++
if _, ok := rowRangePerFP[p.SeriesFingerprint]; !ok {
rowRangePerFP[p.SeriesFingerprint] = &rowRange{
rowNum: int64(rowNum),
}
}
rowRange := rowRangePerFP[p.SeriesFingerprint]
rowRange.length++
// sanity check
if (int(rowRange.rowNum) + rowRange.length - 1) != rowNum {
return fmt.Errorf("rowRange is not matching up, ensure that the ordering of the profile row group is ordered correctly, current row_num=%d, expect range %d-%d", rowNum, rowRange.rowNum, int(rowRange.rowNum)+rowRange.length)
}
}
pi.rowGroupsOnDisk += 1
for fp, ps := range pi.profilesPerFP {
count := countPerFP[fp]
// empty all in memory profiles
for i := range ps.profiles[:count] {
// Allow GC to evict the object.
ps.profiles[i] = nil
}
ps.profiles = ps.profiles[count:]
// attach rowGroup and rowNum information
rowRange := rowRangePerFP[ps.fp]
ps.profilesOnDisk = append(
ps.profilesOnDisk,
rowRange,
)
}
return nil
}
// SplitFiltersAndMatchers splits empty matchers off, which are treated as filters, see #220
func SplitFiltersAndMatchers(allMatchers []*labels.Matcher) (filters, matchers []*labels.Matcher) {
for _, matcher := range allMatchers {
// If a matcher matches "", we need to fetch possible chunks where
// there is no value and will therefore not be in our label index.
// e.g. {foo=""} and {foo!="bar"} both match "", so we need to return
// chunks which do not have a foo label set. When looking entries in
// the index, we should ignore this matcher to fetch all possible chunks
// and then filter on the matcher after the chunks have been fetched.
if matcher.Matches("") {
filters = append(filters, matcher)
} else {
matchers = append(matchers, matcher)
}
}
return
}