2020-12-08 07:51:34 +00:00
|
|
|
package meta_test
|
2020-10-30 13:42:24 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-21 13:34:31 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2020-10-30 13:42:24 +00:00
|
|
|
"testing"
|
2022-03-21 13:34:31 +00:00
|
|
|
"time"
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-08 07:51:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-03-21 13:34:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/rand"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2020-10-30 13:42:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-21 13:34:31 +00:00
|
|
|
"go.uber.org/atomic"
|
2020-10-30 13:42:24 +00:00
|
|
|
)
|
|
|
|
|
2022-03-21 13:34:31 +00:00
|
|
|
func prepareObjects(t testing.TB, n int) []*objectSDK.Object {
|
|
|
|
cid := cidtest.ID()
|
|
|
|
parentID := objecttest.ID()
|
|
|
|
objs := make([]*objectSDK.Object, n)
|
|
|
|
for i := range objs {
|
|
|
|
objs[i] = generateObjectWithCID(t, cid)
|
|
|
|
|
|
|
|
// FKBT indices.
|
|
|
|
attrs := make([]objectSDK.Attribute, 20)
|
|
|
|
for j := range attrs {
|
|
|
|
attrs[j].SetKey("abc" + strconv.FormatUint(rand.Uint64()%4, 16))
|
|
|
|
attrs[j].SetValue("xyz" + strconv.FormatUint(rand.Uint64()%4, 16))
|
|
|
|
}
|
|
|
|
objs[i].SetAttributes(attrs...)
|
|
|
|
|
|
|
|
// List indices.
|
|
|
|
if i%2 == 0 {
|
|
|
|
objs[i].SetParentID(parentID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return objs
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPut(b *testing.B) {
|
|
|
|
b.Run("parallel", func(b *testing.B) {
|
|
|
|
db := newDB(b,
|
|
|
|
meta.WithBatchDelay(time.Millisecond*10),
|
|
|
|
meta.WithBatchSize(runtime.NumCPU()))
|
|
|
|
// Ensure the benchmark is bound by CPU and not waiting batch-delay time.
|
|
|
|
b.SetParallelism(1)
|
|
|
|
|
|
|
|
index := atomic.NewInt64(-1)
|
|
|
|
objs := prepareObjects(b, b.N)
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
if err := meta.Put(db, objs[index.Inc()], nil); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
b.Run("sequential", func(b *testing.B) {
|
|
|
|
db := newDB(b,
|
|
|
|
meta.WithBatchDelay(time.Millisecond*10),
|
|
|
|
meta.WithBatchSize(1))
|
|
|
|
index := atomic.NewInt64(-1)
|
|
|
|
objs := prepareObjects(b, b.N)
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := meta.Put(db, objs[index.Inc()], nil); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func TestDB_PutBlobovnicaUpdate(t *testing.T) {
|
|
|
|
db := newDB(t)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
raw1 := generateObject(t)
|
2020-12-08 07:51:34 +00:00
|
|
|
blobovniczaID := blobovnicza.ID{1, 2, 3, 4}
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// put one object with blobovniczaID
|
2022-03-03 14:19:05 +00:00
|
|
|
err := meta.Put(db, raw1, &blobovniczaID)
|
2020-10-30 13:42:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
fetchedBlobovniczaID, err := meta.IsSmall(db, object.AddressOf(raw1))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &blobovniczaID, fetchedBlobovniczaID)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("update blobovniczaID", func(t *testing.T) {
|
|
|
|
newID := blobovnicza.ID{5, 6, 7, 8}
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := meta.Put(db, raw1, &newID)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
fetchedBlobovniczaID, err := meta.IsSmall(db, object.AddressOf(raw1))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &newID, fetchedBlobovniczaID)
|
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("update blobovniczaID on bad object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
raw2 := generateObject(t)
|
|
|
|
err := putBig(db, raw2)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
fetchedBlobovniczaID, err := meta.IsSmall(db, object.AddressOf(raw2))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, fetchedBlobovniczaID)
|
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
}
|