2023-10-24 22:52:33 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage"
|
2024-01-06 16:48:14 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu"
|
2023-10-24 22:52:33 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlobBucket struct {
|
2023-11-30 19:09:51 +01:00
|
|
|
bucket storage.BlobBucket
|
2024-01-06 16:48:14 +01:00
|
|
|
blobCache *lfu.Cache[string, []byte]
|
|
|
|
bucketCache *lfu.Cache[string, storage.BlobBucket]
|
|
|
|
blobInfoCache *lfu.Cache[string, storage.BlobInfo]
|
2023-10-24 22:52:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) Close() error {
|
2023-11-30 19:09:51 +01:00
|
|
|
// Close only when bucket is evicted from cache
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) Delete(ctx context.Context, id storage.BlobID) error {
|
2023-11-30 19:09:51 +01:00
|
|
|
defer b.clearCache(ctx, id)
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
if err := b.bucket.Delete(ctx, id); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) Get(ctx context.Context, id storage.BlobID) (storage.BlobInfo, error) {
|
2023-11-30 19:09:51 +01:00
|
|
|
key := b.getCacheKey(id)
|
2024-01-06 16:48:14 +01:00
|
|
|
blobInfo, err := b.blobInfoCache.Get(key)
|
|
|
|
if err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not retrieve blob info from cache",
|
|
|
|
logger.F("cacheKey", key),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobInfo != nil {
|
2023-11-30 19:09:51 +01:00
|
|
|
logger.Debug(
|
|
|
|
ctx, "found blob info in cache",
|
|
|
|
logger.F("cacheKey", key),
|
|
|
|
)
|
|
|
|
|
|
|
|
return blobInfo, nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
info, err := b.bucket.Get(ctx, id)
|
|
|
|
if err != nil {
|
2023-11-30 19:09:51 +01:00
|
|
|
if errors.Is(err, storage.ErrBucketClosed) {
|
|
|
|
b.clearCache(ctx, id)
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.bucketCache.Delete(b.Name()); err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not delete bucket from cache",
|
|
|
|
logger.F("cacheKey", b.Name()),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.blobInfoCache.Set(key, info); err != nil {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not set blob info in cache",
|
|
|
|
logger.F("cacheKey", key),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) List(ctx context.Context) ([]storage.BlobInfo, error) {
|
|
|
|
infos, err := b.bucket.List(ctx)
|
|
|
|
if err != nil {
|
2023-11-30 19:09:51 +01:00
|
|
|
if errors.Is(err, storage.ErrBucketClosed) {
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.bucketCache.Delete(b.Name()); err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not delete bucket from cache",
|
|
|
|
logger.F("cacheKey", b.Name()),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
for _, ifo := range infos {
|
|
|
|
key := b.getCacheKey(ifo.ID())
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.blobInfoCache.Set(key, ifo); err != nil {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not set blob info in cache",
|
|
|
|
logger.F("cacheKey", key),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return infos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) Name() string {
|
|
|
|
return b.bucket.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) NewReader(ctx context.Context, id storage.BlobID) (io.ReadSeekCloser, error) {
|
2023-11-30 19:09:51 +01:00
|
|
|
if cached, exist := b.inContentCache(id); exist {
|
|
|
|
logger.Debug(
|
|
|
|
ctx, "found blob content in cache",
|
|
|
|
logger.F("cacheKey", b.getCacheKey(id)),
|
|
|
|
)
|
2023-10-24 22:52:33 +02:00
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := b.bucket.NewReader(ctx, id)
|
|
|
|
if err != nil {
|
2023-11-30 19:09:51 +01:00
|
|
|
if errors.Is(err, storage.ErrBucketClosed) {
|
|
|
|
b.clearCache(ctx, id)
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.bucketCache.Delete(b.Name()); err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not delete bucket from cache",
|
|
|
|
logger.F("cacheKey", b.Name()),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &readCacher{
|
|
|
|
reader: reader,
|
2024-01-06 16:48:14 +01:00
|
|
|
cache: b.blobCache,
|
2023-10-24 22:52:33 +02:00
|
|
|
key: b.getCacheKey(id),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlobBucket) getCacheKey(id storage.BlobID) string {
|
|
|
|
return fmt.Sprintf("%s-%s", b.Name(), id)
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
func (b *BlobBucket) inContentCache(id storage.BlobID) (io.ReadSeekCloser, bool) {
|
2023-10-24 22:52:33 +02:00
|
|
|
key := b.getCacheKey(id)
|
2024-01-06 16:48:14 +01:00
|
|
|
data, err := b.blobCache.Get(key)
|
2023-11-29 11:10:29 +01:00
|
|
|
if err != nil {
|
2024-01-06 16:48:14 +01:00
|
|
|
if errors.Is(err, lfu.ErrNotFound) {
|
2023-11-29 11:10:29 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
logger.Error(context.Background(), "could not retrieve cached value", logger.CapturedE(errors.WithStack(err)))
|
2023-11-29 11:10:29 +01:00
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cachedReader{data, 0}, true
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
func (b *BlobBucket) clearCache(ctx context.Context, id storage.BlobID) {
|
|
|
|
key := b.getCacheKey(id)
|
|
|
|
|
|
|
|
logger.Debug(ctx, "clearing cache", logger.F("cacheKey", key))
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.blobCache.Delete(key); err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(ctx, "could not clear cache", logger.F("cacheKey", key), logger.CapturedE(errors.WithStack(err)))
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.blobInfoCache.Delete(key); err != nil {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not delete blob info from cache",
|
|
|
|
logger.F("cacheKey", key),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
// NewWriter implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) NewWriter(ctx context.Context, id storage.BlobID) (io.WriteCloser, error) {
|
2023-11-30 19:09:51 +01:00
|
|
|
defer b.clearCache(ctx, id)
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
writer, err := b.bucket.NewWriter(ctx, id)
|
|
|
|
if err != nil {
|
2023-11-30 19:09:51 +01:00
|
|
|
if errors.Is(err, storage.ErrBucketClosed) {
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.bucketCache.Delete(b.Name()); err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not delete bucket from cache",
|
|
|
|
logger.F("cacheKey", b.Name()),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements storage.BlobBucket.
|
|
|
|
func (b *BlobBucket) Size(ctx context.Context) (int64, error) {
|
|
|
|
size, err := b.bucket.Size(ctx)
|
|
|
|
if err != nil {
|
2023-11-30 19:09:51 +01:00
|
|
|
if errors.Is(err, storage.ErrBucketClosed) {
|
2024-01-06 16:48:14 +01:00
|
|
|
if err := b.bucketCache.Delete(b.Name()); err != nil && !errors.Is(err, lfu.ErrNotFound) {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not delete bucket from cache",
|
|
|
|
logger.F("cacheKey", b.Name()),
|
|
|
|
logger.CapturedE(errors.WithStack(err)),
|
|
|
|
)
|
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return 0, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ storage.BlobBucket = &BlobBucket{}
|