forked from TrueCloudLab/frostfs-node
[#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:
parent
3a48b282b6
commit
286df198c9
3 changed files with 170 additions and 41 deletions
69
pkg/services/object_manager/placement/cache.go
Normal file
69
pkg/services/object_manager/placement/cache.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package placement
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
||||
"github.com/hashicorp/golang-lru/v2/simplelru"
|
||||
)
|
||||
|
||||
// ContainerNodesCache caches results of ContainerNodes() invocation between epochs.
|
||||
type ContainerNodesCache struct {
|
||||
// mtx protects lastEpoch and containerCache fields.
|
||||
mtx sync.Mutex
|
||||
// lastEpoch 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]
|
||||
}
|
||||
|
||||
// defaultContainerCacheSize is the default size for the container cache.
|
||||
const defaultContainerCacheSize = 10
|
||||
|
||||
// NewContainerNodesCache creates new cache which saves the result of the ContainerNodes() invocations.
|
||||
// If size is <= 0, defaultContainerCacheSize (10) is used.
|
||||
func NewContainerNodesCache(size int) *ContainerNodesCache {
|
||||
if size <= 0 {
|
||||
size = defaultContainerCacheSize
|
||||
}
|
||||
|
||||
cache, _ := simplelru.NewLRU[cid.ID, [][]netmapSDK.NodeInfo](size, nil) // no error
|
||||
return &ContainerNodesCache{
|
||||
containerCache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
// ContainerNodes returns the result of nm.ContainerNodes(), possibly from the cache.
|
||||
func (c *ContainerNodesCache) ContainerNodes(nm *netmapSDK.NetMap, cnr cid.ID, p netmapSDK.PlacementPolicy) ([][]netmapSDK.NodeInfo, error) {
|
||||
c.mtx.Lock()
|
||||
if nm.Epoch() == c.lastEpoch {
|
||||
raw, ok := c.containerCache.Get(cnr)
|
||||
c.mtx.Unlock()
|
||||
if ok {
|
||||
return raw, nil
|
||||
}
|
||||
} else {
|
||||
c.lastEpoch = nm.Epoch()
|
||||
c.containerCache.Purge()
|
||||
c.mtx.Unlock()
|
||||
}
|
||||
|
||||
binCnr := make([]byte, sha256.Size)
|
||||
cnr.Encode(binCnr)
|
||||
|
||||
cn, err := nm.ContainerNodes(p, binCnr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get container nodes: %w", err)
|
||||
}
|
||||
|
||||
c.mtx.Lock()
|
||||
if c.lastEpoch == nm.Epoch() {
|
||||
c.containerCache.Add(cnr, cn)
|
||||
}
|
||||
c.mtx.Unlock()
|
||||
return cn, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue