2021-04-14 08:50:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-23 14:59:14 +00:00
|
|
|
"context"
|
2021-04-14 08:50:21 +00:00
|
|
|
"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"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/blobovniczatree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
2023-03-21 10:38:44 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/teststore"
|
2023-03-20 14:10:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
|
2023-03-07 13:38:26 +00:00
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
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"
|
2023-03-30 11:58:20 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2021-04-14 08:50:21 +00:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
e := testNewEngine(b).setInitializedShards(b, shards...).engine
|
2022-02-22 07:35:53 +00:00
|
|
|
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++ {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
type testEngineWrapper struct {
|
|
|
|
engine *StorageEngine
|
|
|
|
shardIDs []*shard.ID
|
|
|
|
}
|
2021-04-14 08:50:21 +00:00
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
func testNewEngine(t testing.TB, opts ...Option) *testEngineWrapper {
|
|
|
|
engine := New(WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}))
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(engine.cfg)
|
|
|
|
}
|
|
|
|
return &testEngineWrapper{
|
|
|
|
engine: engine,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *testEngineWrapper) setInitializedShards(t testing.TB, shards ...*shard.Shard) *testEngineWrapper {
|
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))
|
2023-03-28 14:16:03 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-08 13:26:24 +00:00
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
te.engine.shards[s.ID().String()] = hashedShard{
|
2023-02-27 13:16:37 +00:00
|
|
|
shardWrapper: shardWrapper{
|
|
|
|
errorCount: atomic.NewUint32(0),
|
|
|
|
Shard: s,
|
|
|
|
},
|
|
|
|
hash: hrw.Hash([]byte(s.ID().String())),
|
2022-01-31 14:58:32 +00:00
|
|
|
}
|
2023-03-30 11:58:20 +00:00
|
|
|
te.engine.shardPools[s.ID().String()] = pool
|
|
|
|
te.shardIDs = append(te.shardIDs, s.ID())
|
2021-04-14 08:50:21 +00:00
|
|
|
}
|
2023-03-30 11:58:20 +00:00
|
|
|
return te
|
|
|
|
}
|
2021-04-14 08:50:21 +00:00
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
func (te *testEngineWrapper) setShardsNum(t testing.TB, num int) *testEngineWrapper {
|
|
|
|
shards := make([]*shard.Shard, 0, num)
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
shards = append(shards, testNewShard(t, i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return te.setInitializedShards(t, shards...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *testEngineWrapper) setShardsNumOpts(t testing.TB, num int, shardOpts func(id int) []shard.Option) *testEngineWrapper {
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
opts := shardOpts(i)
|
|
|
|
id, err := te.engine.AddShard(opts...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
te.shardIDs = append(te.shardIDs, id)
|
|
|
|
}
|
|
|
|
return te
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *testEngineWrapper) setShardsNumAdditionalOpts(t testing.TB, num int, shardOpts func(id int) []shard.Option) *testEngineWrapper {
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
defaultOpts := testDefaultShardOptions(t, i)
|
|
|
|
opts := append(defaultOpts, shardOpts(i)...)
|
|
|
|
id, err := te.engine.AddShard(opts...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
te.shardIDs = append(te.shardIDs, id)
|
|
|
|
}
|
|
|
|
return te
|
2021-04-14 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
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)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:38:44 +00:00
|
|
|
func newTestStorages(root string, smallSize uint64) ([]blobstor.SubStorage, *teststore.TestStore, *teststore.TestStore) {
|
|
|
|
smallFileStorage := teststore.New(
|
|
|
|
teststore.WithSubstorage(blobovniczatree.NewBlobovniczaTree(
|
|
|
|
blobovniczatree.WithRootPath(filepath.Join(root, "blobovnicza")),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowDepth(1),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobovniczatree.WithPermissions(0700)),
|
|
|
|
))
|
|
|
|
largeFileStorage := teststore.New(
|
|
|
|
teststore.WithSubstorage(fstree.New(
|
|
|
|
fstree.WithPath(root),
|
|
|
|
fstree.WithDepth(1)),
|
|
|
|
))
|
|
|
|
return []blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: smallFileStorage,
|
|
|
|
Policy: func(_ *object.Object, data []byte) bool {
|
|
|
|
return uint64(len(data)) < smallSize
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: largeFileStorage,
|
|
|
|
},
|
|
|
|
}, smallFileStorage, largeFileStorage
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
shardOpts := append([]shard.Option{shard.WithID(sid)}, testDefaultShardOptions(t, id)...)
|
|
|
|
s := shard.New(shardOpts...)
|
|
|
|
|
|
|
|
require.NoError(t, s.Open())
|
|
|
|
require.NoError(t, s.Init(context.Background()))
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDefaultShardOptions(t testing.TB, id int) []shard.Option {
|
|
|
|
return []shard.Option{
|
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{}),
|
2023-03-30 11:58:20 +00:00
|
|
|
)}
|
2021-11-11 13:58:07 +00:00
|
|
|
}
|