mirror of https://github.com/grpc/grpc.git
[testing]: Add "orca_per_rpc" test case (#32524)
parent
ae55fb04ab
commit
c62ecd5cb4
20 changed files with 628 additions and 79 deletions
@ -0,0 +1,12 @@ |
|||||||
|
# Running a test locally during development |
||||||
|
|
||||||
|
To start a server during development: |
||||||
|
1. Choose an available port number. |
||||||
|
2. Start the server: |
||||||
|
``` |
||||||
|
GRPC_VERBOSITY=DEBUG ibazel run --compilation_mode=dbg //test/cpp/interop:interop_server -- --port={port_number} |
||||||
|
``` |
||||||
|
3. Start the client: |
||||||
|
``` |
||||||
|
GRPC_VERBOSITY=DEBUG ibazel run --test_output=streamed //test/cpp/interop:interop_client -- --server_port={port_number} --test_case={test_case} |
||||||
|
``` |
@ -0,0 +1,247 @@ |
|||||||
|
//
|
||||||
|
//
|
||||||
|
// Copyright 2023 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "test/cpp/interop/backend_metrics_lb_policy.h" |
||||||
|
|
||||||
|
#include "src/core/lib/iomgr/pollset_set.h" |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
namespace testing { |
||||||
|
|
||||||
|
namespace { |
||||||
|
|
||||||
|
using grpc_core::CoreConfiguration; |
||||||
|
using grpc_core::LoadBalancingPolicy; |
||||||
|
using grpc_core::MakeRefCounted; |
||||||
|
using grpc_core::OrphanablePtr; |
||||||
|
using grpc_core::RefCountedPtr; |
||||||
|
|
||||||
|
constexpr absl::string_view kBackendMetricsLbPolicyName = |
||||||
|
"test_backend_metrics_load_balancer"; |
||||||
|
constexpr absl::string_view kMetricsTrackerArgument = "orca_metrics_tracker"; |
||||||
|
|
||||||
|
LoadReportTracker::LoadReportEntry BackendMetricDataToOrcaLoadReport( |
||||||
|
const grpc_core::BackendMetricData* backend_metric_data) { |
||||||
|
if (backend_metric_data == nullptr) { |
||||||
|
return absl::nullopt; |
||||||
|
} |
||||||
|
xds::data::orca::v3::OrcaLoadReport load_report; |
||||||
|
load_report.set_cpu_utilization(backend_metric_data->cpu_utilization); |
||||||
|
load_report.set_mem_utilization(backend_metric_data->mem_utilization); |
||||||
|
load_report.set_rps_fractional(backend_metric_data->qps); |
||||||
|
for (const auto& p : backend_metric_data->request_cost) { |
||||||
|
std::string name(p.first); |
||||||
|
(*load_report.mutable_request_cost())[name] = p.second; |
||||||
|
} |
||||||
|
for (const auto& p : backend_metric_data->utilization) { |
||||||
|
std::string name(p.first); |
||||||
|
(*load_report.mutable_utilization())[name] = p.second; |
||||||
|
} |
||||||
|
return load_report; |
||||||
|
} |
||||||
|
|
||||||
|
class BackendMetricsLbPolicy : public LoadBalancingPolicy { |
||||||
|
public: |
||||||
|
explicit BackendMetricsLbPolicy(Args args) |
||||||
|
: LoadBalancingPolicy(std::move(args), /*initial_refcount=*/2) { |
||||||
|
load_report_tracker_ = |
||||||
|
channel_args().GetPointer<LoadReportTracker>(kMetricsTrackerArgument); |
||||||
|
GPR_ASSERT(load_report_tracker_ != nullptr); |
||||||
|
Args delegate_args; |
||||||
|
delegate_args.work_serializer = work_serializer(); |
||||||
|
delegate_args.args = channel_args(); |
||||||
|
delegate_args.channel_control_helper = |
||||||
|
std::make_unique<Helper>(RefCountedPtr<BackendMetricsLbPolicy>(this)); |
||||||
|
delegate_ = |
||||||
|
CoreConfiguration::Get().lb_policy_registry().CreateLoadBalancingPolicy( |
||||||
|
"pick_first", std::move(delegate_args)); |
||||||
|
grpc_pollset_set_add_pollset_set(delegate_->interested_parties(), |
||||||
|
interested_parties()); |
||||||
|
} |
||||||
|
|
||||||
|
~BackendMetricsLbPolicy() override = default; |
||||||
|
|
||||||
|
absl::string_view name() const override { |
||||||
|
return kBackendMetricsLbPolicyName; |
||||||
|
} |
||||||
|
|
||||||
|
absl::Status UpdateLocked(UpdateArgs args) override { |
||||||
|
return delegate_->UpdateLocked(std::move(args)); |
||||||
|
} |
||||||
|
|
||||||
|
void ExitIdleLocked() override { delegate_->ExitIdleLocked(); } |
||||||
|
|
||||||
|
void ResetBackoffLocked() override { delegate_->ResetBackoffLocked(); } |
||||||
|
|
||||||
|
private: |
||||||
|
class Picker : public SubchannelPicker { |
||||||
|
public: |
||||||
|
Picker(RefCountedPtr<SubchannelPicker> delegate_picker, |
||||||
|
LoadReportTracker* load_report_tracker) |
||||||
|
: delegate_picker_(std::move(delegate_picker)), |
||||||
|
load_report_tracker_(load_report_tracker) {} |
||||||
|
|
||||||
|
PickResult Pick(PickArgs args) override { |
||||||
|
// Do pick.
|
||||||
|
PickResult result = delegate_picker_->Pick(args); |
||||||
|
// Intercept trailing metadata.
|
||||||
|
auto* complete_pick = absl::get_if<PickResult::Complete>(&result.result); |
||||||
|
if (complete_pick != nullptr) { |
||||||
|
complete_pick->subchannel_call_tracker = |
||||||
|
std::make_unique<SubchannelCallTracker>(load_report_tracker_); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
RefCountedPtr<SubchannelPicker> delegate_picker_; |
||||||
|
LoadReportTracker* load_report_tracker_; |
||||||
|
}; |
||||||
|
|
||||||
|
class Helper : public ChannelControlHelper { |
||||||
|
public: |
||||||
|
explicit Helper(RefCountedPtr<BackendMetricsLbPolicy> parent) |
||||||
|
: parent_(std::move(parent)) {} |
||||||
|
|
||||||
|
RefCountedPtr<grpc_core::SubchannelInterface> CreateSubchannel( |
||||||
|
grpc_core::ServerAddress address, |
||||||
|
const grpc_core::ChannelArgs& args) override { |
||||||
|
return parent_->channel_control_helper()->CreateSubchannel( |
||||||
|
std::move(address), args); |
||||||
|
} |
||||||
|
|
||||||
|
void UpdateState(grpc_connectivity_state state, const absl::Status& status, |
||||||
|
RefCountedPtr<SubchannelPicker> picker) override { |
||||||
|
parent_->channel_control_helper()->UpdateState( |
||||||
|
state, status, |
||||||
|
MakeRefCounted<Picker>(std::move(picker), |
||||||
|
parent_->load_report_tracker_)); |
||||||
|
} |
||||||
|
|
||||||
|
void RequestReresolution() override { |
||||||
|
parent_->channel_control_helper()->RequestReresolution(); |
||||||
|
} |
||||||
|
|
||||||
|
absl::string_view GetAuthority() override { |
||||||
|
return parent_->channel_control_helper()->GetAuthority(); |
||||||
|
} |
||||||
|
|
||||||
|
grpc_event_engine::experimental::EventEngine* GetEventEngine() override { |
||||||
|
return parent_->channel_control_helper()->GetEventEngine(); |
||||||
|
} |
||||||
|
|
||||||
|
void AddTraceEvent(TraceSeverity severity, |
||||||
|
absl::string_view message) override { |
||||||
|
parent_->channel_control_helper()->AddTraceEvent(severity, message); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
RefCountedPtr<BackendMetricsLbPolicy> parent_; |
||||||
|
}; |
||||||
|
|
||||||
|
class SubchannelCallTracker : public SubchannelCallTrackerInterface { |
||||||
|
public: |
||||||
|
explicit SubchannelCallTracker(LoadReportTracker* load_report_tracker) |
||||||
|
: load_report_tracker_(load_report_tracker) {} |
||||||
|
|
||||||
|
void Start() override {} |
||||||
|
|
||||||
|
void Finish(FinishArgs args) override { |
||||||
|
load_report_tracker_->RecordPerRpcLoadReport( |
||||||
|
args.backend_metric_accessor->GetBackendMetricData()); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
LoadReportTracker* load_report_tracker_; |
||||||
|
}; |
||||||
|
|
||||||
|
void ShutdownLocked() override { |
||||||
|
grpc_pollset_set_del_pollset_set(delegate_->interested_parties(), |
||||||
|
interested_parties()); |
||||||
|
delegate_.reset(); |
||||||
|
} |
||||||
|
|
||||||
|
OrphanablePtr<LoadBalancingPolicy> delegate_; |
||||||
|
LoadReportTracker* load_report_tracker_; |
||||||
|
}; |
||||||
|
|
||||||
|
class BackendMetricsLbPolicyFactory |
||||||
|
: public grpc_core::LoadBalancingPolicyFactory { |
||||||
|
private: |
||||||
|
class BackendMetricsLbPolicyFactoryConfig |
||||||
|
: public LoadBalancingPolicy::Config { |
||||||
|
private: |
||||||
|
absl::string_view name() const override { |
||||||
|
return kBackendMetricsLbPolicyName; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
absl::string_view name() const override { |
||||||
|
return kBackendMetricsLbPolicyName; |
||||||
|
} |
||||||
|
|
||||||
|
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
||||||
|
LoadBalancingPolicy::Args args) const override { |
||||||
|
return grpc_core::MakeOrphanable<BackendMetricsLbPolicy>(std::move(args)); |
||||||
|
} |
||||||
|
|
||||||
|
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
||||||
|
ParseLoadBalancingConfig(const grpc_core::Json& /*json*/) const override { |
||||||
|
return MakeRefCounted<BackendMetricsLbPolicyFactoryConfig>(); |
||||||
|
} |
||||||
|
}; |
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void RegisterBackendMetricsLbPolicy(CoreConfiguration::Builder* builder) { |
||||||
|
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
||||||
|
std::make_unique<BackendMetricsLbPolicyFactory>()); |
||||||
|
} |
||||||
|
|
||||||
|
void LoadReportTracker::RecordPerRpcLoadReport( |
||||||
|
const grpc_core::BackendMetricData* backend_metric_data) { |
||||||
|
absl::MutexLock lock(&per_rpc_load_reports_mu_); |
||||||
|
per_rpc_load_reports_.emplace_back( |
||||||
|
BackendMetricDataToOrcaLoadReport(backend_metric_data)); |
||||||
|
} |
||||||
|
|
||||||
|
absl::optional<LoadReportTracker::LoadReportEntry> |
||||||
|
LoadReportTracker::GetNextLoadReport() { |
||||||
|
absl::MutexLock lock(&per_rpc_load_reports_mu_); |
||||||
|
if (per_rpc_load_reports_.empty()) { |
||||||
|
return absl::nullopt; |
||||||
|
} |
||||||
|
auto report = std::move(per_rpc_load_reports_.front()); |
||||||
|
per_rpc_load_reports_.pop_front(); |
||||||
|
return report; |
||||||
|
} |
||||||
|
|
||||||
|
void LoadReportTracker::ResetCollectedLoadReports() { |
||||||
|
absl::MutexLock lock(&per_rpc_load_reports_mu_); |
||||||
|
per_rpc_load_reports_.clear(); |
||||||
|
} |
||||||
|
|
||||||
|
ChannelArguments LoadReportTracker::GetChannelArguments() { |
||||||
|
ChannelArguments arguments; |
||||||
|
arguments.SetPointer(std::string(kMetricsTrackerArgument), this); |
||||||
|
return arguments; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace testing
|
||||||
|
} // namespace grpc
|
@ -0,0 +1,54 @@ |
|||||||
|
//
|
||||||
|
//
|
||||||
|
// Copyright 2023 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GRPC_TEST_CPP_INTEROP_BACKEND_METRICS_LB_POLICY_H |
||||||
|
#define GRPC_TEST_CPP_INTEROP_BACKEND_METRICS_LB_POLICY_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include <grpcpp/support/channel_arguments.h> |
||||||
|
|
||||||
|
#include "src/core/lib/config/core_configuration.h" |
||||||
|
#include "src/proto/grpc/testing/xds/v3/orca_load_report.pb.h" |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
namespace testing { |
||||||
|
class LoadReportTracker { |
||||||
|
public: |
||||||
|
// A load report, or nullopt if the call had no load report.
|
||||||
|
using LoadReportEntry = absl::optional<xds::data::orca::v3::OrcaLoadReport>; |
||||||
|
|
||||||
|
ChannelArguments GetChannelArguments(); |
||||||
|
void ResetCollectedLoadReports(); |
||||||
|
void RecordPerRpcLoadReport( |
||||||
|
const grpc_core::BackendMetricData* backend_metric_data); |
||||||
|
// Returns the next load report, or nullopt if the queue is empty.
|
||||||
|
absl::optional<LoadReportEntry> GetNextLoadReport(); |
||||||
|
|
||||||
|
private: |
||||||
|
std::deque<LoadReportEntry> per_rpc_load_reports_ |
||||||
|
ABSL_GUARDED_BY(per_rpc_load_reports_mu_); |
||||||
|
absl::Mutex per_rpc_load_reports_mu_; |
||||||
|
}; |
||||||
|
|
||||||
|
void RegisterBackendMetricsLbPolicy( |
||||||
|
grpc_core::CoreConfiguration::Builder* builder); |
||||||
|
} // namespace testing
|
||||||
|
} // namespace grpc
|
||||||
|
|
||||||
|
#endif // GRPC_TEST_CPP_INTEROP_BACKEND_METRICS_LB_POLICY_H
|
Loading…
Reference in new issue