2022-01-31 14:58:32 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-01-31 14:58:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
|
|
|
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-01-31 14:58:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2022-06-28 14:05:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
2022-06-29 17:33:48 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-01-31 14:58:32 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-31 14:58:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
const errSmallSize = 256
|
|
|
|
|
2022-05-24 12:06:29 +00:00
|
|
|
func newEngineWithErrorThreshold(t testing.TB, dir string, errThreshold uint32) (*StorageEngine, string, [2]*shard.ID) {
|
2022-02-22 07:20:33 +00:00
|
|
|
if dir == "" {
|
|
|
|
var err error
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
dir, err = os.MkdirTemp("", "*")
|
2022-01-31 14:58:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
e := New(
|
|
|
|
WithLogger(zaptest.NewLogger(t)),
|
|
|
|
WithShardPoolSize(1),
|
|
|
|
WithErrorThreshold(errThreshold))
|
|
|
|
|
|
|
|
var ids [2]*shard.ID
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for i := range ids {
|
|
|
|
ids[i], err = e.AddShard(
|
|
|
|
shard.WithLogger(zaptest.NewLogger(t)),
|
|
|
|
shard.WithBlobStorOptions(
|
|
|
|
blobstor.WithRootPath(filepath.Join(dir, strconv.Itoa(i))),
|
|
|
|
blobstor.WithShallowDepth(1),
|
|
|
|
blobstor.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(1),
|
|
|
|
blobstor.WithSmallSizeLimit(errSmallSize),
|
|
|
|
blobstor.WithRootPerm(0700)),
|
|
|
|
shard.WithMetaBaseOptions(
|
|
|
|
meta.WithPath(filepath.Join(dir, fmt.Sprintf("%d.metabase", i))),
|
2022-06-09 08:09:18 +00:00
|
|
|
meta.WithPermissions(0700)),
|
|
|
|
shard.WithPiloramaOptions(
|
|
|
|
pilorama.WithPath(filepath.Join(dir, fmt.Sprintf("%d.pilorama", i))),
|
|
|
|
pilorama.WithPerm(0700)))
|
2022-02-22 07:20:33 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-31 14:58:32 +00:00
|
|
|
}
|
2022-02-22 07:20:33 +00:00
|
|
|
require.NoError(t, e.Open())
|
|
|
|
require.NoError(t, e.Init())
|
|
|
|
|
|
|
|
return e, dir, ids
|
|
|
|
}
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
func TestErrorReporting(t *testing.T) {
|
2022-01-31 14:58:32 +00:00
|
|
|
t.Run("ignore errors by default", func(t *testing.T) {
|
2022-02-22 07:20:33 +00:00
|
|
|
e, dir, id := newEngineWithErrorThreshold(t, "", 0)
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObjectWithCID(t, cidtest.ID())
|
2022-02-22 07:20:33 +00:00
|
|
|
obj.SetPayload(make([]byte, errSmallSize))
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var prm shard.PutPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
prm.SetObject(obj)
|
2022-01-31 14:58:32 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
_, err := e.shards[id[0].String()].Shard.Put(prm)
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Get(GetPrm{addr: object.AddressOf(obj)})
|
2022-01-31 14:58:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], 0, mode.ReadWrite)
|
|
|
|
checkShardState(t, e, id[1], 0, mode.ReadWrite)
|
2022-01-31 14:58:32 +00:00
|
|
|
|
|
|
|
corruptSubDir(t, filepath.Join(dir, "0"))
|
|
|
|
|
|
|
|
for i := uint32(1); i < 3; i++ {
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Get(GetPrm{addr: object.AddressOf(obj)})
|
2022-01-31 14:58:32 +00:00
|
|
|
require.Error(t, err)
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], i, mode.ReadWrite)
|
|
|
|
checkShardState(t, e, id[1], 0, mode.ReadWrite)
|
2022-01-31 14:58:32 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("with error threshold", func(t *testing.T) {
|
|
|
|
const errThreshold = 3
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
e, dir, id := newEngineWithErrorThreshold(t, "", errThreshold)
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObjectWithCID(t, cidtest.ID())
|
2022-02-22 07:20:33 +00:00
|
|
|
obj.SetPayload(make([]byte, errSmallSize))
|
2022-01-31 14:58:32 +00:00
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var prm shard.PutPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
prm.SetObject(obj)
|
2022-01-31 14:58:32 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
_, err := e.shards[id[0].String()].Put(prm)
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Get(GetPrm{addr: object.AddressOf(obj)})
|
2022-01-31 14:58:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], 0, mode.ReadWrite)
|
|
|
|
checkShardState(t, e, id[1], 0, mode.ReadWrite)
|
2022-01-31 14:58:32 +00:00
|
|
|
|
|
|
|
corruptSubDir(t, filepath.Join(dir, "0"))
|
|
|
|
|
|
|
|
for i := uint32(1); i < errThreshold; i++ {
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Get(GetPrm{addr: object.AddressOf(obj)})
|
2022-01-31 14:58:32 +00:00
|
|
|
require.Error(t, err)
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], i, mode.ReadWrite)
|
|
|
|
checkShardState(t, e, id[1], 0, mode.ReadWrite)
|
2022-01-31 14:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); i < 2; i++ {
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Get(GetPrm{addr: object.AddressOf(obj)})
|
2022-01-31 14:58:32 +00:00
|
|
|
require.Error(t, err)
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], errThreshold+i, mode.Degraded)
|
|
|
|
checkShardState(t, e, id[1], 0, mode.ReadWrite)
|
2022-01-31 14:58:32 +00:00
|
|
|
}
|
2022-01-31 15:15:33 +00:00
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
require.NoError(t, e.SetShardMode(id[0], mode.ReadWrite, false))
|
|
|
|
checkShardState(t, e, id[0], errThreshold+1, mode.ReadWrite)
|
2022-01-31 15:15:33 +00:00
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
require.NoError(t, e.SetShardMode(id[0], mode.ReadWrite, true))
|
|
|
|
checkShardState(t, e, id[0], 0, mode.ReadWrite)
|
2022-01-31 14:58:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
// Issue #1186.
|
|
|
|
func TestBlobstorFailback(t *testing.T) {
|
|
|
|
dir, err := os.MkdirTemp("", "*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { require.NoError(t, os.RemoveAll(dir)) })
|
|
|
|
|
|
|
|
e, _, id := newEngineWithErrorThreshold(t, dir, 1)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
objs := make([]*objectSDK.Object, 0, 2)
|
2022-03-04 12:57:43 +00:00
|
|
|
for _, size := range []int{15, errSmallSize + 1} {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObjectWithCID(t, cidtest.ID())
|
2022-02-22 07:20:33 +00:00
|
|
|
obj.SetPayload(make([]byte, size))
|
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var prm shard.PutPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
prm.SetObject(obj)
|
2022-02-22 07:20:33 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
_, err = e.shards[id[0].String()].Shard.Put(prm)
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
require.NoError(t, err)
|
2022-03-05 08:49:19 +00:00
|
|
|
objs = append(objs, obj)
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range objs {
|
2022-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(objs[i])
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.Get(GetPrm{addr: addr})
|
2022-02-22 07:20:33 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.GetRange(RngPrm{addr: addr})
|
2022-03-04 12:57:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], 0, mode.ReadWrite)
|
2022-02-22 07:20:33 +00:00
|
|
|
require.NoError(t, e.Close())
|
|
|
|
|
|
|
|
p1 := e.shards[id[0].String()].Shard.DumpInfo().BlobStorInfo.RootPath
|
|
|
|
p2 := e.shards[id[1].String()].Shard.DumpInfo().BlobStorInfo.RootPath
|
|
|
|
tmp := filepath.Join(dir, "tmp")
|
|
|
|
require.NoError(t, os.Rename(p1, tmp))
|
|
|
|
require.NoError(t, os.Rename(p2, p1))
|
|
|
|
require.NoError(t, os.Rename(tmp, p2))
|
|
|
|
|
|
|
|
e, _, id = newEngineWithErrorThreshold(t, dir, 1)
|
|
|
|
|
|
|
|
for i := range objs {
|
2022-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(objs[i])
|
2022-05-23 13:12:32 +00:00
|
|
|
getRes, err := e.Get(GetPrm{addr: addr})
|
2022-03-04 12:57:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, objs[i], getRes.Object())
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
rngRes, err := e.GetRange(RngPrm{addr: addr, off: 1, ln: 10})
|
2022-02-22 07:20:33 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-04 12:57:43 +00:00
|
|
|
require.Equal(t, objs[i].Payload()[1:11], rngRes.Object().Payload())
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
_, err = e.GetRange(RngPrm{addr: addr, off: errSmallSize + 10, ln: 1})
|
2022-06-29 17:33:48 +00:00
|
|
|
require.ErrorAs(t, err, &apistatus.ObjectOutOfRange{})
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
checkShardState(t, e, id[0], 4, mode.Degraded)
|
|
|
|
checkShardState(t, e, id[1], 0, mode.ReadWrite)
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
func checkShardState(t *testing.T, e *StorageEngine, id *shard.ID, errCount uint32, mode mode.Mode) {
|
2022-01-31 14:58:32 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
sh := e.shards[id.String()]
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
|
|
|
require.Equal(t, mode, sh.GetMode())
|
|
|
|
require.Equal(t, errCount, sh.errorCount.Load())
|
|
|
|
}
|
|
|
|
|
2022-05-17 15:46:48 +00:00
|
|
|
// corruptSubDir makes random directory except "blobovnicza" in blobstor FSTree unreadable.
|
2022-01-31 14:58:32 +00:00
|
|
|
func corruptSubDir(t *testing.T, dir string) {
|
|
|
|
de, err := os.ReadDir(dir)
|
|
|
|
require.NoError(t, err)
|
2022-05-17 15:46:48 +00:00
|
|
|
|
|
|
|
// FIXME(@cthulhu-rider): copy-paste of unexported const from blobstor package, see #1407
|
|
|
|
const dirBlobovnicza = "blobovnicza"
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
for i := range de {
|
2022-05-17 15:46:48 +00:00
|
|
|
if de[i].IsDir() && de[i].Name() != dirBlobovnicza {
|
2022-01-31 14:58:32 +00:00
|
|
|
require.NoError(t, os.Chmod(filepath.Join(dir, de[i].Name()), 0))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|