-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
593 lines (497 loc) · 18.8 KB
/
reader.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spanstore
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"math"
"time"
"github.com/dgraph-io/badger"
"github.com/golang/protobuf/proto"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
// Most of these errors are common with the ES and Cassandra backends. Each backend has slightly different validation rules.
var (
// ErrServiceNameNotSet occurs when attempting to query with an empty service name
ErrServiceNameNotSet = errors.New("service name must be set")
// ErrStartTimeMinGreaterThanMax occurs when start time min is above start time max
ErrStartTimeMinGreaterThanMax = errors.New("min start time is above max")
// ErrDurationMinGreaterThanMax occurs when duration min is above duration max
ErrDurationMinGreaterThanMax = errors.New("min duration is above max")
// ErrMalformedRequestObject occurs when a request object is nil
ErrMalformedRequestObject = errors.New("malformed request object")
// ErrStartAndEndTimeNotSet occurs when start time and end time are not set
ErrStartAndEndTimeNotSet = errors.New("start and end time must be set")
// ErrUnableToFindTraceIDAggregation occurs when an aggregation query for TraceIDs fail.
ErrUnableToFindTraceIDAggregation = errors.New("could not find aggregation of traceIDs")
// ErrNotSupported during development, don't support every option - yet
ErrNotSupported = errors.New("this query parameter is not supported yet")
)
const (
defaultNumTraces = 100
sizeOfTraceID = 16
encodingTypeBits = 0x0F
)
// TraceReader reads traces from the local badger store
type TraceReader struct {
store *badger.DB
cache *CacheStore
}
// NewTraceReader returns a TraceReader with cache
func NewTraceReader(db *badger.DB, c *CacheStore) *TraceReader {
return &TraceReader{
store: db,
cache: c,
}
}
func decodeValue(val []byte, encodeType byte) (*model.Span, error) {
sp := model.Span{}
switch encodeType {
case jsonEncoding:
if err := json.Unmarshal(val, &sp); err != nil {
return nil, err
}
case protoEncoding:
if err := proto.Unmarshal(val, &sp); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unknown encoding type: %#02x", encodeType)
}
return &sp, nil
}
// getTraces enriches TraceIDs to Traces
func (r *TraceReader) getTraces(traceIDs []model.TraceID) ([]*model.Trace, error) {
// Get by PK
traces := make([]*model.Trace, 0, len(traceIDs))
prefixes := make([][]byte, 0, len(traceIDs))
for _, traceID := range traceIDs {
prefixes = append(prefixes, createPrimaryKeySeekPrefix(traceID))
}
err := r.store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()
val := []byte{}
for _, prefix := range prefixes {
spans := make([]*model.Span, 0, 32) // reduce reallocation requirements by defining some initial length
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
// Add value to the span store (decode from JSON / defined encoding first)
// These are in the correct order because of the sorted nature
item := it.Item()
val, err := item.ValueCopy(val)
if err != nil {
return err
}
sp, err := decodeValue(val, item.UserMeta()&encodingTypeBits)
if err != nil {
return err
}
spans = append(spans, sp)
}
if len(spans) > 0 {
trace := &model.Trace{
Spans: spans,
}
traces = append(traces, trace)
}
}
return nil
})
return traces, err
}
// GetTrace takes a traceID and returns a Trace associated with that traceID
func (r *TraceReader) GetTrace(ctx context.Context, traceID model.TraceID) (*model.Trace, error) {
traces, err := r.getTraces([]model.TraceID{traceID})
if err != nil {
return nil, err
}
if len(traces) == 1 {
return traces[0], nil
}
return nil, nil
}
// scanTimeRange returns all the Traces found between startTs and endTs
func (r *TraceReader) scanTimeRange(startTime time.Time, endTime time.Time) ([]*model.Trace, error) {
// We need to do a full table scan
traces := make([]*model.Trace, 0)
startTs := model.TimeAsEpochMicroseconds(startTime)
endTs := model.TimeAsEpochMicroseconds(endTime)
err := r.store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = true
it := txn.NewIterator(opts)
defer it.Close()
val := []byte{}
startIndex := []byte{spanKeyPrefix}
spans := make([]*model.Span, 0)
prevTraceID := []byte{}
for it.Seek(startIndex); it.ValidForPrefix(startIndex); it.Next() {
item := it.Item()
key := []byte{}
key = item.KeyCopy(key)
timestamp := binary.BigEndian.Uint64(key[sizeOfTraceID+1 : sizeOfTraceID+1+8])
traceID := key[1 : sizeOfTraceID+1]
if timestamp >= startTs && timestamp <= endTs {
val, err := item.ValueCopy(val)
if err != nil {
return err
}
sp, err := decodeValue(val, item.UserMeta()&encodingTypeBits)
if err != nil {
return err
}
if bytes.Equal(prevTraceID, traceID) {
// Still processing the same one
spans = append(spans, sp)
} else {
// Process last complete span
trace := &model.Trace{
Spans: spans,
}
traces = append(traces, trace)
spans = make([]*model.Span, 0, cap(spans)) // Use previous cap
spans = append(spans, sp)
}
prevTraceID = traceID
}
}
if len(spans) > 0 {
trace := &model.Trace{
Spans: spans,
}
traces = append(traces, trace)
}
return nil
})
return traces, err
}
func createPrimaryKeySeekPrefix(traceID model.TraceID) []byte {
key := make([]byte, 1+sizeOfTraceID)
key[0] = spanKeyPrefix
pos := 1
binary.BigEndian.PutUint64(key[pos:], traceID.High)
pos += 8
binary.BigEndian.PutUint64(key[pos:], traceID.Low)
return key
}
// GetServices fetches the sorted service list that have not expired
func (r *TraceReader) GetServices(ctx context.Context) ([]string, error) {
return r.cache.GetServices()
}
// GetOperations fetches operations in the service and empty slice if service does not exists
func (r *TraceReader) GetOperations(ctx context.Context, service string) ([]string, error) {
return r.cache.GetOperations(service)
}
// setQueryDefaults alters the query with defaults if certain parameters are not set
func setQueryDefaults(query *spanstore.TraceQueryParameters) {
if query.NumTraces == 0 {
query.NumTraces = defaultNumTraces
}
}
// serviceQueries parses the query to index seeks which are unique index seeks
func serviceQueries(query *spanstore.TraceQueryParameters, indexSeeks [][]byte) [][]byte {
if query.ServiceName != "" {
indexSearchKey := make([]byte, 0, 64) // 64 is a magic guess
if query.OperationName != "" {
indexSearchKey = append(indexSearchKey, operationNameIndexKey)
indexSearchKey = append(indexSearchKey, []byte(query.ServiceName+query.OperationName)...)
} else {
indexSearchKey = append(indexSearchKey, serviceNameIndexKey)
indexSearchKey = append(indexSearchKey, []byte(query.ServiceName)...)
}
indexSeeks = append(indexSeeks, indexSearchKey)
if len(query.Tags) > 0 {
for k, v := range query.Tags {
tagSearch := []byte(query.ServiceName + k + v)
tagSearchKey := make([]byte, 0, len(tagSearch)+1)
tagSearchKey = append(tagSearchKey, tagIndexKey)
tagSearchKey = append(tagSearchKey, tagSearch...)
indexSeeks = append(indexSeeks, tagSearchKey)
}
}
}
return indexSeeks
}
// indexSeeksToTraceIDs does the index scanning against badger based on the parsed index queries
func (r *TraceReader) indexSeeksToTraceIDs(query *spanstore.TraceQueryParameters, indexSeeks [][]byte, ids [][][]byte) ([][][]byte, error) {
for i, s := range indexSeeks {
indexResults, err := r.scanIndexKeys(s, query.StartTimeMin, query.StartTimeMax)
if err != nil {
return nil, err
}
// Same traceID can be returned multiple times, but always in sorted order so checking the previous key is enough
prevTraceID := []byte{}
ids = append(ids, make([][]byte, 0, len(indexResults)))
for _, k := range indexResults {
traceID := k[len(k)-sizeOfTraceID:]
if !bytes.Equal(prevTraceID, traceID) {
ids[i] = append(ids[i], traceID)
prevTraceID = traceID
}
}
}
return ids, nil
}
// durationQueries checks non unique index of durations
func (r *TraceReader) durationQueries(query *spanstore.TraceQueryParameters, ids [][][]byte) [][][]byte {
durMax := uint64(model.DurationAsMicroseconds(query.DurationMax))
durMin := uint64(model.DurationAsMicroseconds(query.DurationMin))
startKey := make([]byte, 0, 9)
endKey := make([]byte, 0, 9)
startKey = append(startKey, durationIndexKey) // [0] =
endKey = append(endKey, durationIndexKey)
endVal := make([]byte, 8)
if query.DurationMax == 0 {
// Set MAX to infinite, if Min is missing, 0 is a fine search result for us
durMax = math.MaxUint64
}
binary.BigEndian.PutUint64(endVal, durMax)
startVal := make([]byte, 8)
binary.BigEndian.PutUint64(startVal, durMin)
startKey = append(startKey, startVal...)
endKey = append(endKey, endVal...)
// This is not unique index result - same TraceID can be matched from multiple spans
indexResults, _ := r.scanRangeIndex(startKey, endKey, query.StartTimeMin, query.StartTimeMax)
hashFilter := make(map[model.TraceID]struct{}, len(indexResults))
filteredResults := make([]*model.TraceID, 0, len(indexResults)) // Max possible length
appendableResults := make([][]byte, 0, len(indexResults)) // Max possible length
var value struct{}
for _, k := range indexResults {
key := k[len(k)-sizeOfTraceID:]
id := &model.TraceID{
High: binary.BigEndian.Uint64(key[:8]),
Low: binary.BigEndian.Uint64(key[8:]),
}
if _, exists := hashFilter[*id]; !exists {
filteredResults = append(filteredResults, id)
hashFilter[*id] = value
}
}
model.SortTraceIDs(filteredResults)
// This is an ugly hack at this point - but has no impact on performance really
for _, tr := range filteredResults {
appendableResults = append(appendableResults, traceIDToComparableBytes(tr))
}
ids = append(ids, appendableResults)
return ids
}
func mergeJoinIds(left, right [][]byte) [][]byte {
// len(left) or len(right) is the maximum, whichever is the smallest
allocateSize := len(left)
if len(right) < allocateSize {
allocateSize = len(right)
}
merged := make([][]byte, 0, allocateSize)
lMax := len(left) - 1
rMax := len(right) - 1
for r, l := 0, 0; r <= rMax && l <= lMax; {
switch bytes.Compare(left[l], right[r]) {
case 0:
// Left matches right - merge
merged = append(merged, left[l])
// Advance both
l++
r++
case 1:
// left > right, increase right one
r++
case -1:
// left < right, increase left one
l++
}
}
return merged
}
// sortMergeIds does a sort-merge join operation to the list of TraceIDs to remove duplicates
func sortMergeIds(query *spanstore.TraceQueryParameters, ids [][][]byte) []model.TraceID {
// Key only scan is a lot faster in the badger - use sort-merge join algorithm instead of hash join since we have the keys in sorted order already
var merged [][]byte
if len(ids) > 1 {
merged = mergeJoinIds(ids[0], ids[1])
for i := 2; i < len(ids); i++ {
merged = mergeJoinIds(merged, ids[i])
}
} else {
merged = ids[0]
}
// Get top query.NumTraces results (order in DESC)
if query.NumTraces < len(merged) {
merged = merged[len(merged)-query.NumTraces:]
}
// Results are in ASC (badger's default order), but Jaeger uses DESC, thus we need to reverse the array
for left, right := 0, len(merged)-1; left < right; left, right = left+1, right-1 {
merged[left], merged[right] = merged[right], merged[left]
}
// Create the structs from [][]byte to TraceID
keys := make([]model.TraceID, 0, len(merged))
for _, key := range merged {
keys = append(keys, model.TraceID{
High: binary.BigEndian.Uint64(key[:8]),
Low: binary.BigEndian.Uint64(key[8:]),
})
}
return keys
}
// FindTraces retrieves traces that match the traceQuery
func (r *TraceReader) FindTraces(ctx context.Context, query *spanstore.TraceQueryParameters) ([]*model.Trace, error) {
keys, err := r.FindTraceIDs(ctx, query)
if err != nil {
if err == ErrNotSupported && (!query.StartTimeMax.IsZero() && !query.StartTimeMin.IsZero()) {
return r.scanTimeRange(query.StartTimeMin, query.StartTimeMax)
}
return nil, err
}
return r.getTraces(keys)
}
// FindTraceIDs retrieves only the TraceIDs that match the traceQuery, but not the trace data
func (r *TraceReader) FindTraceIDs(ctx context.Context, query *spanstore.TraceQueryParameters) ([]model.TraceID, error) {
// Validate and set query defaults which were not defined
if err := validateQuery(query); err != nil {
return nil, err
}
setQueryDefaults(query)
// Find matches using indexes that are using service as part of the key
indexSeeks := make([][]byte, 0, 1)
indexSeeks = serviceQueries(query, indexSeeks)
ids := make([][][]byte, 0, len(indexSeeks)+1)
ids, err := r.indexSeeksToTraceIDs(query, indexSeeks, ids)
if err != nil {
return nil, err
}
// Only secondary range index for now (StartTime filtering should be done using the PK)
if query.DurationMax != 0 || query.DurationMin != 0 {
ids = r.durationQueries(query, ids)
}
// Transform index seeks (both unique indexes as well as non-unique indexes) to a list of TraceIDs without duplicates
if len(ids) > 0 {
keys := sortMergeIds(query, ids)
return keys, nil
}
// TODO We could support here all the other scans, such as time range only. These are not currently backed by an index, so a "full table scan" of traces is required.
return nil, ErrNotSupported
}
// validateQuery returns an error if certain restrictions are not met
func validateQuery(p *spanstore.TraceQueryParameters) error {
if p == nil {
return ErrMalformedRequestObject
}
if p.ServiceName == "" && len(p.Tags) > 0 {
return ErrServiceNameNotSet
}
if p.ServiceName == "" && p.OperationName != "" {
return ErrServiceNameNotSet
}
if p.StartTimeMin.IsZero() || p.StartTimeMax.IsZero() {
return ErrStartAndEndTimeNotSet
}
if !p.StartTimeMax.IsZero() && p.StartTimeMax.Before(p.StartTimeMin) {
return ErrStartTimeMinGreaterThanMax
}
if p.DurationMin != 0 && p.DurationMax != 0 && p.DurationMin > p.DurationMax {
return ErrDurationMinGreaterThanMax
}
return nil
}
// scanIndexKeys scans the time range for index keys matching the given prefix.
func (r *TraceReader) scanIndexKeys(indexKeyValue []byte, startTimeMin time.Time, startTimeMax time.Time) ([][]byte, error) {
indexResults := make([][]byte, 0)
startStampBytes := make([]byte, 8)
binary.BigEndian.PutUint64(startStampBytes, model.TimeAsEpochMicroseconds(startTimeMin))
err := r.store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false // Don't fetch values since we're only interested in the keys
it := txn.NewIterator(opts)
defer it.Close()
// Create starting point for sorted index scan
startIndex := make([]byte, len(indexKeyValue)+len(startStampBytes))
copy(startIndex, indexKeyValue)
copy(startIndex[len(indexKeyValue):], startStampBytes)
timeMax := model.TimeAsEpochMicroseconds(startTimeMax)
for it.Seek(startIndex); scanFunction(it, indexKeyValue, timeMax); it.Next() {
item := it.Item()
// ScanFunction is a prefix scanning (since we could have for example service1 & service12)
// Now we need to match only the exact key if we want to add it
timestampStartIndex := len(it.Item().Key()) - (sizeOfTraceID + 8) // timestamp is stored with 8 bytes
if bytes.Equal(indexKeyValue, it.Item().Key()[:timestampStartIndex]) {
key := make([]byte, len(item.Key()))
copy(key, item.Key())
indexResults = append(indexResults, key)
}
}
return nil
})
return indexResults, err
}
// scanFunction compares the index name as well as the time range in the index key
func scanFunction(it *badger.Iterator, indexPrefix []byte, timeIndexEnd uint64) bool {
if it.Item() != nil {
// We can't use the indexPrefix length, because we might have the same prefixValue for non-matching cases also
timestampStartIndex := len(it.Item().Key()) - (sizeOfTraceID + 8) // timestamp is stored with 8 bytes
timestamp := binary.BigEndian.Uint64(it.Item().Key()[timestampStartIndex : timestampStartIndex+8])
return bytes.HasPrefix(it.Item().Key()[:timestampStartIndex], indexPrefix) && timestamp <= timeIndexEnd
}
return false
}
// scanIndexKeys scans the time range for index keys matching the given prefix.
func (r *TraceReader) scanRangeIndex(indexStartValue []byte, indexEndValue []byte, startTimeMin time.Time, startTimeMax time.Time) ([][]byte, error) {
indexResults := make([][]byte, 0)
startStampBytes := make([]byte, 8)
binary.BigEndian.PutUint64(startStampBytes, model.TimeAsEpochMicroseconds(startTimeMin))
err := r.store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false // Don't fetch values since we're only interested in the keys
it := txn.NewIterator(opts)
defer it.Close()
// Create starting point for sorted index scan
startIndex := make([]byte, len(indexStartValue)+len(startStampBytes))
copy(startIndex, indexStartValue)
copy(startIndex[len(indexStartValue):], startStampBytes)
timeIndexEnd := model.TimeAsEpochMicroseconds(startTimeMax)
for it.Seek(startIndex); scanRangeFunction(it, indexEndValue); it.Next() {
item := it.Item()
// ScanFunction is a prefix scanning (since we could have for example service1 & service12)
// Now we need to match only the exact key if we want to add it
timestampStartIndex := len(it.Item().Key()) - (sizeOfTraceID + 8) // timestamp is stored with 8 bytes
timestamp := binary.BigEndian.Uint64(it.Item().Key()[timestampStartIndex : timestampStartIndex+8])
if timestamp <= timeIndexEnd {
key := make([]byte, len(item.Key()))
copy(key, item.Key())
indexResults = append(indexResults, key)
}
}
return nil
})
return indexResults, err
}
// scanRangeFunction seeks until the index end has been reached
func scanRangeFunction(it *badger.Iterator, indexEndValue []byte) bool {
if it.Item() != nil {
compareSlice := it.Item().Key()[:len(indexEndValue)]
return bytes.Compare(indexEndValue, compareSlice) >= 0
}
return false
}
// traceIDToComparableBytes transforms model.TraceID to BigEndian sorted []byte
func traceIDToComparableBytes(traceID *model.TraceID) []byte {
traceIDBytes := make([]byte, sizeOfTraceID)
binary.BigEndian.PutUint64(traceIDBytes, traceID.High)
binary.BigEndian.PutUint64(traceIDBytes[8:], traceID.Low)
return traceIDBytes
}