-
Notifications
You must be signed in to change notification settings - Fork 519
/
shared.go
132 lines (108 loc) · 2.9 KB
/
shared.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
package main
import (
"context"
"encoding/json"
"fmt"
"sort"
"strconv"
"time"
"github.com/google/uuid"
"github.com/grafana/tempo/pkg/boundedwaitgroup"
"github.com/grafana/tempo/tempodb/backend"
)
type unifiedBlockMeta struct {
backend.BlockMeta
backend.CompactedBlockMeta
window int64
compacted bool
}
func getMeta(meta *backend.BlockMeta, compactedMeta *backend.CompactedBlockMeta, windowRange time.Duration) unifiedBlockMeta {
if meta != nil {
return unifiedBlockMeta{
BlockMeta: *meta,
window: meta.EndTime.Unix() / int64(windowRange/time.Second),
compacted: false,
}
}
if compactedMeta != nil {
return unifiedBlockMeta{
BlockMeta: compactedMeta.BlockMeta,
CompactedBlockMeta: *compactedMeta,
window: compactedMeta.EndTime.Unix() / int64(windowRange/time.Second),
compacted: true,
}
}
return unifiedBlockMeta{
BlockMeta: backend.BlockMeta{
BlockID: uuid.UUID{},
CompactionLevel: 0,
TotalObjects: -1,
},
window: -1,
compacted: false,
}
}
type blockStats struct {
unifiedBlockMeta
}
func loadBucket(r backend.Reader, c backend.Compactor, tenantID string, windowRange time.Duration, includeCompacted bool) ([]blockStats, error) {
blockIDs, err := r.Blocks(context.Background(), tenantID)
if err != nil {
return nil, err
}
fmt.Println("total blocks: ", len(blockIDs))
// Load in parallel
wg := boundedwaitgroup.New(20)
resultsCh := make(chan blockStats, len(blockIDs))
for blockNum, id := range blockIDs {
wg.Add(1)
go func(id2 uuid.UUID, blockNum2 int) {
defer wg.Done()
b, err := loadBlock(r, c, tenantID, id2, blockNum2, windowRange, includeCompacted)
if err != nil {
fmt.Println("Error loading block:", id2, err)
return
}
if b != nil {
resultsCh <- *b
}
}(id, blockNum)
}
wg.Wait()
close(resultsCh)
results := make([]blockStats, 0)
for b := range resultsCh {
results = append(results, b)
}
sort.Slice(results, func(i, j int) bool {
return results[i].EndTime.Before(results[j].EndTime)
})
return results, nil
}
func loadBlock(r backend.Reader, c backend.Compactor, tenantID string, id uuid.UUID, blockNum int, windowRange time.Duration, includeCompacted bool) (*blockStats, error) {
fmt.Print(".")
if blockNum%100 == 0 {
fmt.Print(strconv.Itoa(blockNum))
}
meta, err := r.BlockMeta(context.Background(), id, tenantID)
if err == backend.ErrDoesNotExist && !includeCompacted {
return nil, nil
} else if err != nil && err != backend.ErrDoesNotExist {
return nil, err
}
compactedMeta, err := c.CompactedBlockMeta(id, tenantID)
if err != nil && err != backend.ErrDoesNotExist {
return nil, err
}
return &blockStats{
unifiedBlockMeta: getMeta(meta, compactedMeta, windowRange),
}, nil
}
func printAsJSON(value interface{}) error {
traceJSON, err := json.Marshal(value)
if err != nil {
return err
}
fmt.Println(string(traceJSON))
return nil
}