forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
407 lines (331 loc) · 10.6 KB
/
instance.go
File metadata and controls
407 lines (331 loc) · 10.6 KB
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
package ingester
import (
"context"
"fmt"
"hash"
"hash/fnv"
"sync"
"time"
"github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/gogo/status"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/grpc/codes"
"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/pkg/util"
"github.com/grafana/tempo/tempodb"
"github.com/grafana/tempo/tempodb/encoding"
"github.com/grafana/tempo/tempodb/encoding/common"
"github.com/grafana/tempo/tempodb/wal"
)
// Errors returned on Query.
var (
ErrTraceMissing = errors.New("Trace missing")
)
var (
metricTracesCreatedTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "tempo",
Name: "ingester_traces_created_total",
Help: "The total number of traces created per tenant.",
}, []string{"tenant"})
metricBytesWrittenTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "tempo",
Name: "ingester_bytes_written_total",
Help: "The total bytes written per tenant.",
}, []string{"tenant"})
metricBlocksClearedTotal = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "tempo",
Name: "ingester_blocks_cleared_total",
Help: "The total number of blocks cleared.",
})
)
type instance struct {
tracesMtx sync.Mutex
traces map[uint32]*trace
blocksMtx sync.RWMutex
headBlock *wal.AppendBlock
completingBlocks []*wal.AppendBlock
completeBlocks []*encoding.CompleteBlock
lastBlockCut time.Time
instanceID string
tracesCreatedTotal prometheus.Counter
bytesWrittenTotal prometheus.Counter
limiter *Limiter
writer tempodb.Writer
hash hash.Hash32
}
func newInstance(instanceID string, limiter *Limiter, writer tempodb.Writer) (*instance, error) {
i := &instance{
traces: map[uint32]*trace{},
instanceID: instanceID,
tracesCreatedTotal: metricTracesCreatedTotal.WithLabelValues(instanceID),
bytesWrittenTotal: metricBytesWrittenTotal.WithLabelValues(instanceID),
limiter: limiter,
writer: writer,
hash: fnv.New32(),
}
err := i.resetHeadBlock()
if err != nil {
return nil, err
}
return i, nil
}
func (i *instance) Push(ctx context.Context, req *tempopb.PushRequest) error {
i.tracesMtx.Lock()
defer i.tracesMtx.Unlock()
trace, err := i.getOrCreateTrace(req)
if err != nil {
return err
}
if err := trace.Push(ctx, req); err != nil {
return err
}
return nil
}
// PushBytes is used by the wal replay code and so it can push directly into the head block with 0 shenanigans
func (i *instance) PushBytes(ctx context.Context, id []byte, object []byte) error {
i.blocksMtx.Lock()
defer i.blocksMtx.Unlock()
return i.headBlock.Write(id, object)
}
// Moves any complete traces out of the map to complete traces
func (i *instance) CutCompleteTraces(cutoff time.Duration, immediate bool) error {
tracesToCut := i.tracesToCut(cutoff, immediate)
for _, t := range tracesToCut {
out, err := proto.Marshal(t.trace)
if err != nil {
return err
}
err = i.writeTraceToHeadBlock(t.traceID, out)
if err != nil {
return err
}
i.bytesWrittenTotal.Add(float64(len(out)))
}
return nil
}
// CutBlockIfReady cuts a completingBlock from the HeadBlock if ready
// Returns a bool indicating if a block was cut along with the error (if any).
func (i *instance) CutBlockIfReady(maxBlockLifetime time.Duration, maxBlockBytes uint64, immediate bool) (uuid.UUID, error) {
i.blocksMtx.Lock()
defer i.blocksMtx.Unlock()
if i.headBlock == nil || i.headBlock.DataLength() == 0 {
return uuid.Nil, nil
}
now := time.Now()
if i.lastBlockCut.Add(maxBlockLifetime).Before(now) || i.headBlock.DataLength() >= maxBlockBytes || immediate {
completingBlock := i.headBlock
i.completingBlocks = append(i.completingBlocks, completingBlock)
err := i.resetHeadBlock()
if err != nil {
return uuid.Nil, fmt.Errorf("failed to resetHeadBlock: %w", err)
}
return completingBlock.BlockID(), nil
}
return uuid.Nil, nil
}
// CompleteBlock() moves a completingBlock to a completeBlock. The new completeBlock has the same ID
func (i *instance) CompleteBlock(blockID uuid.UUID) error {
i.blocksMtx.Lock()
var completingBlock *wal.AppendBlock
for _, iterBlock := range i.completingBlocks {
if iterBlock.BlockID() == blockID {
completingBlock = iterBlock
break
}
}
i.blocksMtx.Unlock()
if completingBlock == nil {
return fmt.Errorf("error finding completingBlock")
}
// potentially long running operation placed outside blocksMtx
completeBlock, err := i.writer.CompleteBlock(completingBlock, i)
if err != nil {
metricFailedFlushes.Inc()
level.Error(log.Logger).Log("msg", "unable to complete block.", "tenantID", i.instanceID, "err", err)
return err
}
i.blocksMtx.Lock()
i.completeBlocks = append(i.completeBlocks, completeBlock)
i.blocksMtx.Unlock()
return nil
}
// nolint:interfacer
func (i *instance) ClearCompletingBlock(blockID uuid.UUID) error {
i.blocksMtx.Lock()
var completingBlock *wal.AppendBlock
for j, iterBlock := range i.completingBlocks {
if iterBlock.BlockID() == blockID {
completingBlock = iterBlock
i.completingBlocks = append(i.completingBlocks[:j], i.completingBlocks[j+1:]...)
break
}
}
i.blocksMtx.Unlock()
if completingBlock != nil {
return completingBlock.Clear()
//if err != nil {
// return err
//level.Error(log.Logger).Log("msg", "Error clearing wal", "tenantID", i.instanceID, "blockID", blockID.String(), "err", err)
//}
}
return fmt.Errorf("Error finding wal completingBlock to clear")
}
// GetBlockToBeFlushed gets a list of blocks that can be flushed to the backend
func (i *instance) GetBlockToBeFlushed(blockID uuid.UUID) *encoding.CompleteBlock {
i.blocksMtx.Lock()
defer i.blocksMtx.Unlock()
for _, c := range i.completeBlocks {
if c.BlockMeta().BlockID == blockID && c.FlushedTime().IsZero() {
return c
}
}
return nil
}
func (i *instance) ClearFlushedBlocks(completeBlockTimeout time.Duration) error {
var err error
i.blocksMtx.Lock()
defer i.blocksMtx.Unlock()
for idx, b := range i.completeBlocks {
flushedTime := b.FlushedTime()
if flushedTime.IsZero() {
continue
}
if flushedTime.Add(completeBlockTimeout).Before(time.Now()) {
i.completeBlocks = append(i.completeBlocks[:idx], i.completeBlocks[idx+1:]...)
err = b.Clear() // todo: don't remove from complete blocks slice until after clear succeeds?
if err == nil {
metricBlocksClearedTotal.Inc()
}
break
}
}
return err
}
func (i *instance) FindTraceByID(id []byte) (*tempopb.Trace, error) {
var allBytes []byte
// live traces
i.tracesMtx.Lock()
if liveTrace, ok := i.traces[i.tokenForTraceID(id)]; ok {
foundBytes, err := proto.Marshal(liveTrace.trace)
if err != nil {
i.tracesMtx.Unlock()
return nil, fmt.Errorf("unable to marshal liveTrace: %w", err)
}
allBytes = i.Combine(foundBytes, allBytes)
}
i.tracesMtx.Unlock()
i.blocksMtx.Lock()
defer i.blocksMtx.Unlock()
// headBlock
foundBytes, err := i.headBlock.Find(id, i)
if err != nil {
return nil, fmt.Errorf("headBlock.Find failed: %w", err)
}
allBytes = i.Combine(foundBytes, allBytes)
// completingBlock
for _, c := range i.completingBlocks {
foundBytes, err = c.Find(id, i)
if err != nil {
return nil, fmt.Errorf("completingBlock.Find failed: %w", err)
}
allBytes = i.Combine(foundBytes, allBytes)
}
// completeBlock
for _, c := range i.completeBlocks {
foundBytes, err = c.Find(id, i)
if err != nil {
return nil, fmt.Errorf("completeBlock.Find failed: %w", err)
}
allBytes = i.Combine(foundBytes, allBytes)
}
// now marshal it all
if allBytes != nil {
out := &tempopb.Trace{}
err = proto.Unmarshal(allBytes, out)
if err != nil {
return nil, err
}
return out, nil
}
return nil, nil
}
// getOrCreateTrace will return a new trace object for the given request
// It must be called under the i.tracesMtx lock
func (i *instance) getOrCreateTrace(req *tempopb.PushRequest) (*trace, error) {
traceID, err := pushRequestTraceID(req)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unable to extract traceID: %v", err)
}
fp := i.tokenForTraceID(traceID)
trace, ok := i.traces[fp]
if ok {
return trace, nil
}
err = i.limiter.AssertMaxTracesPerUser(i.instanceID, len(i.traces))
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "%s max live traces per tenant exceeded: %v", overrides.ErrorPrefixLiveTracesExceeded, err)
}
maxSpans := i.limiter.limits.MaxSpansPerTrace(i.instanceID)
trace = newTrace(maxSpans, fp, traceID)
i.traces[fp] = trace
i.tracesCreatedTotal.Inc()
return trace, nil
}
// tokenForTraceID hash trace ID, should be called under lock
func (i *instance) tokenForTraceID(id []byte) uint32 {
i.hash.Reset()
_, _ = i.hash.Write(id)
return i.hash.Sum32()
}
// resetHeadBlock() should be called under lock
func (i *instance) resetHeadBlock() error {
var err error
i.headBlock, err = i.writer.WAL().NewBlock(uuid.New(), i.instanceID)
i.lastBlockCut = time.Now()
return err
}
func (i *instance) tracesToCut(cutoff time.Duration, immediate bool) []*trace {
i.tracesMtx.Lock()
defer i.tracesMtx.Unlock()
cutoffTime := time.Now().Add(cutoff)
tracesToCut := make([]*trace, 0, len(i.traces))
for key, trace := range i.traces {
if cutoffTime.After(trace.lastAppend) || immediate {
tracesToCut = append(tracesToCut, trace)
delete(i.traces, key)
}
}
return tracesToCut
}
func (i *instance) writeTraceToHeadBlock(id common.ID, b []byte) error {
i.blocksMtx.Lock()
defer i.blocksMtx.Unlock()
return i.headBlock.Write(id, b)
}
func (i *instance) Combine(objA []byte, objB []byte) []byte {
combinedTrace, err := util.CombineTraces(objA, objB)
if err != nil {
level.Error(log.Logger).Log("msg", "error combining trace protos", "err", err.Error())
}
return combinedTrace
}
// pushRequestTraceID gets the TraceID of the first span in the batch and assumes its the trace ID throughout
// this assumption should hold b/c the distributors make sure each batch all belong to the same trace
func pushRequestTraceID(req *tempopb.PushRequest) ([]byte, error) {
if req == nil || req.Batch == nil {
return nil, errors.New("req or req.Batch nil")
}
if len(req.Batch.InstrumentationLibrarySpans) == 0 {
return nil, errors.New("InstrumentationLibrarySpans has length 0")
}
if len(req.Batch.InstrumentationLibrarySpans[0].Spans) == 0 {
return nil, errors.New("Spans has length 0")
}
return req.Batch.InstrumentationLibrarySpans[0].Spans[0].TraceId, nil
}