diff options
author | hcpp <hcpp@ydb.tech> | 2022-10-04 10:00:15 +0300 |
---|---|---|
committer | hcpp <hcpp@ydb.tech> | 2022-10-04 10:00:15 +0300 |
commit | c639d8e279f133e4a7dd8689aa5d1359107296b9 (patch) | |
tree | 0c32caf443170895b7ff1b407220a31e663210c4 | |
parent | ca4729498cfea47ea893c95fe24cab8cf4c54488 (diff) | |
download | ydb-c639d8e279f133e4a7dd8689aa5d1359107296b9.tar.gz |
ttl has been added for cps
-rw-r--r-- | ydb/core/yq/libs/common/cache.h | 4 | ||||
-rw-r--r-- | ydb/core/yq/libs/control_plane_proxy/control_plane_proxy.cpp | 50 | ||||
-rw-r--r-- | ydb/core/yq/libs/control_plane_storage/ydb_control_plane_storage_impl.h | 32 |
3 files changed, 39 insertions, 47 deletions
diff --git a/ydb/core/yq/libs/common/cache.h b/ydb/core/yq/libs/common/cache.h index 40693838562..f72960cba16 100644 --- a/ydb/core/yq/libs/common/cache.h +++ b/ydb/core/yq/libs/common/cache.h @@ -33,7 +33,7 @@ struct TTtlCacheSettings { } }; -template<typename TKey, typename TValue> +template<typename TKey, typename TValue, template <typename... Args> class TContainer = THashMap> class TTtlCache { public: TTtlCache(const TTtlCacheSettings& config = TTtlCacheSettings()) @@ -110,7 +110,7 @@ private: typename TList<TKeyAndTime>::iterator Position; TList<TKeyAndTime>* Queue; }; - THashMap<TKey, TCacheObject> Data; + TContainer<TKey, TCacheObject> Data; TTtlCacheSettings Config; }; diff --git a/ydb/core/yq/libs/control_plane_proxy/control_plane_proxy.cpp b/ydb/core/yq/libs/control_plane_proxy/control_plane_proxy.cpp index b3bfbe54a81..0d0b987ec17 100644 --- a/ydb/core/yq/libs/control_plane_proxy/control_plane_proxy.cpp +++ b/ydb/core/yq/libs/control_plane_proxy/control_plane_proxy.cpp @@ -3,12 +3,13 @@ #include "utils.h" #include <ydb/core/yq/libs/actors/logging/log.h> +#include <ydb/core/yq/libs/common/cache.h> #include <ydb/core/yq/libs/control_plane_storage/control_plane_storage.h> #include <ydb/core/yq/libs/control_plane_storage/events/events.h> #include <ydb/core/yq/libs/control_plane_storage/util.h> #include <ydb/core/yq/libs/quota_manager/quota_manager.h> -#include <ydb/core/yq/libs/test_connection/test_connection.h> #include <ydb/core/yq/libs/test_connection/events/events.h> +#include <ydb/core/yq/libs/test_connection/test_connection.h> #include <ydb/core/yq/libs/ydb/util.h> #include <ydb/core/yq/libs/ydb/ydb.h> @@ -492,6 +493,13 @@ class TControlPlaneProxyActor : public NActors::TActorBootstrapped<TControlPlane TString CloudId; TString Scope; + TMetricsScope() = default; + + TMetricsScope(const TString& cloudId, const TString& scope) + : CloudId(cloudId) + , Scope(scope) + {} + bool operator<(const TMetricsScope& right) const { return std::make_pair(CloudId, Scope) < std::make_pair(right.CloudId, right.Scope); } @@ -527,13 +535,7 @@ class TControlPlaneProxyActor : public NActors::TActorBootstrapped<TControlPlane { MakeIntrusive<TRequestCommonCounters>("DeleteBinding") }, }); - struct TScopeValue { - TScopeCountersPtr Counters; - TInstant LastAccess; - }; - - TMap<TMetricsScope, TScopeValue> ScopeCounters; - TMap<TInstant, TSet<TMetricsScope>> LastAccess; + TTtlCache<TMetricsScope, TScopeCountersPtr, TMap> ScopeCounters{TTtlCacheSettings{}.SetTtl(TDuration::Days(1))}; ::NMonitoring::TDynamicCounterPtr Counters; public: @@ -545,17 +547,6 @@ class TControlPlaneProxyActor : public NActors::TActorBootstrapped<TControlPlane } } - void CleanupByTtl() { - auto now = TInstant::Now(); - auto it = LastAccess.begin(); - for (; it != LastAccess.end() && (now - it->first) > MetricsTtl; ++it) { - for (const auto& scope: it->second) { - ScopeCounters.erase(scope); - } - } - LastAccess.erase(LastAccess.begin(), it); - } - TRequestCounters GetCounters(const TString& cloudId, const TString& scope, ERequestTypeScope scopeType, ERequestTypeCommon commonType) { return {GetScopeCounters(cloudId, scope, scopeType), GetCommonCounters(commonType)}; } @@ -565,19 +556,11 @@ class TControlPlaneProxyActor : public NActors::TActorBootstrapped<TControlPlane } TRequestScopeCountersPtr GetScopeCounters(const TString& cloudId, const TString& scope, ERequestTypeScope type) { - CleanupByTtl(); TMetricsScope key{cloudId, scope}; - auto it = ScopeCounters.find(key); - if (it != ScopeCounters.end()) { - auto& value = it->second; - auto& l = LastAccess[value.LastAccess]; - l.erase(key); - if (l.empty()) { - LastAccess.erase(value.LastAccess); - } - value.LastAccess = TInstant::Now(); - LastAccess[value.LastAccess].insert(key); - return (*value.Counters)[type]; + TMaybe<TScopeCountersPtr> cacheVal; + ScopeCounters.Get(key, &cacheVal); + if (cacheVal) { + return (**cacheVal)[type]; } auto scopeRequests = std::make_shared<TScopeCounters>(CreateArray<RTS_MAX, TRequestScopeCountersPtr>({ @@ -611,9 +594,8 @@ class TControlPlaneProxyActor : public NActors::TActorBootstrapped<TControlPlane for (auto& request: *scopeRequests) { request->Register(scopeCounters); } - auto now = TInstant::Now(); - LastAccess[now].insert(key); - ScopeCounters[key] = TScopeValue{scopeRequests, now}; + cacheVal = scopeRequests; + ScopeCounters.Put(key, cacheVal); return (*scopeRequests)[type]; } }; diff --git a/ydb/core/yq/libs/control_plane_storage/ydb_control_plane_storage_impl.h b/ydb/core/yq/libs/control_plane_storage/ydb_control_plane_storage_impl.h index ce7e71f31b4..7c35e5749ff 100644 --- a/ydb/core/yq/libs/control_plane_storage/ydb_control_plane_storage_impl.h +++ b/ydb/core/yq/libs/control_plane_storage/ydb_control_plane_storage_impl.h @@ -27,6 +27,7 @@ #include <ydb/core/base/appdata.h> #include <ydb/core/mon/mon.h> +#include <ydb/core/yq/libs/common/cache.h> #include <ydb/core/yq/libs/common/entity_id.h> #include <ydb/core/yq/libs/config/protos/issue_id.pb.h> #include <ydb/core/yq/libs/config/yq_issue.h> @@ -272,6 +273,12 @@ class TYdbControlPlaneStorageActor : public NActors::TActorBootstrapped<TYdbCont TString CloudId; TString Scope; + TMetricsScope() = default; + + TMetricsScope(const TString& cloudId, const TString& scope) + : CloudId(cloudId), Scope(scope) + {} + bool operator<(const TMetricsScope& right) const { return std::make_pair(CloudId, Scope) < std::make_pair(right.CloudId, right.Scope); } @@ -311,8 +318,8 @@ class TYdbControlPlaneStorageActor : public NActors::TActorBootstrapped<TYdbCont { MakeIntrusive<TRequestCommonCounters>("PingTask") }, }); - TMap<TMetricsScope, TScopeCountersPtr> ScopeCounters; - TMap<TMetricsScope, TFinalStatusCountersPtr> FinalStatusCounters; + TTtlCache<TMetricsScope, TScopeCountersPtr, TMap> ScopeCounters{TTtlCacheSettings{}.SetTtl(TDuration::Days(1))}; + TTtlCache<TMetricsScope, TFinalStatusCountersPtr, TMap> FinalStatusCounters{TTtlCacheSettings{}.SetTtl(TDuration::Days(1))}; public: ::NMonitoring::TDynamicCounterPtr Counters; @@ -335,25 +342,27 @@ class TYdbControlPlaneStorageActor : public NActors::TActorBootstrapped<TYdbCont TFinalStatusCountersPtr GetFinalStatusCounters(const TString& cloudId, const TString& scope) { TMetricsScope key{cloudId, scope}; - auto it = FinalStatusCounters.find(key); - if (it != FinalStatusCounters.end()) { - return it->second; + TMaybe<TFinalStatusCountersPtr> cacheVal; + FinalStatusCounters.Get(key, &cacheVal); + if (cacheVal) { + return *cacheVal; } auto scopeCounters = (cloudId ? Counters->GetSubgroup("cloud_id", cloudId) : Counters) ->GetSubgroup("scope", scope); auto finalStatusCounters = MakeIntrusive<TFinalStatusCounters>(scopeCounters); - - FinalStatusCounters[key] = finalStatusCounters; + cacheVal = finalStatusCounters; + FinalStatusCounters.Put(key, cacheVal); return finalStatusCounters; } TRequestScopeCountersPtr GetScopeCounters(const TString& cloudId, const TString& scope, ERequestTypeScope type) { TMetricsScope key{cloudId, scope}; - auto it = ScopeCounters.find(key); - if (it != ScopeCounters.end()) { - return (*it->second)[type]; + TMaybe<TScopeCountersPtr> cacheVal; + ScopeCounters.Get(key, &cacheVal); + if (cacheVal) { + return (**cacheVal)[type]; } auto scopeRequests = std::make_shared<TScopeCounters>(CreateArray<RTS_MAX, TRequestScopeCountersPtr>({ @@ -387,7 +396,8 @@ class TYdbControlPlaneStorageActor : public NActors::TActorBootstrapped<TYdbCont request->Register(scopeCounters); } - ScopeCounters[key] = scopeRequests; + cacheVal = scopeRequests; + ScopeCounters.Put(key, cacheVal); return (*scopeRequests)[type]; } }; |