2023-02-18 07:13:14 +00:00
|
|
|
package transformer
|
|
|
|
|
|
|
|
import (
|
2023-04-21 10:04:44 +00:00
|
|
|
"context"
|
2023-02-18 07:13:14 +00:00
|
|
|
"crypto/rand"
|
2023-02-24 13:21:56 +00:00
|
|
|
"crypto/sha256"
|
2023-02-18 07:13:14 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-16 06:51:03 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2023-02-18 07:13:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransformer(t *testing.T) {
|
|
|
|
const maxSize = 100
|
|
|
|
|
|
|
|
tt := new(testTarget)
|
|
|
|
|
2023-02-24 13:21:56 +00:00
|
|
|
target, pk := newPayloadSizeLimiter(maxSize, tt)
|
2023-02-18 07:13:14 +00:00
|
|
|
|
|
|
|
cnr := cidtest.ID()
|
2023-02-23 14:13:13 +00:00
|
|
|
hdr := newObject(cnr)
|
2023-02-18 07:13:14 +00:00
|
|
|
|
2023-02-24 13:21:56 +00:00
|
|
|
var owner user.ID
|
|
|
|
user.IDFromKey(&owner, pk.PrivateKey.PublicKey)
|
|
|
|
hdr.SetOwnerID(&owner)
|
|
|
|
|
2023-02-18 07:13:14 +00:00
|
|
|
expectedPayload := make([]byte, maxSize*2+maxSize/2)
|
|
|
|
_, _ = rand.Read(expectedPayload)
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
ids := writeObject(t, context.Background(), target, hdr, expectedPayload)
|
2023-02-18 07:13:14 +00:00
|
|
|
require.Equal(t, 4, len(tt.objects)) // 3 parts + linking object
|
|
|
|
|
|
|
|
var actualPayload []byte
|
|
|
|
for i := range tt.objects {
|
|
|
|
childCnr, ok := tt.objects[i].ContainerID()
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, cnr, childCnr)
|
|
|
|
require.Equal(t, objectSDK.TypeRegular, tt.objects[i].Type())
|
2023-02-24 13:21:56 +00:00
|
|
|
require.Equal(t, &owner, tt.objects[i].OwnerID())
|
2023-02-18 07:13:14 +00:00
|
|
|
|
|
|
|
payload := tt.objects[i].Payload()
|
|
|
|
require.EqualValues(t, tt.objects[i].PayloadSize(), len(payload))
|
|
|
|
actualPayload = append(actualPayload, payload...)
|
|
|
|
|
2023-02-24 13:21:56 +00:00
|
|
|
if len(payload) != 0 {
|
|
|
|
cs, ok := tt.objects[i].PayloadChecksum()
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
h := sha256.Sum256(payload)
|
|
|
|
require.Equal(t, h[:], cs.Value())
|
|
|
|
}
|
|
|
|
|
2023-02-18 07:13:14 +00:00
|
|
|
switch i {
|
|
|
|
case 0, 1:
|
|
|
|
require.EqualValues(t, maxSize, len(payload))
|
|
|
|
case 2:
|
|
|
|
require.EqualValues(t, maxSize/2, len(payload))
|
|
|
|
case 3:
|
|
|
|
parID, ok := tt.objects[i].ParentID()
|
|
|
|
require.True(t, ok)
|
2023-02-17 10:00:27 +00:00
|
|
|
require.Equal(t, ids.ParentID, &parID)
|
2023-02-18 07:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, expectedPayload, actualPayload)
|
2023-02-24 13:21:56 +00:00
|
|
|
|
|
|
|
t.Run("parent checksum", func(t *testing.T) {
|
|
|
|
cs, ok := ids.ParentHeader.PayloadChecksum()
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
h := sha256.Sum256(expectedPayload)
|
|
|
|
require.Equal(t, h[:], cs.Value())
|
|
|
|
})
|
2023-02-18 07:13:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 14:13:13 +00:00
|
|
|
func newObject(cnr cid.ID) *objectSDK.Object {
|
2023-02-18 07:13:14 +00:00
|
|
|
ver := version.Current()
|
|
|
|
hdr := objectSDK.New()
|
|
|
|
hdr.SetContainerID(cnr)
|
|
|
|
hdr.SetType(objectSDK.TypeRegular)
|
|
|
|
hdr.SetVersion(&ver)
|
2023-02-23 14:13:13 +00:00
|
|
|
return hdr
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func writeObject(t *testing.T, ctx context.Context, target ObjectTarget, header *objectSDK.Object, payload []byte) *AccessIdentifiers {
|
|
|
|
require.NoError(t, target.WriteHeader(ctx, header))
|
2023-02-23 14:13:13 +00:00
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
_, err := target.Write(ctx, payload)
|
2023-02-23 14:13:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
ids, err := target.Close(ctx)
|
2023-02-23 14:13:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTransformer(b *testing.B) {
|
|
|
|
hdr := newObject(cidtest.ID())
|
2023-02-18 07:13:14 +00:00
|
|
|
|
|
|
|
b.Run("small", func(b *testing.B) {
|
|
|
|
benchmarkTransformer(b, hdr, 8*1024)
|
|
|
|
})
|
|
|
|
b.Run("big", func(b *testing.B) {
|
|
|
|
benchmarkTransformer(b, hdr, 64*1024*1024*9/2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkTransformer(b *testing.B, header *objectSDK.Object, payloadSize int) {
|
|
|
|
const maxSize = 64 * 1024 * 1024
|
|
|
|
|
|
|
|
payload := make([]byte, payloadSize)
|
2023-04-21 10:04:44 +00:00
|
|
|
ctx := context.Background()
|
2023-02-18 07:13:14 +00:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
f, _ := newPayloadSizeLimiter(maxSize, benchTarget{})
|
2023-04-21 10:04:44 +00:00
|
|
|
if err := f.WriteHeader(ctx, header); err != nil {
|
2023-02-18 07:13:14 +00:00
|
|
|
b.Fatalf("write header: %v", err)
|
|
|
|
}
|
2023-04-21 10:04:44 +00:00
|
|
|
if _, err := f.Write(ctx, payload); err != nil {
|
2023-02-18 07:13:14 +00:00
|
|
|
b.Fatalf("write: %v", err)
|
|
|
|
}
|
2023-04-21 10:04:44 +00:00
|
|
|
if _, err := f.Close(ctx); err != nil {
|
2023-02-18 07:13:14 +00:00
|
|
|
b.Fatalf("close: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPayloadSizeLimiter(maxSize uint64, nextTarget ObjectTarget) (ObjectTarget, *keys.PrivateKey) {
|
|
|
|
p, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-02-18 07:43:34 +00:00
|
|
|
return NewPayloadSizeLimiter(Params{
|
|
|
|
Key: &p.PrivateKey,
|
|
|
|
NextTarget: nextTarget,
|
|
|
|
NetworkState: dummyEpochSource(123),
|
|
|
|
MaxSize: maxSize,
|
|
|
|
WithoutHomomorphicHash: true,
|
2023-02-18 07:13:14 +00:00
|
|
|
}), p
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyEpochSource uint64
|
|
|
|
|
|
|
|
func (s dummyEpochSource) CurrentEpoch() uint64 {
|
|
|
|
return uint64(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type benchTarget struct{}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func (benchTarget) WriteHeader(_ context.Context, object *objectSDK.Object) error {
|
2023-02-18 07:13:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func (benchTarget) Write(_ context.Context, p []byte) (n int, err error) {
|
2023-02-18 07:13:14 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func (benchTarget) Close(context.Context) (*AccessIdentifiers, error) {
|
2023-02-18 07:13:14 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testTarget struct {
|
|
|
|
current *objectSDK.Object
|
|
|
|
payload []byte
|
|
|
|
objects []*objectSDK.Object
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func (tt *testTarget) WriteHeader(_ context.Context, object *objectSDK.Object) error {
|
2023-02-18 07:13:14 +00:00
|
|
|
tt.current = object
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func (tt *testTarget) Write(_ context.Context, p []byte) (n int, err error) {
|
2023-02-18 07:13:14 +00:00
|
|
|
tt.payload = append(tt.payload, p...)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:04:44 +00:00
|
|
|
func (tt *testTarget) Close(_ context.Context) (*AccessIdentifiers, error) {
|
2023-02-18 07:13:14 +00:00
|
|
|
tt.current.SetPayload(tt.payload)
|
2023-02-24 13:31:12 +00:00
|
|
|
tt.objects = append(tt.objects, tt.current)
|
2023-02-18 07:13:14 +00:00
|
|
|
tt.current = nil
|
|
|
|
tt.payload = nil
|
|
|
|
return nil, nil // AccessIdentifiers should not be used.
|
|
|
|
}
|