-
Notifications
You must be signed in to change notification settings - Fork 508
/
compactor.go
81 lines (65 loc) · 1.67 KB
/
compactor.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
package gcs
import (
"context"
"encoding/json"
"fmt"
"cloud.google.com/go/storage"
"github.com/google/uuid"
"github.com/grafana/tempo/tempodb/backend"
"google.golang.org/api/iterator"
)
func (rw *readerWriter) MarkBlockCompacted(blockID uuid.UUID, tenantID string) error {
// move meta file to a new location
metaFilename := backend.MetaFileName(blockID, tenantID)
compactedMetaFilename := backend.CompactedMetaFileName(blockID, tenantID)
src := rw.bucket.Object(metaFilename)
dst := rw.bucket.Object(compactedMetaFilename)
ctx := context.TODO()
_, err := dst.CopierFrom(src).Run(ctx)
if err != nil {
return err
}
return src.Delete(ctx)
}
func (rw *readerWriter) ClearBlock(blockID uuid.UUID, tenantID string) error {
if len(tenantID) == 0 {
return fmt.Errorf("empty tenant id")
}
if blockID == uuid.Nil {
return fmt.Errorf("empty block id")
}
ctx := context.TODO()
iter := rw.bucket.Objects(ctx, &storage.Query{
Prefix: backend.RootPath(blockID, tenantID),
Versions: false,
})
for {
attrs, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
o := rw.bucket.Object(attrs.Name)
err = o.Delete(ctx)
if err != nil {
return err
}
}
return nil
}
func (rw *readerWriter) CompactedBlockMeta(blockID uuid.UUID, tenantID string) (*backend.CompactedBlockMeta, error) {
name := backend.CompactedMetaFileName(blockID, tenantID)
bytes, modTime, err := rw.readAllWithModTime(context.Background(), name)
if err != nil {
return nil, readError(err)
}
out := &backend.CompactedBlockMeta{}
err = json.Unmarshal(bytes, out)
if err != nil {
return nil, err
}
out.CompactedTime = modTime
return out, nil
}