-
Notifications
You must be signed in to change notification settings - Fork 503
/
create_block.go
54 lines (43 loc) · 1.42 KB
/
create_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
package v2
import (
"context"
"io"
"github.com/grafana/tempo/pkg/model"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/encoding/common"
"github.com/pkg/errors"
)
const DefaultFlushSizeBytes int = 30 * 1024 * 1024 // 30 MiB
func CreateBlock(ctx context.Context, cfg *common.BlockConfig, meta *backend.BlockMeta, i common.Iterator, dec model.ObjectDecoder, to backend.Writer) (*backend.BlockMeta, error) {
defer i.Close()
newBlock, err := NewStreamingBlock(cfg, meta.BlockID, meta.TenantID, []*backend.BlockMeta{meta}, meta.TotalObjects)
if err != nil {
return nil, errors.Wrap(err, "error creating streaming block")
}
var tracker backend.AppendTracker
for {
id, trBytes, err := i.Next(ctx)
if err != nil && err != io.EOF {
return nil, errors.Wrap(err, "error iterating")
}
if id == nil {
break
}
// This assumes the incoming bytes are the same data encoding.
err = newBlock.AddObject(id, trBytes)
if err != nil {
return nil, errors.Wrap(err, "error adding object to compactor block")
}
if newBlock.CurrentBufferLength() > DefaultFlushSizeBytes {
tracker, _, err = newBlock.FlushBuffer(ctx, tracker, to)
if err != nil {
return nil, errors.Wrap(err, "error flushing compactor block")
}
}
}
_, err = newBlock.Complete(ctx, tracker, to)
if err != nil {
return nil, errors.Wrap(err, "error completing compactor block")
}
return newBlock.BlockMeta(), nil
}