-
Notifications
You must be signed in to change notification settings - Fork 503
/
backend_block.go
230 lines (187 loc) · 6.41 KB
/
backend_block.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
package v2
import (
"bytes"
"context"
"fmt"
"io"
"github.com/opentracing/opentracing-go"
willf_bloom "github.com/willf/bloom"
"github.com/grafana/tempo/pkg/model"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/encoding/common"
)
// BackendBlock represents a block already in the backend.
type BackendBlock struct {
meta *backend.BlockMeta
reader backend.Reader
}
var _ common.Finder = (*BackendBlock)(nil)
var _ common.Searcher = (*BackendBlock)(nil)
// NewBackendBlock returns a BackendBlock for the given backend.BlockMeta
func NewBackendBlock(meta *backend.BlockMeta, r backend.Reader) (*BackendBlock, error) {
return &BackendBlock{
meta: meta,
reader: r,
}, nil
}
// Find searches a block for the ID and returns an object if found.
func (b *BackendBlock) find(ctx context.Context, id common.ID) ([]byte, error) {
var err error
span, ctx := opentracing.StartSpanFromContext(ctx, "BackendBlock.Find")
defer func() {
if err != nil {
span.SetTag("error", true)
}
span.Finish()
}()
span.SetTag("block", b.meta.BlockID.String())
shardKey := common.ShardKeyForTraceID(id, int(b.meta.BloomShardCount))
blockID := b.meta.BlockID
tenantID := b.meta.TenantID
nameBloom := common.BloomName(shardKey)
bloomBytes, err := b.reader.Read(ctx, nameBloom, blockID, tenantID, true)
if err != nil {
return nil, fmt.Errorf("error retrieving bloom (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
filter := &willf_bloom.BloomFilter{}
_, err = filter.ReadFrom(bytes.NewReader(bloomBytes))
if err != nil {
return nil, fmt.Errorf("error parsing bloom (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
if !filter.Test(id) {
return nil, nil
}
indexReaderAt := backend.NewContextReader(b.meta, common.NameIndex, b.reader, false)
indexReader, err := NewIndexReader(indexReaderAt, int(b.meta.IndexPageSize), int(b.meta.TotalRecords))
if err != nil {
return nil, fmt.Errorf("error building index reader (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
ra := backend.NewContextReader(b.meta, common.NameObjects, b.reader, false)
dataReader, err := NewDataReader(ra, b.meta.Encoding)
if err != nil {
return nil, fmt.Errorf("error building page reader (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
defer dataReader.Close()
// passing nil for objectCombiner here. this is fine b/c a backend block should never have dupes
finder := NewPagedFinder(indexReader, dataReader, nil, NewObjectReaderWriter(), b.meta.DataEncoding)
objectBytes, err := finder.Find(ctx, id)
if err != nil {
return nil, fmt.Errorf("error using pageFinder (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
return objectBytes, nil
}
// Iterator returns an Iterator that iterates over the objects in the block from the backend
func (b *BackendBlock) Iterator(chunkSizeBytes uint32) (common.Iterator, error) {
// read index
ra := backend.NewContextReader(b.meta, common.NameObjects, b.reader, false)
dataReader, err := NewDataReader(ra, b.meta.Encoding)
if err != nil {
return nil, fmt.Errorf("failed to create dataReader (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
reader, err := b.NewIndexReader()
if err != nil {
return nil, err
}
return newPagedIterator(chunkSizeBytes, reader, dataReader, NewObjectReaderWriter()), nil
}
// partialIterator returns an Iterator that iterates over the a subset of pages in the block from the backend
func (b *BackendBlock) partialIterator(chunkSizeBytes uint32, startPage int, totalPages int) (common.Iterator, error) {
// read index
ra := backend.NewContextReader(b.meta, common.NameObjects, b.reader, false)
dataReader, err := NewDataReader(ra, b.meta.Encoding)
if err != nil {
return nil, fmt.Errorf("failed to create dataReader (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
reader, err := b.NewIndexReader()
if err != nil {
return nil, err
}
return newPartialPagedIterator(chunkSizeBytes, reader, dataReader, NewObjectReaderWriter(), startPage, totalPages), nil
}
func (b *BackendBlock) NewIndexReader() (common.IndexReader, error) {
indexReaderAt := backend.NewContextReader(b.meta, common.NameIndex, b.reader, false)
reader, err := NewIndexReader(indexReaderAt, int(b.meta.IndexPageSize), int(b.meta.TotalRecords))
if err != nil {
return nil, fmt.Errorf("failed to create index reader (%s, %s): %w", b.meta.TenantID, b.meta.BlockID, err)
}
return reader, nil
}
func (b *BackendBlock) BlockMeta() *backend.BlockMeta {
return b.meta
}
func (b *BackendBlock) FindTraceByID(ctx context.Context, id common.ID) (*tempopb.Trace, error) {
obj, err := b.find(ctx, id)
if err != nil {
return nil, err
}
if obj == nil {
// Not found in this block
return nil, nil
}
dec, err := model.NewObjectDecoder(b.meta.DataEncoding)
if err != nil {
return nil, err
}
return dec.PrepareForRead(obj)
}
func (b *BackendBlock) Search(ctx context.Context, req *tempopb.SearchRequest, opt common.SearchOptions) (resp *tempopb.SearchResponse, err error) {
decoder, err := model.NewObjectDecoder(b.meta.DataEncoding)
if err != nil {
return nil, fmt.Errorf("failed to create NewDecoder: %w", err)
}
// Iterator
var iter common.Iterator
if opt.TotalPages > 0 {
iter, err = b.partialIterator(opt.ChunkSizeBytes, opt.StartPage, opt.TotalPages)
} else {
iter, err = b.Iterator(opt.ChunkSizeBytes)
}
if err != nil {
return nil, err
}
if opt.PrefetchTraceCount > 0 {
iter = NewPrefetchIterator(ctx, iter, opt.PrefetchTraceCount)
}
defer iter.Close()
resp = &tempopb.SearchResponse{
Metrics: &tempopb.SearchMetrics{},
}
for {
id, obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("error iterating %s, %w", b.meta.BlockID, err)
}
err = search(decoder, opt.MaxBytes, id, obj, req, resp)
if err != nil {
return nil, err
}
if len(resp.Traces) >= int(req.Limit) {
break
}
}
if err != nil {
return nil, err
}
return resp, nil
}
func search(decoder model.ObjectDecoder, maxBytes int, id common.ID, obj []byte, req *tempopb.SearchRequest, resp *tempopb.SearchResponse) error {
resp.Metrics.InspectedTraces++
resp.Metrics.InspectedBytes += uint64(len(obj))
if maxBytes > 0 && len(obj) > maxBytes {
resp.Metrics.SkippedTraces++
return nil
}
metadata, err := decoder.Matches(id, obj, req)
if err != nil {
return err
}
if metadata != nil {
// Found a match
resp.Traces = append(resp.Traces, metadata)
}
return nil
}