2021-09-13 13:38:58 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-09-13 13:38:58 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-06-20 12:28:20 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-06-20 12:28:20 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/test"
|
2021-09-13 13:38:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefillMetabase(t *testing.T) {
|
|
|
|
p := t.Name()
|
|
|
|
|
|
|
|
defer os.RemoveAll(p)
|
|
|
|
|
|
|
|
blobOpts := []blobstor.Option{
|
2022-02-02 13:28:08 +00:00
|
|
|
blobstor.WithRootPath(filepath.Join(p, "blob")),
|
2021-09-13 13:38:58 +00:00
|
|
|
blobstor.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
sh := New(
|
|
|
|
WithBlobStorOptions(blobOpts...),
|
|
|
|
WithMetaBaseOptions(
|
2022-02-02 13:28:08 +00:00
|
|
|
meta.WithPath(filepath.Join(p, "meta")),
|
2021-09-13 13:38:58 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// open Blobstor
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
|
|
|
|
// initialize Blobstor
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
|
|
|
|
const objNum = 5
|
|
|
|
|
|
|
|
type objAddr struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mObjs := make(map[string]objAddr)
|
2022-06-20 12:28:20 +00:00
|
|
|
locked := make([]oid.ID, 1, 2)
|
|
|
|
locked[0] = oidtest.ID()
|
|
|
|
cnrLocked := cidtest.ID()
|
2021-09-13 13:38:58 +00:00
|
|
|
for i := uint64(0); i < objNum; i++ {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := objecttest.Object()
|
|
|
|
obj.SetType(objectSDK.TypeRegular)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2022-06-20 12:28:20 +00:00
|
|
|
if len(locked) < 2 {
|
|
|
|
obj.SetContainerID(cnrLocked)
|
|
|
|
id, _ := obj.ID()
|
|
|
|
locked = append(locked, id)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(obj)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
mObjs[addr.EncodeToString()] = objAddr{
|
2021-09-13 13:38:58 +00:00
|
|
|
obj: obj,
|
|
|
|
addr: addr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
tombObj := objecttest.Object()
|
|
|
|
tombObj.SetType(objectSDK.TypeTombstone)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
tombstone := objecttest.Tombstone()
|
|
|
|
|
|
|
|
tombData, err := tombstone.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
tombObj.SetPayload(tombData)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
tombMembers := make([]oid.Address, 0, len(tombstone.Members()))
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
members := tombstone.Members()
|
|
|
|
for i := range tombstone.Members() {
|
2022-05-31 17:00:41 +00:00
|
|
|
var a oid.Address
|
|
|
|
a.SetObject(members[i])
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, _ := tombObj.ContainerID()
|
2022-05-31 17:00:41 +00:00
|
|
|
a.SetContainer(cnr)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
tombMembers = append(tombMembers, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var putPrm PutPrm
|
|
|
|
|
|
|
|
for _, v := range mObjs {
|
2022-05-20 18:08:59 +00:00
|
|
|
putPrm.WithObject(v.obj)
|
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
putPrm.WithObject(tombObj)
|
|
|
|
|
|
|
|
_, err = sh.Put(putPrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-20 12:28:20 +00:00
|
|
|
// LOCK object handling
|
|
|
|
var lock objectSDK.Lock
|
|
|
|
lock.WriteMembers(locked)
|
|
|
|
|
|
|
|
lockObj := objecttest.Object()
|
|
|
|
lockObj.SetContainerID(cnrLocked)
|
|
|
|
objectSDK.WriteLock(lockObj, lock)
|
|
|
|
|
|
|
|
putPrm.WithObject(lockObj)
|
|
|
|
_, err = sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lockID, _ := lockObj.ID()
|
|
|
|
require.NoError(t, sh.Lock(cnrLocked, lockID, locked))
|
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var inhumePrm InhumePrm
|
|
|
|
inhumePrm.WithTarget(object.AddressOf(tombObj), tombMembers...)
|
|
|
|
|
|
|
|
_, err = sh.Inhume(inhumePrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var headPrm HeadPrm
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
checkObj := func(addr oid.Address, expObj *objectSDK.Object) {
|
2022-05-20 18:08:59 +00:00
|
|
|
headPrm.WithAddress(addr)
|
|
|
|
|
|
|
|
res, err := sh.Head(headPrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
if expObj == nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectNotFound))
|
2021-09-13 13:38:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, expObj.CutPayload(), res.Object())
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkAllObjs := func(exists bool) {
|
|
|
|
for _, v := range mObjs {
|
|
|
|
if exists {
|
|
|
|
checkObj(v.addr, v.obj)
|
|
|
|
} else {
|
|
|
|
checkObj(v.addr, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkTombMembers := func(exists bool) {
|
|
|
|
for _, member := range tombMembers {
|
2022-05-20 18:08:59 +00:00
|
|
|
headPrm.WithAddress(member)
|
|
|
|
|
|
|
|
_, err := sh.Head(headPrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
if exists {
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectAlreadyRemoved))
|
2021-09-13 13:38:58 +00:00
|
|
|
} else {
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectNotFound))
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 12:28:20 +00:00
|
|
|
checkLocked := func(t *testing.T, cnr cid.ID, locked []oid.ID) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
|
|
|
|
for i := range locked {
|
|
|
|
addr.SetObject(locked[i])
|
|
|
|
|
|
|
|
var prm InhumePrm
|
|
|
|
prm.MarkAsGarbage(addr)
|
|
|
|
|
|
|
|
_, err := sh.Inhume(prm)
|
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectLocked),
|
|
|
|
"object %s should be locked", locked[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:38:58 +00:00
|
|
|
checkAllObjs(true)
|
2022-03-03 14:19:05 +00:00
|
|
|
checkObj(object.AddressOf(tombObj), tombObj)
|
2021-09-13 13:38:58 +00:00
|
|
|
checkTombMembers(true)
|
2022-06-20 12:28:20 +00:00
|
|
|
checkLocked(t, cnrLocked, locked)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
err = sh.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sh = New(
|
|
|
|
WithBlobStorOptions(blobOpts...),
|
|
|
|
WithMetaBaseOptions(
|
2022-02-02 13:28:08 +00:00
|
|
|
meta.WithPath(filepath.Join(p, "meta_restored")),
|
2021-09-13 13:38:58 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// open Blobstor
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
|
|
|
|
// initialize Blobstor
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
|
|
|
|
defer sh.Close()
|
|
|
|
|
|
|
|
checkAllObjs(false)
|
2022-03-03 14:19:05 +00:00
|
|
|
checkObj(object.AddressOf(tombObj), nil)
|
2021-09-13 13:38:58 +00:00
|
|
|
checkTombMembers(false)
|
|
|
|
|
|
|
|
err = sh.refillMetabase()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkAllObjs(true)
|
2022-03-03 14:19:05 +00:00
|
|
|
checkObj(object.AddressOf(tombObj), tombObj)
|
2021-09-13 13:38:58 +00:00
|
|
|
checkTombMembers(true)
|
2022-06-20 12:28:20 +00:00
|
|
|
checkLocked(t, cnrLocked, locked)
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|