1 //
2 // Copyright 2019 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <grpc/support/port_platform.h>
18
19 #include "src/core/load_balancing/backend_metric_parser.h"
20
21 #include <string.h>
22
23 #include <map>
24
25 #include "absl/strings/string_view.h"
26 #include "upb/base/string_view.h"
27 #include "upb/mem/arena.hpp"
28 #include "upb/message/map.h"
29 #include "xds/data/orca/v3/orca_load_report.upb.h"
30
31 namespace grpc_core {
32
33 namespace {
34
35 template <typename EntryType>
ParseMap(xds_data_orca_v3_OrcaLoadReport * msg,const EntryType * (* entry_func)(const xds_data_orca_v3_OrcaLoadReport *,size_t *),upb_StringView (* key_func)(const EntryType *),double (* value_func)(const EntryType *),BackendMetricAllocatorInterface * allocator)36 std::map<absl::string_view, double> ParseMap(
37 xds_data_orca_v3_OrcaLoadReport* msg,
38 const EntryType* (*entry_func)(const xds_data_orca_v3_OrcaLoadReport*,
39 size_t*),
40 upb_StringView (*key_func)(const EntryType*),
41 double (*value_func)(const EntryType*),
42 BackendMetricAllocatorInterface* allocator) {
43 std::map<absl::string_view, double> result;
44 size_t i = kUpb_Map_Begin;
45 while (true) {
46 const auto* entry = entry_func(msg, &i);
47 if (entry == nullptr) break;
48 upb_StringView key_view = key_func(entry);
49 char* key = allocator->AllocateString(key_view.size);
50 memcpy(key, key_view.data, key_view.size);
51 result[absl::string_view(key, key_view.size)] = value_func(entry);
52 }
53 return result;
54 }
55
56 } // namespace
57
ParseBackendMetricData(absl::string_view serialized_load_report,BackendMetricAllocatorInterface * allocator)58 const BackendMetricData* ParseBackendMetricData(
59 absl::string_view serialized_load_report,
60 BackendMetricAllocatorInterface* allocator) {
61 upb::Arena upb_arena;
62 xds_data_orca_v3_OrcaLoadReport* msg = xds_data_orca_v3_OrcaLoadReport_parse(
63 serialized_load_report.data(), serialized_load_report.size(),
64 upb_arena.ptr());
65 if (msg == nullptr) return nullptr;
66 BackendMetricData* backend_metric_data =
67 allocator->AllocateBackendMetricData();
68 backend_metric_data->cpu_utilization =
69 xds_data_orca_v3_OrcaLoadReport_cpu_utilization(msg);
70 backend_metric_data->mem_utilization =
71 xds_data_orca_v3_OrcaLoadReport_mem_utilization(msg);
72 backend_metric_data->application_utilization =
73 xds_data_orca_v3_OrcaLoadReport_application_utilization(msg);
74 backend_metric_data->qps =
75 xds_data_orca_v3_OrcaLoadReport_rps_fractional(msg);
76 backend_metric_data->eps = xds_data_orca_v3_OrcaLoadReport_eps(msg);
77 backend_metric_data->request_cost =
78 ParseMap<xds_data_orca_v3_OrcaLoadReport_RequestCostEntry>(
79 msg, xds_data_orca_v3_OrcaLoadReport_request_cost_next,
80 xds_data_orca_v3_OrcaLoadReport_RequestCostEntry_key,
81 xds_data_orca_v3_OrcaLoadReport_RequestCostEntry_value, allocator);
82 backend_metric_data->utilization =
83 ParseMap<xds_data_orca_v3_OrcaLoadReport_UtilizationEntry>(
84 msg, xds_data_orca_v3_OrcaLoadReport_utilization_next,
85 xds_data_orca_v3_OrcaLoadReport_UtilizationEntry_key,
86 xds_data_orca_v3_OrcaLoadReport_UtilizationEntry_value, allocator);
87 backend_metric_data->named_metrics =
88 ParseMap<xds_data_orca_v3_OrcaLoadReport_NamedMetricsEntry>(
89 msg, xds_data_orca_v3_OrcaLoadReport_named_metrics_next,
90 xds_data_orca_v3_OrcaLoadReport_NamedMetricsEntry_key,
91 xds_data_orca_v3_OrcaLoadReport_NamedMetricsEntry_value, allocator);
92 return backend_metric_data;
93 }
94
95 } // namespace grpc_core
96