2020-12-01 13:58:34 +00:00
|
|
|
package shard_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2021-01-20 18:13:18 +00:00
|
|
|
"math/rand"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2020-12-01 13:58:34 +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"
|
|
|
|
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/local_object_storage/writecache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
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"
|
|
|
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
|
|
|
"git.frostfs.info/TrueCloudLab/tzhash/tz"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
2022-07-11 12:34:17 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2020-12-01 13:58:34 +00:00
|
|
|
)
|
|
|
|
|
2023-03-17 07:56:37 +00:00
|
|
|
type epochState struct {
|
|
|
|
Value uint64
|
|
|
|
}
|
2022-07-27 18:34:25 +00:00
|
|
|
|
|
|
|
func (s epochState) CurrentEpoch() uint64 {
|
2023-03-17 07:56:37 +00:00
|
|
|
return s.Value
|
2022-07-27 18:34:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:58:34 +00:00
|
|
|
func newShard(t testing.TB, enableWriteCache bool) *shard.Shard {
|
2022-01-20 16:56:07 +00:00
|
|
|
return newCustomShard(t, t.TempDir(), enableWriteCache,
|
2022-09-01 06:03:01 +00:00
|
|
|
nil,
|
2022-01-20 16:56:07 +00:00
|
|
|
nil)
|
2022-01-18 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 16:56:07 +00:00
|
|
|
func newCustomShard(t testing.TB, rootPath string, enableWriteCache bool, wcOpts []writecache.Option, bsOpts []blobstor.Option) *shard.Shard {
|
2020-12-01 13:58:34 +00:00
|
|
|
if enableWriteCache {
|
2022-02-02 13:28:08 +00:00
|
|
|
rootPath = filepath.Join(rootPath, "wc")
|
2020-12-01 13:58:34 +00:00
|
|
|
} else {
|
2022-02-02 13:28:08 +00:00
|
|
|
rootPath = filepath.Join(rootPath, "nowc")
|
2020-12-01 13:58:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
if bsOpts == nil {
|
|
|
|
bsOpts = []blobstor.Option{
|
2022-09-28 07:41:01 +00:00
|
|
|
blobstor.WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}),
|
2022-07-11 12:34:17 +00:00
|
|
|
blobstor.WithStorages([]blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: blobovniczatree.NewBlobovniczaTree(
|
2022-09-28 07:41:01 +00:00
|
|
|
blobovniczatree.WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}),
|
2022-07-11 12:34:17 +00:00
|
|
|
blobovniczatree.WithRootPath(filepath.Join(rootPath, "blob", "blobovnicza")),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowDepth(1),
|
|
|
|
blobovniczatree.WithBlobovniczaShallowWidth(1)),
|
|
|
|
Policy: func(_ *object.Object, data []byte) bool {
|
|
|
|
return len(data) <= 1<<20
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(filepath.Join(rootPath, "blob"))),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:58:34 +00:00
|
|
|
opts := []shard.Option{
|
2022-09-28 07:41:01 +00:00
|
|
|
shard.WithLogger(&logger.Logger{Logger: zap.L()}),
|
2022-07-11 12:34:17 +00:00
|
|
|
shard.WithBlobStorOptions(bsOpts...),
|
2020-12-01 13:58:34 +00:00
|
|
|
shard.WithMetaBaseOptions(
|
2022-02-02 13:28:08 +00:00
|
|
|
meta.WithPath(filepath.Join(rootPath, "meta")),
|
2022-07-27 18:34:25 +00:00
|
|
|
meta.WithEpochState(epochState{}),
|
2020-12-01 13:58:34 +00:00
|
|
|
),
|
2022-06-09 08:09:18 +00:00
|
|
|
shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(rootPath, "pilorama"))),
|
2020-12-01 13:58:34 +00:00
|
|
|
shard.WithWriteCache(enableWriteCache),
|
|
|
|
shard.WithWriteCacheOptions(
|
2022-01-20 16:56:07 +00:00
|
|
|
append(
|
2022-02-02 13:28:08 +00:00
|
|
|
[]writecache.Option{writecache.WithPath(filepath.Join(rootPath, "wcache"))},
|
2022-01-20 16:56:07 +00:00
|
|
|
wcOpts...)...,
|
2020-12-01 13:58:34 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
sh := shard.New(opts...)
|
|
|
|
|
|
|
|
require.NoError(t, sh.Open())
|
|
|
|
require.NoError(t, sh.Init())
|
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|
|
|
|
|
|
|
|
func releaseShard(s *shard.Shard, t testing.TB) {
|
2022-07-11 12:34:17 +00:00
|
|
|
require.NoError(t, s.Close())
|
2020-12-01 13:58:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func generateObject(t *testing.T) *object.Object {
|
|
|
|
return generateObjectWithCID(t, cidtest.ID())
|
2020-12-01 13:58:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
func generateObjectWithCID(t *testing.T, cnr cid.ID) *object.Object {
|
2022-05-17 13:59:46 +00:00
|
|
|
data := make([]byte, 32)
|
|
|
|
rand.Read(data)
|
2022-05-12 16:37:46 +00:00
|
|
|
return generateObjectWithPayload(cnr, data)
|
2022-01-18 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
func generateObjectWithPayload(cnr cid.ID, data []byte) *object.Object {
|
2022-05-11 14:58:52 +00:00
|
|
|
var ver version.Version
|
|
|
|
ver.SetMajor(2)
|
|
|
|
ver.SetMinor(1)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2022-05-11 16:35:01 +00:00
|
|
|
var csum checksum.Checksum
|
2022-01-18 13:40:11 +00:00
|
|
|
csum.SetSHA256(sha256.Sum256(data))
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2022-05-11 16:35:01 +00:00
|
|
|
var csumTZ checksum.Checksum
|
|
|
|
csumTZ.SetTillichZemor(tz.Sum(csum.Value()))
|
2020-12-01 13:58:34 +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)
|
2022-01-18 13:40:11 +00:00
|
|
|
obj.SetPayload(data)
|
2020-12-01 13:58:34 +00:00
|
|
|
obj.SetPayloadChecksum(csum)
|
|
|
|
obj.SetPayloadHomomorphicHash(csumTZ)
|
|
|
|
|
|
|
|
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
|
2020-12-01 13:58:34 +00:00
|
|
|
attr.SetKey(key)
|
|
|
|
attr.SetValue(val)
|
|
|
|
|
|
|
|
attrs := obj.Attributes()
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func addPayload(obj *object.Object, size int) {
|
2020-12-01 13:58:34 +00:00
|
|
|
buf := make([]byte, size)
|
|
|
|
_, _ = rand.Read(buf)
|
|
|
|
|
|
|
|
obj.SetPayload(buf)
|
|
|
|
obj.SetPayloadSize(uint64(size))
|
|
|
|
}
|