forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjstore.go
406 lines (343 loc) · 11.8 KB
/
objstore.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
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
package objstore
import (
"context"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/thanos-io/thanos/pkg/runutil"
)
// Bucket provides read and write access to an object storage bucket.
// NOTE: We assume strong consistency for write-read flow.
type Bucket interface {
io.Closer
BucketReader
// Upload the contents of the reader as an object into the bucket.
// Upload should be idempotent.
Upload(ctx context.Context, name string, r io.Reader) error
// Delete removes the object with the given name.
// If object does not exists in the moment of deletion, Delete should throw error.
Delete(ctx context.Context, name string) error
// Name returns the bucket name for the provider.
Name() string
}
// BucketReader provides read access to an object storage bucket.
type BucketReader interface {
// Iter calls f for each entry in the given directory (not recursive.). The argument to f is the full
// object name including the prefix of the inspected directory.
Iter(ctx context.Context, dir string, f func(string) error) error
// Get returns a reader for the given object name.
Get(ctx context.Context, name string) (io.ReadCloser, error)
// GetRange returns a new range reader for the given object name and range.
GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error)
// Exists checks if the given object exists in the bucket.
// TODO(bplotka): Consider removing Exists in favor of helper that do Get & IsObjNotFoundErr (less code to maintain).
Exists(ctx context.Context, name string) (bool, error)
// IsObjNotFoundErr returns true if error means that object is not found. Relevant to Get operations.
IsObjNotFoundErr(err error) bool
// ObjectSize returns the size of the specified object.
ObjectSize(ctx context.Context, name string) (uint64, error)
}
// UploadDir uploads all files in srcdir to the bucket with into a top-level directory
// named dstdir. It is a caller responsibility to clean partial upload in case of failure.
func UploadDir(ctx context.Context, logger log.Logger, bkt Bucket, srcdir, dstdir string) error {
df, err := os.Stat(srcdir)
if err != nil {
return errors.Wrap(err, "stat dir")
}
if !df.IsDir() {
return errors.Errorf("%s is not a directory", srcdir)
}
return filepath.Walk(srcdir, func(src string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
dst := filepath.Join(dstdir, strings.TrimPrefix(src, srcdir))
return UploadFile(ctx, logger, bkt, src, dst)
})
}
// UploadFile uploads the file with the given name to the bucket.
// It is a caller responsibility to clean partial upload in case of failure.
func UploadFile(ctx context.Context, logger log.Logger, bkt Bucket, src, dst string) error {
r, err := os.Open(src)
if err != nil {
return errors.Wrapf(err, "open file %s", src)
}
defer runutil.CloseWithLogOnErr(logger, r, "close file %s", src)
if err := bkt.Upload(ctx, dst, r); err != nil {
return errors.Wrapf(err, "upload file %s as %s", src, dst)
}
level.Debug(logger).Log("msg", "uploaded file", "from", src, "dst", dst, "bucket", bkt.Name())
return nil
}
// DirDelim is the delimiter used to model a directory structure in an object store bucket.
const DirDelim = "/"
// DownloadFile downloads the src file from the bucket to dst. If dst is an existing
// directory, a file with the same name as the source is created in dst.
// If destination file is already existing, download file will overwrite it.
func DownloadFile(ctx context.Context, logger log.Logger, bkt BucketReader, src, dst string) (err error) {
if fi, err := os.Stat(dst); err == nil {
if fi.IsDir() {
dst = filepath.Join(dst, filepath.Base(src))
}
} else if !os.IsNotExist(err) {
return err
}
rc, err := bkt.Get(ctx, src)
if err != nil {
return errors.Wrapf(err, "get file %s", src)
}
defer runutil.CloseWithLogOnErr(logger, rc, "download block's file reader")
f, err := os.Create(dst)
if err != nil {
return errors.Wrap(err, "create file")
}
defer func() {
if err != nil {
if rerr := os.Remove(dst); rerr != nil {
level.Warn(logger).Log("msg", "failed to remove partially downloaded file", "file", dst, "err", rerr)
}
}
}()
defer runutil.CloseWithLogOnErr(logger, f, "download block's output file")
if _, err = io.Copy(f, rc); err != nil {
return errors.Wrap(err, "copy object to file")
}
return nil
}
// DownloadDir downloads all object found in the directory into the local directory.
func DownloadDir(ctx context.Context, logger log.Logger, bkt BucketReader, src, dst string) error {
if err := os.MkdirAll(dst, 0777); err != nil {
return errors.Wrap(err, "create dir")
}
var downloadedFiles []string
if err := bkt.Iter(ctx, src, func(name string) error {
if strings.HasSuffix(name, DirDelim) {
return DownloadDir(ctx, logger, bkt, name, filepath.Join(dst, filepath.Base(name)))
}
if err := DownloadFile(ctx, logger, bkt, name, dst); err != nil {
return err
}
downloadedFiles = append(downloadedFiles, dst)
return nil
}); err != nil {
// Best-effort cleanup if the download failed.
for _, f := range downloadedFiles {
if rerr := os.Remove(f); rerr != nil {
level.Warn(logger).Log("msg", "failed to remove file on partial dir download error", "file", f, "err", rerr)
}
}
return err
}
return nil
}
// Exists returns true, if file exists, otherwise false and nil error if presence IsObjNotFoundErr, otherwise false with
// returning error.
func Exists(ctx context.Context, bkt Bucket, src string) (bool, error) {
rc, err := bkt.Get(ctx, src)
if rc != nil {
_ = rc.Close()
}
if err != nil {
if bkt.IsObjNotFoundErr(err) {
return false, nil
}
return false, errors.Wrap(err, "stat object")
}
return true, nil
}
// BucketWithMetrics takes a bucket and registers metrics with the given registry for
// operations run against the bucket.
func BucketWithMetrics(name string, b Bucket, r prometheus.Registerer) Bucket {
bkt := &metricBucket{
bkt: b,
ops: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "thanos_objstore_bucket_operations_total",
Help: "Total number of operations against a bucket.",
ConstLabels: prometheus.Labels{"bucket": name},
}, []string{"operation"}),
opsFailures: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "thanos_objstore_bucket_operation_failures_total",
Help: "Total number of operations against a bucket that failed.",
ConstLabels: prometheus.Labels{"bucket": name},
}, []string{"operation"}),
opsDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "thanos_objstore_bucket_operation_duration_seconds",
Help: "Duration of operations against the bucket",
ConstLabels: prometheus.Labels{"bucket": name},
Buckets: []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120},
}, []string{"operation"}),
lastSuccessfullUploadTime: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "thanos_objstore_bucket_last_successful_upload_time",
Help: "Second timestamp of the last successful upload to the bucket.",
}, []string{"bucket"}),
}
if r != nil {
r.MustRegister(bkt.ops, bkt.opsFailures, bkt.opsDuration, bkt.lastSuccessfullUploadTime)
}
return bkt
}
type metricBucket struct {
bkt Bucket
ops *prometheus.CounterVec
opsFailures *prometheus.CounterVec
opsDuration *prometheus.HistogramVec
lastSuccessfullUploadTime *prometheus.GaugeVec
}
func (b *metricBucket) Iter(ctx context.Context, dir string, f func(name string) error) error {
const op = "iter"
err := b.bkt.Iter(ctx, dir, f)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
}
b.ops.WithLabelValues(op).Inc()
return err
}
// ObjectSize returns the size of the specified object.
func (b *metricBucket) ObjectSize(ctx context.Context, name string) (uint64, error) {
const op = "objectsize"
b.ops.WithLabelValues(op).Inc()
start := time.Now()
rc, err := b.bkt.ObjectSize(ctx, name)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
return 0, err
}
b.opsDuration.WithLabelValues(op).Observe(time.Since(start).Seconds())
return rc, nil
}
func (b *metricBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
const op = "get"
b.ops.WithLabelValues(op).Inc()
rc, err := b.bkt.Get(ctx, name)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
return nil, err
}
rc = newTimingReadCloser(
rc,
op,
b.opsDuration,
b.opsFailures,
)
return rc, nil
}
func (b *metricBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
const op = "get_range"
b.ops.WithLabelValues(op).Inc()
rc, err := b.bkt.GetRange(ctx, name, off, length)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
return nil, err
}
rc = newTimingReadCloser(
rc,
op,
b.opsDuration,
b.opsFailures,
)
return rc, nil
}
func (b *metricBucket) Exists(ctx context.Context, name string) (bool, error) {
const op = "exists"
start := time.Now()
ok, err := b.bkt.Exists(ctx, name)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
}
b.ops.WithLabelValues(op).Inc()
b.opsDuration.WithLabelValues(op).Observe(time.Since(start).Seconds())
return ok, err
}
func (b *metricBucket) Upload(ctx context.Context, name string, r io.Reader) error {
const op = "upload"
start := time.Now()
err := b.bkt.Upload(ctx, name, r)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
} else {
// TODO: Use SetToCurrentTime() once we update the Prometheus client_golang.
b.lastSuccessfullUploadTime.WithLabelValues(b.bkt.Name()).Set(float64(time.Now().UnixNano()) / 1e9)
}
b.ops.WithLabelValues(op).Inc()
b.opsDuration.WithLabelValues(op).Observe(time.Since(start).Seconds())
return err
}
func (b *metricBucket) Delete(ctx context.Context, name string) error {
const op = "delete"
start := time.Now()
err := b.bkt.Delete(ctx, name)
if err != nil {
b.opsFailures.WithLabelValues(op).Inc()
}
b.ops.WithLabelValues(op).Inc()
b.opsDuration.WithLabelValues(op).Observe(time.Since(start).Seconds())
return err
}
func (b *metricBucket) IsObjNotFoundErr(err error) bool {
return b.bkt.IsObjNotFoundErr(err)
}
func (b *metricBucket) Close() error {
return b.bkt.Close()
}
func (b *metricBucket) Name() string {
return b.bkt.Name()
}
type timingReadCloser struct {
io.ReadCloser
ok bool
start time.Time
op string
duration *prometheus.HistogramVec
failed *prometheus.CounterVec
}
func newTimingReadCloser(rc io.ReadCloser, op string, dur *prometheus.HistogramVec, failed *prometheus.CounterVec) *timingReadCloser {
// Initialize the metrics with 0.
dur.WithLabelValues(op)
failed.WithLabelValues(op)
return &timingReadCloser{
ReadCloser: rc,
ok: true,
start: time.Now(),
op: op,
duration: dur,
failed: failed,
}
}
func (rc *timingReadCloser) Close() error {
err := rc.ReadCloser.Close()
rc.duration.WithLabelValues(rc.op).Observe(time.Since(rc.start).Seconds())
if rc.ok && err != nil {
rc.failed.WithLabelValues(rc.op).Inc()
rc.ok = false
}
return err
}
func (rc *timingReadCloser) Read(b []byte) (n int, err error) {
n, err = rc.ReadCloser.Read(b)
if rc.ok && err != nil && err != io.EOF {
rc.failed.WithLabelValues(rc.op).Inc()
rc.ok = false
}
return n, err
}
func CreateTemporaryTestBucketName(t testing.TB) string {
src := rand.NewSource(time.Now().UnixNano())
// Bucket name need to conform: https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html.
name := strings.Replace(strings.Replace(fmt.Sprintf("test_%x_%s", src.Int63(), strings.ToLower(t.Name())), "_", "-", -1), "/", "-", -1)
if len(name) >= 63 {
name = name[:63]
}
return name
}