From 4449006862cafebaafc67424a2fc777e3f85006b Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Wed, 14 Jun 2023 11:00:44 +0300 Subject: [PATCH] [#424] metrics: Use mode value as metric value for shard Signed-off-by: Dmitrii Stepanov --- pkg/local_object_storage/engine/metrics.go | 3 +- pkg/local_object_storage/engine/shards.go | 6 +-- .../shard/metrics_test.go | 10 ++-- pkg/local_object_storage/shard/mode.go | 2 +- pkg/local_object_storage/shard/shard.go | 4 +- pkg/metrics/engine.go | 27 ++++------ pkg/metrics/mode.go | 39 +++++++++++++++ pkg/metrics/writecache.go | 49 ++----------------- 8 files changed, 66 insertions(+), 74 deletions(-) create mode 100644 pkg/metrics/mode.go diff --git a/pkg/local_object_storage/engine/metrics.go b/pkg/local_object_storage/engine/metrics.go index dd1240ea7..fcac2dc60 100644 --- a/pkg/local_object_storage/engine/metrics.go +++ b/pkg/local_object_storage/engine/metrics.go @@ -3,6 +3,7 @@ package engine import ( "time" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/metrics" ) @@ -12,7 +13,7 @@ type MetricRegister interface { SetObjectCounter(shardID, objectType string, v uint64) AddToObjectCounter(shardID, objectType string, delta int) - SetReadonly(shardID string, readonly bool) + SetMode(shardID string, mode mode.Mode) AddToContainerSize(cnrID string, size int64) AddToPayloadCounter(shardID string, size int64) diff --git a/pkg/local_object_storage/engine/shards.go b/pkg/local_object_storage/engine/shards.go index 5f4f13635..cfa64929b 100644 --- a/pkg/local_object_storage/engine/shards.go +++ b/pkg/local_object_storage/engine/shards.go @@ -50,8 +50,8 @@ func (m *metricsWithID) DecObjectCounter(objectType string) { m.mw.AddToObjectCounter(m.id, objectType, -1) } -func (m *metricsWithID) SetReadonly(readonly bool) { - m.mw.SetReadonly(m.id, readonly) +func (m *metricsWithID) SetMode(mode mode.Mode) { + m.mw.SetMode(m.id, mode) } func (m *metricsWithID) AddToContainerSize(cnr string, size int64) { @@ -90,7 +90,7 @@ func (e *StorageEngine) AddShard(opts ...shard.Option) (*shard.ID, error) { } if e.cfg.metrics != nil { - e.cfg.metrics.SetReadonly(sh.ID().String(), sh.GetMode() != mode.ReadWrite) + e.cfg.metrics.SetMode(sh.ID().String(), sh.GetMode()) } return sh.ID(), nil diff --git a/pkg/local_object_storage/shard/metrics_test.go b/pkg/local_object_storage/shard/metrics_test.go index f9e1bc760..d76c240b7 100644 --- a/pkg/local_object_storage/shard/metrics_test.go +++ b/pkg/local_object_storage/shard/metrics_test.go @@ -22,7 +22,7 @@ type metricsStore struct { objCounters map[string]uint64 cnrSize map[string]int64 pldSize int64 - readOnly bool + mode mode.Mode errCounter int64 } @@ -57,8 +57,8 @@ func (m metricsStore) DecObjectCounter(objectType string) { m.AddToObjectCounter(objectType, -1) } -func (m *metricsStore) SetReadonly(r bool) { - m.readOnly = r +func (m *metricsStore) SetMode(mode mode.Mode) { + m.mode = mode } func (m metricsStore) AddToContainerSize(cnr string, size int64) { @@ -91,9 +91,9 @@ func TestCounters(t *testing.T) { sh, mm := shardWithMetrics(t, dir) sh.SetMode(mode.ReadOnly) - require.True(t, mm.readOnly) + require.Equal(t, mode.ReadOnly, mm.mode) sh.SetMode(mode.ReadWrite) - require.False(t, mm.readOnly) + require.Equal(t, mode.ReadWrite, mm.mode) const objNumber = 10 oo := make([]*object.Object, objNumber) diff --git a/pkg/local_object_storage/shard/mode.go b/pkg/local_object_storage/shard/mode.go index efd41863b..a59f08704 100644 --- a/pkg/local_object_storage/shard/mode.go +++ b/pkg/local_object_storage/shard/mode.go @@ -64,7 +64,7 @@ func (s *Shard) setMode(m mode.Mode) error { s.info.Mode = m if s.metricsWriter != nil { - s.metricsWriter.SetReadonly(s.info.Mode != mode.ReadWrite) + s.metricsWriter.SetMode(s.info.Mode) } s.log.Info(logs.ShardShardModeSetSuccessfully, diff --git a/pkg/local_object_storage/shard/shard.go b/pkg/local_object_storage/shard/shard.go index 8c4db87b5..1d2cab9f2 100644 --- a/pkg/local_object_storage/shard/shard.go +++ b/pkg/local_object_storage/shard/shard.go @@ -73,8 +73,8 @@ type MetricsWriter interface { // SetShardID must set (update) the shard identifier that will be used in // metrics. SetShardID(id string) - // SetReadonly must set shard readonly state. - SetReadonly(readonly bool) + // SetReadonly must set shard mode. + SetMode(mode mode.Mode) // IncErrorCounter increment error counter. IncErrorCounter() // ClearErrorCounter clear error counter. diff --git a/pkg/metrics/engine.go b/pkg/metrics/engine.go index d3f3238ac..7eac85861 100644 --- a/pkg/metrics/engine.go +++ b/pkg/metrics/engine.go @@ -3,6 +3,7 @@ package metrics import ( "time" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode" "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" "github.com/prometheus/client_golang/prometheus" ) @@ -16,7 +17,7 @@ type EngineMetrics interface { AddToObjectCounter(shardID, objectType string, delta int) SetObjectCounter(shardID, objectType string, v uint64) AddToPayloadCounter(shardID string, size int64) - SetReadonly(shardID string, readonly bool) + SetMode(shardID string, mode mode.Mode) WriteCache() WriteCacheMetrics GC() GCMetrics @@ -28,7 +29,7 @@ type engineMetrics struct { containerSize *prometheus.GaugeVec payloadSize *prometheus.GaugeVec errorCounter *prometheus.GaugeVec - shardsReadonly *prometheus.GaugeVec + mode *shardIDModeValue gc *gcMetrics writeCache *writeCacheMetrics @@ -50,10 +51,10 @@ func newEngineMetrics() *engineMetrics { Name: "request_duration_seconds", Help: "Duration of Engine requests", }, []string{engineMethod}), - objectCounter: newEngineGaugeVector("objects_total", "Objects counters per shards", []string{shardIDLabelKey, counterTypeLabelKey}), - shardsReadonly: newEngineGaugeVector("mode", "Shard mode", []string{shardIDLabelKey}), - gc: newGCMetrics(), - writeCache: newWriteCacheMetrics(), + objectCounter: newEngineGaugeVector("objects_total", "Objects counters per shards", []string{shardIDLabelKey, counterTypeLabelKey}), + gc: newGCMetrics(), + writeCache: newWriteCacheMetrics(), + mode: newShardIDMode(engineSubsystem, "mode", "Shard mode"), } } @@ -92,7 +93,7 @@ func (m *engineMetrics) DeleteShardMetrics(shardID string) { m.errorCounter.Delete(prometheus.Labels{shardIDLabelKey: shardID}) m.payloadSize.Delete(prometheus.Labels{shardIDLabelKey: shardID}) m.objectCounter.DeletePartialMatch(prometheus.Labels{shardIDLabelKey: shardID}) - m.shardsReadonly.Delete(prometheus.Labels{shardIDLabelKey: shardID}) + m.mode.Delete(shardID) } func (m *engineMetrics) AddToObjectCounter(shardID, objectType string, delta int) { @@ -113,16 +114,8 @@ func (m *engineMetrics) SetObjectCounter(shardID, objectType string, v uint64) { ).Set(float64(v)) } -func (m *engineMetrics) SetReadonly(shardID string, readonly bool) { - var flag float64 - if readonly { - flag = 1 - } - m.shardsReadonly.With( - prometheus.Labels{ - shardIDLabelKey: shardID, - }, - ).Set(flag) +func (m *engineMetrics) SetMode(shardID string, mode mode.Mode) { + m.mode.SetMode(shardID, mode.String()) } func (m *engineMetrics) WriteCache() WriteCacheMetrics { diff --git a/pkg/metrics/mode.go b/pkg/metrics/mode.go new file mode 100644 index 000000000..7ae3fd5f9 --- /dev/null +++ b/pkg/metrics/mode.go @@ -0,0 +1,39 @@ +package metrics + +import ( + "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" + "github.com/prometheus/client_golang/prometheus" +) + +type shardIDModeValue struct { + modeValue *prometheus.GaugeVec +} + +func newShardIDMode(subsystem, name, help string) *shardIDModeValue { + return &shardIDModeValue{ + modeValue: metrics.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: subsystem, + Name: name, + Help: help, + }, []string{wcShardID, wcMode}), + } +} + +func (m *shardIDModeValue) SetMode(shardID string, mode string) { + m.modeValue.DeletePartialMatch(prometheus.Labels{ + wcShardID: shardID, + }) + + m.modeValue.With(prometheus.Labels{ + wcShardID: shardID, + wcMode: mode, + }).Set(1) +} + +func (m *shardIDModeValue) Delete(shardID string) { + m.modeValue.DeletePartialMatch(prometheus.Labels{ + wcShardID: shardID, + }) +} diff --git a/pkg/metrics/writecache.go b/pkg/metrics/writecache.go index 8498e3930..373772a62 100644 --- a/pkg/metrics/writecache.go +++ b/pkg/metrics/writecache.go @@ -2,7 +2,6 @@ package metrics import ( "fmt" - "sync" "time" "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" @@ -19,10 +18,6 @@ const ( wcOperation = "operation" ) -type shardIDMode struct { - shardID, mode string -} - type WriteCacheMetrics interface { AddMethodDuration(shardID string, method string, success bool, d time.Duration, storageType string) @@ -46,9 +41,7 @@ type writeCacheMetrics struct { estimatedSize *prometheus.GaugeVec - modeMetrics map[shardIDMode]prometheus.GaugeFunc - modeValues map[string]string - modeMtx sync.RWMutex + mode *shardIDModeValue } func newWriteCacheMetrics() *writeCacheMetrics { @@ -67,9 +60,7 @@ func newWriteCacheMetrics() *writeCacheMetrics { }, []string{wcShardID, wcStorage, wcSuccess, wcOperation}), actualCount: newWCGaugeVec("actual_objects_total", "Actual objects count in writecache", []string{wcShardID, wcStorage}), estimatedSize: newWCGaugeVec("estimated_size_bytes", "Estimated writecache size", []string{wcShardID, wcStorage}), - modeMtx: sync.RWMutex{}, - modeMetrics: make(map[shardIDMode]prometheus.GaugeFunc), - modeValues: make(map[string]string), + mode: newShardIDMode(wcSubsystem, "mode", "Writecache mode value"), } } @@ -113,39 +104,7 @@ func (m *writeCacheMetrics) SetEstimateSize(shardID string, size uint64, storage } func (m *writeCacheMetrics) SetMode(shardID string, mode string) { - m.modeMtx.Lock() - defer m.modeMtx.Unlock() - - m.modeValues[shardID] = mode - key := shardIDMode{ - shardID: shardID, - mode: mode, - } - if _, found := m.modeMetrics[key]; found { - return - } - - metric := metrics.NewGaugeFunc( - prometheus.GaugeOpts{ - Namespace: namespace, - Subsystem: wcSubsystem, - Name: "writecache_mode", - Help: "Writecache mode value", - ConstLabels: prometheus.Labels{ - wcShardID: shardID, - wcMode: mode, - }, - }, func() float64 { - m.modeMtx.RLock() - defer m.modeMtx.RUnlock() - - value := m.modeValues[shardID] - if value == mode { - return 1 - } - return 0 - }) - m.modeMetrics[key] = metric + m.mode.SetMode(shardID, mode) } func (m *writeCacheMetrics) IncOperationCounter(shardID string, operation string, success NullBool, storageType string) { @@ -158,7 +117,7 @@ func (m *writeCacheMetrics) IncOperationCounter(shardID string, operation string } func (m *writeCacheMetrics) Close(shardID string) { - m.SetMode(shardID, "CLOSED") + m.mode.Delete(shardID) m.methodDuration.DeletePartialMatch(prometheus.Labels{wcShardID: shardID}) m.operationCounter.DeletePartialMatch(prometheus.Labels{wcShardID: shardID}) m.actualCount.DeletePartialMatch(prometheus.Labels{wcShardID: shardID})