2020-12-01 13:58:34 +00:00
|
|
|
package shard_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2021-01-20 18:13:18 +00:00
|
|
|
"math/rand"
|
2020-12-01 13:58:34 +00:00
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-10-27 14:50:58 +00:00
|
|
|
"strings"
|
2020-12-01 13:58:34 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2020-12-08 07:51:34 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-04-06 10:56:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
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-01-26 12:11:13 +00:00
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newShard(t testing.TB, enableWriteCache bool) *shard.Shard {
|
2022-01-20 16:56:07 +00:00
|
|
|
return newCustomShard(t, t.TempDir(), enableWriteCache,
|
|
|
|
[]writecache.Option{writecache.WithMaxMemSize(0)},
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
opts := []shard.Option{
|
|
|
|
shard.WithLogger(zap.L()),
|
|
|
|
shard.WithBlobStorOptions(
|
2022-01-20 16:56:07 +00:00
|
|
|
append([]blobstor.Option{
|
2022-02-02 13:28:08 +00:00
|
|
|
blobstor.WithRootPath(filepath.Join(rootPath, "blob")),
|
2022-01-20 16:56:07 +00:00
|
|
|
blobstor.WithBlobovniczaShallowWidth(2),
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(2),
|
|
|
|
}, 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")),
|
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) {
|
|
|
|
s.Close()
|
2021-10-27 14:50:58 +00:00
|
|
|
os.RemoveAll(strings.Split(t.Name(), string(os.PathSeparator))[0])
|
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-03-03 14:19:05 +00:00
|
|
|
func generateObjectWithCID(t *testing.T, cid *cid.ID) *object.Object {
|
2022-01-18 13:40:11 +00:00
|
|
|
data := owner.PublicKeyToIDBytes(&test.DecodeKey(-1).PublicKey)
|
2022-03-03 14:19:05 +00:00
|
|
|
return generateObjectWithPayload(cid, data)
|
2022-01-18 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func generateObjectWithPayload(cid *cid.ID, data []byte) *object.Object {
|
2021-11-10 07:08:33 +00:00
|
|
|
version := version.New()
|
2020-12-01 13:58:34 +00:00
|
|
|
version.SetMajor(2)
|
|
|
|
version.SetMinor(1)
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
csum := new(checksum.Checksum)
|
2022-01-18 13:40:11 +00:00
|
|
|
csum.SetSHA256(sha256.Sum256(data))
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
csumTZ := new(checksum.Checksum)
|
2020-12-01 13:58:34 +00:00
|
|
|
csumTZ.SetTillichZemor(tz.Sum(csum.Sum()))
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2020-12-01 13:58:34 +00:00
|
|
|
obj.SetID(generateOID())
|
2021-12-01 13:56:48 +00:00
|
|
|
obj.SetOwnerID(ownertest.ID())
|
2020-12-01 13:58:34 +00:00
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetVersion(version)
|
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) {
|
|
|
|
attr := object.NewAttribute()
|
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))
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func generateOID() *oidSDK.ID {
|
2020-12-01 13:58:34 +00:00
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
_, _ = rand.Read(cs[:])
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
id := oidSDK.NewID()
|
2020-12-01 13:58:34 +00:00
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|