2021-04-14 08:50:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-22 07:35:53 +00:00
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-04-14 08:50:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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"
|
2021-04-14 08:50:21 +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"
|
2021-04-14 08:50:21 +00:00
|
|
|
"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"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2022-05-17 13:59:46 +00:00
|
|
|
checksumtest "github.com/nspcc-dev/neofs-sdk-go/checksum/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-02-22 07:35:53 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-12 16:37:46 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2022-05-17 13:59:46 +00:00
|
|
|
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2021-04-14 08:50:21 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
2021-10-08 13:26:24 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2021-04-14 08:50:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-31 14:58:32 +00:00
|
|
|
"go.uber.org/atomic"
|
2021-04-14 08:50:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-07-27 18:34:25 +00:00
|
|
|
type epochState struct{}
|
|
|
|
|
|
|
|
func (s epochState) CurrentEpoch() uint64 {
|
2022-09-09 10:27:15 +00:00
|
|
|
return 0
|
2022-07-27 18:34:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 07:35:53 +00:00
|
|
|
func BenchmarkExists(b *testing.B) {
|
|
|
|
b.Run("2 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 2)
|
|
|
|
})
|
|
|
|
b.Run("4 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 4)
|
|
|
|
})
|
|
|
|
b.Run("8 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 8)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkExists(b *testing.B, shardNum int) {
|
|
|
|
shards := make([]*shard.Shard, shardNum)
|
|
|
|
for i := 0; i < shardNum; i++ {
|
|
|
|
shards[i] = testNewShard(b, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := testNewEngineWithShards(shards...)
|
|
|
|
b.Cleanup(func() {
|
|
|
|
_ = e.Close()
|
|
|
|
_ = os.RemoveAll(b.Name())
|
|
|
|
})
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr := oidtest.Address()
|
2022-02-22 07:35:53 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObjectWithCID(b, cidtest.ID())
|
2022-03-05 08:49:19 +00:00
|
|
|
err := Put(e, obj)
|
2022-02-22 07:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ok, err := e.exists(addr)
|
|
|
|
if err != nil || ok {
|
|
|
|
b.Fatalf("%t %v", ok, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 08:50:21 +00:00
|
|
|
func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine {
|
2022-03-10 17:58:58 +00:00
|
|
|
engine := New()
|
2021-04-14 08:50:21 +00:00
|
|
|
|
|
|
|
for _, s := range shards {
|
2021-10-08 13:26:24 +00:00
|
|
|
pool, err := ants.NewPool(10, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
engine.shards[s.ID().String()] = shardWrapper{
|
|
|
|
errorCount: atomic.NewUint32(0),
|
|
|
|
Shard: s,
|
|
|
|
}
|
2021-10-08 13:26:24 +00:00
|
|
|
engine.shardPools[s.ID().String()] = pool
|
2021-04-14 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
func newStorages(root string, smallSize uint64) []blobstor.SubStorage {
|
|
|
|
return []blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: blobovniczatree.NewBlobovniczaTree(
|
|
|
|
blobovniczatree.WithRootPath(filepath.Join(root, "blobovnicza")),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowDepth(1),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobovniczatree.WithPermissions(0700)),
|
|
|
|
Policy: func(_ *object.Object, data []byte) bool {
|
|
|
|
return uint64(len(data)) < smallSize
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(root),
|
|
|
|
fstree.WithDepth(1)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:35:53 +00:00
|
|
|
func testNewShard(t testing.TB, id int) *shard.Shard {
|
2021-04-14 08:50:21 +00:00
|
|
|
sid, err := generateShardID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := shard.New(
|
|
|
|
shard.WithID(sid),
|
2022-09-28 07:41:01 +00:00
|
|
|
shard.WithLogger(&logger.Logger{Logger: zap.L()}),
|
2021-04-14 08:50:21 +00:00
|
|
|
shard.WithBlobStorOptions(
|
2022-07-11 12:34:17 +00:00
|
|
|
blobstor.WithStorages(
|
|
|
|
newStorages(filepath.Join(t.Name(), fmt.Sprintf("%d.blobstor", id)),
|
|
|
|
1<<20))),
|
2022-06-09 08:09:18 +00:00
|
|
|
shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.pilorama", id)))),
|
2021-04-14 08:50:21 +00:00
|
|
|
shard.WithMetaBaseOptions(
|
2022-02-02 13:28:08 +00:00
|
|
|
meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.metabase", id))),
|
2021-04-14 08:50:21 +00:00
|
|
|
meta.WithPermissions(0700),
|
2022-07-27 18:34:25 +00:00
|
|
|
meta.WithEpochState(epochState{}),
|
2021-04-14 08:50:21 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
require.NoError(t, s.Open())
|
|
|
|
require.NoError(t, s.Init())
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-09-26 06:30:41 +00:00
|
|
|
func testEngineFromShardOpts(t *testing.T, num int, extraOpts []shard.Option) *StorageEngine {
|
2022-03-10 17:58:58 +00:00
|
|
|
engine := New()
|
|
|
|
for i := 0; i < num; i++ {
|
2022-03-11 07:13:33 +00:00
|
|
|
_, err := engine.AddShard(append([]shard.Option{
|
2022-03-10 17:58:58 +00:00
|
|
|
shard.WithBlobStorOptions(
|
2022-07-11 12:34:17 +00:00
|
|
|
blobstor.WithStorages(
|
|
|
|
newStorages(filepath.Join(t.Name(), fmt.Sprintf("blobstor%d", i)),
|
|
|
|
1<<20)),
|
2022-03-10 17:58:58 +00:00
|
|
|
),
|
|
|
|
shard.WithMetaBaseOptions(
|
2022-03-11 07:13:33 +00:00
|
|
|
meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("metabase%d", i))),
|
2022-03-10 17:58:58 +00:00
|
|
|
meta.WithPermissions(0700),
|
2022-07-27 18:34:25 +00:00
|
|
|
meta.WithEpochState(epochState{}),
|
2022-06-09 08:09:18 +00:00
|
|
|
),
|
|
|
|
shard.WithPiloramaOptions(
|
|
|
|
pilorama.WithPath(filepath.Join(t.Name(), fmt.Sprintf("pilorama%d", i)))),
|
2022-09-26 06:30:41 +00:00
|
|
|
}, extraOpts...)...)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, engine.Open())
|
|
|
|
require.NoError(t, engine.Init())
|
|
|
|
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
func generateObjectWithCID(t testing.TB, cnr cid.ID) *object.Object {
|
2022-05-11 14:58:52 +00:00
|
|
|
var ver version.Version
|
|
|
|
ver.SetMajor(2)
|
|
|
|
ver.SetMinor(1)
|
2021-04-14 08:50:21 +00:00
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
csum := checksumtest.Checksum()
|
2021-04-14 08:50:21 +00:00
|
|
|
|
2022-05-11 16:35:01 +00:00
|
|
|
var csumTZ checksum.Checksum
|
|
|
|
csumTZ.SetTillichZemor(tz.Sum(csum.Value()))
|
2021-04-14 08:50:21 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetID(oidtest.ID())
|
2022-05-17 13:59:46 +00:00
|
|
|
obj.SetOwnerID(usertest.ID())
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetContainerID(cnr)
|
2022-05-11 14:58:52 +00:00
|
|
|
obj.SetVersion(&ver)
|
2021-04-14 08:50:21 +00:00
|
|
|
obj.SetPayloadChecksum(csum)
|
|
|
|
obj.SetPayloadHomomorphicHash(csumTZ)
|
|
|
|
obj.SetPayload([]byte{1, 2, 3, 4, 5})
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func addAttribute(obj *object.Object, key, val string) {
|
2022-03-15 12:11:35 +00:00
|
|
|
var attr object.Attribute
|
2021-04-14 08:50:21 +00:00
|
|
|
attr.SetKey(key)
|
|
|
|
attr.SetValue(val)
|
|
|
|
|
|
|
|
attrs := obj.Attributes()
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
}
|
2021-11-11 13:58:07 +00:00
|
|
|
|
|
|
|
func testNewEngineWithShardNum(t *testing.T, num int) *StorageEngine {
|
|
|
|
shards := make([]*shard.Shard, 0, num)
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
shards = append(shards, testNewShard(t, i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return testNewEngineWithShards(shards...)
|
|
|
|
}
|