Skip to content

Commit f7d665b

Browse files
authored
24-4: Resource labels (#14850)
1 parent db75faa commit f7d665b

File tree

8 files changed

+64
-2
lines changed

8 files changed

+64
-2
lines changed

ydb/core/grpc_services/rpc_rate_limiter_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ static void CopyProps(const Ydb::RateLimiter::Resource& src, NKikimrKesus::TStre
287287
auto copyMetric = [] (const Ydb::RateLimiter::MeteringConfig::Metric& srcMetric, NKikimrKesus::TAccountingConfig::TMetric& metric) {
288288
metric.SetEnabled(srcMetric.enabled());
289289
metric.SetBillingPeriodSec(srcMetric.billing_period_sec());
290+
*metric.MutableLabels() = srcMetric.labels();
290291

291292
/* overwrite if we have new fields */
292293
/* TODO: support arbitrary fields in metering core */
@@ -355,6 +356,7 @@ static void CopyProps(const NKikimrKesus::TStreamingQuoterResource& src, Ydb::Ra
355356
auto copyMetric = [] (const NKikimrKesus::TAccountingConfig::TMetric& srcMetric, Ydb::RateLimiter::MeteringConfig::Metric& metric) {
356357
metric.set_enabled(srcMetric.GetEnabled());
357358
metric.set_billing_period_sec(srcMetric.GetBillingPeriodSec());
359+
*metric.mutable_labels() = srcMetric.GetLabels();
358360

359361
/* TODO: support arbitrary fields in metering core */
360362
auto& metricFields = *metric.mutable_metric_fields()->mutable_fields();

ydb/core/kesus/tablet/quoter_resource_tree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ void THierarchicalDRRQuoterResourceTree::CalcParametersForAccounting() {
804804
if (!cfg->GetResourceId() && parent) { cfg->SetResourceId(parent->GetResourceId()); }
805805
if (!cfg->GetSourceId() && parent) { cfg->SetSourceId(parent->GetSourceId()); }
806806
if (cfg->GetTags().empty() && parent) { *cfg->MutableTags() = parent->GetTags(); }
807+
if (cfg->GetLabels().empty() && parent) { *cfg->MutableLabels() = parent->GetLabels(); }
807808
};
808809
calcMetricsParams(accCfg->MutableProvisioned(), accCfgParent ? &accCfgParent->GetProvisioned() : nullptr);
809810
calcMetricsParams(accCfg->MutableOnDemand(), accCfgParent ? &accCfgParent->GetOnDemand() : nullptr);

ydb/core/kesus/tablet/rate_accounting.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ class TBillingMetric {
170170
.FolderId(Cfg.GetFolderId())
171171
.ResourceId(Cfg.GetResourceId())
172172
.SourceId(Cfg.GetSourceId())
173-
.Tags(ToJsonMap(Cfg.GetTags()));
173+
.Tags(ToJsonMap(Cfg.GetTags()))
174+
.Labels(ToJsonMap(Cfg.GetLabels()));
174175

175176
LWPROBE(ResourceBillSend,
176177
QuoterPath,

ydb/core/kesus/tablet/tablet_ut.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,53 @@ Y_UNIT_TEST_SUITE(TKesusTest) {
21522152
}
21532153
}
21542154

2155+
Y_UNIT_TEST(TestQuoterAccountLabels) {
2156+
TTestContext ctx;
2157+
ctx.Setup();
2158+
2159+
TString billRecord;
2160+
ctx.Runtime->SetObserverFunc([&billRecord](TAutoPtr<IEventHandle>& ev) {
2161+
if (ev->GetTypeRewrite() == NMetering::TEvMetering::EvWriteMeteringJson) {
2162+
billRecord = ev->Get<NMetering::TEvMetering::TEvWriteMeteringJson>()->MeteringJson;
2163+
}
2164+
return TTestActorRuntime::EEventAction::PROCESS;
2165+
});
2166+
2167+
NKikimrKesus::TStreamingQuoterResource cfg;
2168+
cfg.SetResourcePath("/Root");
2169+
cfg.MutableHierarchicalDRRResourceConfig()->SetMaxUnitsPerSecond(100.0);
2170+
cfg.MutableHierarchicalDRRResourceConfig()->SetPrefetchCoefficient(300.0);
2171+
cfg.MutableAccountingConfig()->SetEnabled(true);
2172+
cfg.MutableAccountingConfig()->SetReportPeriodMs(1000);
2173+
cfg.MutableAccountingConfig()->SetAccountPeriodMs(1000);
2174+
cfg.MutableAccountingConfig()->SetCollectPeriodSec(2);
2175+
cfg.MutableAccountingConfig()->MutableOnDemand()->SetEnabled(true);
2176+
cfg.MutableAccountingConfig()->MutableOnDemand()->SetBillingPeriodSec(2);
2177+
cfg.MutableAccountingConfig()->MutableOnDemand()->MutableLabels()->insert({"k1", "v1"});
2178+
cfg.MutableAccountingConfig()->MutableOnDemand()->MutableLabels()->insert({"k2", "v2"});
2179+
ctx.AddQuoterResource(cfg);
2180+
2181+
auto edge = ctx.Runtime->AllocateEdgeActor();
2182+
auto client = ctx.Runtime->AllocateEdgeActor();
2183+
const NKikimrKesus::TEvSubscribeOnResourcesResult subscribeResult = ctx.SubscribeOnResource(client, edge, "/Root", false, 0);
2184+
2185+
TInstant start = ctx.Runtime->GetCurrentTime();
2186+
TDuration interval = TConsumptionHistory::Interval();
2187+
ctx.AccountResources(client, edge, subscribeResult.GetResults(0).GetResourceId(), start, interval, {50.0});
2188+
2189+
if (billRecord.empty()) {
2190+
TDispatchOptions opts;
2191+
opts.FinalEvents.emplace_back([&billRecord](IEventHandle&) -> bool {
2192+
return !billRecord.empty();
2193+
});
2194+
ctx.Runtime->DispatchEvents(opts);
2195+
}
2196+
2197+
const TString expectedBillRecord = R"({"usage":{"start":0,"quantity":50,"finish":1,"unit":"request_unit","type":"delta"},"tags":{},"id":"Root-ondemand-0-1","cloud_id":"","source_wt":5,"source_id":"","resource_id":"","schema":"","labels":{"k2":"v2","k1":"v1"},"folder_id":"","version":""})";
2198+
2199+
UNIT_ASSERT_NO_DIFF(billRecord, expectedBillRecord + "\n");
2200+
}
2201+
21552202
Y_UNIT_TEST(TestPassesUpdatedPropsToSession) {
21562203
TTestContext ctx;
21572204
ctx.Setup();

ydb/core/metering/bill_record.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TString TBillRecord::TUsage::ToString() const {
2222
}
2323

2424
NJson::TJsonMap TBillRecord::ToJson() const {
25-
return NJson::TJsonMap{
25+
auto json = NJson::TJsonMap{
2626
{"version", Version_},
2727
{"id", Id_},
2828
{"schema", Schema_},
@@ -34,6 +34,12 @@ NJson::TJsonMap TBillRecord::ToJson() const {
3434
{"tags", Tags_},
3535
{"usage", Usage_.ToJson()},
3636
};
37+
38+
if (Labels_.IsMap() && Labels_.GetMap()) {
39+
json["labels"] = Labels_;
40+
}
41+
42+
return json;
3743
}
3844

3945
TString TBillRecord::ToString() const {

ydb/core/metering/bill_record.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct TBillRecord {
5555
BILL_RECORD_FIELD_DEFAULT(TString, SourceId, "sless-docapi-ydb-ss");
5656
BILL_RECORD_FIELD(TInstant, SourceWt);
5757
BILL_RECORD_FIELD_DEFAULT(NJson::TJsonMap, Tags, {});
58+
BILL_RECORD_FIELD_DEFAULT(NJson::TJsonMap, Labels, {});
5859
BILL_RECORD_FIELD(TUsage, Usage);
5960

6061
NJson::TJsonMap ToJson() const;

ydb/core/protos/kesus.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ message TAccountingConfig {
298298
string ResourceId = 8;
299299
string SourceId = 9;
300300
map<string, string> Tags = 10;
301+
map<string, string> Labels = 11;
301302
}
302303

303304
// Consumption within provisioned limit.

ydb/public/api/protos/ydb_rate_limiter.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ message MeteringConfig {
6363
// Default value is inherited from parent or equals 60 seconds for root.
6464
uint64 billing_period_sec = 2;
6565

66+
// User-defined labels.
67+
map<string, string> labels = 3 [(map_key).length.le = 256, (length).le = 10240, (size).le = 100];
68+
6669
// Billing metric JSON fields (inherited from parent if not set)
6770
google.protobuf.Struct metric_fields = 10;
6871
}

0 commit comments

Comments
 (0)