/
global_markers_bucket_client.go
194 lines (157 loc) · 5.81 KB
/
global_markers_bucket_client.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/storage/tsdb/bucketindex/markers_bucket_client.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package block
import (
"bytes"
"context"
"io"
"path"
"github.com/grafana/dskit/multierror"
"github.com/oklog/ulid"
"github.com/thanos-io/objstore"
)
// globalMarkersBucket is a bucket client which stores markers (eg. block deletion marks) in a per-tenant
// global location too.
type globalMarkersBucket struct {
parent objstore.Bucket
}
// BucketWithGlobalMarkers wraps the input bucket into a bucket which also keeps track of markers
// in the global markers location.
func BucketWithGlobalMarkers(b objstore.Bucket) objstore.Bucket {
return &globalMarkersBucket{
parent: b,
}
}
// Upload implements objstore.Bucket.
func (b *globalMarkersBucket) Upload(ctx context.Context, name string, r io.Reader) error {
globalMarkPath := getGlobalMarkPathFromBlockMark(name)
if globalMarkPath == "" {
return b.parent.Upload(ctx, name, r)
}
// Read the marker.
body, err := io.ReadAll(r)
if err != nil {
return err
}
// Upload it to the original location.
if err := b.parent.Upload(ctx, name, bytes.NewBuffer(body)); err != nil {
return err
}
// Upload it to the global markers location too.
return b.parent.Upload(ctx, globalMarkPath, bytes.NewBuffer(body))
}
// Delete implements objstore.Bucket.
func (b *globalMarkersBucket) Delete(ctx context.Context, name string) error {
// Call the parent. Only return error here (without deleting global marker too) if error is different than "not found".
err1 := b.parent.Delete(ctx, name)
if err1 != nil && !b.parent.IsObjNotFoundErr(err1) {
return err1
}
// Delete the marker in the global markers location too.
globalMarkPath := getGlobalMarkPathFromBlockMark(name)
if globalMarkPath == "" {
return err1
}
var err2 error
if err := b.parent.Delete(ctx, globalMarkPath); err != nil {
if !b.parent.IsObjNotFoundErr(err) {
err2 = err
}
}
if err1 != nil {
// In this case err1 is "ObjNotFound". If we tried to wrap it together with err2, we would need to
// handle this possibility in globalMarkersBucket.IsObjNotFoundErr(). Instead we just ignore err2, if any.
return err1
}
return err2
}
// Name implements objstore.Bucket.
func (b *globalMarkersBucket) Name() string {
return b.parent.Name()
}
// Close implements objstore.Bucket.
func (b *globalMarkersBucket) Close() error {
return b.parent.Close()
}
// Iter implements objstore.Bucket.
func (b *globalMarkersBucket) Iter(ctx context.Context, dir string, f func(string) error, options ...objstore.IterOption) error {
return b.parent.Iter(ctx, dir, f, options...)
}
// Get implements objstore.Bucket.
func (b *globalMarkersBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
return b.parent.Get(ctx, name)
}
// GetRange implements objstore.Bucket.
func (b *globalMarkersBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
return b.parent.GetRange(ctx, name, off, length)
}
// Exists implements objstore.Bucket.
func (b *globalMarkersBucket) Exists(ctx context.Context, name string) (bool, error) {
globalMarkPath := getGlobalMarkPathFromBlockMark(name)
if globalMarkPath == "" {
return b.parent.Exists(ctx, name)
}
// Report "exists" only if BOTH (block-local, and global) files exist, otherwise Thanos
// code will never try to upload the file again, if it finds that it exist.
ok1, err1 := b.parent.Exists(ctx, name)
ok2, err2 := b.parent.Exists(ctx, globalMarkPath)
var me multierror.MultiError
me.Add(err1)
me.Add(err2)
return ok1 && ok2, me.Err()
}
// IsObjNotFoundErr implements objstore.Bucket.
func (b *globalMarkersBucket) IsObjNotFoundErr(err error) bool {
return b.parent.IsObjNotFoundErr(err)
}
// IsAccessDeniedErr implements objstore.Bucket.
func (b *globalMarkersBucket) IsAccessDeniedErr(err error) bool {
return b.parent.IsAccessDeniedErr(err)
}
// Attributes implements objstore.Bucket.
func (b *globalMarkersBucket) Attributes(ctx context.Context, name string) (objstore.ObjectAttributes, error) {
return b.parent.Attributes(ctx, name)
}
// WithExpectedErrs implements objstore.InstrumentedBucket.
func (b *globalMarkersBucket) WithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.Bucket {
if ib, ok := b.parent.(objstore.InstrumentedBucket); ok {
return ib.WithExpectedErrs(fn)
}
return b
}
// ReaderWithExpectedErrs implements objstore.InstrumentedBucketReader.
func (b *globalMarkersBucket) ReaderWithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.BucketReader {
if ib, ok := b.parent.(objstore.InstrumentedBucketReader); ok {
return ib.ReaderWithExpectedErrs(fn)
}
return b
}
// getGlobalMarkPathFromBlockMark returns path to global mark, if name points to a block-local mark file. If name
// doesn't point to a block-local mark file, returns empty string.
func getGlobalMarkPathFromBlockMark(name string) string {
if blockID, ok := isDeletionMark(name); ok {
return path.Clean(path.Join(path.Dir(name), "../", DeletionMarkFilepath(blockID)))
}
if blockID, ok := isNoCompactMark(name); ok {
return path.Clean(path.Join(path.Dir(name), "../", NoCompactMarkFilepath(blockID)))
}
return ""
}
func isDeletionMark(name string) (ulid.ULID, bool) {
if path.Base(name) != DeletionMarkFilename {
return ulid.ULID{}, false
}
// Parse the block ID in the path. If there's no block ID, then it's not the per-block
// deletion mark.
return IsBlockDir(path.Dir(name))
}
func isNoCompactMark(name string) (ulid.ULID, bool) {
if path.Base(name) != NoCompactMarkFilename {
return ulid.ULID{}, false
}
// Parse the block ID in the path. If there's no block ID, then it's not the per-block
// no-compact mark.
return IsBlockDir(path.Dir(name))
}