feat: Implement Least Request Load Balancing Policy (gRFC A48)#2651
Open
emil10001 wants to merge 2 commits into
Open
feat: Implement Least Request Load Balancing Policy (gRFC A48)#2651emil10001 wants to merge 2 commits into
emil10001 wants to merge 2 commits into
Conversation
Implements the "all weights equal" Least Request Load Balancing policy in
gRPC-Rust, in compliance with gRFC A48. The Least Request policy improves tail
latencies in heterogeneous environments by tracking active request counts per
endpoint and directing new requests to the backend with the lowest load.
Detailed Changes:
1. Core Load Balancing Policy (`least_request.rs`):
- Defined `LeastRequestLoadBalancingConfig` to parse and validate the
`choiceCount` config parameter (default = 2, clamped from 2 to 10).
- Implemented `LeastRequestBuilder` registering policy name
`least_request_experimental`.
- Implemented `LeastRequestPolicy` managing endpoint-level connections
via `ChildManager` children delegating to `pick_first`.
- Maintained a persistent mapping of weak subchannel references to active
request counters (`subchannel_counters`) so that outstanding request
metrics survive picker updates and name re-resolutions.
- Implemented `LeastRequestPicker` utilizing a random sampling selection
algorithm over `choice_count` subchannels.
2. Active Request Cancellation Safety:
- Identified and resolved a request counter leak bug where async task
cancellations during `dyn_invoke.await` dropped the `Pick` closure
without calling it.
- Implemented a custom, defusable `ActiveRequestGuard` using an `AtomicBool`
inside `LeastRequestPicker::pick`. The guard guarantees that the active
request count is decremented upon drop if the picker's `on_complete`
callback is never invoked.
3. Channel & Service Config Integration:
- Registered the builder with the global LB registry in `Channel::new`
inside `channel.rs`.
- Added `CallbackRecvStream` wrapping the stream in the channel's
`Invoke` implementation to trigger `on_complete` callbacks when client
streams are completed or dropped.
- Added `LeastRequest` variant to `LbPolicyType` enum in `service_config.rs`.
- Mapped `LbPolicyType::LeastRequest` configuration inside
`ResolverChannelController::update` in `channel.rs`.
4. Test Additions & Verification:
- Added comprehensive unit tests in `least_request.rs` covering configuration
parsing/clamping/validation, least request selection, tie-breaking,
fewer subchannels than choice count, and cancellation drop-guard safety.
- Modified the `InMemoryResolver` in `inmemory/mod.rs` to dynamically set
the `LeastRequest` load-balancing policy based on target URI path prefixes.
- Wrote a robust E2E integration test `test_in_memory_least_request_load_balancing`
in `inmemory/mod.rs` verifying dynamic load balancing across multiple
in-memory backends concurrently.
Member
Author
|
/gemini review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements the "all weights equal" Least Request Load Balancing policy in gRPC-Rust, in compliance with gRFC A48. The Least Request policy improves tail latencies in heterogeneous environments by tracking active request counts per endpoint and directing new requests to the backend with the lowest load.
Detailed Changes:
Core Load Balancing Policy (
least_request.rs):LeastRequestLoadBalancingConfigto parse and validate thechoiceCountconfig parameter (default = 2, clamped from 2 to 10).LeastRequestBuilderregistering policy nameleast_request_experimental.LeastRequestPolicymanaging endpoint-level connections viaChildManagerchildren delegating topick_first.subchannel_counters) so that outstanding request metrics survive picker updates and name re-resolutions.LeastRequestPickerutilizing a random sampling selection algorithm overchoice_countsubchannels.Active Request Cancellation Safety:
dyn_invoke.awaitdropped thePickclosure without calling it.ActiveRequestGuardusing anAtomicBoolinsideLeastRequestPicker::pick. The guard guarantees that the active request count is decremented upon drop if the picker'son_completecallback is never invoked.Channel & Service Config Integration:
Channel::newinsidechannel.rs.CallbackRecvStreamwrapping the stream in the channel'sInvokeimplementation to triggeron_completecallbacks when client streams are completed or dropped.LeastRequestvariant toLbPolicyTypeenum inservice_config.rs.LbPolicyType::LeastRequestconfiguration insideResolverChannelController::updateinchannel.rs.Test Additions & Verification:
least_request.rscovering configuration parsing/clamping/validation, least request selection, tie-breaking, fewer subchannels than choice count, and cancellation drop-guard safety.InMemoryResolverininmemory/mod.rsto dynamically set theLeastRequestload-balancing policy based on target URI path prefixes.test_in_memory_least_request_load_balancingininmemory/mod.rsverifying dynamic load balancing across multiple in-memory backends concurrently.Motivation
Solution