[#1248] placement: Decouple ContainerNodes() cache from the placement builder.

Also, write tests.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-07-12 17:27:59 +03:00 committed by Evgenii Stratonikov
parent 3a48b282b6
commit 286df198c9
3 changed files with 170 additions and 41 deletions

View file

@ -3,24 +3,16 @@ package placement
import (
"crypto/sha256"
"fmt"
"sync"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"github.com/hashicorp/golang-lru/v2/simplelru"
)
type netMapBuilder struct {
nmSrc netmap.Source
// mtx protects lastEpoch and containerCache fields.
mtx sync.Mutex
// lastEpoch contains contains network map epoch for all values in the container cache.
lastEpoch uint64
// containerCache caches container nodes by ID. It is used to skip `GetContainerNodes` invocation if
// neither netmap nor container has changed.
containerCache simplelru.LRUCache[cid.ID, [][]netmapSDK.NodeInfo]
nmSrc netmap.Source
containerCache *ContainerNodesCache
}
type netMapSrc struct {
@ -29,22 +21,17 @@ 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[cid.ID, [][]netmapSDK.NodeInfo](defaultContainerCacheSize, nil) // no error
return &netMapBuilder{
nmSrc: &netMapSrc{nm: nm},
containerCache: cache,
containerCache: NewContainerNodesCache(0),
}
}
func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder {
cache, _ := simplelru.NewLRU[cid.ID, [][]netmapSDK.NodeInfo](defaultContainerCacheSize, nil) // no error
return &netMapBuilder{
nmSrc: nmSrc,
containerCache: cache,
containerCache: NewContainerNodesCache(0),
}
}
@ -58,33 +45,11 @@ func (b *netMapBuilder) BuildPlacement(cnr cid.ID, obj *oid.ID, p netmapSDK.Plac
return nil, fmt.Errorf("could not get network map: %w", err)
}
binCnr := make([]byte, sha256.Size)
cnr.Encode(binCnr)
b.mtx.Lock()
if nm.Epoch() == b.lastEpoch {
raw, ok := b.containerCache.Get(cnr)
b.mtx.Unlock()
if ok {
return BuildObjectPlacement(nm, raw, obj)
}
} else {
b.lastEpoch = nm.Epoch()
b.containerCache.Purge()
b.mtx.Unlock()
}
cn, err := nm.ContainerNodes(p, binCnr)
cn, err := b.containerCache.ContainerNodes(nm, cnr, p)
if err != nil {
return nil, fmt.Errorf("could not get container nodes: %w", err)
return nil, err
}
b.mtx.Lock()
if b.lastEpoch == nm.Epoch() {
b.containerCache.Add(cnr, cn)
}
b.mtx.Unlock()
return BuildObjectPlacement(nm, cn, obj)
}