[#5] services/tree: User generic LRU cache

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2022-12-30 12:53:41 +03:00 committed by fyrchik
parent d0a0432a51
commit 1b3374ac7f
2 changed files with 9 additions and 10 deletions

View file

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/TrueCloudLab/frostfs-node/pkg/network" "github.com/TrueCloudLab/frostfs-node/pkg/network"
"github.com/hashicorp/golang-lru/simplelru" "github.com/hashicorp/golang-lru/v2/simplelru"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/connectivity" "google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
@ -17,7 +17,7 @@ import (
type clientCache struct { type clientCache struct {
sync.Mutex sync.Mutex
simplelru.LRU simplelru.LRU[string, cacheItem]
} }
type cacheItem struct { type cacheItem struct {
@ -34,8 +34,8 @@ const (
var errRecentlyFailed = errors.New("client has recently failed") var errRecentlyFailed = errors.New("client has recently failed")
func (c *clientCache) init() { func (c *clientCache) init() {
l, _ := simplelru.NewLRU(defaultClientCacheSize, func(key, value interface{}) { l, _ := simplelru.NewLRU[string, cacheItem](defaultClientCacheSize, func(_ string, value cacheItem) {
_ = value.(*grpc.ClientConn).Close() _ = value.cc.Close()
}) })
c.LRU = *l c.LRU = *l
} }
@ -46,7 +46,7 @@ func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceCl
c.Unlock() c.Unlock()
if ok { if ok {
item := ccInt.(cacheItem) item := ccInt
if item.cc == nil { if item.cc == nil {
if d := time.Since(item.lastTry); d < defaultReconnectInterval { if d := time.Since(item.lastTry); d < defaultReconnectInterval {
return nil, fmt.Errorf("%w: %s till the next reconnection to %s", return nil, fmt.Errorf("%w: %s till the next reconnection to %s",

View file

@ -10,17 +10,17 @@ import (
"github.com/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement" "github.com/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
cidSDK "github.com/TrueCloudLab/frostfs-sdk-go/container/id" cidSDK "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap" netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap"
"github.com/hashicorp/golang-lru/simplelru" "github.com/hashicorp/golang-lru/v2/simplelru"
) )
type containerCache struct { type containerCache struct {
sync.Mutex sync.Mutex
nm *netmapSDK.NetMap nm *netmapSDK.NetMap
lru *simplelru.LRU lru *simplelru.LRU[string, containerCacheItem]
} }
func (c *containerCache) init(size int) { func (c *containerCache) init(size int) {
c.lru, _ = simplelru.NewLRU(size, nil) // no error, size is positive c.lru, _ = simplelru.NewLRU[string, containerCacheItem](size, nil) // no error, size is positive
} }
type containerCacheItem struct { type containerCacheItem struct {
@ -48,8 +48,7 @@ func (s *Service) getContainerNodes(cid cidSDK.ID) ([]netmapSDK.NodeInfo, int, e
s.containerCache.Lock() s.containerCache.Lock()
if s.containerCache.nm != nm { if s.containerCache.nm != nm {
s.containerCache.lru.Purge() s.containerCache.lru.Purge()
} else if v, ok := s.containerCache.lru.Get(cidStr); ok { } else if item, ok := s.containerCache.lru.Get(cidStr); ok {
item := v.(containerCacheItem)
if item.cnr == cnr { if item.cnr == cnr {
s.containerCache.Unlock() s.containerCache.Unlock()
return item.nodes, item.local, nil return item.nodes, item.local, nil