-
Notifications
You must be signed in to change notification settings - Fork 581
/
head_queriers.go
376 lines (320 loc) · 11.3 KB
/
head_queriers.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
package phlaredb
import (
"context"
"sort"
"github.com/bufbuild/connect-go"
"github.com/go-kit/log/level"
"github.com/google/pprof/profile"
"github.com/opentracing/opentracing-go"
"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
ingestv1 "github.com/grafana/pyroscope/api/gen/proto/go/ingester/v1"
typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/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/symdb"
)
type headOnDiskQuerier struct {
head *Head
rowGroupIdx int
}
func (q *headOnDiskQuerier) rowGroup() *rowGroupOnDisk {
q.head.profiles.rowsLock.RLock()
defer q.head.profiles.rowsLock.RUnlock()
return q.head.profiles.rowGroups[q.rowGroupIdx]
}
func (q *headOnDiskQuerier) Open(_ context.Context) error {
return nil
}
func (q *headOnDiskQuerier) BlockID() string {
return q.head.meta.ULID.String()
}
func (q *headOnDiskQuerier) SelectMatchingProfiles(ctx context.Context, params *ingestv1.SelectProfilesRequest) (iter.Iterator[Profile], error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "SelectMatchingProfiles - HeadOnDisk")
defer sp.Finish()
// query the index for rows
rowIter, labelsPerFP, err := q.head.profiles.index.selectMatchingRowRanges(ctx, params, q.rowGroupIdx)
if err != nil {
return nil, err
}
// get time nano information for profiles
var (
start = model.Time(params.Start)
end = model.Time(params.End)
)
pIt := query.NewBinaryJoinIterator(0,
query.NewBinaryJoinIterator(
0,
rowIter,
q.rowGroup().columnIter(ctx, "TimeNanos", query.NewIntBetweenPredicate(start.UnixNano(), end.UnixNano()), "TimeNanos"),
),
q.rowGroup().columnIter(ctx, "StacktracePartition", nil, "StacktracePartition"),
)
defer pIt.Close()
var (
profiles []Profile
buf = make([][]parquet.Value, 2)
)
for pIt.Next() {
res := pIt.At()
v, ok := res.Entries[0].RowValue.(fingerprintWithRowNum)
if !ok {
panic("no fingerprint information found")
}
lbls, ok := labelsPerFP[v.fp]
if !ok {
panic("no profile series labels with matching fingerprint found")
}
buf = res.Columns(buf, "TimeNanos", "StacktracePartition")
if len(buf) < 1 || len(buf[0]) != 1 {
level.Error(q.head.logger).Log("msg", "unable to read timeNanos from profiles", "row", res.RowNumber[0], "rowGroup", q.rowGroupIdx)
continue
}
profiles = append(profiles, BlockProfile{
labels: lbls,
fp: v.fp,
ts: model.TimeFromUnixNano(buf[0][0].Int64()),
stacktracePartition: retrieveStacktracePartition(buf, 1),
RowNum: res.RowNumber[0],
})
}
if err := pIt.Err(); err != nil {
return nil, errors.Wrap(pIt.Err(), "iterator error")
}
// Sort profiles by time, the slice is already sorted by series order
sort.Slice(profiles, func(i, j int) bool {
return profiles[i].Timestamp() < profiles[j].Timestamp()
})
return iter.NewSliceIterator(profiles), nil
}
func (q *headOnDiskQuerier) Bounds() (model.Time, model.Time) {
// TODO: Use per rowgroup information
return q.head.Bounds()
}
func (q *headOnDiskQuerier) ProfileTypes(context.Context, *connect.Request[ingestv1.ProfileTypesRequest]) (*connect.Response[ingestv1.ProfileTypesResponse], error) {
return connect.NewResponse(&ingestv1.ProfileTypesResponse{}), nil
}
func (q *headOnDiskQuerier) LabelValues(ctx context.Context, req *connect.Request[typesv1.LabelValuesRequest]) (*connect.Response[typesv1.LabelValuesResponse], error) {
return connect.NewResponse(&typesv1.LabelValuesResponse{}), nil
}
func (q *headOnDiskQuerier) LabelNames(ctx context.Context, req *connect.Request[typesv1.LabelNamesRequest]) (*connect.Response[typesv1.LabelNamesResponse], error) {
return connect.NewResponse(&typesv1.LabelNamesResponse{}), nil
}
func (q *headOnDiskQuerier) MergeByStacktraces(ctx context.Context, rows iter.Iterator[Profile]) (*phlaremodel.Tree, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByStacktraces")
defer sp.Finish()
r := symdb.NewResolver(ctx, q.head.symdb)
defer r.Release()
if err := mergeByStacktraces(ctx, q.rowGroup(), rows, r); err != nil {
return nil, err
}
return r.Tree()
}
func (q *headOnDiskQuerier) MergePprof(ctx context.Context, rows iter.Iterator[Profile]) (*profile.Profile, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergePprof")
defer sp.Finish()
r := symdb.NewResolver(ctx, q.head.symdb)
defer r.Release()
if err := mergeByStacktraces(ctx, q.rowGroup(), rows, r); err != nil {
return nil, err
}
return r.Profile()
}
func (q *headOnDiskQuerier) MergeByLabels(ctx context.Context, rows iter.Iterator[Profile], by ...string) ([]*typesv1.Series, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByLabels - HeadOnDisk")
defer sp.Finish()
seriesByLabels := make(seriesByLabels)
if err := mergeByLabels(ctx, q.rowGroup(), "TotalValue", rows, seriesByLabels, by...); err != nil {
return nil, err
}
return seriesByLabels.normalize(), nil
}
func (q *headOnDiskQuerier) Series(ctx context.Context, params *ingestv1.SeriesRequest) ([]*typesv1.Labels, error) {
// The TSDB is kept in memory until the head block is flushed to disk.
return []*typesv1.Labels{}, nil
}
func (q *headOnDiskQuerier) MergeBySpans(ctx context.Context, rows iter.Iterator[Profile], spanSelector phlaremodel.SpanSelector) (*phlaremodel.Tree, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeBySpans")
defer sp.Finish()
r := symdb.NewResolver(ctx, q.head.symdb)
defer r.Release()
if err := mergeBySpans(ctx, q.rowGroup(), rows, r, spanSelector); err != nil {
return nil, err
}
return r.Tree()
}
func (q *headOnDiskQuerier) Sort(in []Profile) []Profile {
var rowI, rowJ int64
sort.Slice(in, func(i, j int) bool {
if pI, ok := in[i].(BlockProfile); ok {
rowI = pI.RowNum
}
if pJ, ok := in[j].(BlockProfile); ok {
rowJ = pJ.RowNum
}
return rowI < rowJ
})
return in
}
type headInMemoryQuerier struct {
head *Head
}
func (q *headInMemoryQuerier) Open(_ context.Context) error {
return nil
}
func (q *headInMemoryQuerier) BlockID() string {
return q.head.meta.ULID.String()
}
func (q *headInMemoryQuerier) SelectMatchingProfiles(ctx context.Context, params *ingestv1.SelectProfilesRequest) (iter.Iterator[Profile], error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "SelectMatchingProfiles - HeadInMemory")
defer sp.Finish()
index := q.head.profiles.index
ids, err := index.selectMatchingFPs(ctx, params)
if err != nil {
return nil, err
}
// get time nano information for profiles
var (
start = model.Time(params.Start)
end = model.Time(params.End)
)
iters := make([]iter.Iterator[Profile], 0, len(ids))
index.mutex.RLock()
defer index.mutex.RUnlock()
for _, fp := range ids {
profileSeries, ok := index.profilesPerFP[fp]
if !ok {
continue
}
profiles := make([]*schemav1.InMemoryProfile, len(profileSeries.profiles))
copy(profiles, profileSeries.profiles)
iters = append(iters,
NewSeriesIterator(
profileSeries.lbs,
profileSeries.fp,
iter.NewTimeRangedIterator(iter.NewSliceIterator(profiles), start, end),
),
)
}
return iter.NewMergeIterator(maxBlockProfile, false, iters...), nil
}
func (q *headInMemoryQuerier) Bounds() (model.Time, model.Time) {
// TODO: Use per rowgroup information
return q.head.Bounds()
}
func (q *headInMemoryQuerier) ProfileTypes(ctx context.Context, req *connect.Request[ingestv1.ProfileTypesRequest]) (*connect.Response[ingestv1.ProfileTypesResponse], error) {
return q.head.ProfileTypes(ctx, req)
}
func (q *headInMemoryQuerier) LabelValues(ctx context.Context, req *connect.Request[typesv1.LabelValuesRequest]) (*connect.Response[typesv1.LabelValuesResponse], error) {
return q.head.LabelValues(ctx, req)
}
func (q *headInMemoryQuerier) LabelNames(ctx context.Context, req *connect.Request[typesv1.LabelNamesRequest]) (*connect.Response[typesv1.LabelNamesResponse], error) {
return q.head.LabelNames(ctx, req)
}
func (q *headInMemoryQuerier) MergeByStacktraces(ctx context.Context, rows iter.Iterator[Profile]) (*phlaremodel.Tree, error) {
sp, _ := opentracing.StartSpanFromContext(ctx, "MergeByStacktraces - HeadInMemory")
defer sp.Finish()
r := symdb.NewResolver(ctx, q.head.symdb)
defer r.Release()
for rows.Next() {
p, ok := rows.At().(ProfileWithLabels)
if !ok {
return nil, errors.New("expected ProfileWithLabels")
}
r.AddSamples(p.StacktracePartition(), p.Samples())
}
if err := rows.Err(); err != nil {
return nil, err
}
return r.Tree()
}
func (q *headInMemoryQuerier) MergePprof(ctx context.Context, rows iter.Iterator[Profile]) (*profile.Profile, error) {
sp, _ := opentracing.StartSpanFromContext(ctx, "MergePprof - HeadInMemory")
defer sp.Finish()
r := symdb.NewResolver(ctx, q.head.symdb)
defer r.Release()
for rows.Next() {
p, ok := rows.At().(ProfileWithLabels)
if !ok {
return nil, errors.New("expected ProfileWithLabels")
}
r.AddSamples(p.StacktracePartition(), p.Samples())
}
if err := rows.Err(); err != nil {
return nil, err
}
return r.Profile()
}
func (q *headInMemoryQuerier) MergeByLabels(ctx context.Context, rows iter.Iterator[Profile], by ...string) ([]*typesv1.Series, error) {
sp, _ := opentracing.StartSpanFromContext(ctx, "MergeByLabels - HeadInMemory")
defer sp.Finish()
labelsByFingerprint := map[model.Fingerprint]string{}
seriesByLabels := make(seriesByLabels)
labelBuf := make([]byte, 0, 1024)
for rows.Next() {
p, ok := rows.At().(ProfileWithLabels)
if !ok {
return nil, errors.New("expected ProfileWithLabels")
}
labelsByString, ok := labelsByFingerprint[p.fp]
if !ok {
labelBuf = p.Labels().BytesWithLabels(labelBuf, by...)
labelsByString = string(labelBuf)
labelsByFingerprint[p.fp] = labelsByString
if _, ok := seriesByLabels[labelsByString]; !ok {
seriesByLabels[labelsByString] = &typesv1.Series{
Labels: p.Labels().WithLabels(by...),
Points: []*typesv1.Point{
{
Timestamp: int64(p.Timestamp()),
Value: float64(p.Total()),
},
},
}
continue
}
}
series := seriesByLabels[labelsByString]
series.Points = append(series.Points, &typesv1.Point{
Timestamp: int64(p.Timestamp()),
Value: float64(p.Total()),
})
}
if err := rows.Err(); err != nil {
return nil, err
}
return seriesByLabels.normalize(), nil
}
func (q *headInMemoryQuerier) Series(ctx context.Context, params *ingestv1.SeriesRequest) ([]*typesv1.Labels, error) {
res, err := q.head.Series(ctx, connect.NewRequest(params))
if err != nil {
return nil, err
}
return res.Msg.LabelsSet, nil
}
func (q *headInMemoryQuerier) MergeBySpans(ctx context.Context, rows iter.Iterator[Profile], spanSelector phlaremodel.SpanSelector) (*phlaremodel.Tree, error) {
sp, _ := opentracing.StartSpanFromContext(ctx, "MergeBySpans - HeadInMemory")
defer sp.Finish()
r := symdb.NewResolver(ctx, q.head.symdb)
defer r.Release()
for rows.Next() {
p, ok := rows.At().(ProfileWithLabels)
if !ok {
return nil, errors.New("expected ProfileWithLabels")
}
samples := p.Samples()
if len(samples.Spans) > 0 {
r.AddSamplesWithSpanSelector(p.StacktracePartition(), samples, spanSelector)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return r.Tree()
}
func (q *headInMemoryQuerier) Sort(in []Profile) []Profile {
return in
}