frostfs-node/pkg/services/object_manager/placement/cache_test.go
Airat Arifullin 7e542906ef
All checks were successful
Tests and linters / Run gofumpt (pull_request) Successful in 2m49s
DCO action / DCO (pull_request) Successful in 3m5s
Vulncheck / Vulncheck (pull_request) Successful in 3m4s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m21s
Tests and linters / gopls check (pull_request) Successful in 3m29s
Build / Build Components (pull_request) Successful in 3m43s
Tests and linters / Staticcheck (pull_request) Successful in 4m22s
Tests and linters / Lint (pull_request) Successful in 5m3s
Tests and linters / Tests (pull_request) Successful in 5m23s
Tests and linters / Tests with -race (pull_request) Successful in 6m37s
Tests and linters / Run gofumpt (push) Successful in 2m18s
Tests and linters / Staticcheck (push) Successful in 4m36s
Vulncheck / Vulncheck (push) Successful in 5m14s
Build / Build Components (push) Successful in 5m41s
Pre-commit hooks / Pre-commit (push) Successful in 6m24s
Tests and linters / Tests with -race (push) Successful in 6m21s
Tests and linters / Lint (push) Successful in 6m57s
Tests and linters / gopls check (push) Successful in 7m27s
Tests and linters / Tests (push) Successful in 9m23s
[#1539] go.mod: Bump frostfs-sdk-go version
* Also fix placement unit-test in object manager

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
2024-12-06 15:29:37 +03:00

97 lines
2.7 KiB
Go

package placement_test
import (
"strconv"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
"github.com/stretchr/testify/require"
)
func TestContainerNodesCache(t *testing.T) {
const size = 3
nodes := [6]netmapSDK.NodeInfo{}
for i := range nodes {
nodes[i].SetAttribute("ATTR", strconv.Itoa(i))
}
nm := func(epoch uint64, nodes []netmapSDK.NodeInfo) *netmapSDK.NetMap {
var nm netmapSDK.NetMap
nm.SetEpoch(epoch)
nm.SetNodes(nodes)
return &nm
}
var pp netmapSDK.PlacementPolicy
require.NoError(t, pp.DecodeString("REP 1"))
t.Run("update netmap on the new epoch", func(t *testing.T) {
c := placement.NewContainerNodesCache(size)
cnr := cidtest.ID()
res, err := c.ContainerNodes(nm(1, nodes[0:1]), cnr, pp)
require.NoError(t, err)
// Use other nodes in the argument to ensure the result is taken from cache.
resCached, err := c.ContainerNodes(nm(1, nodes[1:2]), cnr, pp)
require.NoError(t, err)
require.Equal(t, res, resCached)
// Update epoch, netmap should be purged.
resCached, err = c.ContainerNodes(nm(2, nodes[2:3]), cnr, pp)
require.NoError(t, err)
require.NotEqual(t, res, resCached)
})
t.Run("cache uses container as a key", func(t *testing.T) {
c := placement.NewContainerNodesCache(size)
res1, err := c.ContainerNodes(nm(1, nodes[0:1]), cidtest.ID(), pp)
require.NoError(t, err)
res2, err := c.ContainerNodes(nm(1, nodes[1:2]), cidtest.ID(), pp)
require.NoError(t, err)
require.NotEqual(t, res1, res2)
})
t.Run("cache respects size parameter", func(t *testing.T) {
c := placement.NewContainerNodesCache(size)
nm1 := nm(1, nodes[0:1])
nm2 := nm(1, nodes[1:2])
cnr := [size * 2]cid.ID{}
res := [size * 2][][]netmapSDK.NodeInfo{}
for i := range size * 2 {
cnr[i] = cidtest.ID()
var err error
res[i], err = c.ContainerNodes(nm1, cnr[i], pp)
require.NoError(t, err)
}
for i := size; i < size*2; i++ {
r, err := c.ContainerNodes(nm2, cnr[i], pp)
require.NoError(t, err)
require.Equal(t, res[i], r)
}
for i := range size {
r, err := c.ContainerNodes(nm2, cnr[i], pp)
require.NoError(t, err)
require.NotEqual(t, res[i], r)
}
})
t.Run("the error is propagated", func(t *testing.T) {
var pp netmapSDK.PlacementPolicy
r := netmapSDK.ReplicaDescriptor{}
r.SetNumberOfObjects(1)
r.SetSelectorName("Missing")
pp.AddReplicas(r)
c := placement.NewContainerNodesCache(size)
_, err := c.ContainerNodes(nm(1, nodes[0:1]), cidtest.ID(), pp)
require.Error(t, err)
})
}