2021-03-04 09:26:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-06-14 12:56:03 +00:00
|
|
|
"sync"
|
2021-03-04 09:26:47 +00:00
|
|
|
"time"
|
|
|
|
|
2024-06-24 15:04:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/metrics"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
2024-08-30 09:09:14 +00:00
|
|
|
objectwriter "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/common/writer"
|
2023-08-11 08:32:43 +00:00
|
|
|
utilSync "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sync"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2022-12-30 10:23:41 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2023-11-30 08:45:31 +00:00
|
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
2021-03-04 09:26:47 +00:00
|
|
|
)
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
type netValueReader[K any, V any] func(K) (V, error)
|
2021-03-04 09:26:47 +00:00
|
|
|
|
2023-11-30 08:45:31 +00:00
|
|
|
type valueWithError[V any] struct {
|
2022-12-30 10:23:41 +00:00
|
|
|
v V
|
2022-08-11 14:44:48 +00:00
|
|
|
// cached error in order to not repeat failed request for some time
|
|
|
|
e error
|
2021-03-04 09:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// entity that provides TTL cache interface.
|
2022-12-30 10:23:41 +00:00
|
|
|
type ttlNetCache[K comparable, V any] struct {
|
2023-11-30 08:45:31 +00:00
|
|
|
cache *expirable.LRU[K, *valueWithError[V]]
|
|
|
|
netRdr netValueReader[K, V]
|
2023-08-11 08:32:43 +00:00
|
|
|
keyLocker *utilSync.KeyLocker[K]
|
2024-06-24 15:04:35 +00:00
|
|
|
metrics cacheMetrics
|
2021-03-04 09:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// complicates netValueReader with TTL caching mechanism.
|
2024-06-24 15:04:35 +00:00
|
|
|
func newNetworkTTLCache[K comparable, V any](sz int, ttl time.Duration, netRdr netValueReader[K, V], metrics cacheMetrics) *ttlNetCache[K, V] {
|
2023-11-30 08:45:31 +00:00
|
|
|
cache := expirable.NewLRU[K, *valueWithError[V]](sz, nil, ttl)
|
2021-03-04 09:26:47 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
return &ttlNetCache[K, V]{
|
2023-04-21 10:49:05 +00:00
|
|
|
cache: cache,
|
|
|
|
netRdr: netRdr,
|
2024-06-24 15:04:35 +00:00
|
|
|
metrics: metrics,
|
2023-08-11 08:32:43 +00:00
|
|
|
keyLocker: utilSync.NewKeyLocker[K](),
|
2021-03-04 09:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads value by the key.
|
|
|
|
//
|
|
|
|
// updates the value from the network on cache miss or by TTL.
|
|
|
|
//
|
|
|
|
// returned value should not be modified.
|
2022-12-30 10:23:41 +00:00
|
|
|
func (c *ttlNetCache[K, V]) get(key K) (V, error) {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit := false
|
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.AddMethodDuration("Get", time.Since(startedAt), hit)
|
|
|
|
}()
|
|
|
|
|
2023-04-21 10:49:05 +00:00
|
|
|
val, ok := c.cache.Peek(key)
|
2023-11-30 08:45:31 +00:00
|
|
|
if ok {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit = true
|
2023-04-21 10:49:05 +00:00
|
|
|
return val.v, val.e
|
2021-03-04 09:26:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
c.keyLocker.Lock(key)
|
|
|
|
defer c.keyLocker.Unlock(key)
|
2023-03-22 17:28:14 +00:00
|
|
|
|
2023-04-21 10:49:05 +00:00
|
|
|
val, ok = c.cache.Peek(key)
|
2023-11-30 08:45:31 +00:00
|
|
|
if ok {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit = true
|
2023-04-21 10:49:05 +00:00
|
|
|
return val.v, val.e
|
2023-03-22 17:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v, err := c.netRdr(key)
|
|
|
|
|
2023-11-30 08:45:31 +00:00
|
|
|
c.cache.Add(key, &valueWithError[V]{
|
2023-04-21 10:49:05 +00:00
|
|
|
v: v,
|
|
|
|
e: err,
|
|
|
|
})
|
2023-03-22 17:28:14 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
return v, err
|
2022-08-11 14:44:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
func (c *ttlNetCache[K, V]) set(k K, v V, e error) {
|
2024-06-24 15:04:35 +00:00
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.AddMethodDuration("Set", time.Since(startedAt), false)
|
|
|
|
}()
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
c.keyLocker.Lock(k)
|
|
|
|
defer c.keyLocker.Unlock(k)
|
2023-04-21 10:49:05 +00:00
|
|
|
|
2023-11-30 08:45:31 +00:00
|
|
|
c.cache.Add(k, &valueWithError[V]{
|
2022-08-11 14:44:48 +00:00
|
|
|
v: v,
|
|
|
|
e: e,
|
2021-03-04 09:26:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
func (c *ttlNetCache[K, V]) remove(key K) {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit := false
|
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.AddMethodDuration("Remove", time.Since(startedAt), hit)
|
|
|
|
}()
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
c.keyLocker.Lock(key)
|
|
|
|
defer c.keyLocker.Unlock(key)
|
2023-04-21 10:49:05 +00:00
|
|
|
|
2024-06-24 15:04:35 +00:00
|
|
|
hit = c.cache.Remove(key)
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 12:32:16 +00:00
|
|
|
// entity that provides LRU cache interface.
|
|
|
|
type lruNetCache struct {
|
2022-12-30 10:23:41 +00:00
|
|
|
cache *lru.Cache[uint64, *netmapSDK.NetMap]
|
2021-03-04 12:32:16 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
netRdr netValueReader[uint64, *netmapSDK.NetMap]
|
2024-06-24 15:04:35 +00:00
|
|
|
|
|
|
|
metrics cacheMetrics
|
2021-03-04 12:32:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:30:30 +00:00
|
|
|
// newNetworkLRUCache returns wrapper over netValueReader with LRU cache.
|
2024-06-24 15:04:35 +00:00
|
|
|
func newNetworkLRUCache(sz int, netRdr netValueReader[uint64, *netmapSDK.NetMap], metrics cacheMetrics) *lruNetCache {
|
2022-12-30 10:23:41 +00:00
|
|
|
cache, err := lru.New[uint64, *netmapSDK.NetMap](sz)
|
2021-03-04 12:32:16 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
return &lruNetCache{
|
2024-06-24 15:04:35 +00:00
|
|
|
cache: cache,
|
|
|
|
netRdr: netRdr,
|
|
|
|
metrics: metrics,
|
2021-03-04 12:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads value by the key.
|
|
|
|
//
|
|
|
|
// updates the value from the network on cache miss.
|
|
|
|
//
|
|
|
|
// returned value should not be modified.
|
2022-12-30 10:23:41 +00:00
|
|
|
func (c *lruNetCache) get(key uint64) (*netmapSDK.NetMap, error) {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit := false
|
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.AddMethodDuration("Get", time.Since(startedAt), hit)
|
|
|
|
}()
|
|
|
|
|
2021-03-04 12:32:16 +00:00
|
|
|
val, ok := c.cache.Get(key)
|
|
|
|
if ok {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit = true
|
2021-03-04 12:32:16 +00:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := c.netRdr(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cache.Add(key, val)
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2021-03-04 09:26:47 +00:00
|
|
|
// wrapper over TTL cache of values read from the network
|
|
|
|
// that implements container storage.
|
2022-12-30 10:23:41 +00:00
|
|
|
type ttlContainerStorage struct {
|
2023-08-24 12:27:24 +00:00
|
|
|
containerCache *ttlNetCache[cid.ID, *container.Container]
|
|
|
|
delInfoCache *ttlNetCache[cid.ID, *container.DelInfo]
|
2022-12-30 10:23:41 +00:00
|
|
|
}
|
2021-03-04 09:26:47 +00:00
|
|
|
|
2024-09-27 09:39:43 +00:00
|
|
|
func newCachedContainerStorage(v container.Source, ttl time.Duration, containerCacheSize uint32) ttlContainerStorage {
|
|
|
|
lruCnrCache := newNetworkTTLCache(int(containerCacheSize), ttl, func(id cid.ID) (*container.Container, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
return v.Get(id)
|
2024-06-24 15:04:35 +00:00
|
|
|
}, metrics.NewCacheMetrics("container"))
|
2024-09-27 09:39:43 +00:00
|
|
|
lruDelInfoCache := newNetworkTTLCache(int(containerCacheSize), ttl, func(id cid.ID) (*container.DelInfo, error) {
|
2023-08-24 12:27:24 +00:00
|
|
|
return v.DeletionInfo(id)
|
2024-06-24 15:04:35 +00:00
|
|
|
}, metrics.NewCacheMetrics("container_deletion_info"))
|
2021-03-04 09:26:47 +00:00
|
|
|
|
2023-08-24 12:27:24 +00:00
|
|
|
return ttlContainerStorage{
|
|
|
|
containerCache: lruCnrCache,
|
|
|
|
delInfoCache: lruDelInfoCache,
|
|
|
|
}
|
2021-03-04 09:26:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
func (s ttlContainerStorage) handleRemoval(cnr cid.ID) {
|
2023-08-24 12:27:24 +00:00
|
|
|
s.containerCache.set(cnr, nil, new(apistatus.ContainerNotFound))
|
|
|
|
|
2023-08-24 12:35:19 +00:00
|
|
|
// The removal invalidates possibly stored error response.
|
2023-08-24 12:27:24 +00:00
|
|
|
s.delInfoCache.remove(cnr)
|
2022-08-11 14:48:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
// Get returns container value from the cache. If value is missing in the cache
|
|
|
|
// or expired, then it returns value from side chain and updates the cache.
|
2022-12-30 10:23:41 +00:00
|
|
|
func (s ttlContainerStorage) Get(cnr cid.ID) (*container.Container, error) {
|
2023-08-24 12:27:24 +00:00
|
|
|
return s.containerCache.get(cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ttlContainerStorage) DeletionInfo(cnr cid.ID) (*container.DelInfo, error) {
|
|
|
|
return s.delInfoCache.get(cnr)
|
2021-03-04 09:26:47 +00:00
|
|
|
}
|
2021-03-04 11:26:19 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
type ttlEACLStorage struct {
|
|
|
|
*ttlNetCache[cid.ID, *container.EACL]
|
|
|
|
}
|
2021-03-04 11:26:19 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
func newCachedEACLStorage(v container.EACLSource, ttl time.Duration) ttlEACLStorage {
|
2022-08-16 11:59:30 +00:00
|
|
|
const eaclCacheSize = 100
|
2021-03-04 11:26:19 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
lruCnrCache := newNetworkTTLCache(eaclCacheSize, ttl, func(id cid.ID) (*container.EACL, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
return v.GetEACL(id)
|
2024-06-24 15:04:35 +00:00
|
|
|
}, metrics.NewCacheMetrics("eacl"))
|
2021-03-04 11:26:19 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
return ttlEACLStorage{lruCnrCache}
|
2021-03-04 11:26:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
// GetEACL returns eACL value from the cache. If value is missing in the cache
|
|
|
|
// or expired, then it returns value from side chain and updates cache.
|
2022-12-30 10:23:41 +00:00
|
|
|
func (s ttlEACLStorage) GetEACL(cnr cid.ID) (*container.EACL, error) {
|
|
|
|
return s.get(cnr)
|
2021-03-04 11:26:19 +00:00
|
|
|
}
|
2021-03-04 12:32:16 +00:00
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
// InvalidateEACL removes cached eACL value.
|
2022-12-30 10:23:41 +00:00
|
|
|
func (s ttlEACLStorage) InvalidateEACL(cnr cid.ID) {
|
|
|
|
s.remove(cnr)
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 12:32:16 +00:00
|
|
|
type lruNetmapSource struct {
|
|
|
|
netState netmap.State
|
|
|
|
|
|
|
|
cache *lruNetCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCachedNetmapStorage(s netmap.State, v netmap.Source) netmap.Source {
|
|
|
|
const netmapCacheSize = 10
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
lruNetmapCache := newNetworkLRUCache(netmapCacheSize, func(key uint64) (*netmapSDK.NetMap, error) {
|
|
|
|
return v.GetNetMapByEpoch(key)
|
2024-06-24 15:04:35 +00:00
|
|
|
}, metrics.NewCacheMetrics("netmap"))
|
2021-03-04 12:32:16 +00:00
|
|
|
|
|
|
|
return &lruNetmapSource{
|
|
|
|
netState: s,
|
|
|
|
cache: lruNetmapCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (s *lruNetmapSource) GetNetMap(diff uint64) (*netmapSDK.NetMap, error) {
|
2021-03-04 12:32:16 +00:00
|
|
|
return s.getNetMapByEpoch(s.netState.CurrentEpoch() - diff)
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (s *lruNetmapSource) GetNetMapByEpoch(epoch uint64) (*netmapSDK.NetMap, error) {
|
2021-03-04 12:32:16 +00:00
|
|
|
return s.getNetMapByEpoch(epoch)
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (s *lruNetmapSource) getNetMapByEpoch(epoch uint64) (*netmapSDK.NetMap, error) {
|
2021-03-04 12:32:16 +00:00
|
|
|
val, err := s.cache.get(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
return val, nil
|
2021-03-04 12:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *lruNetmapSource) Epoch() (uint64, error) {
|
|
|
|
return s.netState.CurrentEpoch(), nil
|
|
|
|
}
|
2021-08-30 11:16:41 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
type cachedIRFetcher struct {
|
|
|
|
*ttlNetCache[struct{}, [][]byte]
|
|
|
|
}
|
2022-03-29 13:25:03 +00:00
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
func newCachedIRFetcher(f interface{ InnerRingKeys() ([][]byte, error) }) cachedIRFetcher {
|
2022-03-29 13:25:03 +00:00
|
|
|
const (
|
|
|
|
irFetcherCacheSize = 1 // we intend to store only one value
|
|
|
|
|
|
|
|
// Without the cache in the testnet we can see several hundred simultaneous
|
2022-12-23 17:35:35 +00:00
|
|
|
// requests (frostfs-node #1278), so limiting the request rate solves the issue.
|
2022-03-29 13:25:03 +00:00
|
|
|
//
|
|
|
|
// Exact request rate doesn't really matter because Inner Ring list update
|
|
|
|
// happens extremely rare, but there is no side chain events for that as
|
2022-12-23 17:35:35 +00:00
|
|
|
// for now (frostfs-contract v0.15.0 notary disabled env) to monitor it.
|
2022-03-29 13:25:03 +00:00
|
|
|
irFetcherCacheTTL = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
2023-04-06 13:24:43 +00:00
|
|
|
irFetcherCache := newNetworkTTLCache(irFetcherCacheSize, irFetcherCacheTTL,
|
2022-12-30 10:23:41 +00:00
|
|
|
func(_ struct{}) ([][]byte, error) {
|
2022-03-29 13:25:03 +00:00
|
|
|
return f.InnerRingKeys()
|
2024-06-24 15:04:35 +00:00
|
|
|
}, metrics.NewCacheMetrics("ir_keys"),
|
2022-03-29 13:25:03 +00:00
|
|
|
)
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
return cachedIRFetcher{irFetcherCache}
|
2022-03-29 13:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InnerRingKeys returns cached list of Inner Ring keys. If keys are missing in
|
|
|
|
// the cache or expired, then it returns keys from side chain and updates
|
|
|
|
// the cache.
|
2022-12-30 10:23:41 +00:00
|
|
|
func (f cachedIRFetcher) InnerRingKeys() ([][]byte, error) {
|
|
|
|
val, err := f.get(struct{}{})
|
2022-03-29 13:25:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:23:41 +00:00
|
|
|
return val, nil
|
2022-03-29 13:25:03 +00:00
|
|
|
}
|
2022-06-14 12:56:03 +00:00
|
|
|
|
|
|
|
type ttlMaxObjectSizeCache struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
lastUpdated time.Time
|
|
|
|
lastSize uint64
|
2024-08-30 09:09:14 +00:00
|
|
|
src objectwriter.MaxSizeSource
|
2024-06-24 15:04:35 +00:00
|
|
|
metrics cacheMetrics
|
2022-06-14 12:56:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
func newCachedMaxObjectSizeSource(src objectwriter.MaxSizeSource) objectwriter.MaxSizeSource {
|
2022-06-14 12:56:03 +00:00
|
|
|
return &ttlMaxObjectSizeCache{
|
2024-06-24 15:04:35 +00:00
|
|
|
src: src,
|
|
|
|
metrics: metrics.NewCacheMetrics("max_object_size"),
|
2022-06-14 12:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ttlMaxObjectSizeCache) MaxObjectSize() uint64 {
|
|
|
|
const ttl = time.Second * 30
|
|
|
|
|
2024-06-24 15:04:35 +00:00
|
|
|
hit := false
|
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.AddMethodDuration("Get", time.Since(startedAt), hit)
|
|
|
|
}()
|
|
|
|
|
2022-06-14 12:56:03 +00:00
|
|
|
c.mtx.RLock()
|
|
|
|
prevUpdated := c.lastUpdated
|
|
|
|
size := c.lastSize
|
|
|
|
c.mtx.RUnlock()
|
|
|
|
|
|
|
|
if time.Since(prevUpdated) < ttl {
|
2024-06-24 15:04:35 +00:00
|
|
|
hit = true
|
2022-06-14 12:56:03 +00:00
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mtx.Lock()
|
|
|
|
size = c.lastSize
|
|
|
|
if !c.lastUpdated.After(prevUpdated) {
|
|
|
|
size = c.src.MaxObjectSize()
|
|
|
|
c.lastSize = size
|
|
|
|
c.lastUpdated = time.Now()
|
|
|
|
}
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
2024-06-24 15:04:35 +00:00
|
|
|
|
|
|
|
type cacheMetrics interface {
|
|
|
|
AddMethodDuration(method string, d time.Duration, hit bool)
|
|
|
|
}
|