2022-09-09 11:36:34 +00:00
|
|
|
package shard_test
|
|
|
|
|
|
|
|
import (
|
2023-03-23 14:59:14 +00:00
|
|
|
"context"
|
2022-09-09 11:36:34 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-09-09 11:36:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type metricsStore struct {
|
2022-12-01 11:59:22 +00:00
|
|
|
objCounters map[string]uint64
|
|
|
|
cnrSize map[string]int64
|
2023-01-25 14:01:25 +00:00
|
|
|
pldSize int64
|
2022-12-01 11:59:22 +00:00
|
|
|
readOnly bool
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:55:35 +00:00
|
|
|
func (m metricsStore) SetShardID(_ string) {}
|
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
func (m metricsStore) SetObjectCounter(objectType string, v uint64) {
|
2022-12-01 11:59:22 +00:00
|
|
|
m.objCounters[objectType] = v
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m metricsStore) AddToObjectCounter(objectType string, delta int) {
|
|
|
|
switch {
|
|
|
|
case delta > 0:
|
2022-12-01 11:59:22 +00:00
|
|
|
m.objCounters[objectType] += uint64(delta)
|
2022-09-09 11:36:34 +00:00
|
|
|
case delta < 0:
|
|
|
|
uDelta := uint64(-delta)
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
if m.objCounters[objectType] >= uDelta {
|
|
|
|
m.objCounters[objectType] -= uDelta
|
2022-09-09 11:36:34 +00:00
|
|
|
} else {
|
2022-12-01 11:59:22 +00:00
|
|
|
m.objCounters[objectType] = 0
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
case delta == 0:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metricsStore) IncObjectCounter(objectType string) {
|
2022-12-01 11:59:22 +00:00
|
|
|
m.objCounters[objectType] += 1
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m metricsStore) DecObjectCounter(objectType string) {
|
|
|
|
m.AddToObjectCounter(objectType, -1)
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
func (m *metricsStore) SetReadonly(r bool) {
|
|
|
|
m.readOnly = r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metricsStore) AddToContainerSize(cnr string, size int64) {
|
|
|
|
m.cnrSize[cnr] += size
|
2022-12-09 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 14:01:25 +00:00
|
|
|
func (m *metricsStore) AddToPayloadSize(size int64) {
|
|
|
|
m.pldSize += size
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
const physical = "phy"
|
|
|
|
const logical = "logic"
|
|
|
|
|
|
|
|
func TestCounters(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
sh, mm := shardWithMetrics(t, dir)
|
|
|
|
|
2022-12-09 13:52:13 +00:00
|
|
|
sh.SetMode(mode.ReadOnly)
|
2022-12-01 11:59:22 +00:00
|
|
|
require.True(t, mm.readOnly)
|
2022-12-09 13:52:13 +00:00
|
|
|
sh.SetMode(mode.ReadWrite)
|
2022-12-01 11:59:22 +00:00
|
|
|
require.False(t, mm.readOnly)
|
2022-12-09 13:52:13 +00:00
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
const objNumber = 10
|
|
|
|
oo := make([]*object.Object, objNumber)
|
|
|
|
for i := 0; i < objNumber; i++ {
|
2023-03-20 14:10:26 +00:00
|
|
|
oo[i] = testutil.GenerateObject()
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("defaults", func(t *testing.T) {
|
2022-12-01 11:59:22 +00:00
|
|
|
require.Zero(t, mm.objCounters[physical])
|
|
|
|
require.Zero(t, mm.objCounters[logical])
|
|
|
|
require.Empty(t, mm.cnrSize)
|
2023-01-25 14:01:25 +00:00
|
|
|
require.Zero(t, mm.pldSize)
|
2022-09-09 11:36:34 +00:00
|
|
|
})
|
|
|
|
|
2023-01-25 14:01:25 +00:00
|
|
|
var totalPayload int64
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
expectedSizes := make(map[string]int64)
|
|
|
|
for i := range oo {
|
|
|
|
cnr, _ := oo[i].ContainerID()
|
2023-01-25 14:01:25 +00:00
|
|
|
oSize := int64(oo[i].PayloadSize())
|
|
|
|
expectedSizes[cnr.EncodeToString()] += oSize
|
|
|
|
totalPayload += oSize
|
2022-12-01 11:59:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
t.Run("put", func(t *testing.T) {
|
|
|
|
var prm shard.PutPrm
|
|
|
|
|
|
|
|
for i := 0; i < objNumber; i++ {
|
|
|
|
prm.SetObject(oo[i])
|
|
|
|
|
|
|
|
_, err := sh.Put(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
require.Equal(t, uint64(objNumber), mm.objCounters[physical])
|
|
|
|
require.Equal(t, uint64(objNumber), mm.objCounters[logical])
|
|
|
|
require.Equal(t, expectedSizes, mm.cnrSize)
|
2023-01-25 14:01:25 +00:00
|
|
|
require.Equal(t, totalPayload, mm.pldSize)
|
2022-09-09 11:36:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inhume_GC", func(t *testing.T) {
|
|
|
|
var prm shard.InhumePrm
|
|
|
|
inhumedNumber := objNumber / 4
|
|
|
|
|
|
|
|
for i := 0; i < inhumedNumber; i++ {
|
|
|
|
prm.MarkAsGarbage(objectcore.AddressOf(oo[i]))
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err := sh.Inhume(context.Background(), prm)
|
2022-09-09 11:36:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
require.Equal(t, uint64(objNumber), mm.objCounters[physical])
|
|
|
|
require.Equal(t, uint64(objNumber-inhumedNumber), mm.objCounters[logical])
|
|
|
|
require.Equal(t, expectedSizes, mm.cnrSize)
|
2023-01-25 14:01:25 +00:00
|
|
|
require.Equal(t, totalPayload, mm.pldSize)
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
oo = oo[inhumedNumber:]
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inhume_TS", func(t *testing.T) {
|
|
|
|
var prm shard.InhumePrm
|
2023-03-20 14:10:26 +00:00
|
|
|
ts := objectcore.AddressOf(testutil.GenerateObject())
|
2022-09-09 11:36:34 +00:00
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
phy := mm.objCounters[physical]
|
|
|
|
logic := mm.objCounters[logical]
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
inhumedNumber := int(phy / 4)
|
|
|
|
prm.SetTarget(ts, addrFromObjs(oo[:inhumedNumber])...)
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
_, err := sh.Inhume(context.Background(), prm)
|
2022-09-09 11:36:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
require.Equal(t, phy, mm.objCounters[physical])
|
|
|
|
require.Equal(t, logic-uint64(inhumedNumber), mm.objCounters[logical])
|
|
|
|
require.Equal(t, expectedSizes, mm.cnrSize)
|
2023-01-25 14:01:25 +00:00
|
|
|
require.Equal(t, totalPayload, mm.pldSize)
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
oo = oo[inhumedNumber:]
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
var prm shard.DeletePrm
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
phy := mm.objCounters[physical]
|
|
|
|
logic := mm.objCounters[logical]
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
deletedNumber := int(phy / 4)
|
|
|
|
prm.SetAddresses(addrFromObjs(oo[:deletedNumber])...)
|
|
|
|
|
|
|
|
_, err := sh.Delete(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
require.Equal(t, phy-uint64(deletedNumber), mm.objCounters[physical])
|
|
|
|
require.Equal(t, logic-uint64(deletedNumber), mm.objCounters[logical])
|
2023-01-25 14:01:25 +00:00
|
|
|
var totalRemovedpayload uint64
|
2022-12-01 11:59:22 +00:00
|
|
|
for i := range oo[:deletedNumber] {
|
2023-01-25 14:01:25 +00:00
|
|
|
removedPayload := oo[i].PayloadSize()
|
|
|
|
totalRemovedpayload += removedPayload
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
cnr, _ := oo[i].ContainerID()
|
2023-01-25 14:01:25 +00:00
|
|
|
expectedSizes[cnr.EncodeToString()] -= int64(removedPayload)
|
2022-12-01 11:59:22 +00:00
|
|
|
}
|
|
|
|
require.Equal(t, expectedSizes, mm.cnrSize)
|
2023-01-25 14:01:25 +00:00
|
|
|
require.Equal(t, totalPayload-int64(totalRemovedpayload), mm.pldSize)
|
2022-09-09 11:36:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func shardWithMetrics(t *testing.T, path string) (*shard.Shard, *metricsStore) {
|
|
|
|
blobOpts := []blobstor.Option{
|
|
|
|
blobstor.WithStorages([]blobstor.SubStorage{
|
|
|
|
{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithDirNameLen(2),
|
|
|
|
fstree.WithPath(filepath.Join(path, "blob")),
|
|
|
|
fstree.WithDepth(1)),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
mm := &metricsStore{
|
2022-12-01 11:59:22 +00:00
|
|
|
objCounters: map[string]uint64{
|
|
|
|
"phy": 0,
|
|
|
|
"logic": 0,
|
2022-09-09 11:36:34 +00:00
|
|
|
},
|
2022-12-01 11:59:22 +00:00
|
|
|
cnrSize: make(map[string]int64),
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sh := shard.New(
|
|
|
|
shard.WithBlobStorOptions(blobOpts...),
|
|
|
|
shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(path, "pilorama"))),
|
|
|
|
shard.WithMetaBaseOptions(
|
|
|
|
meta.WithPath(filepath.Join(path, "meta")),
|
|
|
|
meta.WithEpochState(epochState{})),
|
|
|
|
shard.WithMetricsWriter(mm),
|
|
|
|
)
|
|
|
|
require.NoError(t, sh.Open())
|
2023-03-23 14:59:14 +00:00
|
|
|
require.NoError(t, sh.Init(context.Background()))
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
sh.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
return sh, mm
|
|
|
|
}
|
|
|
|
|
|
|
|
func addrFromObjs(oo []*object.Object) []oid.Address {
|
|
|
|
aa := make([]oid.Address, len(oo))
|
|
|
|
|
|
|
|
for i := 0; i < len(oo); i++ {
|
|
|
|
aa[i] = objectcore.AddressOf(oo[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return aa
|
|
|
|
}
|