2021-04-14 08:50:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-23 14:59:14 +00:00
|
|
|
"context"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2023-05-19 15:06:20 +00:00
|
|
|
"sync/atomic"
|
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"
|
2023-08-23 07:53:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger/test"
|
2023-03-07 13:38:26 +00:00
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
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-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)
|
|
|
|
})
|
2023-12-20 14:18:28 +00:00
|
|
|
b.Run("12 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 12)
|
|
|
|
})
|
|
|
|
b.Run("16 shards", func(b *testing.B) {
|
|
|
|
benchmarkExists(b, 12)
|
|
|
|
})
|
2022-02-22 07:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkExists(b *testing.B, shardNum int) {
|
|
|
|
shards := make([]*shard.Shard, shardNum)
|
|
|
|
for i := 0; i < shardNum; i++ {
|
2024-03-01 08:58:17 +00:00
|
|
|
shards[i] = testNewShard(b)
|
2022-02-22 07:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
e := testNewEngine(b).setInitializedShards(b, shards...).engine
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(b, e.Close(context.Background())) }()
|
2022-02-22 07:35:53 +00:00
|
|
|
|
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())
|
2023-04-12 14:01:29 +00:00
|
|
|
err := Put(context.Background(), 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++ {
|
2023-04-12 14:01:29 +00:00
|
|
|
ok, err := e.exists(context.Background(), addr)
|
2022-02-22 07:35:53 +00:00
|
|
|
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 {
|
2024-01-09 08:37:41 +00:00
|
|
|
engine := New(WithLogger(test.NewLogger(t)))
|
2023-03-30 11:58:20 +00:00
|
|
|
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{
|
2023-05-19 15:06:20 +00:00
|
|
|
errorCount: new(atomic.Uint32),
|
2023-02-27 13:16:37 +00:00
|
|
|
Shard: s,
|
|
|
|
},
|
2023-06-02 12:39:16 +00:00
|
|
|
hash: hrw.StringHash(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++ {
|
2024-03-01 08:58:17 +00:00
|
|
|
shards = append(shards, testNewShard(t))
|
2023-03-30 11:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-08-31 16:26:47 +00:00
|
|
|
id, err := te.engine.AddShard(context.Background(), opts...)
|
2023-03-30 11:58:20 +00:00
|
|
|
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++ {
|
2024-03-01 08:58:17 +00:00
|
|
|
defaultOpts := testDefaultShardOptions(t)
|
2023-03-30 11:58:20 +00:00
|
|
|
opts := append(defaultOpts, shardOpts(i)...)
|
2023-08-31 16:26:47 +00:00
|
|
|
id, err := te.engine.AddShard(context.Background(), opts...)
|
2023-03-30 11:58:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
te.shardIDs = append(te.shardIDs, id)
|
|
|
|
}
|
|
|
|
return te
|
2021-04-14 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 14:27:54 +00:00
|
|
|
func newStorages(t testing.TB, root string, smallSize uint64) []blobstor.SubStorage {
|
2022-07-11 12:34:17 +00:00
|
|
|
return []blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: blobovniczatree.NewBlobovniczaTree(
|
|
|
|
blobovniczatree.WithRootPath(filepath.Join(root, "blobovnicza")),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowDepth(1),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowWidth(1),
|
2024-01-09 14:27:54 +00:00
|
|
|
blobovniczatree.WithPermissions(0o700),
|
|
|
|
blobovniczatree.WithLogger(test.NewLogger(t))),
|
2023-07-06 12:36:41 +00:00
|
|
|
Policy: func(_ *objectSDK.Object, data []byte) bool {
|
2022-07-11 12:34:17 +00:00
|
|
|
return uint64(len(data)) < smallSize
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(root),
|
2024-01-09 14:27:54 +00:00
|
|
|
fstree.WithDepth(1),
|
|
|
|
fstree.WithLogger(test.NewLogger(t))),
|
2022-07-11 12:34:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
2023-10-31 11:56:55 +00:00
|
|
|
blobovniczatree.WithPermissions(0o700)),
|
2023-03-21 10:38:44 +00:00
|
|
|
))
|
|
|
|
largeFileStorage := teststore.New(
|
|
|
|
teststore.WithSubstorage(fstree.New(
|
|
|
|
fstree.WithPath(root),
|
|
|
|
fstree.WithDepth(1)),
|
|
|
|
))
|
|
|
|
return []blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: smallFileStorage,
|
2023-07-06 12:36:41 +00:00
|
|
|
Policy: func(_ *objectSDK.Object, data []byte) bool {
|
2023-03-21 10:38:44 +00:00
|
|
|
return uint64(len(data)) < smallSize
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: largeFileStorage,
|
|
|
|
},
|
|
|
|
}, smallFileStorage, largeFileStorage
|
|
|
|
}
|
|
|
|
|
2024-03-01 08:58:17 +00:00
|
|
|
func testNewShard(t testing.TB, opts ...shard.Option) *shard.Shard {
|
2021-04-14 08:50:21 +00:00
|
|
|
sid, err := generateShardID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-03-01 08:58:17 +00:00
|
|
|
shardOpts := append([]shard.Option{shard.WithID(sid)}, testDefaultShardOptions(t)...)
|
2024-01-10 08:56:24 +00:00
|
|
|
s := shard.New(append(shardOpts, opts...)...)
|
2023-03-30 11:58:20 +00:00
|
|
|
|
2023-08-31 16:26:47 +00:00
|
|
|
require.NoError(t, s.Open(context.Background()))
|
2023-03-30 11:58:20 +00:00
|
|
|
require.NoError(t, s.Init(context.Background()))
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2024-03-01 08:58:17 +00:00
|
|
|
func testDefaultShardOptions(t testing.TB) []shard.Option {
|
2023-03-30 11:58:20 +00:00
|
|
|
return []shard.Option{
|
2024-01-09 08:37:41 +00:00
|
|
|
shard.WithLogger(test.NewLogger(t)),
|
2021-04-14 08:50:21 +00:00
|
|
|
shard.WithBlobStorOptions(
|
2022-07-11 12:34:17 +00:00
|
|
|
blobstor.WithStorages(
|
2024-01-09 14:27:54 +00:00
|
|
|
newStorages(t, t.TempDir(), 1<<20)),
|
|
|
|
blobstor.WithLogger(test.NewLogger(t)),
|
|
|
|
),
|
2023-08-14 11:01:39 +00:00
|
|
|
shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(t.TempDir(), "pilorama"))),
|
2021-04-14 08:50:21 +00:00
|
|
|
shard.WithMetaBaseOptions(
|
2023-08-14 11:01:39 +00:00
|
|
|
meta.WithPath(filepath.Join(t.TempDir(), "metabase")),
|
2023-10-31 11:56:55 +00:00
|
|
|
meta.WithPermissions(0o700),
|
2022-07-27 18:34:25 +00:00
|
|
|
meta.WithEpochState(epochState{}),
|
2024-01-09 14:27:54 +00:00
|
|
|
meta.WithLogger(test.NewLogger(t)),
|
2023-10-31 11:56:55 +00:00
|
|
|
),
|
|
|
|
}
|
2021-11-11 13:58:07 +00:00
|
|
|
}
|