2022-06-06 14:00:22 +00:00
|
|
|
package shard_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2022-07-11 12:34:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/blobovniczatree"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2022-06-06 14:00:22 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2022-09-28 07:41:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2022-06-06 14:00:22 +00:00
|
|
|
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-11-12 11:59:58 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-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) {
|
|
|
|
var sh *shard.Shard
|
|
|
|
|
|
|
|
rootPath := t.TempDir()
|
|
|
|
opts := []shard.Option{
|
2022-09-28 07:41:01 +00:00
|
|
|
shard.WithLogger(&logger.Logger{Logger: zap.NewNop()}),
|
2022-06-06 14:00:22 +00:00
|
|
|
shard.WithBlobStorOptions(
|
2022-07-11 12:34:17 +00:00
|
|
|
blobstor.WithStorages([]blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: blobovniczatree.NewBlobovniczaTree(
|
|
|
|
blobovniczatree.WithRootPath(filepath.Join(rootPath, "blob", "blobovnicza")),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowDepth(2),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowWidth(2)),
|
|
|
|
Policy: func(_ *object.Object, data []byte) bool {
|
|
|
|
return len(data) <= 1<<20
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(filepath.Join(rootPath, "blob"))),
|
|
|
|
},
|
|
|
|
}),
|
2022-06-06 14:00:22 +00:00
|
|
|
),
|
|
|
|
shard.WithMetaBaseOptions(
|
|
|
|
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
|
|
|
),
|
2022-06-06 14:01:21 +00:00
|
|
|
shard.WithDeletedLockCallback(func(_ context.Context, addresses []oid.Address) {
|
2022-06-06 14:00:22 +00:00
|
|
|
sh.HandleDeletedLocks(addresses)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
sh = shard.New(opts...)
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
releaseShard(sh, t)
|
|
|
|
})
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
obj := generateObjectWithCID(t, cnr)
|
|
|
|
objID, _ := obj.ID()
|
|
|
|
|
|
|
|
lock := generateObjectWithCID(t, cnr)
|
|
|
|
lock.SetType(object.TypeLock)
|
|
|
|
lockID, _ := lock.ID()
|
|
|
|
|
|
|
|
// put the object
|
|
|
|
|
|
|
|
var putPrm shard.PutPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2022-06-06 14:00:22 +00:00
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// lock the object
|
|
|
|
|
|
|
|
err = sh.Lock(cnr, lockID, []oid.ID{objID})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(lock)
|
2022-06-06 14:00:22 +00:00
|
|
|
_, err = sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("inhuming locked objects", func(t *testing.T) {
|
|
|
|
ts := generateObjectWithCID(t, cnr)
|
|
|
|
|
|
|
|
var inhumePrm shard.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
|
|
|
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectLocked))
|
|
|
|
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(obj))
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectLocked))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inhuming lock objects", func(t *testing.T) {
|
|
|
|
ts := generateObjectWithCID(t, cnr)
|
|
|
|
|
|
|
|
var inhumePrm shard.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
|
|
|
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(lock))
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("force objects inhuming", func(t *testing.T) {
|
|
|
|
var inhumePrm shard.InhumePrm
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(lock))
|
|
|
|
inhumePrm.ForceRemoval()
|
|
|
|
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// it should be possible to remove
|
|
|
|
// lock object now
|
|
|
|
|
|
|
|
inhumePrm = shard.InhumePrm{}
|
|
|
|
inhumePrm.MarkAsGarbage(objectcore.AddressOf(obj))
|
|
|
|
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check that object has been removed
|
|
|
|
|
|
|
|
var getPrm shard.GetPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(objectcore.AddressOf(obj))
|
2022-06-06 14:00:22 +00:00
|
|
|
|
|
|
|
_, err = sh.Get(getPrm)
|
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectNotFound))
|
|
|
|
})
|
2022-11-12 11:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShard_IsLocked(t *testing.T) {
|
|
|
|
sh := newShard(t, false)
|
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
obj := generateObjectWithCID(t, cnr)
|
|
|
|
cnrID, _ := obj.ContainerID()
|
|
|
|
objID, _ := obj.ID()
|
|
|
|
|
|
|
|
lockID := oidtest.ID()
|
|
|
|
|
|
|
|
// put the object
|
|
|
|
|
|
|
|
var putPrm shard.PutPrm
|
|
|
|
putPrm.SetObject(obj)
|
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// not locked object is not locked
|
|
|
|
|
|
|
|
locked, err := sh.IsLocked(objectcore.AddressOf(obj))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.False(t, locked)
|
|
|
|
|
|
|
|
// locked object is locked
|
|
|
|
|
|
|
|
require.NoError(t, sh.Lock(cnrID, lockID, []oid.ID{objID}))
|
|
|
|
|
|
|
|
locked, err = sh.IsLocked(objectcore.AddressOf(obj))
|
|
|
|
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
|
|
|
}
|