-
Notifications
You must be signed in to change notification settings - Fork 578
/
block_reader_load.go
131 lines (113 loc) · 3.38 KB
/
block_reader_load.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
package symdb
import (
"bufio"
"context"
"io"
"sort"
"github.com/grafana/dskit/multierror"
"github.com/parquet-go/parquet-go"
"golang.org/x/sync/errgroup"
"github.com/grafana/pyroscope/pkg/iter"
parquetobj "github.com/grafana/pyroscope/pkg/objstore/parquet"
pparquet "github.com/grafana/pyroscope/pkg/parquet"
)
func (r *Reader) Load(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return r.loadStacktraces(ctx) })
if r.index.Header.Version > FormatV1 {
r.loadParquetTables(g)
}
return g.Wait()
}
func (r *Reader) loadStacktraces(ctx context.Context) error {
f, err := r.file(StacktracesFileName)
if err != nil {
return err
}
partitions := make([]*partition, len(r.partitions))
var size int64
var i int
for _, v := range r.partitions {
for _, c := range v.stacktraceChunks {
size += c.header.Size
}
partitions[i] = v
i++
}
sort.Slice(partitions, func(i, j int) bool {
return partitions[i].stacktraceChunks[0].header.Offset <
partitions[j].stacktraceChunks[0].header.Offset
})
offset := partitions[0].stacktraceChunks[0].header.Offset
rc, err := r.bucket.GetRange(ctx, f.RelPath, offset, size)
if err != nil {
return err
}
defer func() {
err = multierror.New(err, rc.Close()).Err()
}()
buf := bufio.NewReaderSize(rc, r.chunkFetchBufferSize)
for _, p := range partitions {
for _, c := range p.stacktraceChunks {
if err = c.readFrom(io.LimitReader(buf, c.header.Size)); err != nil {
return err
}
}
}
return nil
}
func (r *Reader) loadParquetTables(g *errgroup.Group) {
partitions := make([]*partition, len(r.partitions))
var i int
for _, v := range r.partitions {
partitions[i] = v
i++
}
sort.Slice(partitions, func(i, j int) bool {
a := partitions[i].locations.headers[0]
b := partitions[j].locations.headers[0]
return (a.RowGroup + a.Index) < (b.RowGroup + b.Index)
})
g.Go(func() error { return withRowIterator(r.locations, partitions, loadLocations) })
g.Go(func() error { return withRowIterator(r.functions, partitions, loadFunctions) })
g.Go(func() error { return withRowIterator(r.mappings, partitions, loadMappings) })
g.Go(func() error { return withRowIterator(r.strings, partitions, loadStrings) })
}
func loadLocations(p *partition, i iter.Iterator[parquet.Row]) error { return p.locations.loadFrom(i) }
func loadFunctions(p *partition, i iter.Iterator[parquet.Row]) error { return p.functions.loadFrom(i) }
func loadMappings(p *partition, i iter.Iterator[parquet.Row]) error { return p.mappings.loadFrom(i) }
func loadStrings(p *partition, i iter.Iterator[parquet.Row]) error { return p.strings.loadFrom(i) }
type loader func(*partition, iter.Iterator[parquet.Row]) error
func withRowIterator(f parquetobj.File, partitions []*partition, x loader) error {
rows := parquet.MultiRowGroup(f.RowGroups()...).Rows()
defer func() {
_ = rows.Close()
}()
i := pparquet.NewBufferedRowReaderIterator(rows, inMemoryReaderRowsBufSize)
for _, p := range partitions {
if err := x(p, i); err != nil {
return err
}
}
return nil
}
func (t *parquetTableRange[M, P]) loadFrom(iter iter.Iterator[parquet.Row]) error {
if t.r++; t.r > 1 {
return nil
}
var s uint32
for _, h := range t.headers {
s += h.Rows
}
t.s = make([]M, s)
var c uint32
for c < s && iter.Next() {
_, v, err := t.persister.Reconstruct(iter.At())
if err != nil {
return err
}
t.s[c] = v
c++
}
return iter.Err()
}