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"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mObjs := make(map[string]objAddr)
|
|
|
|
|
|
|
|
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-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(obj)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
mObjs[addr.String()] = objAddr{
|
|
|
|
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-01-26 12:11:13 +00:00
|
|
|
tombMembers := make([]*addressSDK.Address, 0, len(tombstone.Members()))
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
for _, member := range tombstone.Members() {
|
2022-01-26 12:11:13 +00:00
|
|
|
a := addressSDK.NewAddress()
|
2021-09-13 13:38:58 +00:00
|
|
|
a.SetObjectID(member)
|
|
|
|
a.SetContainerID(tombObj.ContainerID())
|
|
|
|
|
|
|
|
tombMembers = append(tombMembers, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var putPrm PutPrm
|
|
|
|
|
|
|
|
for _, v := range mObjs {
|
|
|
|
_, err := sh.Put(putPrm.WithObject(v.obj))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = sh.Put(putPrm.WithObject(tombObj))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
_, err = sh.Inhume(new(InhumePrm).WithTarget(object.AddressOf(tombObj), tombMembers...))
|
2021-09-13 13:38:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var headPrm HeadPrm
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
checkObj := func(addr *addressSDK.Address, expObj *objectSDK.Object) {
|
2021-09-13 13:38:58 +00:00
|
|
|
res, err := sh.Head(headPrm.WithAddress(addr))
|
|
|
|
|
|
|
|
if expObj == nil {
|
|
|
|
require.ErrorIs(t, err, object.ErrNotFound)
|
|
|
|
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 {
|
|
|
|
_, err := sh.Head(headPrm.WithAddress(member))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
require.ErrorIs(t, err, object.ErrAlreadyRemoved)
|
|
|
|
} else {
|
|
|
|
require.ErrorIs(t, err, object.ErrNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|