frostfs-node/pkg/local_object_storage/metabase/put_test.go
Ekaterina Lebedeva a685fcdc96
All checks were successful
DCO action / DCO (pull_request) Successful in 2m41s
Tests and linters / Run gofumpt (pull_request) Successful in 2m32s
Vulncheck / Vulncheck (pull_request) Successful in 2m38s
Build / Build Components (1.23) (pull_request) Successful in 3m0s
Build / Build Components (1.22) (pull_request) Successful in 3m3s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m33s
Tests and linters / Tests (1.22) (pull_request) Successful in 3m34s
Tests and linters / Tests (1.23) (pull_request) Successful in 3m36s
Tests and linters / Staticcheck (pull_request) Successful in 3m35s
Tests and linters / Lint (pull_request) Successful in 4m18s
Tests and linters / Tests with -race (pull_request) Successful in 4m20s
Tests and linters / gopls check (pull_request) Successful in 4m25s
[#1317] go.mod: Use range over int
Since Go 1.22 a "for" statement with a "range" clause is able
to iterate through integer values from zero to an upper limit.

gopatch script:
@@
var i, e expression
@@
-for i := 0; i <= e - 1; i++ {
+for i := range e {
    ...
}

@@
var i, e expression
@@
-for i := 0; i <= e; i++ {
+for i := range e + 1 {
    ...
}

@@
var i, e expression
@@
-for i := 0; i < e; i++ {
+for i := range e {
    ...
}

Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
2024-09-03 13:00:54 +03:00

130 lines
3.4 KiB
Go

package meta_test
import (
"context"
"runtime"
"strconv"
"sync/atomic"
"testing"
"time"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/rand"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
objecttest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
)
func prepareObjects(n int) []*objectSDK.Object {
cnr := cidtest.ID()
parentID := objecttest.ID()
objs := make([]*objectSDK.Object, n)
for i := range objs {
objs[i] = testutil.GenerateObjectWithCID(cnr)
// 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.WithMaxBatchDelay(time.Millisecond*10),
meta.WithMaxBatchSize(runtime.NumCPU()))
defer func() { require.NoError(b, db.Close()) }()
// Ensure the benchmark is bound by CPU and not waiting batch-delay time.
b.SetParallelism(1)
var index atomic.Int64
index.Store(-1)
objs := prepareObjects(b.N)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := metaPut(db, objs[index.Add(1)], nil); err != nil {
b.Fatal(err)
}
}
})
})
b.Run("sequential", func(b *testing.B) {
db := newDB(b,
meta.WithMaxBatchDelay(time.Millisecond*10),
meta.WithMaxBatchSize(1))
defer func() { require.NoError(b, db.Close()) }()
var index atomic.Int64
index.Store(-1)
objs := prepareObjects(b.N)
b.ResetTimer()
b.ReportAllocs()
for range b.N {
if err := metaPut(db, objs[index.Add(1)], nil); err != nil {
b.Fatal(err)
}
}
})
}
func TestDB_PutBlobovniczaUpdate(t *testing.T) {
db := newDB(t)
defer func() { require.NoError(t, db.Close()) }()
raw1 := testutil.GenerateObject()
storageID := []byte{1, 2, 3, 4}
// put one object with storageID
err := metaPut(db, raw1, storageID)
require.NoError(t, err)
fetchedStorageID, err := metaStorageID(db, object.AddressOf(raw1))
require.NoError(t, err)
require.Equal(t, storageID, fetchedStorageID)
t.Run("update storageID", func(t *testing.T) {
newID := []byte{5, 6, 7, 8}
err := metaPut(db, raw1, newID)
require.NoError(t, err)
fetchedBlobovniczaID, err := metaStorageID(db, object.AddressOf(raw1))
require.NoError(t, err)
require.Equal(t, newID, fetchedBlobovniczaID)
})
t.Run("update storageID on bad object", func(t *testing.T) {
raw2 := testutil.GenerateObject()
err := putBig(db, raw2)
require.NoError(t, err)
fetchedBlobovniczaID, err := metaStorageID(db, object.AddressOf(raw2))
require.NoError(t, err)
require.Nil(t, fetchedBlobovniczaID)
})
}
func metaPut(db *meta.DB, obj *objectSDK.Object, id []byte) error {
var putPrm meta.PutPrm
putPrm.SetObject(obj)
putPrm.SetStorageID(id)
_, err := db.Put(context.Background(), putPrm)
return err
}