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/ext/filters/client_channel/backend_metric.h"
20
21 #include "absl/strings/string_view.h"
22
23 #include "udpa/data/orca/v1/orca_load_report.upb.h"
24
25 #include "src/core/lib/gprpp/map.h"
26
27 namespace grpc_core {
28
29 namespace {
30
31 template <typename EntryType>
ParseMap(udpa_data_orca_v1_OrcaLoadReport * msg,EntryType ** (* entry_func)(udpa_data_orca_v1_OrcaLoadReport *,size_t *),upb_strview (* key_func)(const EntryType *),double (* value_func)(const EntryType *),Arena * arena)32 std::map<absl::string_view, double, StringLess> ParseMap(
33 udpa_data_orca_v1_OrcaLoadReport* msg,
34 EntryType** (*entry_func)(udpa_data_orca_v1_OrcaLoadReport*, size_t*),
35 upb_strview (*key_func)(const EntryType*),
36 double (*value_func)(const EntryType*), Arena* arena) {
37 std::map<absl::string_view, double, StringLess> result;
38 size_t size;
39 const auto* const* entries = entry_func(msg, &size);
40 for (size_t i = 0; i < size; ++i) {
41 upb_strview key_view = key_func(entries[i]);
42 char* key = static_cast<char*>(arena->Alloc(key_view.size + 1));
43 memcpy(key, key_view.data, key_view.size);
44 result[absl::string_view(key, key_view.size)] = value_func(entries[i]);
45 }
46 return result;
47 }
48
49 } // namespace
50
ParseBackendMetricData(const grpc_slice & serialized_load_report,Arena * arena)51 const LoadBalancingPolicy::BackendMetricData* ParseBackendMetricData(
52 const grpc_slice& serialized_load_report, Arena* arena) {
53 upb::Arena upb_arena;
54 udpa_data_orca_v1_OrcaLoadReport* msg =
55 udpa_data_orca_v1_OrcaLoadReport_parse(
56 reinterpret_cast<const char*>(
57 GRPC_SLICE_START_PTR(serialized_load_report)),
58 GRPC_SLICE_LENGTH(serialized_load_report), upb_arena.ptr());
59 if (msg == nullptr) return nullptr;
60 LoadBalancingPolicy::BackendMetricData* backend_metric_data =
61 arena->New<LoadBalancingPolicy::BackendMetricData>();
62 backend_metric_data->cpu_utilization =
63 udpa_data_orca_v1_OrcaLoadReport_cpu_utilization(msg);
64 backend_metric_data->mem_utilization =
65 udpa_data_orca_v1_OrcaLoadReport_mem_utilization(msg);
66 backend_metric_data->requests_per_second =
67 udpa_data_orca_v1_OrcaLoadReport_rps(msg);
68 backend_metric_data->request_cost =
69 ParseMap<udpa_data_orca_v1_OrcaLoadReport_RequestCostEntry>(
70 msg, udpa_data_orca_v1_OrcaLoadReport_mutable_request_cost,
71 udpa_data_orca_v1_OrcaLoadReport_RequestCostEntry_key,
72 udpa_data_orca_v1_OrcaLoadReport_RequestCostEntry_value, arena);
73 backend_metric_data->utilization =
74 ParseMap<udpa_data_orca_v1_OrcaLoadReport_UtilizationEntry>(
75 msg, udpa_data_orca_v1_OrcaLoadReport_mutable_utilization,
76 udpa_data_orca_v1_OrcaLoadReport_UtilizationEntry_key,
77 udpa_data_orca_v1_OrcaLoadReport_UtilizationEntry_value, arena);
78 return backend_metric_data;
79 }
80
81 } // namespace grpc_core
82