2021-03-04 09:26:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
2021-03-04 12:32:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2022-01-31 13:34:01 +00:00
|
|
|
cntClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
2021-03-04 11:26:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl"
|
2021-11-10 07:08:33 +00:00
|
|
|
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-03-04 09:26:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type netValueReader func(interface{}) (interface{}, error)
|
|
|
|
|
|
|
|
type valueWithTime struct {
|
|
|
|
v interface{}
|
|
|
|
t time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// entity that provides TTL cache interface.
|
|
|
|
type ttlNetCache struct {
|
|
|
|
ttl time.Duration
|
|
|
|
|
|
|
|
sz int
|
|
|
|
|
|
|
|
cache *lru.Cache
|
|
|
|
|
|
|
|
netRdr netValueReader
|
|
|
|
}
|
|
|
|
|
|
|
|
// complicates netValueReader with TTL caching mechanism.
|
|
|
|
func newNetworkTTLCache(sz int, ttl time.Duration, netRdr netValueReader) *ttlNetCache {
|
|
|
|
cache, err := lru.New(sz)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
return &ttlNetCache{
|
|
|
|
ttl: ttl,
|
|
|
|
sz: sz,
|
|
|
|
cache: cache,
|
|
|
|
netRdr: netRdr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads value by the key.
|
|
|
|
//
|
|
|
|
// updates the value from the network on cache miss or by TTL.
|
|
|
|
//
|
|
|
|
// returned value should not be modified.
|
|
|
|
func (c *ttlNetCache) get(key interface{}) (interface{}, error) {
|
|
|
|
val, ok := c.cache.Peek(key)
|
|
|
|
if ok {
|
|
|
|
valWithTime := val.(*valueWithTime)
|
|
|
|
|
|
|
|
if time.Since(valWithTime.t) < c.ttl {
|
|
|
|
return valWithTime.v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cache.Remove(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := c.netRdr(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cache.Add(key, &valueWithTime{
|
|
|
|
v: val,
|
|
|
|
t: time.Now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
func (c *ttlNetCache) remove(key interface{}) {
|
|
|
|
c.cache.Remove(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ttlNetCache) keys() []interface{} {
|
|
|
|
return c.cache.Keys()
|
|
|
|
}
|
|
|
|
|
2021-03-04 12:32:16 +00:00
|
|
|
// entity that provides LRU cache interface.
|
|
|
|
type lruNetCache struct {
|
|
|
|
cache *lru.Cache
|
|
|
|
|
|
|
|
netRdr netValueReader
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:30:30 +00:00
|
|
|
// newNetworkLRUCache returns wrapper over netValueReader with LRU cache.
|
2021-03-04 12:32:16 +00:00
|
|
|
func newNetworkLRUCache(sz int, netRdr netValueReader) *lruNetCache {
|
|
|
|
cache, err := lru.New(sz)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
return &lruNetCache{
|
|
|
|
cache: cache,
|
|
|
|
netRdr: netRdr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads value by the key.
|
|
|
|
//
|
|
|
|
// updates the value from the network on cache miss.
|
|
|
|
//
|
|
|
|
// returned value should not be modified.
|
|
|
|
func (c *lruNetCache) get(key interface{}) (interface{}, error) {
|
|
|
|
val, ok := c.cache.Get(key)
|
|
|
|
if ok {
|
|
|
|
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.
|
|
|
|
type ttlContainerStorage ttlNetCache
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
func newCachedContainerStorage(v container.Source) *ttlContainerStorage {
|
2021-03-04 09:26:47 +00:00
|
|
|
const (
|
|
|
|
containerCacheSize = 100
|
|
|
|
containerCacheTTL = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
lruCnrCache := newNetworkTTLCache(containerCacheSize, containerCacheTTL, func(key interface{}) (interface{}, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
var id cid.ID
|
2021-03-04 09:26:47 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err := id.DecodeString(key.(string))
|
2021-03-04 09:26:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
return v.Get(&id)
|
2021-03-04 09:26:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return (*ttlContainerStorage)(lruCnrCache)
|
|
|
|
}
|
|
|
|
|
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.
|
2021-05-31 11:03:17 +00:00
|
|
|
func (s *ttlContainerStorage) Get(cid *cid.ID) (*containerSDK.Container, error) {
|
2021-03-04 09:26:47 +00:00
|
|
|
val, err := (*ttlNetCache)(s).get(cid.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.(*containerSDK.Container), nil
|
|
|
|
}
|
2021-03-04 11:26:19 +00:00
|
|
|
|
|
|
|
type ttlEACLStorage ttlNetCache
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
func newCachedEACLStorage(v eacl.Source) *ttlEACLStorage {
|
2021-03-04 11:26:19 +00:00
|
|
|
const (
|
|
|
|
eaclCacheSize = 100
|
|
|
|
eaclCacheTTL = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
lruCnrCache := newNetworkTTLCache(eaclCacheSize, eaclCacheTTL, func(key interface{}) (interface{}, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
var id cid.ID
|
2021-03-04 11:26:19 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err := id.DecodeString(key.(string))
|
2021-03-04 11:26:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
return v.GetEACL(&id)
|
2021-03-04 11:26:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return (*ttlEACLStorage)(lruCnrCache)
|
|
|
|
}
|
|
|
|
|
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.
|
2021-05-31 11:03:17 +00:00
|
|
|
func (s *ttlEACLStorage) GetEACL(cid *cid.ID) (*eaclSDK.Table, error) {
|
2021-03-04 11:26:19 +00:00
|
|
|
val, err := (*ttlNetCache)(s).get(cid.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.(*eaclSDK.Table), nil
|
|
|
|
}
|
2021-03-04 12:32:16 +00:00
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
// InvalidateEACL removes cached eACL value.
|
|
|
|
func (s *ttlEACLStorage) InvalidateEACL(cid *cid.ID) {
|
|
|
|
(*ttlNetCache)(s).remove(cid.String())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
lruNetmapCache := newNetworkLRUCache(netmapCacheSize, func(key interface{}) (interface{}, error) {
|
|
|
|
return v.GetNetMapByEpoch(key.(uint64))
|
|
|
|
})
|
|
|
|
|
|
|
|
return &lruNetmapSource{
|
|
|
|
netState: s,
|
|
|
|
cache: lruNetmapCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *lruNetmapSource) GetNetMap(diff uint64) (*netmapSDK.Netmap, error) {
|
|
|
|
return s.getNetMapByEpoch(s.netState.CurrentEpoch() - diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *lruNetmapSource) GetNetMapByEpoch(epoch uint64) (*netmapSDK.Netmap, error) {
|
|
|
|
return s.getNetMapByEpoch(epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *lruNetmapSource) getNetMapByEpoch(epoch uint64) (*netmapSDK.Netmap, error) {
|
|
|
|
val, err := s.cache.get(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.(*netmapSDK.Netmap), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *lruNetmapSource) Epoch() (uint64, error) {
|
|
|
|
return s.netState.CurrentEpoch(), nil
|
|
|
|
}
|
2021-08-30 11:16:41 +00:00
|
|
|
|
|
|
|
// wrapper over TTL cache of values read from the network
|
|
|
|
// that implements container lister.
|
|
|
|
type ttlContainerLister ttlNetCache
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
func newCachedContainerLister(c *cntClient.Client) *ttlContainerLister {
|
2021-08-30 11:16:41 +00:00
|
|
|
const (
|
|
|
|
containerListerCacheSize = 100
|
|
|
|
containerListerCacheTTL = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
lruCnrListerCache := newNetworkTTLCache(containerListerCacheSize, containerListerCacheTTL, func(key interface{}) (interface{}, error) {
|
|
|
|
var (
|
2022-05-17 13:59:46 +00:00
|
|
|
id *user.ID
|
2021-08-30 11:16:41 +00:00
|
|
|
strID = key.(string)
|
|
|
|
)
|
|
|
|
|
|
|
|
if strID != "" {
|
2022-05-17 13:59:46 +00:00
|
|
|
id = new(user.ID)
|
2021-08-30 11:16:41 +00:00
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
err := id.DecodeString(strID)
|
2021-08-30 11:16:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
return c.List(id)
|
2021-08-30 11:16:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return (*ttlContainerLister)(lruCnrListerCache)
|
|
|
|
}
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
// List returns list of container IDs from the cache. If list is missing in the
|
|
|
|
// cache or expired, then it returns container IDs from side chain and updates
|
|
|
|
// the cache.
|
2022-05-17 13:59:46 +00:00
|
|
|
func (s *ttlContainerLister) List(id *user.ID) ([]*cid.ID, error) {
|
2021-08-30 11:16:41 +00:00
|
|
|
var str string
|
|
|
|
|
|
|
|
if id != nil {
|
|
|
|
str = id.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := (*ttlNetCache)(s).get(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.([]*cid.ID), nil
|
|
|
|
}
|
2021-09-08 13:40:23 +00:00
|
|
|
|
|
|
|
// InvalidateContainerList removes cached list of container IDs.
|
2022-05-17 13:59:46 +00:00
|
|
|
func (s *ttlContainerLister) InvalidateContainerList(id *user.ID) {
|
2021-09-08 13:40:23 +00:00
|
|
|
(*ttlNetCache)(s).remove(id.String())
|
|
|
|
}
|
|
|
|
|
2022-03-29 13:25:03 +00:00
|
|
|
type cachedIRFetcher ttlNetCache
|
|
|
|
|
|
|
|
func newCachedIRFetcher(f interface{ InnerRingKeys() ([][]byte, error) }) *cachedIRFetcher {
|
|
|
|
const (
|
|
|
|
irFetcherCacheSize = 1 // we intend to store only one value
|
|
|
|
|
|
|
|
// Without the cache in the testnet we can see several hundred simultaneous
|
|
|
|
// requests (neofs-node #1278), so limiting the request rate solves the issue.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// for now (neofs-contract v0.15.0 notary disabled env) to monitor it.
|
|
|
|
irFetcherCacheTTL = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
irFetcherCache := newNetworkTTLCache(irFetcherCacheSize, irFetcherCacheTTL,
|
|
|
|
func(key interface{}) (interface{}, error) {
|
|
|
|
return f.InnerRingKeys()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return (*cachedIRFetcher)(irFetcherCache)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (f *cachedIRFetcher) InnerRingKeys() ([][]byte, error) {
|
|
|
|
val, err := (*ttlNetCache)(f).get("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.([][]byte), nil
|
|
|
|
}
|