From 82cc453be9cdca1da43424b0b45676bd7fa3a3f5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 23 Aug 2023 10:43:16 +0300 Subject: [PATCH] [#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 --- .../shard/metrics_test.go | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/local_object_storage/shard/metrics_test.go b/pkg/local_object_storage/shard/metrics_test.go index ad0fc15ca..60adc0724 100644 --- a/pkg/local_object_storage/shard/metrics_test.go +++ b/pkg/local_object_storage/shard/metrics_test.go @@ -3,6 +3,7 @@ package shard_test import ( "context" "path/filepath" + "sync" "testing" objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" @@ -19,6 +20,7 @@ import ( ) type metricsStore struct { + mtx sync.Mutex objCounters map[string]uint64 cnrSize map[string]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 } -func (m metricsStore) DecObjectCounter(objectType string) { +func (m *metricsStore) DecObjectCounter(objectType string) { + m.mtx.Lock() + defer m.mtx.Unlock() m.AddToObjectCounter(objectType, -1) } func (m *metricsStore) SetMode(mode mode.Mode) { + m.mtx.Lock() + defer m.mtx.Unlock() 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 } func (m *metricsStore) AddToPayloadSize(size int64) { + m.mtx.Lock() + defer m.mtx.Unlock() m.pldSize += size } func (m *metricsStore) IncErrorCounter() { + m.mtx.Lock() + defer m.mtx.Unlock() m.errCounter += 1 } func (m *metricsStore) ClearErrorCounter() { + m.mtx.Lock() + defer m.mtx.Unlock() m.errCounter = 0 } func (m *metricsStore) DeleteShardMetrics() { + m.mtx.Lock() + defer m.mtx.Unlock() m.errCounter = 0 }