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