[#5] services/object_manager: Use generic LRU cache

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2022-12-30 12:54:48 +03:00 committed by fyrchik
parent 1b3374ac7f
commit f0be0befc5
3 changed files with 10 additions and 11 deletions

View file

@ -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()

View file

@ -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)

View file

@ -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))
}