2020-12-03 11:29:33 +00:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
2023-06-06 09:27:19 +00:00
|
|
|
"context"
|
2021-01-22 11:07:08 +00:00
|
|
|
"math/rand"
|
2024-02-19 14:02:56 +00:00
|
|
|
"sort"
|
2020-12-03 11:29:33 +00:00
|
|
|
"testing"
|
|
|
|
|
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
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2020-12-03 11:29:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDB_Containers(t *testing.T) {
|
2023-05-05 14:02:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-12-03 11:29:33 +00:00
|
|
|
db := newDB(t)
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, db.Close()) }()
|
2020-12-03 11:29:33 +00:00
|
|
|
|
|
|
|
const N = 10
|
|
|
|
|
|
|
|
cids := make(map[string]int, N)
|
|
|
|
|
|
|
|
for i := 0; i < N; i++ {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
2020-12-03 11:29:33 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, _ := obj.ContainerID()
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cids[cnr.EncodeToString()] = 0
|
2020-12-03 11:29:33 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, obj)
|
2020-12-03 11:29:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
lst, err := db.Containers(context.Background())
|
2020-12-03 11:29:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
for _, cnr := range lst {
|
|
|
|
i, ok := cids[cnr.EncodeToString()]
|
2020-12-03 11:29:33 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 0, i)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cids[cnr.EncodeToString()] = 1
|
2020-12-03 11:29:33 +00:00
|
|
|
}
|
2020-12-17 08:07:51 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
// require.Contains not working since cnrs is a ptr slice
|
2022-05-31 17:00:41 +00:00
|
|
|
assertContains := func(cnrs []cid.ID, cnr cid.ID) {
|
2022-05-12 16:37:46 +00:00
|
|
|
found := false
|
|
|
|
for i := 0; !found && i < len(cnrs); i++ {
|
|
|
|
found = cnrs[i].Equals(cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, found)
|
|
|
|
}
|
|
|
|
|
2020-12-17 08:07:51 +00:00
|
|
|
t.Run("Inhume", func(t *testing.T) {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
2020-12-17 08:07:51 +00:00
|
|
|
|
|
|
|
require.NoError(t, putBig(db, obj))
|
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
cnrs, err := db.Containers(context.Background())
|
2020-12-17 08:07:51 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, _ := obj.ContainerID()
|
|
|
|
|
|
|
|
assertContains(cnrs, cnr)
|
2020-12-17 08:07:51 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
require.NoError(t, metaInhume(db, object.AddressOf(obj), oidtest.Address()))
|
2020-12-17 08:07:51 +00:00
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
cnrs, err = db.Containers(context.Background())
|
2020-12-17 08:07:51 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-12 16:37:46 +00:00
|
|
|
assertContains(cnrs, cnr)
|
2020-12-17 08:07:51 +00:00
|
|
|
})
|
2020-12-03 11:29:33 +00:00
|
|
|
}
|
2021-01-22 11:07:08 +00:00
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
func TestDB_ContainersCount(t *testing.T) {
|
2023-05-05 14:02:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
db := newDB(t)
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, db.Close()) }()
|
2021-10-27 10:52:12 +00:00
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
const R, T, SG, L = 10, 11, 12, 13 // amount of object per type
|
2021-10-27 10:52:12 +00:00
|
|
|
|
|
|
|
uploadObjects := [...]struct {
|
|
|
|
amount int
|
|
|
|
typ objectSDK.Type
|
|
|
|
}{
|
|
|
|
{R, objectSDK.TypeRegular},
|
|
|
|
{T, objectSDK.TypeTombstone},
|
2022-02-15 12:51:56 +00:00
|
|
|
{L, objectSDK.TypeLock},
|
2021-10-27 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
expected := make([]cid.ID, 0, R+T+SG+L)
|
2021-10-27 10:52:12 +00:00
|
|
|
|
|
|
|
for _, upload := range uploadObjects {
|
|
|
|
for i := 0; i < upload.amount; i++ {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
2021-10-27 10:52:12 +00:00
|
|
|
obj.SetType(upload.typ)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, obj)
|
2021-10-27 10:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, _ := obj.ContainerID()
|
2022-05-31 17:00:41 +00:00
|
|
|
expected = append(expected, cnr)
|
2021-10-27 10:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 14:02:56 +00:00
|
|
|
sort.Slice(expected, func(i, j int) bool {
|
|
|
|
return expected[i].EncodeToString() < expected[j].EncodeToString()
|
|
|
|
})
|
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
got, err := db.Containers(context.Background())
|
2021-10-27 10:52:12 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-19 14:02:56 +00:00
|
|
|
|
|
|
|
sort.Slice(got, func(i, j int) bool {
|
|
|
|
return got[i].EncodeToString() < got[j].EncodeToString()
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, expected, got)
|
2021-10-27 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:08 +00:00
|
|
|
func TestDB_ContainerSize(t *testing.T) {
|
2023-05-05 14:02:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-01-22 11:07:08 +00:00
|
|
|
db := newDB(t)
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, db.Close()) }()
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
C = 3
|
|
|
|
N = 5
|
|
|
|
)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cids := make(map[cid.ID]int, C)
|
|
|
|
objs := make(map[cid.ID][]*objectSDK.Object, C*N)
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
for i := 0; i < C; i++ {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
|
|
|
cids[cnr] = 0
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
for j := 0; j < N; j++ {
|
|
|
|
size := rand.Intn(1024)
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
parent := testutil.GenerateObjectWithCID(cnr)
|
2021-01-22 11:07:08 +00:00
|
|
|
parent.SetPayloadSize(uint64(size / 2))
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cnr)
|
2021-01-22 11:07:08 +00:00
|
|
|
obj.SetPayloadSize(uint64(size))
|
2022-05-12 16:37:46 +00:00
|
|
|
idParent, _ := parent.ID()
|
|
|
|
obj.SetParentID(idParent)
|
2022-03-03 14:19:05 +00:00
|
|
|
obj.SetParent(parent)
|
2021-01-22 11:07:08 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cids[cnr] += size
|
|
|
|
objs[cnr] = append(objs[cnr], obj)
|
2021-01-22 11:07:08 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, obj)
|
2021-01-22 11:07:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
for cnr, volume := range cids {
|
2022-05-31 17:00:41 +00:00
|
|
|
n, err := db.ContainerSize(cnr)
|
2021-01-22 11:07:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, volume, int(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Inhume", func(t *testing.T) {
|
2022-05-12 16:37:46 +00:00
|
|
|
for cnr, list := range objs {
|
|
|
|
volume := cids[cnr]
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
for _, obj := range list {
|
2022-07-12 14:42:55 +00:00
|
|
|
require.NoError(t, metaInhume(
|
2021-01-22 11:07:08 +00:00
|
|
|
db,
|
2022-03-03 14:19:05 +00:00
|
|
|
object.AddressOf(obj),
|
2022-05-31 17:00:41 +00:00
|
|
|
oidtest.Address(),
|
2021-01-22 11:07:08 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
volume -= int(obj.PayloadSize())
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
n, err := db.ContainerSize(cnr)
|
2021-01-22 11:07:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, volume, int(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|