2023-08-31 12:47:06 +00:00
|
|
|
package shard
|
2022-06-06 14:00:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/blobovniczatree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
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/util/logger"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
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-06-06 14:00:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShard_Lock(t *testing.T) {
|
2023-05-05 11:08:10 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var sh *Shard
|
2022-06-06 14:00:22 +00:00
|
|
|
|
|
|
|
rootPath := t.TempDir()
|
2023-08-31 12:47:06 +00:00
|
|
|
opts := []Option{
|
|
|
|
WithID(NewIDFromBytes([]byte{})),
|
2024-10-21 07:22:54 +00:00
|
|
|
WithLogger(logger.NewLoggerWrapper(zap.NewNop())),
|
2023-08-31 12:47:06 +00:00
|
|
|
WithBlobStorOptions(
|
2022-07-11 12:34:17 +00:00
|
|
|
blobstor.WithStorages([]blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: blobovniczatree.NewBlobovniczaTree(
|
2024-03-01 11:43:26 +00:00
|
|
|
context.Background(),
|
2022-07-11 12:34:17 +00:00
|
|
|
blobovniczatree.WithRootPath(filepath.Join(rootPath, "blob", "blobovnicza")),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowDepth(2),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowWidth(2)),
|
2023-07-06 12:36:41 +00:00
|
|
|
Policy: func(_ *objectSDK.Object, data []byte) bool {
|
2022-07-11 12:34:17 +00:00
|
|
|
return len(data) <= 1<<20
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(filepath.Join(rootPath, "blob"))),
|
|
|
|
},
|
|
|
|
}),
|
2022-06-06 14:00:22 +00:00
|
|
|
),
|
2023-08-31 12:47:06 +00:00
|
|
|
WithMetaBaseOptions(
|
2022-06-06 14:00:22 +00:00
|
|
|
meta.WithPath(filepath.Join(rootPath, "meta")),
|
2022-07-27 18:34:25 +00:00
|
|
|
meta.WithEpochState(epochState{}),
|
2022-06-06 14:00:22 +00:00
|
|
|
),
|
2023-08-31 12:47:06 +00:00
|
|
|
WithDeletedLockCallback(func(_ context.Context, addresses []oid.Address) {
|
2022-06-06 14:00:22 +00:00
|
|
|
sh.HandleDeletedLocks(addresses)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
sh = New(opts...)
|
2023-08-31 16:26:47 +00:00
|
|
|
require.NoError(t, sh.Open(context.Background()))
|
2023-03-23 14:59:14 +00:00
|
|
|
require.NoError(t, sh.Init(context.Background()))
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, sh.Close()) }()
|
2022-06-06 14:00:22 +00:00
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cnr)
|
2022-06-06 14:00:22 +00:00
|
|
|
objID, _ := obj.ID()
|
|
|
|
|
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:00:22 +00:00
|
|
|
lockID, _ := lock.ID()
|
|
|
|
|
|
|
|
// put the object
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var putPrm PutPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2022-06-06 14:00:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// lock the object
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err = sh.Lock(context.Background(), cnr, lockID, []oid.ID{objID})
|
2022-06-06 14:00:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(lock)
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err = sh.Put(context.Background(), putPrm)
|
2022-06-06 14:00:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("inhuming locked objects", func(t *testing.T) {
|
2023-03-20 14:10:26 +00:00
|
|
|
ts := testutil.GenerateObjectWithCID(cnr)
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var inhumePrm InhumePrm
|
2022-07-13 12:43:04 +00:00
|
|
|
inhumePrm.SetTarget(objectcore.AddressOf(ts), objectcore.AddressOf(obj))
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
var objLockedErr *apistatus.ObjectLocked
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = sh.Inhume(context.Background(), inhumePrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.ErrorAs(t, err, &objLockedErr)
|
2022-06-06 14:00:22 +00:00
|
|
|
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(obj))
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = sh.Inhume(context.Background(), inhumePrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.ErrorAs(t, err, &objLockedErr)
|
2022-06-06 14:00:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inhuming lock objects", func(t *testing.T) {
|
2023-03-20 14:10:26 +00:00
|
|
|
ts := testutil.GenerateObjectWithCID(cnr)
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var inhumePrm InhumePrm
|
2022-07-13 12:43:04 +00:00
|
|
|
inhumePrm.SetTarget(objectcore.AddressOf(ts), objectcore.AddressOf(lock))
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = sh.Inhume(context.Background(), inhumePrm)
|
2022-06-06 14:00:22 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(lock))
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = sh.Inhume(context.Background(), inhumePrm)
|
2022-06-06 14:00:22 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("force objects inhuming", func(t *testing.T) {
|
2023-08-31 12:47:06 +00:00
|
|
|
var inhumePrm InhumePrm
|
2022-06-06 14:00:22 +00:00
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(lock))
|
|
|
|
inhumePrm.ForceRemoval()
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = sh.Inhume(context.Background(), inhumePrm)
|
2022-06-06 14:00:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// it should be possible to remove
|
|
|
|
// lock object now
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
inhumePrm = InhumePrm{}
|
2022-06-06 14:00:22 +00:00
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(obj))
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err = sh.Inhume(context.Background(), inhumePrm)
|
2022-06-06 14:00:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check that object has been removed
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var getPrm GetPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(objectcore.AddressOf(obj))
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
_, err = sh.Get(context.Background(), getPrm)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.True(t, client.IsErrObjectNotFound(err))
|
2022-06-06 14:00:22 +00:00
|
|
|
})
|
2022-11-12 11:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShard_IsLocked(t *testing.T) {
|
|
|
|
sh := newShard(t, false)
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, sh.Close()) }()
|
2022-11-12 11:59:58 +00:00
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cnr)
|
2022-11-12 11:59:58 +00:00
|
|
|
cnrID, _ := obj.ContainerID()
|
|
|
|
objID, _ := obj.ID()
|
|
|
|
|
|
|
|
lockID := oidtest.ID()
|
|
|
|
|
|
|
|
// put the object
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var putPrm PutPrm
|
2022-11-12 11:59:58 +00:00
|
|
|
putPrm.SetObject(obj)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2022-11-12 11:59:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// not locked object is not locked
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
locked, err := sh.IsLocked(context.Background(), objectcore.AddressOf(obj))
|
2022-11-12 11:59:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.False(t, locked)
|
|
|
|
|
|
|
|
// locked object is locked
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
require.NoError(t, sh.Lock(context.Background(), cnrID, lockID, []oid.ID{objID}))
|
2022-11-12 11:59:58 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
locked, err = sh.IsLocked(context.Background(), objectcore.AddressOf(obj))
|
2022-11-12 11:59:58 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-06 14:00:22 +00:00
|
|
|
|
2022-11-12 11:59:58 +00:00
|
|
|
require.True(t, locked)
|
2022-06-06 14:00:22 +00:00
|
|
|
}
|