2020-12-08 07:51:34 +00:00
|
|
|
package meta_test
|
2020-10-30 13:42:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"bytes"
|
2022-05-31 11:42:41 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-11-30 12:17:02 +00:00
|
|
|
"runtime"
|
2020-10-30 13:42:24 +00:00
|
|
|
"testing"
|
2022-11-30 12:17:02 +00:00
|
|
|
"time"
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2020-10-30 13:42:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func TestDB_Get(t *testing.T) {
|
2022-07-27 18:38:28 +00:00
|
|
|
db := newDB(t, meta.WithEpochState(epochState{currEpoch}))
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
raw := generateObject(t)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// equal fails on diff of <nil> attributes and <{}> attributes,
|
|
|
|
/* so we make non empty attribute slice in parent*/
|
|
|
|
addAttribute(raw, "foo", "bar")
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("object not found", func(t *testing.T) {
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err := metaGet(db, object.AddressOf(raw), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put regular object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
newObj, err := metaGet(db, object.AddressOf(raw), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put tombstone object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeTombstone)
|
2022-05-12 16:37:46 +00:00
|
|
|
raw.SetID(oidtest.ID())
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
newObj, err := metaGet(db, object.AddressOf(raw), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put storage group object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeStorageGroup)
|
2022-05-12 16:37:46 +00:00
|
|
|
raw.SetID(oidtest.ID())
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
newObj, err := metaGet(db, object.AddressOf(raw), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2020-10-30 13:42:24 +00:00
|
|
|
})
|
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
t.Run("put lock object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeLock)
|
2022-05-12 16:37:46 +00:00
|
|
|
raw.SetID(oidtest.ID())
|
2022-02-15 12:51:56 +00:00
|
|
|
|
2022-03-05 08:49:19 +00:00
|
|
|
err := putBig(db, raw)
|
2022-02-15 12:51:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
newObj, err := metaGet(db, object.AddressOf(raw), false)
|
2022-02-15 12:51:56 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-05 08:49:19 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2022-02-15 12:51:56 +00:00
|
|
|
})
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put virtual object", func(t *testing.T) {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-08 11:23:23 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
parent := generateObjectWithCID(t, cnr)
|
2020-12-08 07:51:34 +00:00
|
|
|
addAttribute(parent, "foo", "bar")
|
|
|
|
|
2022-05-31 17:00:41 +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-08 11:23:23 +00:00
|
|
|
child.SetSplitID(splitID)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, child)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
t.Run("raw is true", func(t *testing.T) {
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err = metaGet(db, object.AddressOf(parent), true)
|
2020-12-08 11:23:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
var siErr *objectSDK.SplitInfoError
|
|
|
|
require.ErrorAs(t, err, &siErr)
|
2020-12-08 11:23:23 +00:00
|
|
|
require.Equal(t, splitID, siErr.SplitInfo().SplitID())
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
id1, _ := child.ID()
|
|
|
|
id2, _ := siErr.SplitInfo().LastPart()
|
|
|
|
require.Equal(t, id1, id2)
|
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
_, ok := siErr.SplitInfo().Link()
|
2022-05-12 16:37:46 +00:00
|
|
|
require.False(t, ok)
|
2020-12-08 11:23:23 +00:00
|
|
|
})
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
newParent, err := metaGet(db, object.AddressOf(parent), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.True(t, binaryEqual(parent.CutPayload(), newParent))
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
newChild, err := metaGet(db, object.AddressOf(child), true)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.True(t, binaryEqual(child.CutPayload(), newChild))
|
2020-10-30 13:42:24 +00:00
|
|
|
})
|
2021-09-24 15:18:04 +00:00
|
|
|
|
|
|
|
t.Run("get removed object", func(t *testing.T) {
|
2022-05-31 17:00:41 +00:00
|
|
|
obj := oidtest.Address()
|
|
|
|
ts := oidtest.Address()
|
2021-09-24 15:18:04 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
require.NoError(t, metaInhume(db, obj, ts))
|
|
|
|
_, err := metaGet(db, obj, false)
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectAlreadyRemoved))
|
2021-09-24 15:18:04 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
obj = oidtest.Address()
|
2022-05-20 16:48:14 +00:00
|
|
|
|
|
|
|
var prm meta.InhumePrm
|
2022-07-12 14:59:37 +00:00
|
|
|
prm.SetAddresses(obj)
|
2022-05-20 16:48:14 +00:00
|
|
|
|
|
|
|
_, err = db.Inhume(prm)
|
2022-05-31 17:00:41 +00:00
|
|
|
require.NoError(t, err)
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err = metaGet(db, obj, false)
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectNotFound))
|
2021-09-24 15:18:04 +00:00
|
|
|
})
|
2022-07-27 18:38:28 +00:00
|
|
|
|
|
|
|
t.Run("expired object", func(t *testing.T) {
|
|
|
|
checkExpiredObjects(t, db, func(exp, nonExp *objectSDK.Object) {
|
|
|
|
gotExp, err := metaGet(db, object.AddressOf(exp), false)
|
|
|
|
require.Nil(t, gotExp)
|
2022-10-31 07:08:30 +00:00
|
|
|
require.ErrorIs(t, err, meta.ErrObjectIsExpired)
|
2022-07-27 18:38:28 +00:00
|
|
|
|
|
|
|
gotNonExp, err := metaGet(db, object.AddressOf(nonExp), false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, binaryEqual(gotNonExp, nonExp.CutPayload()))
|
|
|
|
})
|
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +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-08 07:51:34 +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)
|
|
|
|
}
|
2022-05-31 11:42:41 +00:00
|
|
|
|
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
numOfObjects := [...]int{
|
|
|
|
1,
|
|
|
|
10,
|
|
|
|
100,
|
|
|
|
}
|
|
|
|
|
2022-11-30 12:17:02 +00:00
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(b.Name())
|
|
|
|
}()
|
|
|
|
|
2022-05-31 11:42:41 +00:00
|
|
|
for _, num := range numOfObjects {
|
2022-11-15 16:17:53 +00:00
|
|
|
b.Run(fmt.Sprintf("%d_objects", num), func(b *testing.B) {
|
2022-05-31 11:42:41 +00:00
|
|
|
benchmarkGet(b, num)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj *objectSDK.Object
|
|
|
|
|
|
|
|
func benchmarkGet(b *testing.B, numOfObj int) {
|
2022-11-30 12:17:02 +00:00
|
|
|
prepareDb := func(batchSize int) (*meta.DB, []oid.Address) {
|
|
|
|
db := newDB(b,
|
|
|
|
meta.WithMaxBatchSize(batchSize),
|
|
|
|
meta.WithMaxBatchDelay(10*time.Millisecond),
|
|
|
|
)
|
|
|
|
addrs := make([]oid.Address, 0, numOfObj)
|
|
|
|
|
|
|
|
for i := 0; i < numOfObj; i++ {
|
|
|
|
raw := generateObject(b)
|
|
|
|
addrs = append(addrs, object.AddressOf(raw))
|
|
|
|
|
|
|
|
err := putBig(db, raw)
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
2022-05-31 11:42:41 +00:00
|
|
|
|
2022-11-30 12:17:02 +00:00
|
|
|
return db, addrs
|
2022-05-31 11:42:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 12:17:02 +00:00
|
|
|
db, addrs := prepareDb(runtime.NumCPU())
|
2022-05-31 11:42:41 +00:00
|
|
|
|
2022-11-15 16:17:53 +00:00
|
|
|
b.Run("parallel", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2022-11-30 12:17:02 +00:00
|
|
|
var counter int
|
|
|
|
|
2022-11-15 16:17:53 +00:00
|
|
|
for pb.Next() {
|
2022-11-30 12:17:02 +00:00
|
|
|
var getPrm meta.GetPrm
|
|
|
|
getPrm.SetAddress(addrs[counter%len(addrs)])
|
|
|
|
counter++
|
|
|
|
|
|
|
|
_, err := db.Get(getPrm)
|
2022-11-15 16:17:53 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2022-05-31 11:42:41 +00:00
|
|
|
|
2022-11-30 12:17:02 +00:00
|
|
|
require.NoError(b, db.Close())
|
|
|
|
require.NoError(b, os.RemoveAll(b.Name()))
|
|
|
|
|
|
|
|
db, addrs = prepareDb(1)
|
|
|
|
|
2022-11-15 16:17:53 +00:00
|
|
|
b.Run("serial", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-11-30 12:17:02 +00:00
|
|
|
var getPrm meta.GetPrm
|
|
|
|
getPrm.SetAddress(addrs[i%len(addrs)])
|
2022-11-15 16:17:53 +00:00
|
|
|
|
2022-11-30 12:17:02 +00:00
|
|
|
_, err := db.Get(getPrm)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
2022-11-15 16:17:53 +00:00
|
|
|
}
|
2022-05-31 11:42:41 +00:00
|
|
|
}
|
2022-11-15 16:17:53 +00:00
|
|
|
})
|
2022-05-31 11:42:41 +00:00
|
|
|
}
|
2022-07-12 14:42:55 +00:00
|
|
|
|
|
|
|
func metaGet(db *meta.DB, addr oid.Address, raw bool) (*objectSDK.Object, error) {
|
|
|
|
var prm meta.GetPrm
|
2022-07-12 14:59:37 +00:00
|
|
|
prm.SetAddress(addr)
|
|
|
|
prm.SetRaw(raw)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
|
|
|
res, err := db.Get(prm)
|
|
|
|
return res.Header(), err
|
|
|
|
}
|