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"
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2024-04-22 06:43:42 +00:00
|
|
|
"errors"
|
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"
|
2023-03-20 14:10:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
|
2023-03-07 13:38:26 +00:00
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2024-04-22 06:43:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/erasurecode"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2024-04-22 06:43:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
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}))
|
2024-10-21 13:27:28 +00:00
|
|
|
defer func() { require.NoError(t, db.Close(context.Background())) }()
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
raw := testutil.GenerateObject()
|
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*/
|
2023-03-20 14:10:26 +00:00
|
|
|
testutil.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
|
|
|
|
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()
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
parent := testutil.GenerateObjectWithCID(cnr)
|
|
|
|
testutil.AddAttribute(parent, "foo", "bar")
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
child := testutil.GenerateObjectWithCID(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
|
|
|
|
2024-04-22 06:43:42 +00:00
|
|
|
t.Run("put erasure-coded object", func(t *testing.T) {
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
virtual := testutil.GenerateObjectWithCID(cnr)
|
|
|
|
c, err := erasurecode.NewConstructor(3, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
parts, err := c.Split(virtual, &pk.PrivateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, part := range parts {
|
|
|
|
err = putBig(db, part)
|
|
|
|
var eiError *objectSDK.ECInfoError
|
|
|
|
if err != nil && !errors.As(err, &eiError) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = metaGet(db, object.AddressOf(virtual), true)
|
|
|
|
var eiError *objectSDK.ECInfoError
|
|
|
|
require.ErrorAs(t, err, &eiError)
|
|
|
|
require.Equal(t, len(eiError.ECInfo().Chunks), len(parts))
|
|
|
|
for _, chunk := range eiError.ECInfo().Chunks {
|
|
|
|
var found bool
|
|
|
|
for _, part := range parts {
|
|
|
|
partID, _ := part.ID()
|
|
|
|
var chunkID oid.ID
|
|
|
|
require.NoError(t, chunkID.ReadFromV2(chunk.ID))
|
|
|
|
if chunkID.Equals(partID) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
require.Fail(t, "chunk not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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()
|
2021-09-24 15:18:04 +00:00
|
|
|
|
2024-09-20 10:28:21 +00:00
|
|
|
require.NoError(t, metaInhume(db, obj, oidtest.ID()))
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err := metaGet(db, obj, false)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.True(t, client.IsErrObjectAlreadyRemoved(err))
|
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
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err = db.Inhume(context.Background(), 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)
|
2023-08-04 11:14:07 +00:00
|
|
|
require.True(t, client.IsErrObjectNotFound(err))
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
2024-10-21 13:27:28 +00:00
|
|
|
defer func() { require.NoError(b, db.Close(context.Background())) }()
|
2022-11-30 12:17:02 +00:00
|
|
|
addrs := make([]oid.Address, 0, numOfObj)
|
|
|
|
|
2024-08-30 16:20:55 +00:00
|
|
|
for range numOfObj {
|
2023-03-20 14:10:26 +00:00
|
|
|
raw := testutil.GenerateObject()
|
2022-11-30 12:17:02 +00:00
|
|
|
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++
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := db.Get(context.Background(), getPrm)
|
2022-11-15 16:17:53 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2022-05-31 11:42:41 +00:00
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
require.NoError(b, db.Close(context.Background()))
|
2022-11-30 12:17:02 +00:00
|
|
|
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()
|
2024-08-30 16:20:55 +00:00
|
|
|
for i := range b.N {
|
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
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := db.Get(context.Background(), getPrm)
|
2022-11-30 12:17:02 +00:00
|
|
|
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
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err := db.Get(context.Background(), prm)
|
2022-07-12 14:42:55 +00:00
|
|
|
return res.Header(), err
|
|
|
|
}
|