| 1 |
// |
| 2 |
// Copyright 2018 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 "test/core/util/test_lb_policies.h" |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <memory> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "absl/status/statusor.h" |
| 26 |
#include "absl/strings/string_view.h" |
| 27 |
#include "absl/types/variant.h" |
| 28 |
|
| 29 |
#include <grpc/event_engine/event_engine.h> |
| 30 |
#include <grpc/grpc.h> |
| 31 |
#include <grpc/support/log.h> |
| 32 |
|
| 33 |
#include "src/core/ext/filters/client_channel/lb_policy/oob_backend_metric.h" |
| 34 |
#include "src/core/lib/address_utils/parse_address.h" |
| 35 |
#include "src/core/lib/channel/channel_args.h" |
| 36 |
#include "src/core/lib/config/core_configuration.h" |
| 37 |
#include "src/core/lib/gprpp/orphanable.h" |
| 38 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 39 |
#include "src/core/lib/gprpp/status_helper.h" |
| 40 |
#include "src/core/lib/gprpp/time.h" |
| 41 |
#include "src/core/lib/iomgr/error.h" |
| 42 |
#include "src/core/lib/iomgr/pollset_set.h" |
| 43 |
#include "src/core/lib/iomgr/resolved_address.h" |
| 44 |
#include "src/core/lib/json/json.h" |
| 45 |
#include "src/core/lib/json/json_util.h" |
| 46 |
#include "src/core/lib/load_balancing/lb_policy.h" |
| 47 |
#include "src/core/lib/load_balancing/lb_policy_factory.h" |
| 48 |
#include "src/core/lib/load_balancing/lb_policy_registry.h" |
| 49 |
#include "src/core/lib/load_balancing/subchannel_interface.h" |
| 50 |
#include "src/core/lib/uri/uri_parser.h" |
| 51 |
|
| 52 |
namespace grpc_core { |
| 53 |
|
| 54 |
namespace { |
| 55 |
|
| 56 |
// |
| 57 |
// ForwardingLoadBalancingPolicy |
| 58 |
// |
| 59 |
|
| 60 |
// A minimal forwarding class to avoid implementing a standalone test LB. |
| 61 |
class ForwardingLoadBalancingPolicy : public LoadBalancingPolicy { |
| 62 |
public: |
| 63 |
ForwardingLoadBalancingPolicy( |
| 64 |
std::unique_ptr<ChannelControlHelper> delegating_helper, Args args, |
| 65 |
absl::string_view delegate_policy_name, intptr_t initial_refcount = 1) |
| 66 |
: LoadBalancingPolicy(std::move(args), initial_refcount) { |
| 67 |
Args delegate_args; |
| 68 |
delegate_args.work_serializer = work_serializer(); |
| 69 |
delegate_args.channel_control_helper = std::move(delegating_helper); |
| 70 |
delegate_args.args = channel_args(); |
| 71 |
delegate_ = |
| 72 |
CoreConfiguration::Get().lb_policy_registry().CreateLoadBalancingPolicy( |
| 73 |
delegate_policy_name, std::move(delegate_args)); |
| 74 |
grpc_pollset_set_add_pollset_set(delegate_->interested_parties(), |
| 75 |
interested_parties()); |
| 76 |
} |
| 77 |
|
| 78 |
~ForwardingLoadBalancingPolicy() override = default; |
| 79 |
|
| 80 |
absl::Status UpdateLocked(UpdateArgs args) override { |
| 81 |
return delegate_->UpdateLocked(std::move(args)); |
| 82 |
} |
| 83 |
|
| 84 |
void ExitIdleLocked() override { delegate_->ExitIdleLocked(); } |
| 85 |
|
| 86 |
void ResetBackoffLocked() override { delegate_->ResetBackoffLocked(); } |
| 87 |
|
| 88 |
private: |
| 89 |
void ShutdownLocked() override { delegate_.reset(); } |
| 90 |
|
| 91 |
OrphanablePtr<LoadBalancingPolicy> delegate_; |
| 92 |
}; |
| 93 |
|
| 94 |
// |
| 95 |
// TestPickArgsLb |
| 96 |
// |
| 97 |
|
| 98 |
constexpr absl::string_view kTestPickArgsLbPolicyName = "test_pick_args_lb"; |
| 99 |
|
| 100 |
class TestPickArgsLb : public ForwardingLoadBalancingPolicy { |
| 101 |
public: |
| 102 |
TestPickArgsLb(Args args, TestPickArgsCallback cb, |
| 103 |
absl::string_view delegate_policy_name) |
| 104 |
: ForwardingLoadBalancingPolicy( |
| 105 |
std::make_unique<Helper>(RefCountedPtr<TestPickArgsLb>(this), cb), |
| 106 |
std::move(args), delegate_policy_name, |
| 107 |
/*initial_refcount=*/2) {} |
| 108 |
|
| 109 |
~TestPickArgsLb() override = default; |
| 110 |
|
| 111 |
absl::string_view name() const override { return kTestPickArgsLbPolicyName; } |
| 112 |
|
| 113 |
private: |
| 114 |
class Picker : public SubchannelPicker { |
| 115 |
public: |
| 116 |
Picker(std::unique_ptr<SubchannelPicker> delegate_picker, |
| 117 |
TestPickArgsCallback cb) |
| 118 |
: delegate_picker_(std::move(delegate_picker)), cb_(std::move(cb)) {} |
| 119 |
|
| 120 |
PickResult Pick(PickArgs args) override { |
| 121 |
// Report args seen. |
| 122 |
PickArgsSeen args_seen; |
| 123 |
args_seen.path = std::string(args.path); |
| 124 |
args_seen.metadata = args.initial_metadata->TestOnlyCopyToVector(); |
| 125 |
cb_(args_seen); |
| 126 |
// Do pick. |
| 127 |
return delegate_picker_->Pick(args); |
| 128 |
} |
| 129 |
|
| 130 |
private: |
| 131 |
std::unique_ptr<SubchannelPicker> delegate_picker_; |
| 132 |
TestPickArgsCallback cb_; |
| 133 |
}; |
| 134 |
|
| 135 |
class Helper : public ChannelControlHelper { |
| 136 |
public: |
| 137 |
Helper(RefCountedPtr<TestPickArgsLb> parent, TestPickArgsCallback cb) |
| 138 |
: parent_(std::move(parent)), cb_(std::move(cb)) {} |
| 139 |
|
| 140 |
RefCountedPtr<SubchannelInterface> CreateSubchannel( |
| 141 |
ServerAddress address, const ChannelArgs& args) override { |
| 142 |
return parent_->channel_control_helper()->CreateSubchannel( |
| 143 |
std::move(address), args); |
| 144 |
} |
| 145 |
|
| 146 |
void UpdateState(grpc_connectivity_state state, const absl::Status& status, |
| 147 |
std::unique_ptr<SubchannelPicker> picker) override { |
| 148 |
parent_->channel_control_helper()->UpdateState( |
| 149 |
state, status, std::make_unique<Picker>(std::move(picker), cb_)); |
| 150 |
} |
| 151 |
|
| 152 |
void RequestReresolution() override { |
| 153 |
parent_->channel_control_helper()->RequestReresolution(); |
| 154 |
} |
| 155 |
|
| 156 |
absl::string_view GetAuthority() override { |
| 157 |
return parent_->channel_control_helper()->GetAuthority(); |
| 158 |
} |
| 159 |
|
| 160 |
grpc_event_engine::experimental::EventEngine* GetEventEngine() override { |
| 161 |
return parent_->channel_control_helper()->GetEventEngine(); |
| 162 |
} |
| 163 |
|
| 164 |
void AddTraceEvent(TraceSeverity severity, |
| 165 |
absl::string_view message) override { |
| 166 |
parent_->channel_control_helper()->AddTraceEvent(severity, message); |
| 167 |
} |
| 168 |
|
| 169 |
private: |
| 170 |
RefCountedPtr<TestPickArgsLb> parent_; |
| 171 |
TestPickArgsCallback cb_; |
| 172 |
}; |
| 173 |
}; |
| 174 |
|
| 175 |
class TestPickArgsLbConfig : public LoadBalancingPolicy::Config { |
| 176 |
public: |
| 177 |
absl::string_view name() const override { return kTestPickArgsLbPolicyName; } |
| 178 |
}; |
| 179 |
|
| 180 |
class TestPickArgsLbFactory : public LoadBalancingPolicyFactory { |
| 181 |
public: |
| 182 |
explicit TestPickArgsLbFactory(TestPickArgsCallback cb, |
| 183 |
absl::string_view delegate_policy_name) |
| 184 |
: cb_(std::move(cb)), delegate_policy_name_(delegate_policy_name) {} |
| 185 |
|
| 186 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 187 |
LoadBalancingPolicy::Args args) const override { |
| 188 |
return MakeOrphanable<TestPickArgsLb>(std::move(args), cb_, |
| 189 |
delegate_policy_name_); |
| 190 |
} |
| 191 |
|
| 192 |
absl::string_view name() const override { return kTestPickArgsLbPolicyName; } |
| 193 |
|
| 194 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 195 |
ParseLoadBalancingConfig(const Json& /*json*/) const override { |
| 196 |
return MakeRefCounted<TestPickArgsLbConfig>(); |
| 197 |
} |
| 198 |
|
| 199 |
private: |
| 200 |
TestPickArgsCallback cb_; |
| 201 |
std::string delegate_policy_name_; |
| 202 |
}; |
| 203 |
|
| 204 |
// |
| 205 |
// InterceptRecvTrailingMetadataLoadBalancingPolicy |
| 206 |
// |
| 207 |
|
| 208 |
constexpr char kInterceptRecvTrailingMetadataLbPolicyName[] = |
| 209 |
"intercept_trailing_metadata_lb"; |
| 210 |
|
| 211 |
class InterceptRecvTrailingMetadataLoadBalancingPolicy |
| 212 |
: public ForwardingLoadBalancingPolicy { |
| 213 |
public: |
| 214 |
InterceptRecvTrailingMetadataLoadBalancingPolicy( |
| 215 |
Args args, InterceptRecvTrailingMetadataCallback cb) |
| 216 |
: ForwardingLoadBalancingPolicy( |
| 217 |
std::make_unique<Helper>( |
| 218 |
RefCountedPtr<InterceptRecvTrailingMetadataLoadBalancingPolicy>( |
| 219 |
this), |
| 220 |
std::move(cb)), |
| 221 |
std::move(args), |
| 222 |
/*delegate_policy_name=*/"pick_first", |
| 223 |
/*initial_refcount=*/2) {} |
| 224 |
|
| 225 |
~InterceptRecvTrailingMetadataLoadBalancingPolicy() override = default; |
| 226 |
|
| 227 |
absl::string_view name() const override { |
| 228 |
return kInterceptRecvTrailingMetadataLbPolicyName; |
| 229 |
} |
| 230 |
|
| 231 |
private: |
| 232 |
class Picker : public SubchannelPicker { |
| 233 |
public: |
| 234 |
Picker(std::unique_ptr<SubchannelPicker> delegate_picker, |
| 235 |
InterceptRecvTrailingMetadataCallback cb) |
| 236 |
: delegate_picker_(std::move(delegate_picker)), cb_(std::move(cb)) {} |
| 237 |
|
| 238 |
PickResult Pick(PickArgs args) override { |
| 239 |
// Do pick. |
| 240 |
PickResult result = delegate_picker_->Pick(args); |
| 241 |
// Intercept trailing metadata. |
| 242 |
auto* complete_pick = absl::get_if<PickResult::Complete>(&result.result); |
| 243 |
if (complete_pick != nullptr) { |
| 244 |
complete_pick->subchannel_call_tracker = |
| 245 |
std::make_unique<SubchannelCallTracker>(cb_); |
| 246 |
} |
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
private: |
| 251 |
std::unique_ptr<SubchannelPicker> delegate_picker_; |
| 252 |
InterceptRecvTrailingMetadataCallback cb_; |
| 253 |
}; |
| 254 |
|
| 255 |
class Helper : public ChannelControlHelper { |
| 256 |
public: |
| 257 |
Helper( |
| 258 |
RefCountedPtr<InterceptRecvTrailingMetadataLoadBalancingPolicy> parent, |
| 259 |
InterceptRecvTrailingMetadataCallback cb) |
| 260 |
: parent_(std::move(parent)), cb_(std::move(cb)) {} |
| 261 |
|
| 262 |
RefCountedPtr<SubchannelInterface> CreateSubchannel( |
| 263 |
ServerAddress address, const ChannelArgs& args) override { |
| 264 |
return parent_->channel_control_helper()->CreateSubchannel( |
| 265 |
std::move(address), args); |
| 266 |
} |
| 267 |
|
| 268 |
void UpdateState(grpc_connectivity_state state, const absl::Status& status, |
| 269 |
std::unique_ptr<SubchannelPicker> picker) override { |
| 270 |
parent_->channel_control_helper()->UpdateState( |
| 271 |
state, status, std::make_unique<Picker>(std::move(picker), cb_)); |
| 272 |
} |
| 273 |
|
| 274 |
void RequestReresolution() override { |
| 275 |
parent_->channel_control_helper()->RequestReresolution(); |
| 276 |
} |
| 277 |
|
| 278 |
absl::string_view GetAuthority() override { |
| 279 |
return parent_->channel_control_helper()->GetAuthority(); |
| 280 |
} |
| 281 |
|
| 282 |
grpc_event_engine::experimental::EventEngine* GetEventEngine() override { |
| 283 |
return parent_->channel_control_helper()->GetEventEngine(); |
| 284 |
} |
| 285 |
|
| 286 |
void AddTraceEvent(TraceSeverity severity, |
| 287 |
absl::string_view message) override { |
| 288 |
parent_->channel_control_helper()->AddTraceEvent(severity, message); |
| 289 |
} |
| 290 |
|
| 291 |
private: |
| 292 |
RefCountedPtr<InterceptRecvTrailingMetadataLoadBalancingPolicy> parent_; |
| 293 |
InterceptRecvTrailingMetadataCallback cb_; |
| 294 |
}; |
| 295 |
|
| 296 |
class SubchannelCallTracker : public SubchannelCallTrackerInterface { |
| 297 |
public: |
| 298 |
explicit SubchannelCallTracker(InterceptRecvTrailingMetadataCallback cb) |
| 299 |
: cb_(std::move(cb)) {} |
| 300 |
|
| 301 |
void Start() override {} |
| 302 |
|
| 303 |
void Finish(FinishArgs args) override { |
| 304 |
TrailingMetadataArgsSeen args_seen; |
| 305 |
args_seen.status = args.status; |
| 306 |
args_seen.backend_metric_data = |
| 307 |
args.backend_metric_accessor->GetBackendMetricData(); |
| 308 |
args_seen.metadata = args.trailing_metadata->TestOnlyCopyToVector(); |
| 309 |
cb_(args_seen); |
| 310 |
} |
| 311 |
|
| 312 |
private: |
| 313 |
InterceptRecvTrailingMetadataCallback cb_; |
| 314 |
}; |
| 315 |
}; |
| 316 |
|
| 317 |
class InterceptTrailingConfig : public LoadBalancingPolicy::Config { |
| 318 |
public: |
| 319 |
absl::string_view name() const override { |
| 320 |
return kInterceptRecvTrailingMetadataLbPolicyName; |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
class InterceptTrailingFactory : public LoadBalancingPolicyFactory { |
| 325 |
public: |
| 326 |
explicit InterceptTrailingFactory(InterceptRecvTrailingMetadataCallback cb) |
| 327 |
: cb_(std::move(cb)) {} |
| 328 |
|
| 329 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 330 |
LoadBalancingPolicy::Args args) const override { |
| 331 |
return MakeOrphanable<InterceptRecvTrailingMetadataLoadBalancingPolicy>( |
| 332 |
std::move(args), cb_); |
| 333 |
} |
| 334 |
|
| 335 |
absl::string_view name() const override { |
| 336 |
return kInterceptRecvTrailingMetadataLbPolicyName; |
| 337 |
} |
| 338 |
|
| 339 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 340 |
ParseLoadBalancingConfig(const Json& /*json*/) const override { |
| 341 |
return MakeRefCounted<InterceptTrailingConfig>(); |
| 342 |
} |
| 343 |
|
| 344 |
private: |
| 345 |
InterceptRecvTrailingMetadataCallback cb_; |
| 346 |
}; |
| 347 |
|
| 348 |
// |
| 349 |
// AddressTestLoadBalancingPolicy |
| 350 |
// |
| 351 |
|
| 352 |
constexpr char kAddressTestLbPolicyName[] = "address_test_lb"; |
| 353 |
|
| 354 |
class AddressTestLoadBalancingPolicy : public ForwardingLoadBalancingPolicy { |
| 355 |
public: |
| 356 |
AddressTestLoadBalancingPolicy(Args args, AddressTestCallback cb) |
| 357 |
: ForwardingLoadBalancingPolicy( |
| 358 |
std::make_unique<Helper>( |
| 359 |
RefCountedPtr<AddressTestLoadBalancingPolicy>(this), |
| 360 |
std::move(cb)), |
| 361 |
std::move(args), |
| 362 |
/*delegate_policy_name=*/"pick_first", |
| 363 |
/*initial_refcount=*/2) {} |
| 364 |
|
| 365 |
~AddressTestLoadBalancingPolicy() override = default; |
| 366 |
|
| 367 |
absl::string_view name() const override { return kAddressTestLbPolicyName; } |
| 368 |
|
| 369 |
private: |
| 370 |
class Helper : public ChannelControlHelper { |
| 371 |
public: |
| 372 |
Helper(RefCountedPtr<AddressTestLoadBalancingPolicy> parent, |
| 373 |
AddressTestCallback cb) |
| 374 |
: parent_(std::move(parent)), cb_(std::move(cb)) {} |
| 375 |
|
| 376 |
RefCountedPtr<SubchannelInterface> CreateSubchannel( |
| 377 |
ServerAddress address, const ChannelArgs& args) override { |
| 378 |
cb_(address); |
| 379 |
return parent_->channel_control_helper()->CreateSubchannel( |
| 380 |
std::move(address), args); |
| 381 |
} |
| 382 |
|
| 383 |
void UpdateState(grpc_connectivity_state state, const absl::Status& status, |
| 384 |
std::unique_ptr<SubchannelPicker> picker) override { |
| 385 |
parent_->channel_control_helper()->UpdateState(state, status, |
| 386 |
std::move(picker)); |
| 387 |
} |
| 388 |
|
| 389 |
void RequestReresolution() override { |
| 390 |
parent_->channel_control_helper()->RequestReresolution(); |
| 391 |
} |
| 392 |
|
| 393 |
absl::string_view GetAuthority() override { |
| 394 |
return parent_->channel_control_helper()->GetAuthority(); |
| 395 |
} |
| 396 |
|
| 397 |
grpc_event_engine::experimental::EventEngine* GetEventEngine() override { |
| 398 |
return parent_->channel_control_helper()->GetEventEngine(); |
| 399 |
} |
| 400 |
|
| 401 |
void AddTraceEvent(TraceSeverity severity, |
| 402 |
absl::string_view message) override { |
| 403 |
parent_->channel_control_helper()->AddTraceEvent(severity, message); |
| 404 |
} |
| 405 |
|
| 406 |
private: |
| 407 |
RefCountedPtr<AddressTestLoadBalancingPolicy> parent_; |
| 408 |
AddressTestCallback cb_; |
| 409 |
}; |
| 410 |
}; |
| 411 |
|
| 412 |
class AddressTestConfig : public LoadBalancingPolicy::Config { |
| 413 |
public: |
| 414 |
absl::string_view name() const override { return kAddressTestLbPolicyName; } |
| 415 |
}; |
| 416 |
|
| 417 |
class AddressTestFactory : public LoadBalancingPolicyFactory { |
| 418 |
public: |
| 419 |
explicit AddressTestFactory(AddressTestCallback cb) : cb_(std::move(cb)) {} |
| 420 |
|
| 421 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 422 |
LoadBalancingPolicy::Args args) const override { |
| 423 |
return MakeOrphanable<AddressTestLoadBalancingPolicy>(std::move(args), cb_); |
| 424 |
} |
| 425 |
|
| 426 |
absl::string_view name() const override { return kAddressTestLbPolicyName; } |
| 427 |
|
| 428 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 429 |
ParseLoadBalancingConfig(const Json& /*json*/) const override { |
| 430 |
return MakeRefCounted<AddressTestConfig>(); |
| 431 |
} |
| 432 |
|
| 433 |
private: |
| 434 |
AddressTestCallback cb_; |
| 435 |
}; |
| 436 |
|
| 437 |
// |
| 438 |
// FixedAddressLoadBalancingPolicy |
| 439 |
// |
| 440 |
|
| 441 |
constexpr char kFixedAddressLbPolicyName[] = "fixed_address_lb"; |
| 442 |
|
| 443 |
class FixedAddressConfig : public LoadBalancingPolicy::Config { |
| 444 |
public: |
| 445 |
explicit FixedAddressConfig(std::string address) |
| 446 |
: address_(std::move(address)) {} |
| 447 |
|
| 448 |
absl::string_view name() const override { return kFixedAddressLbPolicyName; } |
| 449 |
|
| 450 |
const std::string& address() const { return address_; } |
| 451 |
|
| 452 |
private: |
| 453 |
std::string address_; |
| 454 |
}; |
| 455 |
|
| 456 |
class FixedAddressLoadBalancingPolicy : public ForwardingLoadBalancingPolicy { |
| 457 |
public: |
| 458 |
explicit FixedAddressLoadBalancingPolicy(Args args) |
| 459 |
: ForwardingLoadBalancingPolicy( |
| 460 |
std::make_unique<Helper>( |
| 461 |
RefCountedPtr<FixedAddressLoadBalancingPolicy>(this)), |
| 462 |
std::move(args), |
| 463 |
/*delegate_policy_name=*/"pick_first", |
| 464 |
/*initial_refcount=*/2) {} |
| 465 |
|
| 466 |
~FixedAddressLoadBalancingPolicy() override = default; |
| 467 |
|
| 468 |
absl::string_view name() const override { return kFixedAddressLbPolicyName; } |
| 469 |
|
| 470 |
absl::Status UpdateLocked(UpdateArgs args) override { |
| 471 |
auto* config = static_cast<FixedAddressConfig*>(args.config.get()); |
| 472 |
gpr_log(GPR_INFO, "%s: update URI: %s", kFixedAddressLbPolicyName, |
| 473 |
config->address().c_str()); |
| 474 |
auto uri = URI::Parse(config->address()); |
| 475 |
args.config.reset(); |
| 476 |
args.addresses = ServerAddressList(); |
| 477 |
if (uri.ok()) { |
| 478 |
grpc_resolved_address address; |
| 479 |
GPR_ASSERT(grpc_parse_uri(*uri, &address)); |
| 480 |
args.addresses->emplace_back(address, ChannelArgs()); |
| 481 |
} else { |
| 482 |
gpr_log(GPR_ERROR, |
| 483 |
"%s: could not parse URI (%s), using empty address list", |
| 484 |
kFixedAddressLbPolicyName, uri.status().ToString().c_str()); |
| 485 |
args.resolution_note = "no address in fixed_address_lb policy"; |
| 486 |
} |
| 487 |
return ForwardingLoadBalancingPolicy::UpdateLocked(std::move(args)); |
| 488 |
} |
| 489 |
|
| 490 |
private: |
| 491 |
class Helper : public ChannelControlHelper { |
| 492 |
public: |
| 493 |
explicit Helper(RefCountedPtr<FixedAddressLoadBalancingPolicy> parent) |
| 494 |
: parent_(std::move(parent)) {} |
| 495 |
|
| 496 |
RefCountedPtr<SubchannelInterface> CreateSubchannel( |
| 497 |
ServerAddress address, const ChannelArgs& args) override { |
| 498 |
return parent_->channel_control_helper()->CreateSubchannel( |
| 499 |
std::move(address), args); |
| 500 |
} |
| 501 |
|
| 502 |
void UpdateState(grpc_connectivity_state state, const absl::Status& status, |
| 503 |
std::unique_ptr<SubchannelPicker> picker) override { |
| 504 |
parent_->channel_control_helper()->UpdateState(state, status, |
| 505 |
std::move(picker)); |
| 506 |
} |
| 507 |
|
| 508 |
void RequestReresolution() override { |
| 509 |
parent_->channel_control_helper()->RequestReresolution(); |
| 510 |
} |
| 511 |
|
| 512 |
absl::string_view GetAuthority() override { |
| 513 |
return parent_->channel_control_helper()->GetAuthority(); |
| 514 |
} |
| 515 |
|
| 516 |
grpc_event_engine::experimental::EventEngine* GetEventEngine() override { |
| 517 |
return parent_->channel_control_helper()->GetEventEngine(); |
| 518 |
} |
| 519 |
|
| 520 |
void AddTraceEvent(TraceSeverity severity, |
| 521 |
absl::string_view message) override { |
| 522 |
parent_->channel_control_helper()->AddTraceEvent(severity, message); |
| 523 |
} |
| 524 |
|
| 525 |
private: |
| 526 |
RefCountedPtr<FixedAddressLoadBalancingPolicy> parent_; |
| 527 |
}; |
| 528 |
}; |
| 529 |
|
| 530 |
class FixedAddressFactory : public LoadBalancingPolicyFactory { |
| 531 |
public: |
| 532 |
FixedAddressFactory() = default; |
| 533 |
|
| 534 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 535 |
LoadBalancingPolicy::Args args) const override { |
| 536 |
return MakeOrphanable<FixedAddressLoadBalancingPolicy>(std::move(args)); |
| 537 |
} |
| 538 |
|
| 539 |
absl::string_view name() const override { return kFixedAddressLbPolicyName; } |
| 540 |
|
| 541 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 542 |
ParseLoadBalancingConfig(const Json& json) const override { |
| 543 |
std::vector<grpc_error_handle> error_list; |
| 544 |
std::string address; |
| 545 |
ParseJsonObjectField(json.object_value(), "address", &address, &error_list); |
| 546 |
if (!error_list.empty()) { |
| 547 |
grpc_error_handle error = GRPC_ERROR_CREATE_FROM_VECTOR( |
| 548 |
"errors parsing fixed_address_lb config", &error_list); |
| 549 |
absl::Status status = absl::InvalidArgumentError(StatusToString(error)); |
| 550 |
return status; |
| 551 |
} |
| 552 |
return MakeRefCounted<FixedAddressConfig>(std::move(address)); |
| 553 |
} |
| 554 |
}; |
| 555 |
|
| 556 |
// |
| 557 |
// OobBackendMetricTestLoadBalancingPolicy |
| 558 |
// |
| 559 |
|
| 560 |
constexpr char kOobBackendMetricTestLbPolicyName[] = |
| 561 |
"oob_backend_metric_test_lb"; |
| 562 |
|
| 563 |
class OobBackendMetricTestConfig : public LoadBalancingPolicy::Config { |
| 564 |
public: |
| 565 |
absl::string_view name() const override { |
| 566 |
return kOobBackendMetricTestLbPolicyName; |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
class OobBackendMetricTestLoadBalancingPolicy |
| 571 |
: public ForwardingLoadBalancingPolicy { |
| 572 |
public: |
| 573 |
OobBackendMetricTestLoadBalancingPolicy(Args args, |
| 574 |
OobBackendMetricCallback cb) |
| 575 |
: ForwardingLoadBalancingPolicy( |
| 576 |
std::make_unique<Helper>( |
| 577 |
RefCountedPtr<OobBackendMetricTestLoadBalancingPolicy>(this)), |
| 578 |
std::move(args), |
| 579 |
/*delegate_policy_name=*/"pick_first", |
| 580 |
/*initial_refcount=*/2), |
| 581 |
cb_(std::move(cb)) {} |
| 582 |
|
| 583 |
~OobBackendMetricTestLoadBalancingPolicy() override = default; |
| 584 |
|
| 585 |
absl::string_view name() const override { |
| 586 |
return kOobBackendMetricTestLbPolicyName; |
| 587 |
} |
| 588 |
|
| 589 |
private: |
| 590 |
class BackendMetricWatcher : public OobBackendMetricWatcher { |
| 591 |
public: |
| 592 |
BackendMetricWatcher( |
| 593 |
ServerAddress address, |
| 594 |
RefCountedPtr<OobBackendMetricTestLoadBalancingPolicy> parent) |
| 595 |
: address_(std::move(address)), parent_(std::move(parent)) {} |
| 596 |
|
| 597 |
void OnBackendMetricReport( |
| 598 |
const BackendMetricData& backend_metric_data) override { |
| 599 |
parent_->cb_(address_, backend_metric_data); |
| 600 |
} |
| 601 |
|
| 602 |
private: |
| 603 |
ServerAddress address_; |
| 604 |
RefCountedPtr<OobBackendMetricTestLoadBalancingPolicy> parent_; |
| 605 |
}; |
| 606 |
|
| 607 |
class Helper : public ChannelControlHelper { |
| 608 |
public: |
| 609 |
explicit Helper( |
| 610 |
RefCountedPtr<OobBackendMetricTestLoadBalancingPolicy> parent) |
| 611 |
: parent_(std::move(parent)) {} |
| 612 |
|
| 613 |
RefCountedPtr<SubchannelInterface> CreateSubchannel( |
| 614 |
ServerAddress address, const ChannelArgs& args) override { |
| 615 |
auto subchannel = |
| 616 |
parent_->channel_control_helper()->CreateSubchannel(address, args); |
| 617 |
subchannel->AddDataWatcher(MakeOobBackendMetricWatcher( |
| 618 |
Duration::Seconds(1), |
| 619 |
std::make_unique<BackendMetricWatcher>(std::move(address), parent_))); |
| 620 |
return subchannel; |
| 621 |
} |
| 622 |
|
| 623 |
void UpdateState(grpc_connectivity_state state, const absl::Status& status, |
| 624 |
std::unique_ptr<SubchannelPicker> picker) override { |
| 625 |
parent_->channel_control_helper()->UpdateState(state, status, |
| 626 |
std::move(picker)); |
| 627 |
} |
| 628 |
|
| 629 |
void RequestReresolution() override { |
| 630 |
parent_->channel_control_helper()->RequestReresolution(); |
| 631 |
} |
| 632 |
|
| 633 |
absl::string_view GetAuthority() override { |
| 634 |
return parent_->channel_control_helper()->GetAuthority(); |
| 635 |
} |
| 636 |
|
| 637 |
grpc_event_engine::experimental::EventEngine* GetEventEngine() override { |
| 638 |
return parent_->channel_control_helper()->GetEventEngine(); |
| 639 |
} |
| 640 |
|
| 641 |
void AddTraceEvent(TraceSeverity severity, |
| 642 |
absl::string_view message) override { |
| 643 |
parent_->channel_control_helper()->AddTraceEvent(severity, message); |
| 644 |
} |
| 645 |
|
| 646 |
private: |
| 647 |
RefCountedPtr<OobBackendMetricTestLoadBalancingPolicy> parent_; |
| 648 |
}; |
| 649 |
|
| 650 |
private: |
| 651 |
OobBackendMetricCallback cb_; |
| 652 |
}; |
| 653 |
|
| 654 |
class OobBackendMetricTestFactory : public LoadBalancingPolicyFactory { |
| 655 |
public: |
| 656 |
explicit OobBackendMetricTestFactory(OobBackendMetricCallback cb) |
| 657 |
: cb_(std::move(cb)) {} |
| 658 |
|
| 659 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 660 |
LoadBalancingPolicy::Args args) const override { |
| 661 |
return MakeOrphanable<OobBackendMetricTestLoadBalancingPolicy>( |
| 662 |
std::move(args), cb_); |
| 663 |
} |
| 664 |
|
| 665 |
absl::string_view name() const override { |
| 666 |
return kOobBackendMetricTestLbPolicyName; |
| 667 |
} |
| 668 |
|
| 669 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 670 |
ParseLoadBalancingConfig(const Json& /*json*/) const override { |
| 671 |
return MakeRefCounted<OobBackendMetricTestConfig>(); |
| 672 |
} |
| 673 |
|
| 674 |
private: |
| 675 |
OobBackendMetricCallback cb_; |
| 676 |
}; |
| 677 |
|
| 678 |
// |
| 679 |
// FailLoadBalancingPolicy |
| 680 |
// |
| 681 |
|
| 682 |
constexpr char kFailPolicyName[] = "fail_lb"; |
| 683 |
|
| 684 |
class FailPolicy : public LoadBalancingPolicy { |
| 685 |
public: |
| 686 |
FailPolicy(Args args, absl::Status status, std::atomic<int>* pick_counter) |
| 687 |
: LoadBalancingPolicy(std::move(args)), |
| 688 |
status_(std::move(status)), |
| 689 |
pick_counter_(pick_counter) {} |
| 690 |
|
| 691 |
absl::string_view name() const override { return kFailPolicyName; } |
| 692 |
|
| 693 |
absl::Status UpdateLocked(UpdateArgs) override { |
| 694 |
channel_control_helper()->UpdateState( |
| 695 |
GRPC_CHANNEL_TRANSIENT_FAILURE, status_, |
| 696 |
std::make_unique<FailPicker>(status_, pick_counter_)); |
| 697 |
return absl::OkStatus(); |
| 698 |
} |
| 699 |
|
| 700 |
void ResetBackoffLocked() override {} |
| 701 |
void ShutdownLocked() override {} |
| 702 |
|
| 703 |
private: |
| 704 |
class FailPicker : public SubchannelPicker { |
| 705 |
public: |
| 706 |
FailPicker(absl::Status status, std::atomic<int>* pick_counter) |
| 707 |
: status_(std::move(status)), pick_counter_(pick_counter) {} |
| 708 |
|
| 709 |
PickResult Pick(PickArgs /*args*/) override { |
| 710 |
if (pick_counter_ != nullptr) pick_counter_->fetch_add(1); |
| 711 |
return PickResult::Fail(status_); |
| 712 |
} |
| 713 |
|
| 714 |
private: |
| 715 |
absl::Status status_; |
| 716 |
std::atomic<int>* pick_counter_; |
| 717 |
}; |
| 718 |
|
| 719 |
absl::Status status_; |
| 720 |
std::atomic<int>* pick_counter_; |
| 721 |
}; |
| 722 |
|
| 723 |
class FailLbConfig : public LoadBalancingPolicy::Config { |
| 724 |
public: |
| 725 |
absl::string_view name() const override { return kFailPolicyName; } |
| 726 |
}; |
| 727 |
|
| 728 |
class FailLbFactory : public LoadBalancingPolicyFactory { |
| 729 |
public: |
| 730 |
FailLbFactory(absl::Status status, std::atomic<int>* pick_counter) |
| 731 |
: status_(std::move(status)), pick_counter_(pick_counter) {} |
| 732 |
|
| 733 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 734 |
LoadBalancingPolicy::Args args) const override { |
| 735 |
return MakeOrphanable<FailPolicy>(std::move(args), status_, pick_counter_); |
| 736 |
} |
| 737 |
|
| 738 |
absl::string_view name() const override { return kFailPolicyName; } |
| 739 |
|
| 740 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 741 |
ParseLoadBalancingConfig(const Json& /*json*/) const override { |
| 742 |
return MakeRefCounted<FailLbConfig>(); |
| 743 |
} |
| 744 |
|
| 745 |
private: |
| 746 |
absl::Status status_; |
| 747 |
std::atomic<int>* pick_counter_; |
| 748 |
}; |
| 749 |
|
| 750 |
} // namespace |
| 751 |
|
| 752 |
void RegisterTestPickArgsLoadBalancingPolicy( |
| 753 |
CoreConfiguration::Builder* builder, TestPickArgsCallback cb, |
| 754 |
absl::string_view delegate_policy_name) { |
| 755 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 756 |
std::make_unique<TestPickArgsLbFactory>(std::move(cb), |
| 757 |
delegate_policy_name)); |
| 758 |
} |
| 759 |
|
| 760 |
void RegisterInterceptRecvTrailingMetadataLoadBalancingPolicy( |
| 761 |
CoreConfiguration::Builder* builder, |
| 762 |
InterceptRecvTrailingMetadataCallback cb) { |
| 763 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 764 |
std::make_unique<InterceptTrailingFactory>(std::move(cb))); |
| 765 |
} |
| 766 |
|
| 767 |
void RegisterAddressTestLoadBalancingPolicy(CoreConfiguration::Builder* builder, |
| 768 |
AddressTestCallback cb) { |
| 769 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 770 |
std::make_unique<AddressTestFactory>(std::move(cb))); |
| 771 |
} |
| 772 |
|
| 773 |
void RegisterFixedAddressLoadBalancingPolicy( |
| 774 |
CoreConfiguration::Builder* builder) { |
| 775 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 776 |
std::make_unique<FixedAddressFactory>()); |
| 777 |
} |
| 778 |
|
| 779 |
void RegisterOobBackendMetricTestLoadBalancingPolicy( |
| 780 |
CoreConfiguration::Builder* builder, OobBackendMetricCallback cb) { |
| 781 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 782 |
std::make_unique<OobBackendMetricTestFactory>(std::move(cb))); |
| 783 |
} |
| 784 |
|
| 785 |
void RegisterFailLoadBalancingPolicy(CoreConfiguration::Builder* builder, |
| 786 |
absl::Status status, |
| 787 |
std::atomic<int>* pick_counter) { |
| 788 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 789 |
std::make_unique<FailLbFactory>(std::move(status), pick_counter)); |
| 790 |
} |
| 791 |
|
| 792 |
} // namespace grpc_core |