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 "src/core/load_balancing/backend_metric_parser.h"
18
19 #include <grpc/support/port_platform.h>
20 #include <string.h>
21
22 #include <map>
23
24 #include "absl/strings/string_view.h"
25 #include "upb/base/string_view.h"
26 #include "upb/mem/arena.hpp"
27 #include "upb/message/map.h"
28 #include "xds/data/orca/v3/orca_load_report.upb.h"
29
30 namespace grpc_core {
31
32 namespace {
33
34 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)35 std::map<absl::string_view, double> ParseMap(
36 xds_data_orca_v3_OrcaLoadReport* msg,
37 const EntryType* (*entry_func)(const xds_data_orca_v3_OrcaLoadReport*,
38 size_t*),
39 upb_StringView (*key_func)(const EntryType*),
40 double (*value_func)(const EntryType*),
41 BackendMetricAllocatorInterface* allocator) {
42 std::map<absl::string_view, double> result;
43 size_t i = kUpb_Map_Begin;
44 while (true) {
45 const auto* entry = entry_func(msg, &i);
46 if (entry == nullptr) break;
47 upb_StringView key_view = key_func(entry);
48 char* key = allocator->AllocateString(key_view.size);
49 memcpy(key, key_view.data, key_view.size);
50 result[absl::string_view(key, key_view.size)] = value_func(entry);
51 }
52 return result;
53 }
54
55 } // namespace
56
ParseBackendMetricData(absl::string_view serialized_load_report,BackendMetricAllocatorInterface * allocator)57 const BackendMetricData* ParseBackendMetricData(
58 absl::string_view serialized_load_report,
59 BackendMetricAllocatorInterface* allocator) {
60 upb::Arena upb_arena;
61 xds_data_orca_v3_OrcaLoadReport* msg = xds_data_orca_v3_OrcaLoadReport_parse(
62 serialized_load_report.data(), serialized_load_report.size(),
63 upb_arena.ptr());
64 if (msg == nullptr) return nullptr;
65 BackendMetricData* backend_metric_data =
66 allocator->AllocateBackendMetricData();
67 backend_metric_data->cpu_utilization =
68 xds_data_orca_v3_OrcaLoadReport_cpu_utilization(msg);
69 backend_metric_data->mem_utilization =
70 xds_data_orca_v3_OrcaLoadReport_mem_utilization(msg);
71 backend_metric_data->application_utilization =
72 xds_data_orca_v3_OrcaLoadReport_application_utilization(msg);
73 backend_metric_data->qps =
74 xds_data_orca_v3_OrcaLoadReport_rps_fractional(msg);
75 backend_metric_data->eps = xds_data_orca_v3_OrcaLoadReport_eps(msg);
76 backend_metric_data->request_cost =
77 ParseMap<xds_data_orca_v3_OrcaLoadReport_RequestCostEntry>(
78 msg, xds_data_orca_v3_OrcaLoadReport_request_cost_next,
79 xds_data_orca_v3_OrcaLoadReport_RequestCostEntry_key,
80 xds_data_orca_v3_OrcaLoadReport_RequestCostEntry_value, allocator);
81 backend_metric_data->utilization =
82 ParseMap<xds_data_orca_v3_OrcaLoadReport_UtilizationEntry>(
83 msg, xds_data_orca_v3_OrcaLoadReport_utilization_next,
84 xds_data_orca_v3_OrcaLoadReport_UtilizationEntry_key,
85 xds_data_orca_v3_OrcaLoadReport_UtilizationEntry_value, allocator);
86 backend_metric_data->named_metrics =
87 ParseMap<xds_data_orca_v3_OrcaLoadReport_NamedMetricsEntry>(
88 msg, xds_data_orca_v3_OrcaLoadReport_named_metrics_next,
89 xds_data_orca_v3_OrcaLoadReport_NamedMetricsEntry_key,
90 xds_data_orca_v3_OrcaLoadReport_NamedMetricsEntry_value, allocator);
91 return backend_metric_data;
92 }
93
94 } // namespace grpc_core
95