forked from thanos-io/objstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objstore_test.go
596 lines (501 loc) · 26.9 KB
/
objstore_test.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package objstore
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
promtest "github.com/prometheus/client_golang/prometheus/testutil"
"go.uber.org/atomic"
)
func TestMetricBucket_Close(t *testing.T) {
bkt := WrapWithMetrics(NewInMemBucket(), nil, "abc")
// Expected initialized metrics.
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.ops))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.opsFailures))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.opsDuration))
AcceptanceTest(t, bkt.WithExpectedErrs(bkt.IsObjNotFoundErr))
testutil.Equals(t, float64(9), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpIter)))
testutil.Equals(t, float64(2), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpAttributes)))
testutil.Equals(t, float64(3), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpGet)))
testutil.Equals(t, float64(3), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpGetRange)))
testutil.Equals(t, float64(2), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpExists)))
testutil.Equals(t, float64(9), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpUpload)))
testutil.Equals(t, float64(3), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpDelete)))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.ops))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpIter)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpAttributes)))
testutil.Equals(t, float64(1), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpGet)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpGetRange)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpExists)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpUpload)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpDelete)))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.opsFailures))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.opsDuration))
lastUpload := promtest.ToFloat64(bkt.metrics.lastSuccessfulUploadTime)
testutil.Assert(t, lastUpload > 0, "last upload not greater than 0, val: %f", lastUpload)
// Clear bucket, but don't clear metrics to ensure we use same.
bkt.bkt = NewInMemBucket()
AcceptanceTest(t, bkt)
testutil.Equals(t, float64(18), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpIter)))
testutil.Equals(t, float64(4), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpAttributes)))
testutil.Equals(t, float64(6), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpGet)))
testutil.Equals(t, float64(6), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpGetRange)))
testutil.Equals(t, float64(4), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpExists)))
testutil.Equals(t, float64(18), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpUpload)))
testutil.Equals(t, float64(6), promtest.ToFloat64(bkt.metrics.ops.WithLabelValues(OpDelete)))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.ops))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpIter)))
// Not expected not found error here.
testutil.Equals(t, float64(1), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpAttributes)))
// Not expected not found errors, this should increment failure metric on get for not found as well, so +2.
testutil.Equals(t, float64(3), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpGet)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpGetRange)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpExists)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpUpload)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bkt.metrics.opsFailures.WithLabelValues(OpDelete)))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.opsFailures))
testutil.Equals(t, 7, promtest.CollectAndCount(bkt.metrics.opsDuration))
testutil.Assert(t, promtest.ToFloat64(bkt.metrics.lastSuccessfulUploadTime) > lastUpload)
}
// TestMetricBucket_MultipleClients tests that the metrics from two different buckets clients are not conflicting with each other.
func TestMetricBucket_Multiple_Clients(t *testing.T) {
reg := prometheus.NewPedanticRegistry()
WrapWithMetrics(NewInMemBucket(), reg, "abc")
WrapWithMetrics(NewInMemBucket(), reg, "def")
}
func TestMetricBucket_UploadShouldPreserveReaderFeatures(t *testing.T) {
tests := map[string]struct {
reader io.Reader
expectedIsSeeker bool
expectedIsReaderAt bool
}{
"bytes.Reader": {
reader: bytes.NewReader([]byte("1")),
expectedIsSeeker: true,
expectedIsReaderAt: true,
},
"bytes.Buffer": {
reader: bytes.NewBuffer([]byte("1")),
expectedIsSeeker: false,
expectedIsReaderAt: false,
},
"os.File": {
reader: func() io.Reader {
// Create a test file.
testFilepath := filepath.Join(t.TempDir(), "test")
testutil.Ok(t, os.WriteFile(testFilepath, []byte("test"), os.ModePerm))
// Open the file (it will be used as io.Reader).
file, err := os.Open(testFilepath)
testutil.Ok(t, err)
t.Cleanup(func() {
testutil.Ok(t, file.Close())
})
return file
}(),
expectedIsSeeker: true,
expectedIsReaderAt: true,
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
var uploadReader io.Reader
m := &mockBucket{
Bucket: WrapWithMetrics(NewInMemBucket(), nil, ""),
upload: func(ctx context.Context, name string, r io.Reader) error {
uploadReader = r
return nil
},
}
testutil.Ok(t, m.Upload(context.Background(), "dir/obj1", testData.reader))
_, isSeeker := uploadReader.(io.Seeker)
testutil.Equals(t, testData.expectedIsSeeker, isSeeker)
_, isReaderAt := uploadReader.(io.ReaderAt)
testutil.Equals(t, testData.expectedIsReaderAt, isReaderAt)
})
}
}
func TestMetricBucket_ReaderClose(t *testing.T) {
const objPath = "dir/obj1"
t.Run("Upload() should not close the input Reader", func(t *testing.T) {
closeCalled := false
reader := &mockReader{
Reader: bytes.NewBuffer([]byte("test")),
close: func() error {
closeCalled = true
return nil
},
}
bucket := WrapWithMetrics(NewInMemBucket(), nil, "")
testutil.Ok(t, bucket.Upload(context.Background(), objPath, reader))
// Should not call Close() on the reader.
testutil.Assert(t, !closeCalled)
// An explicit call to Close() should close it.
testutil.Ok(t, reader.Close())
testutil.Assert(t, closeCalled)
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.ops.WithLabelValues(OpUpload)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bucket.metrics.opsFailures.WithLabelValues(OpUpload)))
})
t.Run("Get() should return a wrapper io.ReadCloser that correctly Close the wrapped one", func(t *testing.T) {
closeCalled := false
origReader := &mockReader{
Reader: bytes.NewBuffer([]byte("test")),
close: func() error {
closeCalled = true
return nil
},
}
bucket := WrapWithMetrics(&mockBucket{
get: func(_ context.Context, _ string) (io.ReadCloser, error) {
return origReader, nil
},
}, nil, "")
wrappedReader, err := bucket.Get(context.Background(), objPath)
testutil.Ok(t, err)
testutil.Assert(t, origReader != wrappedReader)
// Calling Close() to the wrappedReader should close origReader.
testutil.Assert(t, !closeCalled)
testutil.Ok(t, wrappedReader.Close())
testutil.Assert(t, closeCalled)
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.ops.WithLabelValues(OpGet)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bucket.metrics.opsFailures.WithLabelValues(OpGet)))
})
t.Run("GetRange() should return a wrapper io.ReadCloser that correctly Close the wrapped one", func(t *testing.T) {
closeCalled := false
origReader := &mockReader{
Reader: bytes.NewBuffer([]byte("test")),
close: func() error {
closeCalled = true
return nil
},
}
bucket := WrapWithMetrics(&mockBucket{
getRange: func(_ context.Context, _ string, _, _ int64) (io.ReadCloser, error) {
return origReader, nil
},
}, nil, "")
wrappedReader, err := bucket.GetRange(context.Background(), objPath, 0, 1)
testutil.Ok(t, err)
testutil.Assert(t, origReader != wrappedReader)
// Calling Close() to the wrappedReader should close origReader.
testutil.Assert(t, !closeCalled)
testutil.Ok(t, wrappedReader.Close())
testutil.Assert(t, closeCalled)
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.ops.WithLabelValues(OpGetRange)))
testutil.Equals(t, float64(0), promtest.ToFloat64(bucket.metrics.opsFailures.WithLabelValues(OpGetRange)))
})
}
func TestMetricBucket_ReaderCloseError(t *testing.T) {
origReader := &mockReader{
Reader: bytes.NewBuffer([]byte("test")),
close: func() error {
return errors.New("mocked error")
},
}
t.Run("Get() should track failure if reader Close() returns error", func(t *testing.T) {
bucket := WrapWithMetrics(&mockBucket{
get: func(ctx context.Context, name string) (io.ReadCloser, error) {
return origReader, nil
},
}, nil, "")
testutil.NotOk(t, bucket.Upload(context.Background(), "test", origReader))
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.ops.WithLabelValues(OpUpload)))
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.opsFailures.WithLabelValues(OpUpload)))
})
t.Run("Get() should track failure if reader Close() returns error", func(t *testing.T) {
bucket := WrapWithMetrics(&mockBucket{
get: func(ctx context.Context, name string) (io.ReadCloser, error) {
return origReader, nil
},
}, nil, "")
reader, err := bucket.Get(context.Background(), "test")
testutil.Ok(t, err)
testutil.NotOk(t, reader.Close())
testutil.NotOk(t, reader.Close()) // Called twice to ensure metrics are not tracked twice.
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.ops.WithLabelValues(OpGet)))
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.opsFailures.WithLabelValues(OpGet)))
})
t.Run("GetRange() should track failure if reader Close() returns error", func(t *testing.T) {
bucket := WrapWithMetrics(&mockBucket{
getRange: func(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
return origReader, nil
},
}, nil, "")
reader, err := bucket.GetRange(context.Background(), "test", 0, 1)
testutil.Ok(t, err)
testutil.NotOk(t, reader.Close())
testutil.NotOk(t, reader.Close()) // Called twice to ensure metrics are not tracked twice.
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.ops.WithLabelValues(OpGetRange)))
testutil.Equals(t, float64(1), promtest.ToFloat64(bucket.metrics.opsFailures.WithLabelValues(OpGetRange)))
})
}
func TestDownloadUploadDirConcurrency(t *testing.T) {
r := prometheus.NewRegistry()
m := WrapWithMetrics(NewInMemBucket(), r, "")
tempDir := t.TempDir()
testutil.Ok(t, m.Upload(context.Background(), "dir/obj1", bytes.NewReader([]byte("1"))))
testutil.Ok(t, m.Upload(context.Background(), "dir/obj2", bytes.NewReader([]byte("2"))))
testutil.Ok(t, m.Upload(context.Background(), "dir/obj3", bytes.NewReader(bytes.Repeat([]byte("3"), 1024*1024))))
testutil.Ok(t, promtest.GatherAndCompare(r, strings.NewReader(`
# HELP objstore_bucket_operations_total Total number of all attempted operations against a bucket.
# TYPE objstore_bucket_operations_total counter
objstore_bucket_operations_total{bucket="",operation="attributes"} 0
objstore_bucket_operations_total{bucket="",operation="delete"} 0
objstore_bucket_operations_total{bucket="",operation="exists"} 0
objstore_bucket_operations_total{bucket="",operation="get"} 0
objstore_bucket_operations_total{bucket="",operation="get_range"} 0
objstore_bucket_operations_total{bucket="",operation="iter"} 0
objstore_bucket_operations_total{bucket="",operation="upload"} 3
`), `objstore_bucket_operations_total`))
testutil.Ok(t, DownloadDir(context.Background(), log.NewNopLogger(), m, "dir/", "dir/", tempDir, WithFetchConcurrency(10)))
i, err := os.ReadDir(tempDir)
testutil.Ok(t, err)
testutil.Assert(t, len(i) == 3)
testutil.Ok(t, promtest.GatherAndCompare(r, strings.NewReader(`
# HELP objstore_bucket_operations_total Total number of all attempted operations against a bucket.
# TYPE objstore_bucket_operations_total counter
objstore_bucket_operations_total{bucket="",operation="attributes"} 0
objstore_bucket_operations_total{bucket="",operation="delete"} 0
objstore_bucket_operations_total{bucket="",operation="exists"} 0
objstore_bucket_operations_total{bucket="",operation="get"} 3
objstore_bucket_operations_total{bucket="",operation="get_range"} 0
objstore_bucket_operations_total{bucket="",operation="iter"} 1
objstore_bucket_operations_total{bucket="",operation="upload"} 3
`), `objstore_bucket_operations_total`))
testutil.Ok(t, promtest.GatherAndCompare(r, strings.NewReader(`
# HELP objstore_bucket_operation_fetched_bytes_total Total number of bytes fetched from bucket, per operation.
# TYPE objstore_bucket_operation_fetched_bytes_total counter
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="attributes"} 0
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="delete"} 0
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="exists"} 0
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="get"} 1.048578e+06
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="get_range"} 0
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="iter"} 0
objstore_bucket_operation_fetched_bytes_total{bucket="",operation="upload"} 0
`), `objstore_bucket_operation_fetched_bytes_total`))
testutil.Ok(t, promtest.GatherAndCompare(r, strings.NewReader(`
# HELP objstore_bucket_operation_transferred_bytes Number of bytes transferred from/to bucket per operation.
# TYPE objstore_bucket_operation_transferred_bytes histogram
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="32768"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="65536"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="131072"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="262144"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="524288"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="1.048576e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="2.097152e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="4.194304e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="8.388608e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="1.6777216e+07"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="3.3554432e+07"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="6.7108864e+07"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="1.34217728e+08"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="2.68435456e+08"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="5.36870912e+08"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="1.073741824e+09"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get",le="+Inf"} 3
objstore_bucket_operation_transferred_bytes_sum{bucket="",operation="get"} 1.048578e+06
objstore_bucket_operation_transferred_bytes_count{bucket="",operation="get"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="32768"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="65536"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="131072"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="262144"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="524288"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="1.048576e+06"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="2.097152e+06"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="4.194304e+06"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="8.388608e+06"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="1.6777216e+07"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="3.3554432e+07"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="6.7108864e+07"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="1.34217728e+08"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="2.68435456e+08"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="5.36870912e+08"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="1.073741824e+09"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="get_range",le="+Inf"} 0
objstore_bucket_operation_transferred_bytes_sum{bucket="",operation="get_range"} 0
objstore_bucket_operation_transferred_bytes_count{bucket="",operation="get_range"} 0
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="32768"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="65536"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="131072"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="262144"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="524288"} 2
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="1.048576e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="2.097152e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="4.194304e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="8.388608e+06"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="1.6777216e+07"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="3.3554432e+07"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="6.7108864e+07"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="1.34217728e+08"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="2.68435456e+08"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="5.36870912e+08"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="1.073741824e+09"} 3
objstore_bucket_operation_transferred_bytes_bucket{bucket="",operation="upload",le="+Inf"} 3
objstore_bucket_operation_transferred_bytes_sum{bucket="",operation="upload"} 1.048578e+06
objstore_bucket_operation_transferred_bytes_count{bucket="",operation="upload"} 3
`), `objstore_bucket_operation_transferred_bytes`))
testutil.Ok(t, UploadDir(context.Background(), log.NewNopLogger(), m, tempDir, "/dir-copy", WithUploadConcurrency(10)))
testutil.Ok(t, promtest.GatherAndCompare(r, strings.NewReader(`
# HELP objstore_bucket_operations_total Total number of all attempted operations against a bucket.
# TYPE objstore_bucket_operations_total counter
objstore_bucket_operations_total{bucket="",operation="attributes"} 0
objstore_bucket_operations_total{bucket="",operation="delete"} 0
objstore_bucket_operations_total{bucket="",operation="exists"} 0
objstore_bucket_operations_total{bucket="",operation="get"} 3
objstore_bucket_operations_total{bucket="",operation="get_range"} 0
objstore_bucket_operations_total{bucket="",operation="iter"} 1
objstore_bucket_operations_total{bucket="",operation="upload"} 6
`), `objstore_bucket_operations_total`))
}
func TestTimingReader(t *testing.T) {
m := WrapWithMetrics(NewInMemBucket(), nil, "")
r := bytes.NewReader([]byte("hello world"))
tr := newTimingReader(r, true, OpGet, m.metrics.opsDuration, m.metrics.opsFailures, func(err error) bool {
return false
}, m.metrics.opsFetchedBytes, m.metrics.opsTransferredBytes)
size, err := TryToGetSize(tr)
testutil.Ok(t, err)
testutil.Equals(t, int64(11), size)
smallBuf := make([]byte, 4)
n, err := io.ReadFull(tr, smallBuf)
testutil.Ok(t, err)
testutil.Equals(t, 4, n)
// Verify that size is still the same, after reading 4 bytes.
size, err = TryToGetSize(tr)
testutil.Ok(t, err)
testutil.Equals(t, int64(11), size)
// Given the reader was bytes.Reader it should both implement io.Seeker and io.ReaderAt.
_, isSeeker := tr.(io.Seeker)
testutil.Assert(t, isSeeker)
_, isReaderAt := tr.(io.ReaderAt)
testutil.Assert(t, isReaderAt)
testutil.Equals(t, float64(0), promtest.ToFloat64(m.metrics.opsFailures.WithLabelValues(OpGet)))
}
func TestTimingReader_ExpectedError(t *testing.T) {
readerErr := errors.New("something went wrong")
m := WrapWithMetrics(NewInMemBucket(), nil, "")
r := dummyReader{readerErr}
tr := newTimingReader(r, true, OpGet, m.metrics.opsDuration, m.metrics.opsFailures, func(err error) bool { return errors.Is(err, readerErr) }, m.metrics.opsFetchedBytes, m.metrics.opsTransferredBytes)
buf := make([]byte, 1)
_, err := io.ReadFull(tr, buf)
testutil.Equals(t, readerErr, err)
testutil.Equals(t, float64(0), promtest.ToFloat64(m.metrics.opsFailures.WithLabelValues(OpGet)))
}
func TestTimingReader_UnexpectedError(t *testing.T) {
readerErr := errors.New("something went wrong")
m := WrapWithMetrics(NewInMemBucket(), nil, "")
r := dummyReader{readerErr}
tr := newTimingReader(r, true, OpGet, m.metrics.opsDuration, m.metrics.opsFailures, func(err error) bool { return false }, m.metrics.opsFetchedBytes, m.metrics.opsTransferredBytes)
buf := make([]byte, 1)
_, err := io.ReadFull(tr, buf)
testutil.Equals(t, readerErr, err)
testutil.Equals(t, float64(1), promtest.ToFloat64(m.metrics.opsFailures.WithLabelValues(OpGet)))
}
func TestTimingReader_ContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
m := WrapWithMetrics(NewInMemBucket(), nil, "")
r := dummyReader{ctx.Err()}
tr := newTimingReader(r, true, OpGet, m.metrics.opsDuration, m.metrics.opsFailures, func(err error) bool { return false }, m.metrics.opsFetchedBytes, m.metrics.opsTransferredBytes)
buf := make([]byte, 1)
_, err := io.ReadFull(tr, buf)
testutil.Equals(t, ctx.Err(), err)
testutil.Equals(t, float64(0), promtest.ToFloat64(m.metrics.opsFailures.WithLabelValues(OpGet)))
}
type dummyReader struct {
err error
}
func (r dummyReader) Read(_ []byte) (int, error) {
return 0, r.err
}
func TestTimingReader_ShouldCorrectlyWrapFile(t *testing.T) {
// Create a test file.
testFilepath := filepath.Join(t.TempDir(), "test")
testutil.Ok(t, os.WriteFile(testFilepath, []byte("test"), os.ModePerm))
// Open the file (it will be used as io.Reader).
file, err := os.Open(testFilepath)
testutil.Ok(t, err)
t.Cleanup(func() {
testutil.Ok(t, file.Close())
})
m := WrapWithMetrics(NewInMemBucket(), nil, "")
r := newTimingReader(file, true, "", m.metrics.opsDuration, m.metrics.opsFailures, func(err error) bool {
return false
}, m.metrics.opsFetchedBytes, m.metrics.opsTransferredBytes)
// It must both implement io.Seeker and io.ReaderAt.
_, isSeeker := r.(io.Seeker)
testutil.Assert(t, isSeeker)
_, isReaderAt := r.(io.ReaderAt)
testutil.Assert(t, isReaderAt)
}
func TestDownloadDir_CleanUp(t *testing.T) {
b := unreliableBucket{
Bucket: NewInMemBucket(),
n: 3,
current: atomic.NewInt32(0),
}
tempDir := t.TempDir()
testutil.Ok(t, b.Upload(context.Background(), "dir/obj1", bytes.NewReader([]byte("1"))))
testutil.Ok(t, b.Upload(context.Background(), "dir/obj2", bytes.NewReader([]byte("2"))))
testutil.Ok(t, b.Upload(context.Background(), "dir/obj3", bytes.NewReader([]byte("3"))))
// We exapect the third Get to fail
testutil.NotOk(t, DownloadDir(context.Background(), log.NewNopLogger(), b, "dir/", "dir/", tempDir))
_, err := os.Stat(tempDir)
testutil.Assert(t, os.IsNotExist(err))
}
// unreliableBucket implements Bucket and returns an error on every n-th Get.
type unreliableBucket struct {
Bucket
n int32
current *atomic.Int32
}
func (b unreliableBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
if b.current.Inc()%b.n == 0 {
return nil, errors.Errorf("some error message")
}
return b.Bucket.Get(ctx, name)
}
// mockReader implements io.ReadCloser and allows to mock the functions.
type mockReader struct {
io.Reader
close func() error
}
func (r *mockReader) Close() error {
if r.close != nil {
return r.close()
}
return nil
}
// mockBucket implements Bucket and allows to mock the functions.
type mockBucket struct {
Bucket
upload func(ctx context.Context, name string, r io.Reader) error
get func(ctx context.Context, name string) (io.ReadCloser, error)
getRange func(ctx context.Context, name string, off, length int64) (io.ReadCloser, error)
}
func (b *mockBucket) Upload(ctx context.Context, name string, r io.Reader) error {
if b.upload != nil {
return b.upload(ctx, name, r)
}
return errors.New("Upload has not been mocked")
}
func (b *mockBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
if b.get != nil {
return b.get(ctx, name)
}
return nil, errors.New("Get has not been mocked")
}
func (b *mockBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
if b.getRange != nil {
return b.getRange(ctx, name, off, length)
}
return nil, errors.New("GetRange has not been mocked")
}