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
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
2023-03-20 14:10:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
|
2023-03-07 13:38:26 +00:00
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2024-11-07 14:32:10 +00:00
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2022-03-10 17:58:58 +00:00
|
|
|
"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) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-03-10 17:58:58 +00:00
|
|
|
// 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
|
2022-05-31 20:11:52 +00:00
|
|
|
const lockerExpiresAfter = 13
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
2023-03-20 14:10:26 +00:00
|
|
|
tombObj := testutil.GenerateObjectWithCID(cnr)
|
2022-05-31 20:11:52 +00:00
|
|
|
tombForLockID := oidtest.ID()
|
|
|
|
tombObj.SetID(tombForLockID)
|
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
testEngine := testNewEngine(t).
|
|
|
|
setShardsNumAdditionalOpts(t, 2, func(id int) []shard.Option {
|
|
|
|
return []shard.Option{
|
|
|
|
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
|
|
|
|
pool, err := ants.NewPool(sz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}),
|
|
|
|
shard.WithTombstoneSource(tss{lockerExpiresAfter}),
|
|
|
|
}
|
2024-11-13 10:30:16 +00:00
|
|
|
}).
|
|
|
|
prepare(t)
|
2023-03-30 11:58:20 +00:00
|
|
|
e := testEngine.engine
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, e.Close(context.Background())) }()
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var a objectSDK.Attribute
|
2022-05-31 20:11:52 +00:00
|
|
|
a.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
a.SetValue(strconv.Itoa(lockerExpiresAfter))
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
lockerObj := testutil.GenerateObjectWithCID(cnr)
|
2022-05-31 20:11:52 +00:00
|
|
|
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.
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cnr)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
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
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, obj, false)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2.
|
2023-07-06 12:36:41 +00:00
|
|
|
var locker objectSDK.Lock
|
2022-05-31 20:11:52 +00:00
|
|
|
locker.WriteMembers([]oid.ID{id})
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK.WriteLock(lockerObj, locker)
|
2022-05-31 20:11:52 +00:00
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, lockerObj, false)
|
2022-05-31 20:11:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err = e.Lock(context.Background(), 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)
|
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
var objLockedErr *apistatus.ObjectLocked
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.ErrorAs(t, err, &objLockedErr)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// 4.
|
2023-07-06 12:36:41 +00:00
|
|
|
tombObj.SetType(objectSDK.TypeTombstone)
|
2022-03-10 17:58:58 +00:00
|
|
|
tombObj.SetID(tombForLockID)
|
2022-03-15 12:11:35 +00:00
|
|
|
tombObj.SetAttributes(a)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, tombObj, false)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
inhumePrm.WithTarget(tombForLockAddr, lockerAddr)
|
2022-05-31 20:11:52 +00:00
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
2022-06-06 14:44:37 +00:00
|
|
|
require.ErrorIs(t, err, meta.ErrLockObjectRemoval)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// 5.
|
2024-01-25 08:02:57 +00:00
|
|
|
e.HandleNewEpoch(context.Background(), lockerExpiresAfter+1)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
inhumePrm.WithTarget(tombAddr, objAddr)
|
2022-05-31 20:11:52 +00:00
|
|
|
|
2023-11-09 07:25:46 +00:00
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
|
|
|
return err == nil
|
|
|
|
}, 30*time.Second, time.Second)
|
2022-03-10 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockExpiration(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-03-10 17:58:58 +00:00
|
|
|
// 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
|
2022-09-26 06:30:41 +00:00
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
testEngine := testNewEngine(t).
|
|
|
|
setShardsNumAdditionalOpts(t, 2, func(id int) []shard.Option {
|
|
|
|
return []shard.Option{
|
|
|
|
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
|
|
|
|
pool, err := ants.NewPool(sz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}),
|
|
|
|
}
|
2024-11-13 10:30:16 +00:00
|
|
|
}).
|
|
|
|
prepare(t)
|
2023-03-30 11:58:20 +00:00
|
|
|
e := testEngine.engine
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, e.Close(context.Background())) }()
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
const lockerExpiresAfter = 13
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// 1.
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cnr)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, obj, false)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2.
|
2023-07-06 12:36:41 +00:00
|
|
|
var a objectSDK.Attribute
|
2022-03-10 17:58:58 +00:00
|
|
|
a.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
a.SetValue(strconv.Itoa(lockerExpiresAfter))
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
lock := testutil.GenerateObjectWithCID(cnr)
|
2023-07-06 12:36:41 +00:00
|
|
|
lock.SetType(objectSDK.TypeLock)
|
2022-03-15 12:11:35 +00:00
|
|
|
lock.SetAttributes(a)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, lock, false)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := obj.ID()
|
|
|
|
idLock, _ := lock.ID()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err = e.Lock(context.Background(), 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
|
2024-09-20 10:28:21 +00:00
|
|
|
tombAddr := oidtest.Address()
|
|
|
|
tombAddr.SetContainer(cnr)
|
|
|
|
inhumePrm.WithTarget(tombAddr, objectcore.AddressOf(obj))
|
2022-05-23 13:12:32 +00:00
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
var objLockedErr *apistatus.ObjectLocked
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.ErrorAs(t, err, &objLockedErr)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// 3.
|
2024-01-25 08:02:57 +00:00
|
|
|
e.HandleNewEpoch(context.Background(), lockerExpiresAfter+1)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// 4.
|
2024-09-20 10:28:21 +00:00
|
|
|
tombAddr = oidtest.Address()
|
|
|
|
tombAddr.SetContainer(cnr)
|
|
|
|
inhumePrm.WithTarget(tombAddr, objectcore.AddressOf(obj))
|
2022-05-31 20:11:52 +00:00
|
|
|
|
2023-11-09 07:25:46 +00:00
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
|
|
|
return err == nil
|
|
|
|
}, 30*time.Second, time.Second)
|
2022-03-10 17:58:58 +00:00
|
|
|
}
|
2022-06-06 14:14:59 +00:00
|
|
|
|
|
|
|
func TestLockForceRemoval(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-06-06 14:14:59 +00:00
|
|
|
// Tested scenario:
|
|
|
|
// 1. some object is stored
|
|
|
|
// 2. lock object for it is stored, and the object is locked
|
|
|
|
// 3. try to remove lock object and get error
|
|
|
|
// 4. force lock object removal
|
|
|
|
// 5. the object is not locked anymore
|
|
|
|
var e *StorageEngine
|
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
e = testNewEngine(t).
|
|
|
|
setShardsNumAdditionalOpts(t, 2, func(id int) []shard.Option {
|
|
|
|
return []shard.Option{
|
|
|
|
shard.WithGCWorkerPoolInitializer(func(sz int) util.WorkerPool {
|
|
|
|
pool, err := ants.NewPool(sz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}),
|
|
|
|
shard.WithDeletedLockCallback(e.processDeletedLocks),
|
|
|
|
}
|
2024-11-13 10:30:16 +00:00
|
|
|
}).
|
|
|
|
prepare(t).engine
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, e.Close(context.Background())) }()
|
2022-06-06 14:14:59 +00:00
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// 1.
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cnr)
|
2022-06-06 14:14:59 +00:00
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, obj, false)
|
2022-06-06 14:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2.
|
2023-03-20 14:10:26 +00:00
|
|
|
lock := testutil.GenerateObjectWithCID(cnr)
|
2023-07-06 12:36:41 +00:00
|
|
|
lock.SetType(objectSDK.TypeLock)
|
2022-06-06 14:14:59 +00:00
|
|
|
|
2024-10-01 12:27:06 +00:00
|
|
|
err = Put(context.Background(), e, lock, false)
|
2022-06-06 14:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id, _ := obj.ID()
|
|
|
|
idLock, _ := lock.ID()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err = e.Lock(context.Background(), cnr, idLock, []oid.ID{id})
|
2022-06-06 14:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 3.
|
|
|
|
var inhumePrm InhumePrm
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(obj))
|
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
var objLockedErr *apistatus.ObjectLocked
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.ErrorAs(t, err, &objLockedErr)
|
2022-06-06 14:14:59 +00:00
|
|
|
|
2023-03-21 10:38:44 +00:00
|
|
|
inhumePrm.WithTarget(oidtest.Address(), objectcore.AddressOf(obj))
|
2022-06-06 14:14:59 +00:00
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.ErrorAs(t, err, &objLockedErr)
|
2022-06-06 14:14:59 +00:00
|
|
|
|
|
|
|
// 4.
|
|
|
|
var deletePrm DeletePrm
|
2022-09-14 14:18:11 +00:00
|
|
|
deletePrm.WithAddress(objectcore.AddressOf(lock))
|
2022-06-06 14:14:59 +00:00
|
|
|
deletePrm.WithForceRemoval()
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Delete(context.Background(), deletePrm)
|
2022-06-06 14:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 5.
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(obj))
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = e.Inhume(context.Background(), inhumePrm)
|
2022-06-06 14:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2024-12-02 13:16:50 +00:00
|
|
|
|
|
|
|
func TestLockExpiredRegularObject(t *testing.T) {
|
|
|
|
const currEpoch = 42
|
|
|
|
const objectExpiresAfter = currEpoch - 1
|
|
|
|
|
|
|
|
engine := testNewEngine(t).setShardsNumAdditionalOpts(t, 1, func(_ int) []shard.Option {
|
|
|
|
return []shard.Option{
|
|
|
|
shard.WithDisabledGC(),
|
|
|
|
shard.WithMetaBaseOptions(append(
|
|
|
|
testGetDefaultMetabaseOptions(t),
|
|
|
|
meta.WithEpochState(epochState{currEpoch}),
|
|
|
|
)...),
|
|
|
|
}
|
|
|
|
}).prepare(t).engine
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
|
|
|
|
object := testutil.GenerateObjectWithCID(cnr)
|
|
|
|
testutil.AddAttribute(object, objectV2.SysAttributeExpEpoch, strconv.Itoa(objectExpiresAfter))
|
|
|
|
|
|
|
|
address := objectcore.AddressOf(object)
|
|
|
|
|
|
|
|
var putPrm PutPrm
|
|
|
|
putPrm.Object = object
|
|
|
|
require.NoError(t, engine.Put(context.Background(), putPrm))
|
|
|
|
|
|
|
|
var getPrm GetPrm
|
|
|
|
var errNotFound *apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
getPrm.WithAddress(address)
|
|
|
|
_, err := engine.Get(context.Background(), getPrm)
|
|
|
|
require.ErrorAs(t, err, &errNotFound)
|
|
|
|
|
|
|
|
t.Run("lock expired regular object", func(t *testing.T) {
|
|
|
|
engine.Lock(context.Background(),
|
|
|
|
address.Container(),
|
|
|
|
oidtest.ID(),
|
|
|
|
[]oid.ID{address.Object()},
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := engine.IsLocked(context.Background(), objectcore.AddressOf(object))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, res)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get expired and locked regular object", func(t *testing.T) {
|
|
|
|
getPrm.WithAddress(objectcore.AddressOf(object))
|
|
|
|
|
|
|
|
res, err := engine.Get(context.Background(), getPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, res.Object(), object)
|
|
|
|
})
|
|
|
|
}
|