2 // Copyright 2019 gRPC authors.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <grpc/support/port_platform.h>
19 #include "src/core/ext/filters/client_channel/backend_metric.h"
21 #include "src/core/lib/gprpp/string_view.h"
22 #include "udpa/data/orca/v1/orca_load_report.upb.h"
28 template <typename EntryType>
29 Map<StringView, double, StringLess> ParseMap(
30 udpa_data_orca_v1_OrcaLoadReport* msg,
31 EntryType** (*entry_func)(udpa_data_orca_v1_OrcaLoadReport*, size_t*),
32 upb_strview (*key_func)(const EntryType*),
33 double (*value_func)(const EntryType*), Arena* arena) {
34 Map<StringView, double, StringLess> result;
36 const auto* const* entries = entry_func(msg, &size);
37 for (size_t i = 0; i < size; ++i) {
38 upb_strview key_view = key_func(entries[i]);
39 char* key = static_cast<char*>(arena->Alloc(key_view.size + 1));
40 memcpy(key, key_view.data, key_view.size);
41 result[StringView(key, key_view.size)] = value_func(entries[i]);
48 const LoadBalancingPolicy::BackendMetricData* ParseBackendMetricData(
49 const grpc_slice& serialized_load_report, Arena* arena) {
51 udpa_data_orca_v1_OrcaLoadReport* msg =
52 udpa_data_orca_v1_OrcaLoadReport_parse(
53 reinterpret_cast<const char*>(
54 GRPC_SLICE_START_PTR(serialized_load_report)),
55 GRPC_SLICE_LENGTH(serialized_load_report), upb_arena.ptr());
56 if (msg == nullptr) return nullptr;
57 LoadBalancingPolicy::BackendMetricData* backend_metric_data =
58 arena->New<LoadBalancingPolicy::BackendMetricData>();
59 backend_metric_data->cpu_utilization =
60 udpa_data_orca_v1_OrcaLoadReport_cpu_utilization(msg);
61 backend_metric_data->mem_utilization =
62 udpa_data_orca_v1_OrcaLoadReport_mem_utilization(msg);
63 backend_metric_data->requests_per_second =
64 udpa_data_orca_v1_OrcaLoadReport_rps(msg);
65 backend_metric_data->request_cost =
66 ParseMap<udpa_data_orca_v1_OrcaLoadReport_RequestCostEntry>(
67 msg, udpa_data_orca_v1_OrcaLoadReport_mutable_request_cost,
68 udpa_data_orca_v1_OrcaLoadReport_RequestCostEntry_key,
69 udpa_data_orca_v1_OrcaLoadReport_RequestCostEntry_value, arena);
70 backend_metric_data->utilization =
71 ParseMap<udpa_data_orca_v1_OrcaLoadReport_UtilizationEntry>(
72 msg, udpa_data_orca_v1_OrcaLoadReport_mutable_utilization,
73 udpa_data_orca_v1_OrcaLoadReport_UtilizationEntry_key,
74 udpa_data_orca_v1_OrcaLoadReport_UtilizationEntry_value, arena);
75 return backend_metric_data;
78 } // namespace grpc_core