[#666] shard/test: Fix data race in metrics tests
Some checks failed
DCO action / DCO (pull_request) Successful in 4m48s
Vulncheck / Vulncheck (pull_request) Successful in 6m29s
Build / Build Components (1.21) (pull_request) Successful in 9m23s
Build / Build Components (1.20) (pull_request) Successful in 9m53s
Tests and linters / Tests (1.20) (pull_request) Failing after 10m55s
Tests and linters / Tests with -race (pull_request) Failing after 10m52s
Tests and linters / Staticcheck (pull_request) Successful in 11m10s
Tests and linters / Lint (pull_request) Successful in 11m44s
Tests and linters / Tests (1.21) (pull_request) Successful in 12m38s

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-08-30 15:24:31 +03:00
parent 91f3745b58
commit 9a1cdad66f

View file

@ -36,6 +36,29 @@ func (m *metricsStore) SetObjectCounter(objectType string, v uint64) {
m.objCounters[objectType] = v m.objCounters[objectType] = v
} }
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
}
func (m *metricsStore) AddToObjectCounter(objectType string, delta int) { func (m *metricsStore) AddToObjectCounter(objectType string, delta int) {
m.mtx.Lock() m.mtx.Lock()
defer m.mtx.Unlock() defer m.mtx.Unlock()
@ -124,10 +147,10 @@ func TestCounters(t *testing.T) {
} }
t.Run("defaults", func(t *testing.T) { t.Run("defaults", func(t *testing.T) {
require.Zero(t, mm.objCounters[physical]) require.Zero(t, mm.getObjectCounter(physical))
require.Zero(t, mm.objCounters[logical]) require.Zero(t, mm.getObjectCounter(logical))
require.Empty(t, mm.cnrSize) require.Empty(t, mm.containerSizes())
require.Zero(t, mm.pldSize) require.Zero(t, mm.payloadSize())
}) })
var totalPayload int64 var totalPayload int64
@ -150,10 +173,10 @@ func TestCounters(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
require.Equal(t, uint64(objNumber), mm.objCounters[physical]) require.Equal(t, uint64(objNumber), mm.getObjectCounter(physical))
require.Equal(t, uint64(objNumber), mm.objCounters[logical]) require.Equal(t, uint64(objNumber), mm.getObjectCounter(logical))
require.Equal(t, expectedSizes, mm.cnrSize) require.Equal(t, expectedSizes, mm.containerSizes())
require.Equal(t, totalPayload, mm.pldSize) require.Equal(t, totalPayload, mm.payloadSize())
}) })
t.Run("inhume_GC", func(t *testing.T) { t.Run("inhume_GC", func(t *testing.T) {
@ -167,10 +190,10 @@ func TestCounters(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
require.Equal(t, uint64(objNumber), mm.objCounters[physical]) require.Equal(t, uint64(objNumber), mm.getObjectCounter(physical))
require.Equal(t, uint64(objNumber-inhumedNumber), mm.objCounters[logical]) require.Equal(t, uint64(objNumber-inhumedNumber), mm.getObjectCounter(logical))
require.Equal(t, expectedSizes, mm.cnrSize) require.Equal(t, expectedSizes, mm.containerSizes())
require.Equal(t, totalPayload, mm.pldSize) require.Equal(t, totalPayload, mm.payloadSize())
oo = oo[inhumedNumber:] oo = oo[inhumedNumber:]
}) })
@ -179,8 +202,8 @@ func TestCounters(t *testing.T) {
var prm shard.InhumePrm var prm shard.InhumePrm
ts := objectcore.AddressOf(testutil.GenerateObject()) ts := objectcore.AddressOf(testutil.GenerateObject())
phy := mm.objCounters[physical] phy := mm.getObjectCounter(physical)
logic := mm.objCounters[logical] logic := mm.getObjectCounter(logical)
inhumedNumber := int(phy / 4) inhumedNumber := int(phy / 4)
prm.SetTarget(ts, addrFromObjs(oo[:inhumedNumber])...) prm.SetTarget(ts, addrFromObjs(oo[:inhumedNumber])...)
@ -188,10 +211,10 @@ func TestCounters(t *testing.T) {
_, err := sh.Inhume(context.Background(), prm) _, err := sh.Inhume(context.Background(), prm)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, phy, mm.objCounters[physical]) require.Equal(t, phy, mm.getObjectCounter(physical))
require.Equal(t, logic-uint64(inhumedNumber), mm.objCounters[logical]) require.Equal(t, logic-uint64(inhumedNumber), mm.getObjectCounter(logical))
require.Equal(t, expectedSizes, mm.cnrSize) require.Equal(t, expectedSizes, mm.containerSizes())
require.Equal(t, totalPayload, mm.pldSize) require.Equal(t, totalPayload, mm.payloadSize())
oo = oo[inhumedNumber:] oo = oo[inhumedNumber:]
}) })
@ -199,8 +222,8 @@ func TestCounters(t *testing.T) {
t.Run("Delete", func(t *testing.T) { t.Run("Delete", func(t *testing.T) {
var prm shard.DeletePrm var prm shard.DeletePrm
phy := mm.objCounters[physical] phy := mm.getObjectCounter(physical)
logic := mm.objCounters[logical] logic := mm.getObjectCounter(logical)
deletedNumber := int(phy / 4) deletedNumber := int(phy / 4)
prm.SetAddresses(addrFromObjs(oo[:deletedNumber])...) prm.SetAddresses(addrFromObjs(oo[:deletedNumber])...)
@ -208,8 +231,8 @@ func TestCounters(t *testing.T) {
_, err := sh.Delete(context.Background(), prm) _, err := sh.Delete(context.Background(), prm)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, phy-uint64(deletedNumber), mm.objCounters[physical]) require.Equal(t, phy-uint64(deletedNumber), mm.getObjectCounter(physical))
require.Equal(t, logic-uint64(deletedNumber), mm.objCounters[logical]) require.Equal(t, logic-uint64(deletedNumber), mm.getObjectCounter(logical))
var totalRemovedpayload uint64 var totalRemovedpayload uint64
for i := range oo[:deletedNumber] { for i := range oo[:deletedNumber] {
removedPayload := oo[i].PayloadSize() removedPayload := oo[i].PayloadSize()
@ -218,8 +241,8 @@ func TestCounters(t *testing.T) {
cnr, _ := oo[i].ContainerID() cnr, _ := oo[i].ContainerID()
expectedSizes[cnr.EncodeToString()] -= int64(removedPayload) expectedSizes[cnr.EncodeToString()] -= int64(removedPayload)
} }
require.Equal(t, expectedSizes, mm.cnrSize) require.Equal(t, expectedSizes, mm.containerSizes())
require.Equal(t, totalPayload-int64(totalRemovedpayload), mm.pldSize) require.Equal(t, totalPayload-int64(totalRemovedpayload), mm.payloadSize())
}) })
} }