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"
|
2022-06-21 07:52:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2021-09-13 13:38:58 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-06-09 08:09:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/pilorama"
|
2022-06-27 08:06:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
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"
|
2022-06-27 08:06:01 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2021-09-13 13:38:58 +00:00
|
|
|
)
|
|
|
|
|
2022-06-27 08:06:01 +00:00
|
|
|
func TestShardOpen(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
metaPath := filepath.Join(dir, "meta")
|
|
|
|
|
|
|
|
newShard := func() *Shard {
|
|
|
|
return New(
|
|
|
|
WithLogger(zaptest.NewLogger(t)),
|
|
|
|
WithBlobStorOptions(
|
|
|
|
blobstor.WithRootPath(filepath.Join(dir, "blob")),
|
|
|
|
blobstor.WithShallowDepth(1),
|
|
|
|
blobstor.WithSmallSizeLimit(1),
|
|
|
|
blobstor.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(1)),
|
|
|
|
WithMetaBaseOptions(meta.WithPath(metaPath)),
|
|
|
|
WithPiloramaOptions(
|
|
|
|
pilorama.WithPath(filepath.Join(dir, "pilorama"))),
|
|
|
|
WithWriteCache(true),
|
|
|
|
WithWriteCacheOptions(
|
|
|
|
writecache.WithPath(filepath.Join(dir, "wc"))))
|
|
|
|
}
|
|
|
|
|
|
|
|
sh := newShard()
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
require.Equal(t, mode.ReadWrite, sh.GetMode())
|
|
|
|
require.NoError(t, sh.Close())
|
|
|
|
|
|
|
|
// Metabase can be opened in read-only => start in ReadOnly mode.
|
|
|
|
require.NoError(t, os.Chmod(metaPath, 0444))
|
|
|
|
sh = newShard()
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
require.Equal(t, mode.ReadOnly, sh.GetMode())
|
|
|
|
require.Error(t, sh.SetMode(mode.ReadWrite))
|
|
|
|
require.Equal(t, mode.ReadOnly, sh.GetMode())
|
|
|
|
require.NoError(t, sh.Close())
|
|
|
|
|
|
|
|
// Metabase is corrupted => start in DegradedReadOnly mode.
|
|
|
|
require.NoError(t, os.Chmod(metaPath, 0000))
|
|
|
|
sh = newShard()
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
require.Equal(t, mode.DegradedReadOnly, sh.GetMode())
|
|
|
|
require.NoError(t, sh.Close())
|
|
|
|
}
|
|
|
|
|
2022-06-21 07:52:03 +00:00
|
|
|
func TestRefillMetabaseCorrupted(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
blobOpts := []blobstor.Option{
|
|
|
|
blobstor.WithRootPath(filepath.Join(dir, "blob")),
|
|
|
|
blobstor.WithShallowDepth(1),
|
|
|
|
blobstor.WithSmallSizeLimit(1),
|
|
|
|
blobstor.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(1)}
|
|
|
|
|
|
|
|
sh := New(
|
|
|
|
WithBlobStorOptions(blobOpts...),
|
2022-06-09 08:09:18 +00:00
|
|
|
WithPiloramaOptions(pilorama.WithPath(filepath.Join(dir, "pilorama"))),
|
2022-06-21 07:52:03 +00:00
|
|
|
WithMetaBaseOptions(meta.WithPath(filepath.Join(dir, "meta"))))
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
|
|
|
|
obj := objecttest.Object()
|
|
|
|
obj.SetType(objectSDK.TypeRegular)
|
|
|
|
obj.SetPayload([]byte{0, 1, 2, 3, 4, 5})
|
|
|
|
|
|
|
|
var putPrm PutPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2022-06-21 07:52:03 +00:00
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, sh.Close())
|
|
|
|
|
|
|
|
addr := object.AddressOf(obj)
|
|
|
|
fs := fstree.FSTree{
|
|
|
|
DirNameLen: 2,
|
|
|
|
Depth: 1,
|
|
|
|
Info: sh.blobStor.DumpInfo(),
|
|
|
|
}
|
|
|
|
require.NoError(t, fs.Put(addr, []byte("not an object")))
|
|
|
|
|
|
|
|
sh = New(
|
|
|
|
WithBlobStorOptions(blobOpts...),
|
2022-06-09 08:09:18 +00:00
|
|
|
WithPiloramaOptions(pilorama.WithPath(filepath.Join(dir, "pilorama"))),
|
2022-06-21 07:52:03 +00:00
|
|
|
WithMetaBaseOptions(meta.WithPath(filepath.Join(dir, "meta_new"))),
|
|
|
|
WithRefillMetabase(true))
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
|
|
|
|
var getPrm GetPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(addr)
|
2022-06-21 07:52:03 +00:00
|
|
|
_, err = sh.Get(getPrm)
|
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectNotFound))
|
|
|
|
require.NoError(t, sh.Close())
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:38:58 +00:00
|
|
|
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
|
|
|
),
|
2022-06-09 08:09:18 +00:00
|
|
|
WithPiloramaOptions(
|
|
|
|
pilorama.WithPath(filepath.Join(p, "pilorama"))),
|
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-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(v.obj)
|
2022-05-20 18:08:59 +00:00
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(tombObj)
|
2022-05-20 18:08:59 +00:00
|
|
|
|
|
|
|
_, 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)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(lockObj)
|
2022-06-20 12:28:20 +00:00
|
|
|
_, 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
|
2022-07-13 12:43:04 +00:00
|
|
|
inhumePrm.SetTarget(object.AddressOf(tombObj), tombMembers...)
|
2022-05-20 18:08:59 +00:00
|
|
|
|
|
|
|
_, 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-07-13 12:43:04 +00:00
|
|
|
headPrm.SetAddress(addr)
|
2022-05-20 18:08:59 +00:00
|
|
|
|
|
|
|
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-07-13 12:43:04 +00:00
|
|
|
headPrm.SetAddress(member)
|
2022-05-20 18:08:59 +00:00
|
|
|
|
|
|
|
_, 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
|
|
|
),
|
2022-06-09 08:09:18 +00:00
|
|
|
WithPiloramaOptions(
|
|
|
|
pilorama.WithPath(filepath.Join(p, "pilorama_another"))),
|
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
|
|
|
}
|