[#xx] shard: Fix data race in metrics tests

Protect test metric store fields with a mutex. Probably, not every field
should be protected, but better safe than sorry.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-08-23 10:43:16 +03:00 committed by Evgenii Stratonikov
parent 238b8f10a0
commit 82cc453be9

View file

@ -3,6 +3,7 @@ package shard_test
import ( import (
"context" "context"
"path/filepath" "path/filepath"
"sync"
"testing" "testing"
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
@ -19,6 +20,7 @@ import (
) )
type metricsStore struct { type metricsStore struct {
mtx sync.Mutex
objCounters map[string]uint64 objCounters map[string]uint64
cnrSize map[string]int64 cnrSize map[string]int64
pldSize int64 pldSize int64
@ -49,35 +51,51 @@ func (m metricsStore) AddToObjectCounter(objectType string, delta int) {
} }
} }
func (m metricsStore) IncObjectCounter(objectType string) { func (m *metricsStore) IncObjectCounter(objectType string) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.objCounters[objectType] += 1 m.objCounters[objectType] += 1
} }
func (m metricsStore) DecObjectCounter(objectType string) { func (m *metricsStore) DecObjectCounter(objectType string) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.AddToObjectCounter(objectType, -1) m.AddToObjectCounter(objectType, -1)
} }
func (m *metricsStore) SetMode(mode mode.Mode) { func (m *metricsStore) SetMode(mode mode.Mode) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.mode = mode m.mode = mode
} }
func (m metricsStore) AddToContainerSize(cnr string, size int64) { func (m *metricsStore) AddToContainerSize(cnr string, size int64) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.cnrSize[cnr] += size m.cnrSize[cnr] += size
} }
func (m *metricsStore) AddToPayloadSize(size int64) { func (m *metricsStore) AddToPayloadSize(size int64) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.pldSize += size m.pldSize += size
} }
func (m *metricsStore) IncErrorCounter() { func (m *metricsStore) IncErrorCounter() {
m.mtx.Lock()
defer m.mtx.Unlock()
m.errCounter += 1 m.errCounter += 1
} }
func (m *metricsStore) ClearErrorCounter() { func (m *metricsStore) ClearErrorCounter() {
m.mtx.Lock()
defer m.mtx.Unlock()
m.errCounter = 0 m.errCounter = 0
} }
func (m *metricsStore) DeleteShardMetrics() { func (m *metricsStore) DeleteShardMetrics() {
m.mtx.Lock()
defer m.mtx.Unlock()
m.errCounter = 0 m.errCounter = 0
} }