[#1632] node: Configure TTL of Sidechain caches

From now cache TTL can be parameterized in the `neofs-node` app using
`cache_ttl` config key. `disable_cache` value is no longer supported.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-08-16 15:59:30 +04:00 committed by LeL
parent 5ce8315cd8
commit fa3124fc33
9 changed files with 35 additions and 33 deletions

View file

@ -128,13 +128,10 @@ func (c *lruNetCache) get(key interface{}) (interface{}, error) {
// that implements container storage. // that implements container storage.
type ttlContainerStorage ttlNetCache type ttlContainerStorage ttlNetCache
func newCachedContainerStorage(v container.Source) *ttlContainerStorage { func newCachedContainerStorage(v container.Source, ttl time.Duration) *ttlContainerStorage {
const ( const containerCacheSize = 100
containerCacheSize = 100
containerCacheTTL = 30 * time.Second
)
lruCnrCache := newNetworkTTLCache(containerCacheSize, containerCacheTTL, func(key interface{}) (interface{}, error) { lruCnrCache := newNetworkTTLCache(containerCacheSize, ttl, func(key interface{}) (interface{}, error) {
var id cid.ID var id cid.ID
err := id.DecodeString(key.(string)) err := id.DecodeString(key.(string))
@ -165,13 +162,10 @@ func (s *ttlContainerStorage) Get(cnr cid.ID) (*container.Container, error) {
type ttlEACLStorage ttlNetCache type ttlEACLStorage ttlNetCache
func newCachedEACLStorage(v eacl.Source) *ttlEACLStorage { func newCachedEACLStorage(v eacl.Source, ttl time.Duration) *ttlEACLStorage {
const ( const eaclCacheSize = 100
eaclCacheSize = 100
eaclCacheTTL = 30 * time.Second
)
lruCnrCache := newNetworkTTLCache(eaclCacheSize, eaclCacheTTL, func(key interface{}) (interface{}, error) { lruCnrCache := newNetworkTTLCache(eaclCacheSize, ttl, func(key interface{}) (interface{}, error) {
var id cid.ID var id cid.ID
err := id.DecodeString(key.(string)) err := id.DecodeString(key.(string))
@ -253,13 +247,10 @@ type cacheItemContainerList struct {
list []cid.ID list []cid.ID
} }
func newCachedContainerLister(c *cntClient.Client) *ttlContainerLister { func newCachedContainerLister(c *cntClient.Client, ttl time.Duration) *ttlContainerLister {
const ( const containerListerCacheSize = 100
containerListerCacheSize = 100
containerListerCacheTTL = 30 * time.Second
)
lruCnrListerCache := newNetworkTTLCache(containerListerCacheSize, containerListerCacheTTL, func(key interface{}) (interface{}, error) { lruCnrListerCache := newNetworkTTLCache(containerListerCacheSize, ttl, func(key interface{}) (interface{}, error) {
var ( var (
id *user.ID id *user.ID
strID = key.(string) strID = key.(string)

View file

@ -148,7 +148,8 @@ type cfgMorph struct {
notaryEnabled bool notaryEnabled bool
disableCache bool // TTL of Sidechain cached values. Non-positive value disables caching.
cacheTTL time.Duration
eigenTrustTicker *eigenTrustTickers // timers for EigenTrust iterations eigenTrustTicker *eigenTrustTickers // timers for EigenTrust iterations

View file

@ -27,6 +27,8 @@ const (
// PriorityDefault is a default endpoint priority for the morph client. // PriorityDefault is a default endpoint priority for the morph client.
PriorityDefault = 1 PriorityDefault = 1
CacheTTLDefault = 30 * time.Second
) )
// RPCEndpoint returns list of the values of "rpc_endpoint" config parameter // RPCEndpoint returns list of the values of "rpc_endpoint" config parameter
@ -74,8 +76,15 @@ func DialTimeout(c *config.Config) time.Duration {
return DialTimeoutDefault return DialTimeoutDefault
} }
// DisableCache returns the value of "disable_cache" config parameter // CacheTTL returns the value of "cache_ttl" config parameter
// from "morph" section. // from "morph" section.
func DisableCache(c *config.Config) bool { //
return config.BoolSafe(c.Sub(subsection), "disable_cache") // Returns CacheTTLDefault if value is zero or invalid. Supports negative durations.
func CacheTTL(c *config.Config) time.Duration {
res := config.DurationSafe(c.Sub(subsection), "cache_ttl")
if res != 0 {
return res
}
return CacheTTLDefault
} }

View file

@ -17,7 +17,7 @@ func TestMorphSection(t *testing.T) {
require.Panics(t, func() { morphconfig.RPCEndpoint(empty) }) require.Panics(t, func() { morphconfig.RPCEndpoint(empty) })
require.Equal(t, morphconfig.DialTimeoutDefault, morphconfig.DialTimeout(empty)) require.Equal(t, morphconfig.DialTimeoutDefault, morphconfig.DialTimeout(empty))
require.Equal(t, false, morphconfig.DisableCache(empty)) require.Equal(t, morphconfig.CacheTTLDefault, morphconfig.CacheTTL(empty))
}) })
const path = "../../../../config/example/node" const path = "../../../../config/example/node"
@ -32,7 +32,7 @@ func TestMorphSection(t *testing.T) {
var fileConfigTest = func(c *config.Config) { var fileConfigTest = func(c *config.Config) {
require.Equal(t, rpcs, morphconfig.RPCEndpoint(c)) require.Equal(t, rpcs, morphconfig.RPCEndpoint(c))
require.Equal(t, 30*time.Second, morphconfig.DialTimeout(c)) require.Equal(t, 30*time.Second, morphconfig.DialTimeout(c))
require.Equal(t, true, morphconfig.DisableCache(c)) require.Equal(t, 15*time.Second, morphconfig.CacheTTL(c))
} }
configtest.ForEachFileType(path, fileConfigTest) configtest.ForEachFileType(path, fileConfigTest)

View file

@ -63,7 +63,7 @@ func initContainerService(c *cfg) {
neoClient: wrap, neoClient: wrap,
} }
if c.cfgMorph.disableCache { if c.cfgMorph.cacheTTL <= 0 {
c.cfgObject.eaclSource = eACLFetcher c.cfgObject.eaclSource = eACLFetcher
cnrRdr.eacl = eACLFetcher cnrRdr.eacl = eACLFetcher
c.cfgObject.cnrSource = cnrSrc c.cfgObject.cnrSource = cnrSrc
@ -71,9 +71,9 @@ func initContainerService(c *cfg) {
cnrRdr.lister = wrap cnrRdr.lister = wrap
} else { } else {
// use RPC node as source of Container contract items (with caching) // use RPC node as source of Container contract items (with caching)
cachedContainerStorage := newCachedContainerStorage(cnrSrc) cachedContainerStorage := newCachedContainerStorage(cnrSrc, c.cfgMorph.cacheTTL)
cachedEACLStorage := newCachedEACLStorage(eACLFetcher) cachedEACLStorage := newCachedEACLStorage(eACLFetcher, c.cfgMorph.cacheTTL)
cachedContainerLister := newCachedContainerLister(wrap) cachedContainerLister := newCachedContainerLister(wrap, c.cfgMorph.cacheTTL)
subscribeToContainerCreation(c, func(e event.Event) { subscribeToContainerCreation(c, func(e event.Event) {
ev := e.(containerEvent.PutSuccess) ev := e.(containerEvent.PutSuccess)

View file

@ -86,9 +86,9 @@ func initMorphComponents(c *cfg) {
var netmapSource netmap.Source var netmapSource netmap.Source
c.cfgMorph.disableCache = morphconfig.DisableCache(c.appCfg) c.cfgMorph.cacheTTL = morphconfig.CacheTTL(c.appCfg)
if c.cfgMorph.disableCache { if c.cfgMorph.cacheTTL <= 0 {
netmapSource = wrap netmapSource = wrap
} else { } else {
// use RPC node as source of netmap (with caching) // use RPC node as source of netmap (with caching)

View file

@ -61,7 +61,7 @@ NEOFS_CONTRACTS_PROXY=ad7c6b55b737b696e5c82c85445040964a03e97f
# Morph chain section # Morph chain section
NEOFS_MORPH_DIAL_TIMEOUT=30s NEOFS_MORPH_DIAL_TIMEOUT=30s
NEOFS_MORPH_DISABLE_CACHE=true NEOFS_MORPH_CACHE_TTL=15s
NEOFS_MORPH_RPC_ENDPOINT_0_ADDRESS="wss://rpc1.morph.fs.neo.org:40341/ws" NEOFS_MORPH_RPC_ENDPOINT_0_ADDRESS="wss://rpc1.morph.fs.neo.org:40341/ws"
NEOFS_MORPH_RPC_ENDPOINT_0_PRIORITY=0 NEOFS_MORPH_RPC_ENDPOINT_0_PRIORITY=0
NEOFS_MORPH_RPC_ENDPOINT_1_ADDRESS="wss://rpc2.morph.fs.neo.org:40341/ws" NEOFS_MORPH_RPC_ENDPOINT_1_ADDRESS="wss://rpc2.morph.fs.neo.org:40341/ws"

View file

@ -99,7 +99,7 @@
}, },
"morph": { "morph": {
"dial_timeout": "30s", "dial_timeout": "30s",
"disable_cache": true, "cache_ttl": "15s",
"rpc_endpoint": [ "rpc_endpoint": [
{ {
"address": "wss://rpc1.morph.fs.neo.org:40341/ws", "address": "wss://rpc1.morph.fs.neo.org:40341/ws",

View file

@ -81,7 +81,8 @@ contracts: # side chain NEOFS contract script hashes; optional, override values
morph: morph:
dial_timeout: 30s # timeout for side chain NEO RPC client connection dial_timeout: 30s # timeout for side chain NEO RPC client connection
disable_cache: true # do not use TTL cache for side chain GET operations cache_ttl: 15s # Sidechain cache TTL value (min interval between similar calls). Negative value disables caching.
# Cached entities: containers, container lists, eACL tables.
rpc_endpoint: # side chain NEO RPC endpoints; are shuffled and used one by one until the first success rpc_endpoint: # side chain NEO RPC endpoints; are shuffled and used one by one until the first success
- address: wss://rpc1.morph.fs.neo.org:40341/ws - address: wss://rpc1.morph.fs.neo.org:40341/ws
priority: 0 priority: 0