From 9936b112b83a714b0306b8f015c26ac403dc525d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 30 Dec 2022 13:01:01 +0300 Subject: [PATCH] [#5] blobstor: Use generic LRU cache Signed-off-by: Evgenii Stratonikov --- .../blobstor/blobovniczatree/blobovnicza.go | 13 ++++++------- .../blobstor/blobovniczatree/control.go | 9 ++++----- .../blobstor/blobovniczatree/delete.go | 2 +- .../blobstor/blobovniczatree/get.go | 2 +- .../blobstor/blobovniczatree/get_range.go | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/blobovnicza.go b/pkg/local_object_storage/blobstor/blobovniczatree/blobovnicza.go index 9b9b4b8b1..27ae81546 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/blobovnicza.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/blobovnicza.go @@ -13,7 +13,7 @@ import ( "github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr" oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id" "github.com/TrueCloudLab/hrw" - "github.com/hashicorp/golang-lru/simplelru" + "github.com/hashicorp/golang-lru/v2/simplelru" "go.uber.org/zap" ) @@ -61,7 +61,7 @@ type Blobovniczas struct { cfg // cache of opened filled Blobovniczas - opened *simplelru.LRU + opened *simplelru.LRU[string, *blobovnicza.Blobovnicza] // lruMtx protects opened cache. // It isn't RWMutex because `Get` calls must // lock this mutex on write, as LRU info is updated. @@ -97,21 +97,20 @@ func NewBlobovniczaTree(opts ...Option) (blz *Blobovniczas) { opts[i](&blz.cfg) } - cache, err := simplelru.NewLRU(blz.openedCacheSize, func(key interface{}, value interface{}) { - p := key.(string) + cache, err := simplelru.NewLRU[string, *blobovnicza.Blobovnicza](blz.openedCacheSize, func(p string, value *blobovnicza.Blobovnicza) { lvlPath := filepath.Dir(p) if b, ok := blz.active[lvlPath]; ok && b.ind == u64FromHexString(filepath.Base(p)) { // This branch is taken if we have recently updated active blobovnicza and remove // it from opened cache. return - } else if err := value.(*blobovnicza.Blobovnicza).Close(); err != nil { + } else if err := value.Close(); err != nil { blz.log.Error("could not close Blobovnicza", - zap.String("id", key.(string)), + zap.String("id", p), zap.String("error", err.Error()), ) } else { blz.log.Debug("blobovnicza successfully closed on evict", - zap.String("id", key.(string)), + zap.String("id", p), ) } }) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/control.go b/pkg/local_object_storage/blobstor/blobovniczatree/control.go index 1cae8643a..1c8a5e85b 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/control.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/control.go @@ -57,11 +57,10 @@ func (b *Blobovniczas) Close() error { b.opened.Remove(p) } for _, k := range b.opened.Keys() { - v, _ := b.opened.Get(k) - blz := v.(*blobovnicza.Blobovnicza) + blz, _ := b.opened.Get(k) if err := blz.Close(); err != nil { b.log.Debug("could not close active blobovnicza", - zap.String("path", k.(string)), + zap.String("path", k), zap.String("error", err.Error()), ) } @@ -86,7 +85,7 @@ func (b *Blobovniczas) openBlobovnicza(p string) (*blobovnicza.Blobovnicza, erro b.lruMtx.Unlock() if ok { // blobovnicza should be opened in cache - return v.(*blobovnicza.Blobovnicza), nil + return v, nil } lvlPath := filepath.Dir(p) @@ -105,7 +104,7 @@ func (b *Blobovniczas) openBlobovnicza(p string) (*blobovnicza.Blobovnicza, erro v, ok = b.opened.Get(p) if ok { - return v.(*blobovnicza.Blobovnicza), nil + return v, nil } blz, err := b.openBlobovniczaNoCache(p) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/delete.go b/pkg/local_object_storage/blobstor/blobovniczatree/delete.go index 25e0dcb47..01e8b044f 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/delete.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/delete.go @@ -80,7 +80,7 @@ func (b *Blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath v, ok := b.opened.Get(blzPath) b.lruMtx.Unlock() if ok { - if res, err := b.deleteObject(v.(*blobovnicza.Blobovnicza), prm, dp); err == nil { + if res, err := b.deleteObject(v, prm, dp); err == nil { return res, err } else if !blobovnicza.IsErrNotFound(err) { b.log.Debug("could not remove object from opened blobovnicza", diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/get.go b/pkg/local_object_storage/blobstor/blobovniczatree/get.go index b98c14450..3c7998f97 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/get.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/get.go @@ -72,7 +72,7 @@ func (b *Blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string v, ok := b.opened.Get(blzPath) b.lruMtx.Unlock() if ok { - if res, err := b.getObject(v.(*blobovnicza.Blobovnicza), prm); err == nil { + if res, err := b.getObject(v, prm); err == nil { return res, err } else if !blobovnicza.IsErrNotFound(err) { b.log.Debug("could not read object from opened blobovnicza", diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go b/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go index 1ead1d1e4..4eab32157 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go @@ -76,7 +76,7 @@ func (b *Blobovniczas) getRangeFromLevel(prm common.GetRangePrm, blzPath string, v, ok := b.opened.Get(blzPath) b.lruMtx.Unlock() if ok { - res, err := b.getObjectRange(v.(*blobovnicza.Blobovnicza), prm) + res, err := b.getObjectRange(v, prm) switch { case err == nil, isErrOutOfRange(err):