2023-08-31 12:47:06 +00:00
|
|
|
package shard
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-23 14:59:14 +00:00
|
|
|
"context"
|
2022-09-09 11:36:34 +00:00
|
|
|
"path/filepath"
|
2023-08-23 07:43:16 +00:00
|
|
|
"sync"
|
2022-09-09 11:36:34 +00:00
|
|
|
"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/mode"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
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 {
|
2023-08-23 07:43:16 +00:00
|
|
|
mtx sync.Mutex
|
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
|
2023-06-14 08:00:44 +00:00
|
|
|
mode mode.Mode
|
2023-06-01 14:28:04 +00:00
|
|
|
errCounter int64
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 08:43:35 +00:00
|
|
|
func (m *metricsStore) SetShardID(_ string) {}
|
2022-10-12 17:55:35 +00:00
|
|
|
|
2023-08-25 08:43:35 +00:00
|
|
|
func (m *metricsStore) SetObjectCounter(objectType string, v uint64) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2022-12-01 11:59:22 +00:00
|
|
|
m.objCounters[objectType] = v
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 12:24:31 +00:00
|
|
|
func (m *metricsStore) getObjectCounter(objectType string) uint64 {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
return m.objCounters[objectType]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsStore) containerSizes() map[string]int64 {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
r := make(map[string]int64, len(m.cnrSize))
|
|
|
|
for c, s := range m.cnrSize {
|
|
|
|
r[c] = s
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsStore) payloadSize() int64 {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
return m.pldSize
|
|
|
|
}
|
|
|
|
|
2023-08-25 08:43:35 +00:00
|
|
|
func (m *metricsStore) AddToObjectCounter(objectType string, delta int) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2022-09-09 11:36:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 07:43:16 +00:00
|
|
|
func (m *metricsStore) IncObjectCounter(objectType string) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2022-12-01 11:59:22 +00:00
|
|
|
m.objCounters[objectType] += 1
|
2022-09-09 11:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 07:43:16 +00:00
|
|
|
func (m *metricsStore) DecObjectCounter(objectType string) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2022-09-09 11:36:34 +00:00
|
|
|
m.AddToObjectCounter(objectType, -1)
|
|
|
|
}
|
|
|
|
|
2023-06-14 08:00:44 +00:00
|
|
|
func (m *metricsStore) SetMode(mode mode.Mode) {
|
2023-08-23 07:43:16 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2023-06-14 08:00:44 +00:00
|
|
|
m.mode = mode
|
2022-12-01 11:59:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 07:43:16 +00:00
|
|
|
func (m *metricsStore) AddToContainerSize(cnr string, size int64) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2022-12-01 11:59:22 +00:00
|
|
|
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) {
|
2023-08-23 07:43:16 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2023-01-25 14:01:25 +00:00
|
|
|
m.pldSize += size
|
|
|
|
}
|
|
|
|
|
2023-06-01 14:28:04 +00:00
|
|
|
func (m *metricsStore) IncErrorCounter() {
|
2023-08-23 07:43:16 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2023-06-01 14:28:04 +00:00
|
|
|
m.errCounter += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsStore) ClearErrorCounter() {
|
2023-08-23 07:43:16 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2023-06-01 14:28:04 +00:00
|
|
|
m.errCounter = 0
|
|
|
|
}
|
|
|
|
|
2023-06-13 16:48:15 +00:00
|
|
|
func (m *metricsStore) DeleteShardMetrics() {
|
2023-08-23 07:43:16 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2023-06-01 14:28:04 +00:00
|
|
|
m.errCounter = 0
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
func TestCounters(t *testing.T) {
|
2023-05-05 11:08:10 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
dir := t.TempDir()
|
|
|
|
sh, mm := shardWithMetrics(t, dir)
|
|
|
|
|
2022-12-09 13:52:13 +00:00
|
|
|
sh.SetMode(mode.ReadOnly)
|
2023-06-14 08:00:44 +00:00
|
|
|
require.Equal(t, mode.ReadOnly, mm.mode)
|
2022-12-09 13:52:13 +00:00
|
|
|
sh.SetMode(mode.ReadWrite)
|
2023-06-14 08:00:44 +00:00
|
|
|
require.Equal(t, mode.ReadWrite, mm.mode)
|
2022-12-09 13:52:13 +00:00
|
|
|
|
2022-09-09 11:36:34 +00:00
|
|
|
const objNumber = 10
|
2023-07-06 12:36:41 +00:00
|
|
|
oo := make([]*objectSDK.Object, objNumber)
|
2022-09-09 11:36:34 +00:00
|
|
|
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) {
|
2023-08-30 12:24:31 +00:00
|
|
|
require.Zero(t, mm.getObjectCounter(physical))
|
|
|
|
require.Zero(t, mm.getObjectCounter(logical))
|
|
|
|
require.Empty(t, mm.containerSizes())
|
|
|
|
require.Zero(t, mm.payloadSize())
|
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
|
|
|
}
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var prm PutPrm
|
2022-09-09 11:36:34 +00:00
|
|
|
|
2023-08-30 15:25:56 +00:00
|
|
|
for i := 0; i < objNumber; i++ {
|
|
|
|
prm.SetObject(oo[i])
|
2022-09-09 11:36:34 +00:00
|
|
|
|
2023-08-30 15:25:56 +00:00
|
|
|
_, err := sh.Put(context.Background(), prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-09-09 11:36:34 +00:00
|
|
|
|
2023-08-30 15:25:56 +00:00
|
|
|
require.Equal(t, uint64(objNumber), mm.getObjectCounter(physical))
|
|
|
|
require.Equal(t, uint64(objNumber), mm.getObjectCounter(logical))
|
|
|
|
require.Equal(t, expectedSizes, mm.containerSizes())
|
|
|
|
require.Equal(t, totalPayload, mm.payloadSize())
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
t.Run("inhume_GC", func(t *testing.T) {
|
2023-08-31 12:47:06 +00:00
|
|
|
var prm InhumePrm
|
2022-09-09 11:36:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-30 12:24:31 +00:00
|
|
|
require.Equal(t, uint64(objNumber), mm.getObjectCounter(physical))
|
|
|
|
require.Equal(t, uint64(objNumber-inhumedNumber), mm.getObjectCounter(logical))
|
|
|
|
require.Equal(t, expectedSizes, mm.containerSizes())
|
|
|
|
require.Equal(t, totalPayload, mm.payloadSize())
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
oo = oo[inhumedNumber:]
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inhume_TS", func(t *testing.T) {
|
2023-08-31 12:47:06 +00:00
|
|
|
var prm InhumePrm
|
2023-03-20 14:10:26 +00:00
|
|
|
ts := objectcore.AddressOf(testutil.GenerateObject())
|
2022-09-09 11:36:34 +00:00
|
|
|
|
2023-08-30 12:24:31 +00:00
|
|
|
phy := mm.getObjectCounter(physical)
|
|
|
|
logic := mm.getObjectCounter(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)
|
|
|
|
|
2023-08-30 12:24:31 +00:00
|
|
|
require.Equal(t, phy, mm.getObjectCounter(physical))
|
|
|
|
require.Equal(t, logic-uint64(inhumedNumber), mm.getObjectCounter(logical))
|
|
|
|
require.Equal(t, expectedSizes, mm.containerSizes())
|
|
|
|
require.Equal(t, totalPayload, mm.payloadSize())
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
oo = oo[inhumedNumber:]
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
2023-08-31 12:47:06 +00:00
|
|
|
var prm DeletePrm
|
2022-09-09 11:36:34 +00:00
|
|
|
|
2023-08-30 12:24:31 +00:00
|
|
|
phy := mm.getObjectCounter(physical)
|
|
|
|
logic := mm.getObjectCounter(logical)
|
2022-09-09 11:36:34 +00:00
|
|
|
|
|
|
|
deletedNumber := int(phy / 4)
|
|
|
|
prm.SetAddresses(addrFromObjs(oo[:deletedNumber])...)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Delete(context.Background(), prm)
|
2022-09-09 11:36:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-30 12:24:31 +00:00
|
|
|
require.Equal(t, phy-uint64(deletedNumber), mm.getObjectCounter(physical))
|
|
|
|
require.Equal(t, logic-uint64(deletedNumber), mm.getObjectCounter(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
|
|
|
}
|
2023-08-30 12:24:31 +00:00
|
|
|
require.Equal(t, expectedSizes, mm.containerSizes())
|
|
|
|
require.Equal(t, totalPayload-int64(totalRemovedpayload), mm.payloadSize())
|
2022-09-09 11:36:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
func shardWithMetrics(t *testing.T, path string) (*Shard, *metricsStore) {
|
2022-09-09 11:36:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
sh := New(
|
|
|
|
WithID(NewIDFromBytes([]byte{})),
|
|
|
|
WithBlobStorOptions(blobOpts...),
|
|
|
|
WithPiloramaOptions(pilorama.WithPath(filepath.Join(path, "pilorama"))),
|
|
|
|
WithMetaBaseOptions(
|
2022-09-09 11:36:34 +00:00
|
|
|
meta.WithPath(filepath.Join(path, "meta")),
|
|
|
|
meta.WithEpochState(epochState{})),
|
2023-08-31 12:47:06 +00:00
|
|
|
WithMetricsWriter(mm),
|
2023-10-31 11:21:42 +00:00
|
|
|
WithGCRemoverSleepInterval(time.Hour),
|
2022-09-09 11:36:34 +00:00
|
|
|
)
|
2023-08-31 16:26:47 +00:00
|
|
|
require.NoError(t, sh.Open(context.Background()))
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func addrFromObjs(oo []*objectSDK.Object) []oid.Address {
|
2022-09-09 11:36:34 +00:00
|
|
|
aa := make([]oid.Address, len(oo))
|
|
|
|
|
|
|
|
for i := 0; i < len(oo); i++ {
|
|
|
|
aa[i] = objectcore.AddressOf(oo[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return aa
|
|
|
|
}
|