2022-03-10 17:58:58 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-05-31 20:11:52 +00:00
|
|
|
"context"
|
2022-03-10 17:58:58 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-05-31 20:11:52 +00:00
|
|
|
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2022-03-10 17:58:58 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-31 20:11:52 +00:00
|
|
|
type tss struct {
|
|
|
|
expEpoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t tss) IsTombstoneAvailable(ctx context.Context, _ oid.Address, epoch uint64) bool {
|
|
|
|
return t.expEpoch >= epoch
|
|
|
|
}
|
|
|
|
|
2022-03-10 17:58:58 +00:00
|
|
|
func TestLockUserScenario(t *testing.T) {
|
|
|
|
// Tested user actions:
|
|
|
|
// 1. stores some object
|
|
|
|
// 2. locks the object
|
|
|
|
// 3. tries to inhume the object with tombstone and expects failure
|
2022-05-31 20:11:52 +00:00
|
|
|
// 4. saves tombstone for LOCK-object and receives error
|
|
|
|
// 5. waits for an epoch after the lock expiration one
|
2022-03-10 17:58:58 +00:00
|
|
|
// 6. tries to inhume the object and expects success
|
|
|
|
chEvents := make([]chan shard.Event, 2)
|
|
|
|
|
|
|
|
for i := range chEvents {
|
|
|
|
chEvents[i] = make(chan shard.Event, 1)
|
|
|
|
}
|
|
|
|
|
2022-05-31 20:11:52 +00:00
|
|
|
const lockerExpiresAfter = 13
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
tombObj := generateObjectWithCID(t, cnr)
|
|
|
|
tombForLockID := oidtest.ID()
|
|
|
|
tombObj.SetID(tombForLockID)
|
|
|
|
|
2022-03-10 17:58:58 +00:00
|
|
|
e := testEngineFromShardOpts(t, 2, func(i int) []shard.Option {
|
|
|
|
return []shard.Option{
|
2022-04-21 13:37:42 +00:00
|
|
|
shard.WithGCEventChannel(chEvents[i]),
|
2022-03-10 17:58:58 +00:00
|
|
|
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
|
|
|
|
pool, err := ants.NewPool(sz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}),
|
2022-05-31 20:11:52 +00:00
|
|
|
shard.WithTombstoneSource(tss{lockerExpiresAfter}),
|
2022-03-10 17:58:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = e.Close()
|
|
|
|
_ = os.RemoveAll(t.Name())
|
|
|
|
})
|
|
|
|
|
|
|
|
lockerID := oidtest.ID()
|
|
|
|
tombID := oidtest.ID()
|
|
|
|
var err error
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var objAddr oid.Address
|
|
|
|
objAddr.SetContainer(cnr)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var tombAddr oid.Address
|
|
|
|
tombAddr.SetContainer(cnr)
|
|
|
|
tombAddr.SetObject(tombID)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var lockerAddr oid.Address
|
|
|
|
lockerAddr.SetContainer(cnr)
|
|
|
|
lockerAddr.SetObject(lockerID)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2022-05-31 20:11:52 +00:00
|
|
|
var a object.Attribute
|
|
|
|
a.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
a.SetValue(strconv.Itoa(lockerExpiresAfter))
|
|
|
|
|
|
|
|
lockerObj := generateObjectWithCID(t, cnr)
|
|
|
|
lockerObj.SetID(lockerID)
|
|
|
|
lockerObj.SetAttributes(a)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var tombForLockAddr oid.Address
|
|
|
|
tombForLockAddr.SetContainer(cnr)
|
|
|
|
tombForLockAddr.SetObject(tombForLockID)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// 1.
|
|
|
|
obj := generateObjectWithCID(t, cnr)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := obj.ID()
|
2022-05-31 17:00:41 +00:00
|
|
|
objAddr.SetObject(id)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
err = Put(e, obj)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2.
|
2022-05-31 20:11:52 +00:00
|
|
|
var locker object.Lock
|
|
|
|
locker.WriteMembers([]oid.ID{id})
|
|
|
|
object.WriteLock(lockerObj, locker)
|
|
|
|
|
|
|
|
err = Put(e, lockerObj)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err = e.Lock(cnr, lockerID, []oid.ID{id})
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 3.
|
2022-05-23 13:12:32 +00:00
|
|
|
var inhumePrm InhumePrm
|
|
|
|
inhumePrm.WithTarget(tombAddr, objAddr)
|
|
|
|
|
|
|
|
_, err = e.Inhume(inhumePrm)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectLocked))
|
|
|
|
|
|
|
|
// 4.
|
|
|
|
tombObj.SetType(object.TypeTombstone)
|
|
|
|
tombObj.SetID(tombForLockID)
|
2022-03-15 12:11:35 +00:00
|
|
|
tombObj.SetAttributes(a)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
err = Put(e, tombObj)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
inhumePrm.WithTarget(tombForLockAddr, lockerAddr)
|
2022-05-31 20:11:52 +00:00
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Inhume(inhumePrm)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err, new(apistatus.ObjectLocked))
|
|
|
|
|
|
|
|
// 5.
|
|
|
|
for i := range chEvents {
|
2022-05-31 20:11:52 +00:00
|
|
|
chEvents[i] <- shard.EventNewEpoch(lockerExpiresAfter + 1)
|
2022-03-10 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delay for GC
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
inhumePrm.WithTarget(tombAddr, objAddr)
|
2022-05-31 20:11:52 +00:00
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Inhume(inhumePrm)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockExpiration(t *testing.T) {
|
|
|
|
// Tested scenario:
|
|
|
|
// 1. some object is stored
|
|
|
|
// 2. lock object for it is stored, and the object is locked
|
|
|
|
// 3. lock expiration epoch is coming
|
|
|
|
// 4. after some delay the object is not locked anymore
|
|
|
|
chEvents := make([]chan shard.Event, 2)
|
|
|
|
|
|
|
|
for i := range chEvents {
|
|
|
|
chEvents[i] = make(chan shard.Event, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := testEngineFromShardOpts(t, 2, func(i int) []shard.Option {
|
|
|
|
return []shard.Option{
|
2022-04-21 13:37:42 +00:00
|
|
|
shard.WithGCEventChannel(chEvents[i]),
|
2022-03-10 17:58:58 +00:00
|
|
|
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
|
|
|
|
pool, err := ants.NewPool(sz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = e.Close()
|
|
|
|
_ = os.RemoveAll(t.Name())
|
|
|
|
})
|
|
|
|
|
|
|
|
const lockerExpiresAfter = 13
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// 1.
|
|
|
|
obj := generateObjectWithCID(t, cnr)
|
|
|
|
|
|
|
|
err = Put(e, obj)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2.
|
|
|
|
var a object.Attribute
|
|
|
|
a.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
a.SetValue(strconv.Itoa(lockerExpiresAfter))
|
|
|
|
|
|
|
|
lock := generateObjectWithCID(t, cnr)
|
|
|
|
lock.SetType(object.TypeLock)
|
2022-03-15 12:11:35 +00:00
|
|
|
lock.SetAttributes(a)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
err = Put(e, lock)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := obj.ID()
|
|
|
|
idLock, _ := lock.ID()
|
|
|
|
|
|
|
|
err = e.Lock(cnr, idLock, []oid.ID{id})
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
var inhumePrm InhumePrm
|
2022-05-31 20:11:52 +00:00
|
|
|
inhumePrm.WithTarget(objecttest.Address(), objectcore.AddressOf(obj))
|
2022-05-23 13:12:32 +00:00
|
|
|
|
|
|
|
_, err = e.Inhume(inhumePrm)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectLocked))
|
|
|
|
|
|
|
|
// 3.
|
|
|
|
for i := range chEvents {
|
|
|
|
chEvents[i] <- shard.EventNewEpoch(lockerExpiresAfter + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delay for GC processing. It can't be estimated, but making it bigger
|
|
|
|
// will slow down test
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// 4.
|
2022-05-31 20:11:52 +00:00
|
|
|
inhumePrm.WithTarget(objecttest.Address(), objectcore.AddressOf(obj))
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Inhume(inhumePrm)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|