2020-12-01 13:58:34 +00:00
|
|
|
package shard_test
|
|
|
|
|
|
|
|
import (
|
2020-12-03 09:50:20 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2020-12-01 13:58:34 +00:00
|
|
|
"testing"
|
2021-04-06 10:56:06 +00:00
|
|
|
"time"
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2020-12-03 09:50:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-11-10 07:08:33 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-12 16:37:46 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShard_Get(t *testing.T) {
|
|
|
|
t.Run("without write cache", func(t *testing.T) {
|
2021-04-06 10:56:06 +00:00
|
|
|
testShardGet(t, false)
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with write cache", func(t *testing.T) {
|
2021-04-06 10:56:06 +00:00
|
|
|
testShardGet(t, true)
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
func testShardGet(t *testing.T, hasWriteCache bool) {
|
|
|
|
sh := newShard(t, hasWriteCache)
|
|
|
|
defer releaseShard(sh, t)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
|
|
|
putPrm := new(shard.PutPrm)
|
|
|
|
getPrm := new(shard.GetPrm)
|
|
|
|
|
|
|
|
t.Run("small object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2021-04-06 10:56:06 +00:00
|
|
|
addAttribute(obj, "foo", "bar")
|
2020-12-01 13:58:34 +00:00
|
|
|
addPayload(obj, 1<<5)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
putPrm.WithObject(obj)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
getPrm.WithAddress(object.AddressOf(obj))
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err := testGet(t, sh, getPrm, hasWriteCache)
|
2020-12-01 13:58:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, obj, res.Object())
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("big object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2021-04-06 10:56:06 +00:00
|
|
|
addAttribute(obj, "foo", "bar")
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetID(oidtest.ID())
|
2020-12-01 13:58:34 +00:00
|
|
|
addPayload(obj, 1<<20) // big obj
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
putPrm.WithObject(obj)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
getPrm.WithAddress(object.AddressOf(obj))
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err := testGet(t, sh, getPrm, hasWriteCache)
|
2020-12-01 13:58:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, obj, res.Object())
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
2020-12-03 09:50:20 +00:00
|
|
|
|
|
|
|
t.Run("parent object", func(t *testing.T) {
|
2022-05-12 16:37:46 +00:00
|
|
|
t.Skip("not working, see neofs-sdk-go#242")
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2021-04-06 10:56:06 +00:00
|
|
|
addAttribute(obj, "foo", "bar")
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-03 09:50:20 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
parent := generateObjectWithCID(t, cnr)
|
2020-12-03 09:50:20 +00:00
|
|
|
addAttribute(parent, "parent", "attribute")
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
child := generateObjectWithCID(t, cnr)
|
2022-03-03 14:19:05 +00:00
|
|
|
child.SetParent(parent)
|
2022-05-12 16:37:46 +00:00
|
|
|
idParent, _ := parent.ID()
|
|
|
|
child.SetParentID(idParent)
|
2020-12-03 09:50:20 +00:00
|
|
|
child.SetSplitID(splitID)
|
|
|
|
addPayload(child, 1<<5)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
putPrm.WithObject(child)
|
2020-12-03 09:50:20 +00:00
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
getPrm.WithAddress(object.AddressOf(child))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err := testGet(t, sh, getPrm, hasWriteCache)
|
2020-12-03 09:50:20 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.True(t, binaryEqual(child, res.Object()))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
getPrm.WithAddress(object.AddressOf(parent))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
_, err = testGet(t, sh, getPrm, hasWriteCache)
|
2020-12-03 09:50:20 +00:00
|
|
|
|
|
|
|
var expectedErr *objectSDK.SplitInfoError
|
|
|
|
require.True(t, errors.As(err, &expectedErr))
|
|
|
|
|
|
|
|
si, ok := err.(*objectSDK.SplitInfoError)
|
|
|
|
require.True(t, ok)
|
2022-05-12 16:37:46 +00:00
|
|
|
_, ok = si.SplitInfo().Link()
|
|
|
|
require.False(t, ok)
|
|
|
|
id1, _ := child.ID()
|
|
|
|
id2, _ := si.SplitInfo().LastPart()
|
|
|
|
require.Equal(t, id1, id2)
|
2020-12-03 09:50:20 +00:00
|
|
|
require.Equal(t, splitID, si.SplitInfo().SplitID())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
func testGet(t *testing.T, sh *shard.Shard, getPrm *shard.GetPrm, hasWriteCache bool) (*shard.GetRes, error) {
|
|
|
|
res, err := sh.Get(getPrm)
|
|
|
|
if hasWriteCache {
|
|
|
|
require.Eventually(t, func() bool {
|
2022-03-17 08:03:58 +00:00
|
|
|
if shard.IsErrNotFound(err) {
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err = sh.Get(getPrm)
|
|
|
|
}
|
2022-03-17 08:03:58 +00:00
|
|
|
return !shard.IsErrNotFound(err)
|
2021-04-06 10:56:06 +00:00
|
|
|
}, time.Second, time.Millisecond*100)
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2020-12-03 09:50:20 +00:00
|
|
|
// binary equal is used when object contains empty lists in the structure and
|
|
|
|
// requre.Equal fails on comparing <nil> and []{} lists.
|
2022-03-03 14:19:05 +00:00
|
|
|
func binaryEqual(a, b *objectSDK.Object) bool {
|
2020-12-03 09:50:20 +00:00
|
|
|
binaryA, err := a.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
binaryB, err := b.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.Equal(binaryA, binaryB)
|
2020-12-01 13:58:34 +00:00
|
|
|
}
|