Provide default metrics implementations #1411

Merged
dstepanov-yadro merged 3 commits from fyrchik/frostfs-node:fix-metrics into master 2024-10-03 08:23:07 +00:00
15 changed files with 166 additions and 136 deletions

View file

@ -68,9 +68,7 @@ func ContainerSize(e *StorageEngine, id cid.ID) (uint64, error) {
}
func (e *StorageEngine) containerSize(prm ContainerSizePrm) (res ContainerSizeRes, err error) {
if e.metrics != nil {
defer elapsed("EstimateContainerSize", e.metrics.AddMethodDuration)()
}
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
var csPrm shard.ContainerSizePrm
@ -116,9 +114,7 @@ func ListContainers(ctx context.Context, e *StorageEngine) ([]cid.ID, error) {
}
func (e *StorageEngine) listContainers(ctx context.Context) (ListContainersRes, error) {
if e.metrics != nil {
defer elapsed("ListContainers", e.metrics.AddMethodDuration)()
}
uniqueIDs := make(map[string]cid.ID)

View file

@ -68,9 +68,7 @@ func (e *StorageEngine) Delete(ctx context.Context, prm DeletePrm) (res DeleteRe
}
func (e *StorageEngine) delete(ctx context.Context, prm DeletePrm) (DeleteRes, error) {
if e.metrics != nil {
defer elapsed("Delete", e.metrics.AddMethodDuration)()
}
var locked struct {
is bool

View file

@ -219,6 +219,7 @@ func defaultCfg() *cfg {
res := &cfg{
log: &logger.Logger{Logger: zap.L()},
shardPoolSize: 20,
metrics: noopMetrics{},
}
res.containerSource.Store(&containerSource{})
return res

View file

@ -66,9 +66,7 @@ func (e *StorageEngine) Get(ctx context.Context, prm GetPrm) (res GetRes, err er
}
func (e *StorageEngine) get(ctx context.Context, prm GetPrm) (GetRes, error) {
if e.metrics != nil {
defer elapsed("Get", e.metrics.AddMethodDuration)()
}
errNotFound := new(apistatus.ObjectNotFound)

View file

@ -68,9 +68,7 @@ func (e *StorageEngine) Head(ctx context.Context, prm HeadPrm) (res HeadRes, err
func (e *StorageEngine) head(ctx context.Context, prm HeadPrm) (HeadRes, error) {
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.head")
defer span.End()
if e.metrics != nil {
defer elapsed("Head", e.metrics.AddMethodDuration)()
}
var (
head *objectSDK.Object

View file

@ -80,9 +80,7 @@ func (e *StorageEngine) Inhume(ctx context.Context, prm InhumePrm) (res InhumeRe
}
func (e *StorageEngine) inhume(ctx context.Context, prm InhumePrm) (InhumeRes, error) {
if e.metrics != nil {
defer elapsed("Inhume", e.metrics.AddMethodDuration)()
}
var shPrm shard.InhumePrm
if prm.forceRemoval {

View file

@ -68,3 +68,48 @@ func (m *gcMetrics) AddExpiredObjectCollectionDuration(d time.Duration, success
func (m *gcMetrics) AddInhumedObjectCount(count uint64, objectType string) {
m.storage.AddInhumedObjectCount(m.shardID, count, objectType)
}
type (
noopMetrics struct{}
noopWriteCacheMetrics struct{}
noopGCMetrics struct{}
)
var (
_ MetricRegister = noopMetrics{}
_ metrics.WriteCacheMetrics = noopWriteCacheMetrics{}
_ metrics.GCMetrics = noopGCMetrics{}
)
func (noopMetrics) AddMethodDuration(string, time.Duration) {}
func (noopMetrics) SetObjectCounter(string, string, uint64) {}
func (noopMetrics) AddToObjectCounter(string, string, int) {}
func (noopMetrics) SetMode(string, mode.Mode) {}
func (noopMetrics) AddToContainerSize(string, int64) {}
func (noopMetrics) DeleteContainerSize(string) {}
func (noopMetrics) DeleteContainerCount(string) {}
func (noopMetrics) AddToPayloadCounter(string, int64) {}
func (noopMetrics) IncErrorCounter(string) {}
func (noopMetrics) ClearErrorCounter(string) {}
func (noopMetrics) DeleteShardMetrics(string) {}
func (noopMetrics) SetContainerObjectCounter(string, string, string, uint64) {}
func (noopMetrics) IncContainerObjectCounter(string, string, string) {}
func (noopMetrics) SubContainerObjectCounter(string, string, string, uint64) {}
func (noopMetrics) IncRefillObjectsCount(string, string, int, bool) {}
func (noopMetrics) SetRefillPercent(string, string, uint32) {}
func (noopMetrics) SetRefillStatus(string, string, string) {}
func (noopMetrics) SetEvacuationInProgress(string, bool) {}
func (noopMetrics) WriteCache() metrics.WriteCacheMetrics { return noopWriteCacheMetrics{} }
func (noopMetrics) GC() metrics.GCMetrics { return noopGCMetrics{} }
func (noopWriteCacheMetrics) AddMethodDuration(string, string, string, string, bool, time.Duration) {}
func (noopWriteCacheMetrics) SetActualCount(string, string, string, uint64) {}
func (noopWriteCacheMetrics) SetEstimateSize(string, string, string, uint64) {}
func (noopWriteCacheMetrics) SetMode(string, string) {}
func (noopWriteCacheMetrics) IncOperationCounter(string, string, string, string, metrics.NullBool) {}
func (noopWriteCacheMetrics) Close(string, string) {}
func (noopGCMetrics) AddRunDuration(string, time.Duration, bool) {}
func (noopGCMetrics) AddDeletedCount(string, uint64, uint64) {}
func (noopGCMetrics) AddExpiredObjectCollectionDuration(string, time.Duration, bool, string) {}
func (noopGCMetrics) AddInhumedObjectCount(string, uint64, string) {}

View file

@ -72,9 +72,7 @@ func (e *StorageEngine) Put(ctx context.Context, prm PutPrm) (err error) {
}
func (e *StorageEngine) put(ctx context.Context, prm PutPrm) error {
if e.metrics != nil {
defer elapsed("Put", e.metrics.AddMethodDuration)()
}
addr := object.AddressOf(prm.obj)

View file

@ -82,9 +82,7 @@ func (e *StorageEngine) getRange(ctx context.Context, prm RngPrm) (RngRes, error
))
defer span.End()
if e.metrics != nil {
defer elapsed("GetRange", e.metrics.AddMethodDuration)()
}
var shPrm shard.RngPrm
shPrm.SetAddress(prm.addr)

View file

@ -59,9 +59,7 @@ func (e *StorageEngine) Select(ctx context.Context, prm SelectPrm) (res SelectRe
}
func (e *StorageEngine) _select(ctx context.Context, prm SelectPrm) (SelectRes, error) {
if e.metrics != nil {
defer elapsed("Search", e.metrics.AddMethodDuration)()
}
addrList := make([]oid.Address, 0)
uniqueMap := make(map[string]struct{})
@ -108,9 +106,7 @@ func (e *StorageEngine) List(ctx context.Context, limit uint64) (res SelectRes,
}
func (e *StorageEngine) list(ctx context.Context, limit uint64) (SelectRes, error) {
if e.metrics != nil {
defer elapsed("ListObjects", e.metrics.AddMethodDuration)()
}
addrList := make([]oid.Address, 0, limit)
uniqueMap := make(map[string]struct{})

View file

@ -116,9 +116,7 @@ func (e *StorageEngine) AddShard(ctx context.Context, opts ...shard.Option) (*sh
return nil, fmt.Errorf("could not add %s shard: %w", sh.ID().String(), err)
}
if e.cfg.metrics != nil {
e.cfg.metrics.SetMode(sh.ID().String(), sh.GetMode())
}
return sh.ID(), nil
}
@ -152,7 +150,6 @@ func (e *StorageEngine) appendMetrics(id *shard.ID, opts []shard.Option) []shard
e.mtx.RLock()
defer e.mtx.RUnlock()
if e.metrics != nil {
opts = append(opts,
shard.WithMetricsWriter(
&metricsWithID{
@ -173,7 +170,6 @@ func (e *StorageEngine) appendMetrics(id *shard.ID, opts []shard.Option) []shard
},
),
)
}
return opts
}

View file

@ -45,9 +45,7 @@ func (s *Shard) UpdateID() (err error) {
}
shardID := s.info.ID.String()
if s.cfg.metricsWriter != nil {
s.cfg.metricsWriter.SetShardID(shardID)
}
if s.writeCache != nil && s.writeCache.GetMetrics() != nil {
s.writeCache.GetMetrics().SetShardID(shardID)
}

View file

@ -0,0 +1,69 @@
package shard
import "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
// MetricsWriter is an interface that must store shard's metrics.
type MetricsWriter interface {
// SetObjectCounter must set object counter taking into account object type.
SetObjectCounter(objectType string, v uint64)
// AddToObjectCounter must update object counter taking into account object
// type.
// Negative parameter must decrease the counter.
AddToObjectCounter(objectType string, delta int)
// AddToContainerSize must add a value to the container size.
// Value can be negative.
AddToContainerSize(cnr string, value int64)
// AddToPayloadSize must add a value to the payload size.
// Value can be negative.
AddToPayloadSize(value int64)
// IncObjectCounter must increment shard's object counter taking into account
// object type.
IncObjectCounter(objectType string)
// SetShardID must set (update) the shard identifier that will be used in
// metrics.
SetShardID(id string)
// SetReadonly must set shard mode.
SetMode(mode mode.Mode)
// IncErrorCounter increment error counter.
IncErrorCounter()
// ClearErrorCounter clear error counter.
ClearErrorCounter()
// DeleteShardMetrics deletes shard metrics from registry.
DeleteShardMetrics()
// SetContainerObjectsCount sets container object count.
SetContainerObjectsCount(cnrID string, objectType string, value uint64)
// IncContainerObjectsCount increments container object count.
IncContainerObjectsCount(cnrID string, objectType string)
// SubContainerObjectsCount subtracts container object count.
SubContainerObjectsCount(cnrID string, objectType string, value uint64)
// IncRefillObjectsCount increments refill objects count.
IncRefillObjectsCount(path string, size int, success bool)
// SetRefillPercent sets refill percent.
SetRefillPercent(path string, percent uint32)
// SetRefillStatus sets refill status.
SetRefillStatus(path string, status string)
// SetEvacuationInProgress sets evacuation status
SetEvacuationInProgress(value bool)
}
type noopMetrics struct{}
var _ MetricsWriter = noopMetrics{}
func (noopMetrics) SetObjectCounter(string, uint64) {}
func (noopMetrics) AddToObjectCounter(string, int) {}
func (noopMetrics) AddToContainerSize(string, int64) {}
func (noopMetrics) AddToPayloadSize(int64) {}
func (noopMetrics) IncObjectCounter(string) {}
func (noopMetrics) SetShardID(string) {}
func (noopMetrics) SetMode(mode.Mode) {}
func (noopMetrics) IncErrorCounter() {}
func (noopMetrics) ClearErrorCounter() {}
func (noopMetrics) DeleteShardMetrics() {}
func (noopMetrics) SetContainerObjectsCount(string, string, uint64) {}
func (noopMetrics) IncContainerObjectsCount(string, string) {}
func (noopMetrics) SubContainerObjectsCount(string, string, uint64) {}
func (noopMetrics) IncRefillObjectsCount(string, int, bool) {}
func (noopMetrics) SetRefillPercent(string, uint32) {}
func (noopMetrics) SetRefillStatus(string, string) {}
func (noopMetrics) SetEvacuationInProgress(bool) {}

View file

@ -65,9 +65,7 @@ func (s *Shard) setMode(m mode.Mode) error {
}
s.info.Mode = m
if s.metricsWriter != nil {
s.metricsWriter.SetMode(s.info.Mode)
}
s.log.Info(logs.ShardShardModeSetSuccessfully,
zap.Stringer("mode", s.info.Mode))

View file

@ -57,50 +57,6 @@ type DeletedLockCallback func(context.Context, []oid.Address)
// EmptyContainersCallback is a callback hanfling list of zero-size and zero-count containers.
type EmptyContainersCallback func(context.Context, []cid.ID)
// MetricsWriter is an interface that must store shard's metrics.
type MetricsWriter interface {
// SetObjectCounter must set object counter taking into account object type.
SetObjectCounter(objectType string, v uint64)
// AddToObjectCounter must update object counter taking into account object
// type.
// Negative parameter must decrease the counter.
AddToObjectCounter(objectType string, delta int)
// AddToContainerSize must add a value to the container size.
// Value can be negative.
AddToContainerSize(cnr string, value int64)
// AddToPayloadSize must add a value to the payload size.
// Value can be negative.
AddToPayloadSize(value int64)
// IncObjectCounter must increment shard's object counter taking into account
// object type.
IncObjectCounter(objectType string)
// SetShardID must set (update) the shard identifier that will be used in
// metrics.
SetShardID(id string)
// SetReadonly must set shard mode.
SetMode(mode mode.Mode)
// IncErrorCounter increment error counter.
IncErrorCounter()
// ClearErrorCounter clear error counter.
ClearErrorCounter()
// DeleteShardMetrics deletes shard metrics from registry.
DeleteShardMetrics()
// SetContainerObjectsCount sets container object count.
SetContainerObjectsCount(cnrID string, objectType string, value uint64)
// IncContainerObjectsCount increments container object count.
IncContainerObjectsCount(cnrID string, objectType string)
// SubContainerObjectsCount subtracts container object count.
SubContainerObjectsCount(cnrID string, objectType string, value uint64)
// IncRefillObjectsCount increments refill objects count.
IncRefillObjectsCount(path string, size int, success bool)
// SetRefillPercent sets refill percent.
SetRefillPercent(path string, percent uint32)
// SetRefillStatus sets refill status.
SetRefillStatus(path string, status string)
// SetEvacuationInProgress sets evacuation status
SetEvacuationInProgress(value bool)
}
type cfg struct {
m sync.RWMutex
@ -149,6 +105,7 @@ func defaultCfg() *cfg {
reportErrorFunc: func(string, string, error) {},
zeroSizeContainersCallback: func(context.Context, []cid.ID) {},
zeroCountContainersCallback: func(context.Context, []cid.ID) {},
metricsWriter: noopMetrics{},
}
}
@ -428,7 +385,7 @@ const (
)
func (s *Shard) updateMetrics(ctx context.Context) {
if s.cfg.metricsWriter == nil || s.GetMode().NoMetabase() {
if s.GetMode().NoMetabase() {
return
}
@ -483,7 +440,6 @@ func (s *Shard) updateMetrics(ctx context.Context) {
// incObjectCounter increment both physical and logical object
// counters.
func (s *Shard) incObjectCounter(cnrID cid.ID, isUser bool) {
if s.cfg.metricsWriter != nil {
s.cfg.metricsWriter.IncObjectCounter(physical)
s.cfg.metricsWriter.IncObjectCounter(logical)
s.cfg.metricsWriter.IncContainerObjectsCount(cnrID.EncodeToString(), physical)
@ -492,26 +448,21 @@ func (s *Shard) incObjectCounter(cnrID cid.ID, isUser bool) {
s.cfg.metricsWriter.IncObjectCounter(user)
s.cfg.metricsWriter.IncContainerObjectsCount(cnrID.EncodeToString(), user)
}
}
}
func (s *Shard) decObjectCounterBy(typ string, v uint64) {
if s.cfg.metricsWriter != nil && v > 0 {
if v > 0 {
s.cfg.metricsWriter.AddToObjectCounter(typ, -int(v))
}
}
func (s *Shard) setObjectCounterBy(typ string, v uint64) {
if s.cfg.metricsWriter != nil && v > 0 {
if v > 0 {
s.cfg.metricsWriter.SetObjectCounter(typ, v)
}
}
func (s *Shard) decContainerObjectCounter(byCnr map[cid.ID]meta.ObjectCounters) {
if s.cfg.metricsWriter == nil {
return
}
for cnrID, count := range byCnr {
if count.Phy > 0 {
s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), physical, count.Phy)
@ -526,46 +477,38 @@ func (s *Shard) decContainerObjectCounter(byCnr map[cid.ID]meta.ObjectCounters)
}
func (s *Shard) addToContainerSize(cnr string, size int64) {
if s.cfg.metricsWriter != nil && size != 0 {
if size != 0 {
s.cfg.metricsWriter.AddToContainerSize(cnr, size)
}
}
func (s *Shard) addToPayloadSize(size int64) {
if s.cfg.metricsWriter != nil && size != 0 {
if size != 0 {
s.cfg.metricsWriter.AddToPayloadSize(size)
}
}
func (s *Shard) setContainerObjectsCount(cnr string, typ string, v uint64) {
if s.cfg.metricsWriter != nil && v > 0 {
if v > 0 {
s.metricsWriter.SetContainerObjectsCount(cnr, typ, v)
}
}
func (s *Shard) IncErrorCounter() {
if s.cfg.metricsWriter != nil {
s.cfg.metricsWriter.IncErrorCounter()
}
}
func (s *Shard) ClearErrorCounter() {
if s.cfg.metricsWriter != nil {
s.cfg.metricsWriter.ClearErrorCounter()
}
}
func (s *Shard) DeleteShardMetrics() {
if s.cfg.metricsWriter != nil {
s.cfg.metricsWriter.DeleteShardMetrics()
}
}
func (s *Shard) SetEvacuationInProgress(val bool) {
s.m.Lock()
defer s.m.Unlock()
s.info.EvacuationInProgress = val
if s.metricsWriter != nil {
s.metricsWriter.SetEvacuationInProgress(val)
}
}