-
Notifications
You must be signed in to change notification settings - Fork 473
/
raw.go
239 lines (194 loc) · 7.7 KB
/
raw.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
package backend
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"path"
"github.com/google/uuid"
tempo_io "github.com/grafana/tempo/pkg/io"
)
const (
MetaName = "meta.json"
CompactedMetaName = "meta.compacted.json"
TenantIndexName = "index.json.gz"
// File name for the cluster seed file.
ClusterSeedFileName = "tempo_cluster_seed.json"
)
// KeyPath is an ordered set of strings that govern where data is read/written from the backend
type KeyPath []string
// RawWriter is a collection of methods to write data to tempodb backends
type RawWriter interface {
// Write is for in memory data. shouldCache specifies whether or not caching should be attempted.
Write(ctx context.Context, name string, keypath KeyPath, data io.Reader, size int64, shouldCache bool) error
// Append starts or continues an Append job. Pass nil to AppendTracker to start a job.
Append(ctx context.Context, name string, keypath KeyPath, tracker AppendTracker, buffer []byte) (AppendTracker, error)
// Closes any resources associated with the AppendTracker
CloseAppend(ctx context.Context, tracker AppendTracker) error
}
// RawReader is a collection of methods to read data from tempodb backends
type RawReader interface {
// List returns all objects one level beneath the provided keypath
List(ctx context.Context, keypath KeyPath) ([]string, error)
// Read is for streaming entire objects from the backend. There will be an attempt to retrieve this from cache if shouldCache is true.
Read(ctx context.Context, name string, keyPath KeyPath, shouldCache bool) (io.ReadCloser, int64, error)
// ReadRange is for reading parts of large objects from the backend.
// There will be an attempt to retrieve this from cache if shouldCache is true. Cache key will be tenantID:blockID:offset:bufferLength
ReadRange(ctx context.Context, name string, keypath KeyPath, offset uint64, buffer []byte, shouldCache bool) error
// Shutdown must be called when the Reader is finished and cleans up any associated resources.
Shutdown()
}
type writer struct {
w RawWriter
}
// NewWriter returns an object that implements Writer and bridges to a RawWriter
func NewWriter(w RawWriter) Writer {
return &writer{
w: w,
}
}
func (w *writer) Write(ctx context.Context, name string, blockID uuid.UUID, tenantID string, buffer []byte, shouldCache bool) error {
return w.w.Write(ctx, name, KeyPathForBlock(blockID, tenantID), bytes.NewReader(buffer), int64(len(buffer)), shouldCache)
}
func (w *writer) StreamWriter(ctx context.Context, name string, blockID uuid.UUID, tenantID string, data io.Reader, size int64) error {
return w.w.Write(ctx, name, KeyPathForBlock(blockID, tenantID), data, size, false)
}
func (w *writer) WriteBlockMeta(ctx context.Context, meta *BlockMeta) error {
blockID := meta.BlockID
tenantID := meta.TenantID
bMeta, err := json.Marshal(meta)
if err != nil {
return err
}
return w.w.Write(ctx, MetaName, KeyPathForBlock(blockID, tenantID), bytes.NewReader(bMeta), int64(len(bMeta)), false)
}
func (w *writer) Append(ctx context.Context, name string, blockID uuid.UUID, tenantID string, tracker AppendTracker, buffer []byte) (AppendTracker, error) {
return w.w.Append(ctx, name, KeyPathForBlock(blockID, tenantID), tracker, buffer)
}
func (w *writer) CloseAppend(ctx context.Context, tracker AppendTracker) error {
return w.w.CloseAppend(ctx, tracker)
}
func (w *writer) WriteTenantIndex(ctx context.Context, tenantID string, meta []*BlockMeta, compactedMeta []*CompactedBlockMeta) error {
b := newTenantIndex(meta, compactedMeta)
indexBytes, err := b.marshal()
if err != nil {
return err
}
err = w.w.Write(ctx, TenantIndexName, KeyPath([]string{tenantID}), bytes.NewReader(indexBytes), int64(len(indexBytes)), false)
if err != nil {
return err
}
return nil
}
type reader struct {
r RawReader
}
// NewReader returns an object that implements Reader and bridges to a RawReader
func NewReader(r RawReader) Reader {
return &reader{
r: r,
}
}
func (r *reader) Read(ctx context.Context, name string, blockID uuid.UUID, tenantID string, shouldCache bool) ([]byte, error) {
objReader, size, err := r.r.Read(ctx, name, KeyPathForBlock(blockID, tenantID), shouldCache)
if err != nil {
return nil, err
}
defer objReader.Close()
return tempo_io.ReadAllWithEstimate(objReader, size)
}
func (r *reader) StreamReader(ctx context.Context, name string, blockID uuid.UUID, tenantID string) (io.ReadCloser, int64, error) {
return r.r.Read(ctx, name, KeyPathForBlock(blockID, tenantID), false)
}
func (r *reader) ReadRange(ctx context.Context, name string, blockID uuid.UUID, tenantID string, offset uint64, buffer []byte, shouldCache bool) error {
return r.r.ReadRange(ctx, name, KeyPathForBlock(blockID, tenantID), offset, buffer, shouldCache)
}
func (r *reader) Tenants(ctx context.Context) ([]string, error) {
list, err := r.r.List(ctx, nil)
// this filter is added to fix a GCS usage stats issue that would result in ""
var filteredList []string
for _, tenant := range list {
if tenant != "" && tenant != ClusterSeedFileName {
filteredList = append(filteredList, tenant)
}
}
return filteredList, err
}
func (r *reader) Blocks(ctx context.Context, tenantID string) ([]uuid.UUID, error) {
objects, err := r.r.List(ctx, KeyPath{tenantID})
if err != nil {
return nil, err
}
// translate everything to UUIDs, if we see a bucket index we can skip that
blockIDs := make([]uuid.UUID, 0, len(objects))
for _, id := range objects {
// TODO: this line exists due to behavior differences in backends: https://github.com/grafana/tempo/issues/880
// revisit once #880 is resolved.
if id == TenantIndexName || id == "" {
continue
}
uuid, err := uuid.Parse(id)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %w", id, err)
}
blockIDs = append(blockIDs, uuid)
}
return blockIDs, nil
}
func (r *reader) BlockMeta(ctx context.Context, blockID uuid.UUID, tenantID string) (*BlockMeta, error) {
reader, size, err := r.r.Read(ctx, MetaName, KeyPathForBlock(blockID, tenantID), false)
if err != nil {
return nil, err
}
defer reader.Close()
bytes, err := tempo_io.ReadAllWithEstimate(reader, size)
if err != nil {
return nil, err
}
out := &BlockMeta{}
err = json.Unmarshal(bytes, out)
if err != nil {
return nil, err
}
return out, nil
}
func (r *reader) TenantIndex(ctx context.Context, tenantID string) (*TenantIndex, error) {
reader, size, err := r.r.Read(ctx, TenantIndexName, KeyPath([]string{tenantID}), false)
if err != nil {
return nil, err
}
bytes, err := tempo_io.ReadAllWithEstimate(reader, size)
if err != nil {
return nil, err
}
i := &TenantIndex{}
err = i.unmarshal(bytes)
if err != nil {
return nil, err
}
return i, nil
}
func (r *reader) Shutdown() {
r.r.Shutdown()
}
// KeyPathForBlock returns a correctly ordered keypath given a block id and tenantid
func KeyPathForBlock(blockID uuid.UUID, tenantID string) KeyPath {
return []string{tenantID, blockID.String()}
}
// ObjectFileName returns a unique identifier for an object in object storage given its name and keypath
func ObjectFileName(keypath KeyPath, name string) string {
return path.Join(path.Join(keypath...), name)
}
// MetaFileName returns the object name for the block meta given a block id and tenantid
func MetaFileName(blockID uuid.UUID, tenantID string) string {
return path.Join(RootPath(blockID, tenantID), MetaName)
}
// CompactedMetaFileName returns the object name for the compacted block meta given a block id and tenantid
func CompactedMetaFileName(blockID uuid.UUID, tenantID string) string {
return path.Join(RootPath(blockID, tenantID), CompactedMetaName)
}
// RootPath returns the root path for a block given a block id and tenantid
func RootPath(blockID uuid.UUID, tenantID string) string {
return path.Join(tenantID, blockID.String())
}