Skip to content

feat: Implement Least Request Load Balancing Policy (gRFC A48)#2651

Open
emil10001 wants to merge 3 commits into
grpc:masterfrom
emil10001:master
Open

feat: Implement Least Request Load Balancing Policy (gRFC A48)#2651
emil10001 wants to merge 3 commits into
grpc:masterfrom
emil10001:master

Conversation

@emil10001
Copy link
Copy Markdown
Member

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.

Motivation

Solution

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.
@emil10001
Copy link
Copy Markdown
Member Author

/gemini review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant