Skip to content

Commit 85f4db8

Browse files
CyberROFLydbot
authored andcommitted
Require category, TPublicCounters helper (#26249)
1 parent 7c55262 commit 85f4db8

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

ydb/core/kesus/tablet/rate_accounting.cpp

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <util/string/builder.h>
1616
#include <util/generic/deque.h>
1717

18+
#include <optional>
19+
1820
LWTRACE_USING(KESUS_QUOTER_PROVIDER);
1921

2022
namespace NKikimr {
@@ -200,6 +202,58 @@ class TBillingMetric {
200202
}
201203
};
202204

205+
////////////////////////////////////////////////////////////////////////////////
206+
class TPublicCounters {
207+
static std::optional<TString> GetCategory(const NKikimrKesus::TAccountingConfig::TMetric& cfg) {
208+
for (const auto& [label, value] : cfg.GetLabels()) {
209+
if (to_lower(label) == "category") {
210+
return value;
211+
}
212+
}
213+
214+
return std::nullopt;
215+
}
216+
217+
public:
218+
void Configure(const NKikimrKesus::TAccountingConfig::TMetric& cfg, double limit, ::NMonitoring::TDynamicCounterPtr counters) {
219+
std::optional<TString> category;
220+
if (!cfg.GetEnabled()
221+
|| !cfg.GetCloudId()
222+
|| !cfg.GetFolderId()
223+
|| !cfg.GetResourceId()
224+
|| !(category = GetCategory(cfg))
225+
) {
226+
Limit.Reset();
227+
Consumed.Reset();
228+
Counters.Reset();
229+
return;
230+
}
231+
232+
Y_ABORT_UNLESS(category.has_value());
233+
Counters = GetServiceCounters(counters, "ydb_serverless", false)
234+
->GetSubgroup("host", "")
235+
->GetSubgroup("cloud_id", cfg.GetCloudId())
236+
->GetSubgroup("folder_id", cfg.GetFolderId())
237+
->GetSubgroup("database_id", cfg.GetResourceId())
238+
->GetSubgroup("category", *category);
239+
Limit = Counters->GetExpiringNamedCounter("name", "resources.request_units.limit", false);
240+
Consumed = Counters->GetExpiringNamedCounter("name", "resources.request_units.consumed", true);
241+
242+
*Limit = limit;
243+
}
244+
245+
void Consume(double consumed) {
246+
if (Consumed) {
247+
*Consumed += consumed;
248+
}
249+
}
250+
251+
private:
252+
::NMonitoring::TDynamicCounterPtr Counters;
253+
::NMonitoring::TDynamicCounters::TCounterPtr Limit;
254+
::NMonitoring::TDynamicCounters::TCounterPtr Consumed;
255+
};
256+
203257
////////////////////////////////////////////////////////////////////////////////
204258
class TAccountingActor final: public TActor<TAccountingActor> {
205259
private:
@@ -219,9 +273,7 @@ class TAccountingActor final: public TActor<TAccountingActor> {
219273

220274
// Monitoring
221275
TRateAccountingCounters Counters;
222-
::NMonitoring::TDynamicCounterPtr PublicCounters;
223-
::NMonitoring::TDynamicCounters::TCounterPtr Limit;
224-
::NMonitoring::TDynamicCounters::TCounterPtr Consumed;
276+
TPublicCounters PublicCounters;
225277

226278
public:
227279
explicit TAccountingActor(const IBillSink::TPtr& billSink, const NKikimrKesus::TStreamingQuoterResource& props, const TString& quoterPath)
@@ -278,30 +330,7 @@ class TAccountingActor final: public TActor<TAccountingActor> {
278330
OnDemand.Configure(accCfg.GetOnDemand(), QuoterPath, props.GetResourcePath(), "ondemand", BillSink);
279331
Overshoot.Configure(accCfg.GetOvershoot(), QuoterPath, props.GetResourcePath(), "overshoot", BillSink);
280332

281-
if (const auto& cfg = accCfg.GetOnDemand(); cfg.GetEnabled() && cfg.GetCloudId() && cfg.GetFolderId() && cfg.GetResourceId()) {
282-
TString category = "Generic";
283-
for (const auto& [label, value] : cfg.GetLabels()) {
284-
if (to_lower(label) == "category") {
285-
category = value;
286-
break;
287-
}
288-
}
289-
290-
PublicCounters = GetServiceCounters(AppData()->Counters, "ydb_serverless", false)
291-
->GetSubgroup("host", "")
292-
->GetSubgroup("cloud_id", cfg.GetCloudId())
293-
->GetSubgroup("folder_id", cfg.GetFolderId())
294-
->GetSubgroup("database_id", cfg.GetResourceId())
295-
->GetSubgroup("category", category);
296-
Limit = PublicCounters->GetExpiringNamedCounter("name", "resources.request_units.limit", false);
297-
Consumed = PublicCounters->GetExpiringNamedCounter("name", "resources.request_units.consumed", true);
298-
299-
*Limit = resCfg.GetMaxUnitsPerSecond();
300-
} else {
301-
Limit.Reset();
302-
Consumed.Reset();
303-
PublicCounters.Reset();
304-
}
333+
PublicCounters.Configure(accCfg.GetOnDemand(), resCfg.GetMaxUnitsPerSecond(), AppData()->Counters);
305334

306335
LWPROBE(ResourceAccountConfigure,
307336
QuoterPath,
@@ -349,9 +378,7 @@ class TAccountingActor final: public TActor<TAccountingActor> {
349378
OnDemand.Add(onDemand, t, ctx);
350379
Overshoot.Add(overshoot, t, ctx);
351380

352-
if (Consumed) {
353-
*Consumed += consumed;
354-
}
381+
PublicCounters.Consume(consumed);
355382
}
356383
};
357384

0 commit comments

Comments
 (0)