[#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 <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-06-09 15:51:38 +03:00 committed by LeL
parent 0dad0114f0
commit 9269ed344d

View file

@ -3,7 +3,9 @@ package placement
import ( import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"sync"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap" netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap"
@ -12,6 +14,12 @@ import (
type netMapBuilder struct { type netMapBuilder struct {
nmSrc netmap.Source 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 { type netMapSrc struct {
@ -20,15 +28,22 @@ type netMapSrc struct {
nm *netmapSDK.Netmap nm *netmapSDK.Netmap
} }
// defaultContainerCacheSize is the default size for the container cache.
const defaultContainerCacheSize = 10
func NewNetworkMapBuilder(nm *netmapSDK.Netmap) Builder { func NewNetworkMapBuilder(nm *netmapSDK.Netmap) Builder {
cache, _ := simplelru.NewLRU(defaultContainerCacheSize, nil) // no error
return &netMapBuilder{ return &netMapBuilder{
nmSrc: &netMapSrc{nm: nm}, nmSrc: &netMapSrc{nm: nm},
containerCache: cache,
} }
} }
func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder { func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder {
cache, _ := simplelru.NewLRU(defaultContainerCacheSize, nil) // no error
return &netMapBuilder{ 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) binCnr := make([]byte, sha256.Size)
cnr.Encode(binCnr) 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) cn, err := nm.GetContainerNodes(p, binCnr)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not get container nodes: %w", err) 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) return BuildObjectPlacement(nm, cn, obj)
} }