2023-06-29 09:13:01 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
2023-07-06 15:29:16 +00:00
|
|
|
"time"
|
2023-06-29 09:13:01 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/replicator"
|
2023-09-04 09:00:49 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-06-29 09:13:01 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-06-29 09:13:01 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuryObjectWithoutContainer(t *testing.T) {
|
|
|
|
// Key space
|
|
|
|
addr := oidtest.Address()
|
|
|
|
objs := []objectcore.AddressWithType{
|
|
|
|
{
|
|
|
|
Address: addr,
|
2023-07-06 12:36:41 +00:00
|
|
|
Type: objectSDK.TypeRegular,
|
2023-06-29 09:13:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Container source and bury function
|
|
|
|
buryCh := make(chan oid.Address)
|
2023-08-24 12:27:24 +00:00
|
|
|
containerSrc := containerSrc{
|
|
|
|
get: func(id cid.ID) (*container.Container, error) {
|
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
|
|
|
},
|
|
|
|
deletionInfo: func(id cid.ID) (*container.DelInfo, error) {
|
|
|
|
return &container.DelInfo{}, nil
|
|
|
|
},
|
2023-06-29 09:13:01 +00:00
|
|
|
}
|
|
|
|
buryFn := func(ctx context.Context, a oid.Address) error {
|
|
|
|
buryCh <- a
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policer instance
|
|
|
|
p := New(
|
|
|
|
WithKeySpaceIterator(&sliceKeySpaceIterator{objs: objs}),
|
2023-08-24 12:27:24 +00:00
|
|
|
WithContainerSource(containerSrc),
|
2023-06-29 09:13:01 +00:00
|
|
|
WithBuryFunc(buryFn),
|
2024-02-02 17:57:49 +00:00
|
|
|
WithPool(testPool(t)),
|
2023-06-29 09:13:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go p.Run(ctx)
|
|
|
|
|
|
|
|
require.Equal(t, addr, <-buryCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessObject(t *testing.T) {
|
|
|
|
// Notes:
|
|
|
|
// - nodes are referred to by their index throughout, which is embedded in the public key
|
|
|
|
// - node with index 0 always refers to the local node, so there's no need to add it to objHolders
|
|
|
|
// - policy is used only to match the number of replicas for each index in the placement
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
2023-07-06 12:36:41 +00:00
|
|
|
objType objectSDK.Type
|
2023-06-29 09:13:01 +00:00
|
|
|
nodeCount int
|
|
|
|
policy string
|
|
|
|
placement [][]int
|
|
|
|
objHolders []int
|
|
|
|
maintenanceNodes []int
|
|
|
|
wantRemoveRedundant bool
|
|
|
|
wantReplicateTo []int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "1 copy already held by local node",
|
|
|
|
nodeCount: 1,
|
|
|
|
policy: `REP 1`,
|
|
|
|
placement: [][]int{{0}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "1 copy already held by the remote node",
|
|
|
|
nodeCount: 2,
|
|
|
|
policy: `REP 1`,
|
|
|
|
placement: [][]int{{1}},
|
|
|
|
objHolders: []int{1},
|
|
|
|
wantRemoveRedundant: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "1 copy not yet held by the remote node",
|
|
|
|
nodeCount: 2,
|
|
|
|
policy: `REP 1`,
|
|
|
|
placement: [][]int{{1}},
|
|
|
|
wantReplicateTo: []int{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "2 copies already held by local and remote node",
|
|
|
|
nodeCount: 2,
|
|
|
|
policy: `REP 2`,
|
|
|
|
placement: [][]int{{0, 1}},
|
|
|
|
objHolders: []int{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "2 copies but not held by remote node",
|
|
|
|
nodeCount: 2,
|
|
|
|
policy: `REP 2`,
|
|
|
|
placement: [][]int{{0, 1}},
|
|
|
|
wantReplicateTo: []int{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple vectors already held by remote node",
|
|
|
|
nodeCount: 2,
|
|
|
|
policy: `REP 2 REP 2`,
|
|
|
|
placement: [][]int{{0, 1}, {0, 1}},
|
|
|
|
objHolders: []int{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple vectors not yet held by remote node",
|
|
|
|
nodeCount: 2,
|
|
|
|
policy: `REP 2 REP 2`,
|
|
|
|
placement: [][]int{{0, 1}, {0, 1}},
|
|
|
|
wantReplicateTo: []int{1, 1}, // is this actually good?
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "lock object must be replicated to all nodes",
|
2023-07-06 12:36:41 +00:00
|
|
|
objType: objectSDK.TypeLock,
|
2023-06-29 09:13:01 +00:00
|
|
|
nodeCount: 3,
|
|
|
|
policy: `REP 1`,
|
|
|
|
placement: [][]int{{0, 1, 2}},
|
|
|
|
wantReplicateTo: []int{1, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "preserve local copy when maintenance nodes exist",
|
|
|
|
nodeCount: 3,
|
|
|
|
policy: `REP 2`,
|
|
|
|
placement: [][]int{{1, 2}},
|
|
|
|
objHolders: []int{1},
|
|
|
|
maintenanceNodes: []int{2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tests {
|
|
|
|
ti := tests[i]
|
|
|
|
t.Run(ti.desc, func(t *testing.T) {
|
|
|
|
addr := oidtest.Address()
|
|
|
|
|
|
|
|
// Netmap, placement policy and placement builder
|
|
|
|
nodes := make([]netmap.NodeInfo, ti.nodeCount)
|
|
|
|
for i := range nodes {
|
|
|
|
nodes[i].SetPublicKey([]byte{byte(i)})
|
|
|
|
}
|
|
|
|
for _, i := range ti.maintenanceNodes {
|
|
|
|
nodes[i].SetMaintenance()
|
|
|
|
}
|
|
|
|
|
|
|
|
var policy netmap.PlacementPolicy
|
|
|
|
require.NoError(t, policy.DecodeString(ti.policy))
|
|
|
|
|
|
|
|
placementVectors := make([][]netmap.NodeInfo, len(ti.placement))
|
|
|
|
for i, pv := range ti.placement {
|
|
|
|
for _, nj := range pv {
|
|
|
|
placementVectors[i] = append(placementVectors[i], nodes[nj])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
placementBuilder := func(cnr cid.ID, obj *oid.ID, p netmap.PlacementPolicy) ([][]netmap.NodeInfo, error) {
|
|
|
|
if cnr.Equals(addr.Container()) && obj != nil && obj.Equals(addr.Object()) {
|
|
|
|
return placementVectors, nil
|
|
|
|
}
|
|
|
|
t.Errorf("unexpected placement build: cid=%v oid=%v", cnr, obj)
|
|
|
|
return nil, errors.New("unexpected placement build")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object remote header
|
2023-07-06 12:36:41 +00:00
|
|
|
headFn := func(_ context.Context, ni netmap.NodeInfo, a oid.Address) (*objectSDK.Object, error) {
|
2023-06-29 09:13:01 +00:00
|
|
|
index := int(ni.PublicKey()[0])
|
|
|
|
if a != addr || index < 1 || index >= ti.nodeCount {
|
|
|
|
t.Errorf("unexpected remote object head: node=%+v addr=%v", ni, a)
|
|
|
|
return nil, errors.New("unexpected object head")
|
|
|
|
}
|
|
|
|
for _, i := range ti.objHolders {
|
|
|
|
if index == i {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, new(apistatus.ObjectNotFound)
|
2023-06-29 09:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Container source
|
|
|
|
cnr := &container.Container{}
|
|
|
|
cnr.Value.Init()
|
|
|
|
cnr.Value.SetPlacementPolicy(policy)
|
2023-08-24 12:27:24 +00:00
|
|
|
containerSrc := containerSrc{
|
|
|
|
get: func(id cid.ID) (*container.Container, error) {
|
|
|
|
if id.Equals(addr.Container()) {
|
|
|
|
return cnr, nil
|
|
|
|
}
|
|
|
|
t.Errorf("unexpected container requested: got=%v, want=%v", id, addr.Container())
|
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
|
|
|
},
|
|
|
|
deletionInfo: func(id cid.ID) (*container.DelInfo, error) {
|
|
|
|
return &container.DelInfo{}, nil
|
|
|
|
},
|
2023-06-29 09:13:01 +00:00
|
|
|
}
|
|
|
|
buryFn := func(ctx context.Context, a oid.Address) error {
|
|
|
|
t.Errorf("unexpected object buried: %v", a)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policer instance
|
|
|
|
var gotRemoveRedundant bool
|
|
|
|
var gotReplicateTo []int
|
|
|
|
|
|
|
|
p := New(
|
2023-08-24 12:27:24 +00:00
|
|
|
WithContainerSource(containerSrc),
|
2023-06-29 09:13:01 +00:00
|
|
|
WithPlacementBuilder(placementBuilderFunc(placementBuilder)),
|
|
|
|
WithNetmapKeys(announcedKeysFunc(func(k []byte) bool {
|
|
|
|
return bytes.Equal(k, nodes[0].PublicKey())
|
|
|
|
})),
|
|
|
|
WithRemoteObjectHeaderFunc(headFn),
|
|
|
|
WithBuryFunc(buryFn),
|
|
|
|
WithRedundantCopyCallback(func(_ context.Context, a oid.Address) {
|
2023-07-13 12:04:29 +00:00
|
|
|
require.True(t, a.Equals(addr), "unexpected redundant copy callback: a=%v", a)
|
2023-06-29 09:13:01 +00:00
|
|
|
gotRemoveRedundant = true
|
|
|
|
}),
|
|
|
|
WithReplicator(replicatorFunc(func(_ context.Context, task replicator.Task, res replicator.TaskResult) {
|
2023-07-13 12:04:29 +00:00
|
|
|
require.True(t, task.Addr.Equals(addr), "unexpected replicator task: %+v", task)
|
2023-06-29 09:13:01 +00:00
|
|
|
for _, node := range task.Nodes {
|
|
|
|
gotReplicateTo = append(gotReplicateTo, int(node.PublicKey()[0]))
|
|
|
|
}
|
|
|
|
})),
|
2024-02-02 17:14:47 +00:00
|
|
|
WithPool(testPool(t)),
|
2023-06-29 09:13:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
addrWithType := objectcore.AddressWithType{
|
|
|
|
Address: addr,
|
|
|
|
Type: ti.objType,
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:00:49 +00:00
|
|
|
err := p.processObject(context.Background(), addrWithType)
|
|
|
|
require.NoError(t, err)
|
2023-06-29 09:13:01 +00:00
|
|
|
sort.Ints(gotReplicateTo)
|
|
|
|
|
|
|
|
require.Equal(t, ti.wantRemoveRedundant, gotRemoveRedundant)
|
|
|
|
require.Equal(t, ti.wantReplicateTo, gotReplicateTo)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:00:49 +00:00
|
|
|
func TestProcessObjectError(t *testing.T) {
|
|
|
|
addr := oidtest.Address()
|
|
|
|
// Container source
|
|
|
|
cnr := &container.Container{}
|
|
|
|
cnr.Value.Init()
|
|
|
|
source := containerSrc{
|
|
|
|
get: func(id cid.ID) (*container.Container, error) {
|
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
|
|
|
},
|
|
|
|
deletionInfo: func(id cid.ID) (*container.DelInfo, error) {
|
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buryFn := func(ctx context.Context, a oid.Address) error {
|
|
|
|
t.Errorf("unexpected object buried: %v", a)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := New(
|
|
|
|
WithContainerSource(source),
|
|
|
|
WithBuryFunc(buryFn),
|
2024-02-02 17:14:47 +00:00
|
|
|
WithPool(testPool(t)),
|
2023-09-04 09:00:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
addrWithType := objectcore.AddressWithType{
|
|
|
|
Address: addr,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, client.IsErrContainerNotFound(p.processObject(context.Background(), addrWithType)))
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:23:52 +00:00
|
|
|
func TestIteratorContract(t *testing.T) {
|
|
|
|
addr := oidtest.Address()
|
|
|
|
objs := []objectcore.AddressWithType{{
|
|
|
|
Address: addr,
|
2023-07-07 15:52:40 +00:00
|
|
|
Type: objectSDK.TypeRegular,
|
2023-07-06 15:23:52 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
buryFn := func(ctx context.Context, a oid.Address) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
it := &predefinedIterator{
|
|
|
|
scenario: []nextResult{
|
|
|
|
{objs, nil},
|
|
|
|
{nil, errors.New("opaque")},
|
|
|
|
{nil, engine.ErrEndOfListing},
|
|
|
|
{nil, engine.ErrEndOfListing},
|
|
|
|
{nil, errors.New("opaque")},
|
|
|
|
{objs, engine.ErrEndOfListing},
|
|
|
|
},
|
|
|
|
finishCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:27:24 +00:00
|
|
|
containerSrc := containerSrc{
|
|
|
|
get: func(id cid.ID) (*container.Container, error) {
|
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
|
|
|
},
|
|
|
|
deletionInfo: func(id cid.ID) (*container.DelInfo, error) {
|
|
|
|
return &container.DelInfo{}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:23:52 +00:00
|
|
|
p := New(
|
|
|
|
WithKeySpaceIterator(it),
|
2023-08-24 12:27:24 +00:00
|
|
|
WithContainerSource(containerSrc),
|
2023-07-06 15:23:52 +00:00
|
|
|
WithBuryFunc(buryFn),
|
2024-02-02 17:57:49 +00:00
|
|
|
WithPool(testPool(t)),
|
2023-07-06 15:29:16 +00:00
|
|
|
func(c *cfg) {
|
|
|
|
c.sleepDuration = time.Millisecond
|
|
|
|
},
|
2023-07-06 15:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go p.Run(ctx)
|
|
|
|
|
|
|
|
<-it.finishCh
|
|
|
|
cancel()
|
|
|
|
require.Equal(t, []string{
|
|
|
|
"Next",
|
|
|
|
"Next",
|
|
|
|
"Next",
|
|
|
|
"Rewind",
|
|
|
|
"Next",
|
|
|
|
"Rewind",
|
|
|
|
"Next",
|
|
|
|
"Next",
|
|
|
|
"Rewind",
|
|
|
|
}, it.calls)
|
|
|
|
}
|
|
|
|
|
2024-02-02 17:14:47 +00:00
|
|
|
func testPool(t *testing.T) *ants.Pool {
|
|
|
|
pool, err := ants.NewPool(4)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:23:52 +00:00
|
|
|
type nextResult struct {
|
|
|
|
objs []objectcore.AddressWithType
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type predefinedIterator struct {
|
|
|
|
scenario []nextResult
|
|
|
|
finishCh chan struct{}
|
|
|
|
pos int
|
|
|
|
calls []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *predefinedIterator) Next(ctx context.Context, size uint32) ([]objectcore.AddressWithType, error) {
|
|
|
|
if it.pos == len(it.scenario) {
|
|
|
|
close(it.finishCh)
|
|
|
|
<-ctx.Done()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := it.scenario[it.pos]
|
|
|
|
it.pos += 1
|
|
|
|
it.calls = append(it.calls, "Next")
|
|
|
|
return res.objs, res.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *predefinedIterator) Rewind() {
|
|
|
|
it.calls = append(it.calls, "Rewind")
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:13:01 +00:00
|
|
|
// sliceKeySpaceIterator is a KeySpaceIterator backed by a slice.
|
|
|
|
type sliceKeySpaceIterator struct {
|
|
|
|
objs []objectcore.AddressWithType
|
|
|
|
cur int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *sliceKeySpaceIterator) Next(_ context.Context, size uint32) ([]objectcore.AddressWithType, error) {
|
|
|
|
if it.cur >= len(it.objs) {
|
|
|
|
return nil, engine.ErrEndOfListing
|
|
|
|
}
|
2024-04-10 11:16:18 +00:00
|
|
|
end := min(it.cur+int(size), len(it.objs))
|
2023-06-29 09:13:01 +00:00
|
|
|
ret := it.objs[it.cur:end]
|
|
|
|
it.cur = end
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:23:52 +00:00
|
|
|
func (it *sliceKeySpaceIterator) Rewind() {
|
|
|
|
it.cur = 0
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:27:24 +00:00
|
|
|
type containerSrc struct {
|
|
|
|
get func(id cid.ID) (*container.Container, error)
|
|
|
|
deletionInfo func(id cid.ID) (*container.DelInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f containerSrc) Get(id cid.ID) (*container.Container, error) { return f.get(id) }
|
2023-06-29 09:13:01 +00:00
|
|
|
|
2023-08-24 12:27:24 +00:00
|
|
|
func (f containerSrc) DeletionInfo(id cid.ID) (*container.DelInfo, error) { return f.deletionInfo(id) }
|
2023-06-29 09:13:01 +00:00
|
|
|
|
|
|
|
// placementBuilderFunc is a placement.Builder backed by a function
|
|
|
|
type placementBuilderFunc func(cid.ID, *oid.ID, netmap.PlacementPolicy) ([][]netmap.NodeInfo, error)
|
|
|
|
|
|
|
|
func (f placementBuilderFunc) BuildPlacement(c cid.ID, o *oid.ID, p netmap.PlacementPolicy) ([][]netmap.NodeInfo, error) {
|
|
|
|
return f(c, o, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// announcedKeysFunc is a netmap.AnnouncedKeys backed by a function.
|
|
|
|
type announcedKeysFunc func([]byte) bool
|
|
|
|
|
|
|
|
func (f announcedKeysFunc) IsLocalKey(k []byte) bool { return f(k) }
|
|
|
|
|
|
|
|
// replicatorFunc is a Replicator backed by a function.
|
|
|
|
type replicatorFunc func(context.Context, replicator.Task, replicator.TaskResult)
|
|
|
|
|
|
|
|
func (f replicatorFunc) HandleTask(ctx context.Context, task replicator.Task, res replicator.TaskResult) {
|
|
|
|
f(ctx, task, res)
|
|
|
|
}
|