diff options
author | alexbogo <[email protected]> | 2023-06-01 11:13:37 +0300 |
---|---|---|
committer | alexbogo <[email protected]> | 2023-06-01 11:13:37 +0300 |
commit | 859bb5ff4cd6798053dbbf6e8440a4b124d6678d (patch) | |
tree | 0268cdf17340f50540ef01032388a060da88e4b5 | |
parent | 2ba2c4c1e2f61ac4b37ba0bf16d71da1dbc93214 (diff) |
[ymq] make counter more light
init
-rw-r--r-- | ydb/core/ymq/base/counters.h | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/ydb/core/ymq/base/counters.h b/ydb/core/ymq/base/counters.h index 7e6668b4746..de94cb13b47 100644 --- a/ydb/core/ymq/base/counters.h +++ b/ydb/core/ymq/base/counters.h @@ -123,6 +123,7 @@ namespace NDetails { template <class TTargetCounterType, class TDerived> struct TLazyCachedCounterBase { +public: // Facade for counters aggregation. class TAggregatingCounterFacade { friend struct TLazyCachedCounterBase; @@ -213,9 +214,10 @@ struct TLazyCachedCounterBase { if (counter) { counter->Ref(); } else { - RootCounters = other.RootCounters; - Value = other.Value; - Lifetime = other.Lifetime; + Impl = nullptr; + if (other.Impl) { + Impl = new TImpl(*other.Impl); + } } } @@ -231,10 +233,11 @@ struct TLazyCachedCounterBase { } void Init(const TIntrusivePtr<::NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, const TString& name, const TString& value, ELaziness laziness) { - RootCounters = rootCounters; - Name = name; - Value = value; - Lifetime = lifetime; + Impl = new TImpl(); + Impl->RootCounters = rootCounters; + Impl->Name = name; + Impl->Value = value; + Impl->Lifetime = lifetime; if (laziness == ELaziness::OnStart) { EnsureCreated(); } @@ -290,11 +293,42 @@ protected: } protected: + TIntrusivePtr<NMonitoring::TCounterForPtr> CreateImpl(bool derivative) { + if (Impl && Impl->RootCounters && Impl->Name && Impl->Value) { + return Impl->Create(derivative); + } else { + return nullptr; + } + } + + TIntrusivePtr<NMonitoring::THistogramCounter> CreateHistogramImpl(const NMonitoring::TBucketBounds* buckets) { + if (Impl && Impl->RootCounters && Impl->Name && Impl->Value && buckets) { + return Impl->CreateHistogram(buckets); + } else { + return nullptr; + } + } + +private: + class TImpl : public TAtomicRefCount<TImpl> { + public: + TIntrusivePtr<::NMonitoring::TDynamicCounters> RootCounters; + TString Name; + TString Value; + ELifetime Lifetime = ELifetime::Persistent; + + TIntrusivePtr<NMonitoring::TCounterForPtr> Create(bool derivative) { + return Lifetime == ELifetime::Expiring ? RootCounters->GetExpiringNamedCounter(Name, Value, derivative) : RootCounters->GetNamedCounter(Name, Value, derivative); + } + + TIntrusivePtr<NMonitoring::THistogramCounter> CreateHistogram(const NMonitoring::TBucketBounds* buckets) { + return Lifetime == ELifetime::Expiring ? RootCounters->GetExpiringNamedHistogram(Name, Value, NMonitoring::ExplicitHistogram(*buckets)) : RootCounters->GetNamedHistogram(Name, Value, NMonitoring::ExplicitHistogram(*buckets)); + } + }; + +protected: + TIntrusivePtr<TImpl> Impl; TLazyCachedCounterBase* AggregatedParent = nullptr; - TIntrusivePtr<::NMonitoring::TDynamicCounters> RootCounters; - TString Name; - TString Value; - ELifetime Lifetime = ELifetime::Persistent; std::atomic<TTargetCounterType*> Counter; }; @@ -317,12 +351,8 @@ struct TLazyCachedCounter : public NDetails::TLazyCachedCounterBase<NMonitoring: } TIntrusivePtr<NMonitoring::TCounterForPtr> Create() { - if (RootCounters && Name && Value) { - const bool derivative = ValueType == EValueType::Derivative; - return Lifetime == ELifetime::Expiring ? RootCounters->GetExpiringNamedCounter(Name, Value, derivative) : RootCounters->GetNamedCounter(Name, Value, derivative); - } else { - return nullptr; - } + const bool derivative = ValueType == EValueType::Derivative; + return CreateImpl(derivative); } static TIntrusivePtr<NMonitoring::TCounterForPtr> Default() { @@ -351,11 +381,7 @@ struct TLazyCachedHistogram : public NDetails::TLazyCachedCounterBase<NMonitorin } TIntrusivePtr<NMonitoring::THistogramCounter> Create() { - if (RootCounters && Name && Value && Buckets) { - return Lifetime == ELifetime::Expiring ? RootCounters->GetExpiringNamedHistogram(Name, Value, NMonitoring::ExplicitHistogram(*Buckets)) : RootCounters->GetNamedHistogram(Name, Value, NMonitoring::ExplicitHistogram(*Buckets)); - } else { - return nullptr; - } + return CreateHistogramImpl(Buckets); } static TIntrusivePtr<NMonitoring::THistogramCounter> Default() { |