From f0be0befc5eadf038f15c7cd202a5ac4f2c0f08e Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 30 Dec 2022 12:54:48 +0300 Subject: [PATCH] [#5] services/object_manager: Use generic LRU cache Signed-off-by: Evgenii Stratonikov --- pkg/services/object_manager/placement/netmap.go | 11 +++++------ pkg/services/object_manager/tombstone/checker.go | 6 +++--- pkg/services/object_manager/tombstone/constructor.go | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/services/object_manager/placement/netmap.go b/pkg/services/object_manager/placement/netmap.go index c2d908be2..eb2a0f2f1 100644 --- a/pkg/services/object_manager/placement/netmap.go +++ b/pkg/services/object_manager/placement/netmap.go @@ -9,7 +9,7 @@ import ( cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id" netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap" oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id" - "github.com/hashicorp/golang-lru/simplelru" + "github.com/hashicorp/golang-lru/v2/simplelru" ) type netMapBuilder struct { @@ -19,7 +19,7 @@ type netMapBuilder struct { lastNm *netmapSDK.NetMap // containerCache caches container nodes by ID. It is used to skip `GetContainerNodes` invocation if // neither netmap nor container has changed. - containerCache simplelru.LRUCache + containerCache simplelru.LRUCache[string, [][]netmapSDK.NodeInfo] } type netMapSrc struct { @@ -32,7 +32,7 @@ type netMapSrc struct { const defaultContainerCacheSize = 10 func NewNetworkMapBuilder(nm *netmapSDK.NetMap) Builder { - cache, _ := simplelru.NewLRU(defaultContainerCacheSize, nil) // no error + cache, _ := simplelru.NewLRU[string, [][]netmapSDK.NodeInfo](defaultContainerCacheSize, nil) // no error return &netMapBuilder{ nmSrc: &netMapSrc{nm: nm}, containerCache: cache, @@ -40,7 +40,7 @@ func NewNetworkMapBuilder(nm *netmapSDK.NetMap) Builder { } func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder { - cache, _ := simplelru.NewLRU(defaultContainerCacheSize, nil) // no error + cache, _ := simplelru.NewLRU[string, [][]netmapSDK.NodeInfo](defaultContainerCacheSize, nil) // no error return &netMapBuilder{ nmSrc: nmSrc, containerCache: cache, @@ -65,8 +65,7 @@ func (b *netMapBuilder) BuildPlacement(cnr cid.ID, obj *oid.ID, p netmapSDK.Plac raw, ok := b.containerCache.Get(string(binCnr)) b.mtx.Unlock() if ok { - cn := raw.([][]netmapSDK.NodeInfo) - return BuildObjectPlacement(nm, cn, obj) + return BuildObjectPlacement(nm, raw, obj) } } else { b.containerCache.Purge() diff --git a/pkg/services/object_manager/tombstone/checker.go b/pkg/services/object_manager/tombstone/checker.go index ea2acb408..f062f0c3c 100644 --- a/pkg/services/object_manager/tombstone/checker.go +++ b/pkg/services/object_manager/tombstone/checker.go @@ -8,7 +8,7 @@ import ( "github.com/TrueCloudLab/frostfs-node/pkg/util/logger" "github.com/TrueCloudLab/frostfs-sdk-go/object" oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "go.uber.org/zap" ) @@ -31,7 +31,7 @@ type Source interface { // `ExpirationChecker{}` declarations leads to undefined behaviour // and may lead to panics. type ExpirationChecker struct { - cache *lru.Cache + cache *lru.Cache[string, uint64] log *logger.Logger @@ -51,7 +51,7 @@ func (g *ExpirationChecker) IsTombstoneAvailable(ctx context.Context, a oid.Addr expEpoch, ok := g.cache.Get(addrStr) if ok { - return expEpoch.(uint64) > epoch + return expEpoch > epoch } ts, err := g.tsSource.Tombstone(ctx, a, epoch) diff --git a/pkg/services/object_manager/tombstone/constructor.go b/pkg/services/object_manager/tombstone/constructor.go index e61b561ea..cc0b664b0 100644 --- a/pkg/services/object_manager/tombstone/constructor.go +++ b/pkg/services/object_manager/tombstone/constructor.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/TrueCloudLab/frostfs-node/pkg/util/logger" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "go.uber.org/zap" ) @@ -48,7 +48,7 @@ func NewChecker(oo ...Option) *ExpirationChecker { panicOnNil(cfg.tsSource, "Tombstone source") - cache, err := lru.New(cfg.cacheSize) + cache, err := lru.New[string, uint64](cfg.cacheSize) if err != nil { panic(fmt.Errorf("could not create LRU cache with %d size: %w", cfg.cacheSize, err)) }