xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/backend_metrics_lb_policy.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_TEST_CPP_INTEROP_BACKEND_METRICS_LB_POLICY_H
20 #define GRPC_TEST_CPP_INTEROP_BACKEND_METRICS_LB_POLICY_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <grpcpp/support/channel_arguments.h>
24 
25 #include "src/core/lib/config/core_configuration.h"
26 #include "src/proto/grpc/testing/messages.pb.h"
27 
28 namespace grpc {
29 namespace testing {
30 class LoadReportTracker {
31  public:
32   // A load report, or nullopt if the call had no load report.
33   using LoadReportEntry = absl::optional<TestOrcaReport>;
34 
35   ChannelArguments GetChannelArguments();
36   void ResetCollectedLoadReports();
37   void RecordPerRpcLoadReport(
38       const grpc_core::BackendMetricData* backend_metric_data);
39   void RecordOobLoadReport(const grpc_core::BackendMetricData& oob_metric_data);
40   // Returns the next per-RPC load report, or nullopt if the queue is empty.
41   absl::optional<LoadReportEntry> GetNextLoadReport();
42   LoadReportEntry WaitForOobLoadReport(
43       const std::function<bool(const TestOrcaReport&)>& predicate,
44       absl::Duration poll_timeout, size_t max_attempts);
45 
46  private:
47   std::deque<LoadReportEntry> per_rpc_load_reports_
48       ABSL_GUARDED_BY(load_reports_mu_);
49   std::deque<TestOrcaReport> oob_load_reports_
50       ABSL_GUARDED_BY(load_reports_mu_);
51   grpc_core::Mutex load_reports_mu_;
52   grpc_core::CondVar load_reports_cv_ ABSL_GUARDED_BY(load_reports_mu_);
53 };
54 
55 void RegisterBackendMetricsLbPolicy(
56     grpc_core::CoreConfiguration::Builder* builder);
57 }  // namespace testing
58 }  // namespace grpc
59 
60 #endif  // GRPC_TEST_CPP_INTEROP_BACKEND_METRICS_LB_POLICY_H
61