[#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>
remotes/fyrchik/metabase-list
Evgenii Stratonikov 2022-06-09 15:51:38 +03:00 committed by LeL
parent 0dad0114f0
commit 9269ed344d
1 changed files with 34 additions and 2 deletions

View File

@ -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)
}