Skip to content

Commit e8b17f5

Browse files
committed
Fixes after rebase
Signed-off-by: Marco Pracucci <marco@pracucci.com>
1 parent 0be28e2 commit e8b17f5

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

pkg/store/cache/caching_bucket.go

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package storecache
66
import (
77
"bytes"
88
"context"
9-
"encoding/binary"
109
"encoding/json"
1110
"fmt"
1211
"io"
@@ -36,7 +35,7 @@ const (
3635
opGetRange = "getrange"
3736
opIter = "iter"
3837
opExists = "exists"
39-
opObjectSize = "objectsize"
38+
opAttributes = "attributes"
4039
)
4140

4241
var errObjNotFound = errors.Errorf("object not found")
@@ -291,50 +290,58 @@ func (cb *CachingBucket) GetRange(ctx context.Context, name string, off, length
291290
return r, err
292291
}
293292

294-
func (cb *CachingBucket) ObjectSize(ctx context.Context, name string) (uint64, error) {
295-
cfgName, cfg := cb.cfg.findObjectSizeConfig(name)
293+
func (cb *CachingBucket) Attributes(ctx context.Context, name string) (objstore.ObjectAttributes, error) {
294+
cfgName, cfg := cb.cfg.findAttributesConfig(name)
296295
if cfg == nil {
297-
return cb.Bucket.ObjectSize(ctx, name)
296+
return cb.Bucket.Attributes(ctx, name)
298297
}
299298

300-
return cb.cachedObjectSize(ctx, name, cfgName, cfg.cache, cfg.ttl)
299+
return cb.cachedAttributes(ctx, name, cfgName, cfg.cache, cfg.ttl)
301300
}
302301

303-
func (cb *CachingBucket) cachedObjectSize(ctx context.Context, name string, cfgName string, cache cache.Cache, ttl time.Duration) (uint64, error) {
304-
key := cachingKeyObjectSize(name)
302+
func (cb *CachingBucket) cachedAttributes(ctx context.Context, name string, cfgName string, cache cache.Cache, ttl time.Duration) (objstore.ObjectAttributes, error) {
303+
key := cachingKeyAttributes(name)
305304

306-
cb.operationRequests.WithLabelValues(opObjectSize, cfgName).Inc()
305+
cb.operationRequests.WithLabelValues(opAttributes, cfgName).Inc()
307306

308307
hits := cache.Fetch(ctx, []string{key})
309-
if s := hits[key]; len(s) == 8 {
310-
cb.operationHits.WithLabelValues(opObjectSize, cfgName).Inc()
311-
return binary.BigEndian.Uint64(s), nil
308+
if raw, ok := hits[key]; ok {
309+
var attrs objstore.ObjectAttributes
310+
err := json.Unmarshal(raw, &attrs)
311+
if err == nil {
312+
cb.operationHits.WithLabelValues(opAttributes, cfgName).Inc()
313+
return attrs, nil
314+
}
315+
316+
level.Warn(cb.logger).Log("msg", "failed to decode cached Attributes result", "key", key, "err", err)
312317
}
313318

314-
size, err := cb.Bucket.ObjectSize(ctx, name)
319+
attrs, err := cb.Bucket.Attributes(ctx, name)
315320
if err != nil {
316-
return 0, err
321+
return objstore.ObjectAttributes{}, err
317322
}
318323

319-
var buf [8]byte
320-
binary.BigEndian.PutUint64(buf[:], size)
321-
cache.Store(ctx, map[string][]byte{key: buf[:]}, ttl)
324+
if raw, err := json.Marshal(attrs); err == nil {
325+
cache.Store(ctx, map[string][]byte{key: raw}, ttl)
326+
} else {
327+
level.Warn(cb.logger).Log("msg", "failed to encode cached Attributes result", "key", key, "err", err)
328+
}
322329

323-
return size, nil
330+
return attrs, nil
324331
}
325332

326333
func (cb *CachingBucket) cachedGetRange(ctx context.Context, name string, offset, length int64, cfgName string, cfg *getRangeConfig) (io.ReadCloser, error) {
327334
cb.operationRequests.WithLabelValues(opGetRange, cfgName).Inc()
328335
cb.requestedGetRangeBytes.WithLabelValues(cfgName).Add(float64(length))
329336

330-
size, err := cb.cachedObjectSize(ctx, name, cfgName, cfg.cache, cfg.objectSizeTTL)
337+
attrs, err := cb.cachedAttributes(ctx, name, cfgName, cfg.cache, cfg.attributesTTL)
331338
if err != nil {
332-
return nil, errors.Wrapf(err, "failed to get size of object: %s", name)
339+
return nil, errors.Wrapf(err, "failed to get object attributes: %s", name)
333340
}
334341

335342
// If length goes over object size, adjust length. We use it later to limit number of read bytes.
336-
if uint64(offset+length) > size {
337-
length = int64(size - uint64(offset))
343+
if offset+length > attrs.Size {
344+
length = attrs.Size - offset
338345
}
339346

340347
// Start and end range are subrange-aligned offsets into object, that we're going to read.
@@ -347,9 +354,9 @@ func (cb *CachingBucket) cachedGetRange(ctx context.Context, name string, offset
347354
// The very last subrange in the object may have length that is not divisible by subrange size.
348355
lastSubrangeOffset := endRange - cfg.subrangeSize
349356
lastSubrangeLength := int(cfg.subrangeSize)
350-
if uint64(endRange) > size {
351-
lastSubrangeOffset = (int64(size) / cfg.subrangeSize) * cfg.subrangeSize
352-
lastSubrangeLength = int(int64(size) - lastSubrangeOffset)
357+
if endRange > attrs.Size {
358+
lastSubrangeOffset = (attrs.Size / cfg.subrangeSize) * cfg.subrangeSize
359+
lastSubrangeLength = int(attrs.Size - lastSubrangeOffset)
353360
}
354361

355362
numSubranges := (endRange - startRange) / cfg.subrangeSize
@@ -360,8 +367,8 @@ func (cb *CachingBucket) cachedGetRange(ctx context.Context, name string, offset
360367
totalRequestedBytes := int64(0)
361368
for off := startRange; off < endRange; off += cfg.subrangeSize {
362369
end := off + cfg.subrangeSize
363-
if end > int64(size) {
364-
end = int64(size)
370+
if end > attrs.Size {
371+
end = attrs.Size
365372
}
366373
totalRequestedBytes += (end - off)
367374

@@ -489,8 +496,8 @@ func mergeRanges(input []rng, limit int64) []rng {
489496
return input[:last+1]
490497
}
491498

492-
func cachingKeyObjectSize(name string) string {
493-
return fmt.Sprintf("size:%s", name)
499+
func cachingKeyAttributes(name string) string {
500+
return fmt.Sprintf("attrs:%s", name)
494501
}
495502

496503
func cachingKeyObjectSubrange(name string, start int64, end int64) string {

pkg/store/cache/caching_bucket_config.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type CachingBucketConfig struct {
2323
iter map[string]*iterConfig
2424
exists map[string]*existsConfig
2525
getRange map[string]*getRangeConfig
26-
objectSize map[string]*objectSizeConfig
26+
attributes map[string]*attributesConfig
2727
}
2828

2929
func NewCachingBucketConfig() *CachingBucketConfig {
@@ -32,7 +32,7 @@ func NewCachingBucketConfig() *CachingBucketConfig {
3232
iter: map[string]*iterConfig{},
3333
exists: map[string]*existsConfig{},
3434
getRange: map[string]*getRangeConfig{},
35-
objectSize: map[string]*objectSizeConfig{},
35+
attributes: map[string]*attributesConfig{},
3636
}
3737
}
3838

@@ -65,11 +65,11 @@ type getRangeConfig struct {
6565
operationConfig
6666
subrangeSize int64
6767
maxSubRequests int
68-
objectSizeTTL time.Duration
68+
attributesTTL time.Duration
6969
subrangeTTL time.Duration
7070
}
7171

72-
type objectSizeConfig struct {
72+
type attributesConfig struct {
7373
operationConfig
7474
ttl time.Duration
7575
}
@@ -124,19 +124,19 @@ func (cfg *CachingBucketConfig) CacheExists(configName string, cache cache.Cache
124124
// Single "GetRange" requests can result in multiple smaller GetRange sub-requests issued on the underlying bucket.
125125
// MaxSubRequests specifies how many such subrequests may be issued. Values <= 0 mean there is no limit (requests
126126
// for adjacent missing subranges are still merged).
127-
func (cfg *CachingBucketConfig) CacheGetRange(configName string, cache cache.Cache, matcher func(string) bool, subrangeSize int64, objectSizeTTL, subrangeTTL time.Duration, maxSubRequests int) {
127+
func (cfg *CachingBucketConfig) CacheGetRange(configName string, cache cache.Cache, matcher func(string) bool, subrangeSize int64, attributesTTL, subrangeTTL time.Duration, maxSubRequests int) {
128128
cfg.getRange[configName] = &getRangeConfig{
129129
operationConfig: newOperationConfig(cache, matcher),
130130
subrangeSize: subrangeSize,
131-
objectSizeTTL: objectSizeTTL,
131+
attributesTTL: attributesTTL,
132132
subrangeTTL: subrangeTTL,
133133
maxSubRequests: maxSubRequests,
134134
}
135135
}
136136

137-
// CacheObjectSize configures caching of "ObjectSize" operation for matching files.
138-
func (cfg *CachingBucketConfig) CacheObjectSize(configName string, cache cache.Cache, matcher func(name string) bool, ttl time.Duration) {
139-
cfg.objectSize[configName] = &objectSizeConfig{
137+
// CacheAttributes configures caching of "Attributes" operation for matching files.
138+
func (cfg *CachingBucketConfig) CacheAttributes(configName string, cache cache.Cache, matcher func(name string) bool, ttl time.Duration) {
139+
cfg.attributes[configName] = &attributesConfig{
140140
operationConfig: newOperationConfig(cache, matcher),
141141
ttl: ttl,
142142
}
@@ -156,8 +156,8 @@ func (cfg *CachingBucketConfig) allConfigNames() map[string][]string {
156156
for n := range cfg.getRange {
157157
result[opGetRange] = append(result[opGetRange], n)
158158
}
159-
for n := range cfg.objectSize {
160-
result[opObjectSize] = append(result[opObjectSize], n)
159+
for n := range cfg.attributes {
160+
result[opAttributes] = append(result[opAttributes], n)
161161
}
162162
return result
163163
}
@@ -198,8 +198,8 @@ func (cfg *CachingBucketConfig) findGetRangeConfig(name string) (string, *getRan
198198
return "", nil
199199
}
200200

201-
func (cfg *CachingBucketConfig) findObjectSizeConfig(name string) (string, *objectSizeConfig) {
202-
for n, cfg := range cfg.objectSize {
201+
func (cfg *CachingBucketConfig) findAttributesConfig(name string) (string, *attributesConfig) {
202+
for n, cfg := range cfg.attributes {
203203
if cfg.matcher(name) {
204204
return n, cfg
205205
}

pkg/store/cache/caching_bucket_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func TestObjectSize(t *testing.T) {
620620

621621
cfg := NewCachingBucketConfig()
622622
const cfgName = "test"
623-
cfg.CacheObjectSize(cfgName, cache, matchAll, time.Minute)
623+
cfg.CacheAttributes(cfgName, cache, matchAll, time.Minute)
624624

625625
cb, err := NewCachingBucket(inmem, cfg, nil, nil)
626626
testutil.Ok(t, err)
@@ -637,16 +637,16 @@ func TestObjectSize(t *testing.T) {
637637

638638
func verifyObjectSize(t *testing.T, cb *CachingBucket, file string, expectedLength int, cacheUsed bool, cfgName string) {
639639
t.Helper()
640-
hitsBefore := int(promtest.ToFloat64(cb.operationHits.WithLabelValues(opObjectSize, cfgName)))
640+
hitsBefore := int(promtest.ToFloat64(cb.operationHits.WithLabelValues(opAttributes, cfgName)))
641641

642-
length, err := cb.ObjectSize(context.Background(), file)
642+
attrs, err := cb.Attributes(context.Background(), file)
643643
if expectedLength < 0 {
644644
testutil.Assert(t, cb.IsObjNotFoundErr(err))
645645
} else {
646646
testutil.Ok(t, err)
647-
testutil.Equals(t, uint64(expectedLength), length)
647+
testutil.Equals(t, int64(expectedLength), attrs.Size)
648648

649-
hitsAfter := int(promtest.ToFloat64(cb.operationHits.WithLabelValues(opObjectSize, cfgName)))
649+
hitsAfter := int(promtest.ToFloat64(cb.operationHits.WithLabelValues(opAttributes, cfgName)))
650650
if cacheUsed {
651651
testutil.Equals(t, 1, hitsAfter-hitsBefore)
652652
} else {

0 commit comments

Comments
 (0)