From 9269ed344db58cc29189dc664ba381b9063617af Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 9 Jun 2022 15:51:38 +0300 Subject: [PATCH] [#1510] placement: Cache container nodes In case we have lots of objects in a single container, `GetContainerNodes` invoked indirectly by a policer can be seen in pprof. Signed-off-by: Evgenii Stratonikov --- .../object_manager/placement/netmap.go | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/services/object_manager/placement/netmap.go b/pkg/services/object_manager/placement/netmap.go index 32f1adefe..0c67b7470 100644 --- a/pkg/services/object_manager/placement/netmap.go +++ b/pkg/services/object_manager/placement/netmap.go @@ -3,7 +3,9 @@ package placement import ( "crypto/sha256" "fmt" + "sync" + "github.com/hashicorp/golang-lru/simplelru" "github.com/nspcc-dev/neofs-node/pkg/core/netmap" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap" @@ -12,6 +14,12 @@ import ( type netMapBuilder struct { nmSrc netmap.Source + // mtx protects lastNm and containerCache fields. + mtx sync.Mutex + 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 } type netMapSrc struct { @@ -20,15 +28,22 @@ type netMapSrc struct { nm *netmapSDK.Netmap } +// defaultContainerCacheSize is the default size for the container cache. +const defaultContainerCacheSize = 10 + func NewNetworkMapBuilder(nm *netmapSDK.Netmap) Builder { + cache, _ := simplelru.NewLRU(defaultContainerCacheSize, nil) // no error return &netMapBuilder{ - nmSrc: &netMapSrc{nm: nm}, + nmSrc: &netMapSrc{nm: nm}, + containerCache: cache, } } func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder { + cache, _ := simplelru.NewLRU(defaultContainerCacheSize, nil) // no error return &netMapBuilder{ - nmSrc: nmSrc, + nmSrc: nmSrc, + containerCache: cache, } } @@ -45,11 +60,28 @@ func (b *netMapBuilder) BuildPlacement(cnr cid.ID, obj *oid.ID, p *netmapSDK.Pla binCnr := make([]byte, sha256.Size) cnr.Encode(binCnr) + b.mtx.Lock() + if nm == b.lastNm { + raw, ok := b.containerCache.Get(string(binCnr)) + b.mtx.Unlock() + if ok { + cn := raw.(netmapSDK.ContainerNodes) + return BuildObjectPlacement(nm, cn, obj) + } + } else { + b.containerCache.Purge() + b.mtx.Unlock() + } + cn, err := nm.GetContainerNodes(p, binCnr) if err != nil { return nil, fmt.Errorf("could not get container nodes: %w", err) } + b.mtx.Lock() + b.containerCache.Add(string(binCnr), cn) + b.mtx.Unlock() + return BuildObjectPlacement(nm, cn, obj) }