-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[wip]Lbv2 wiring #2722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gu0keno0
wants to merge
6
commits into
grpc:master
Choose a base branch
from
gu0keno0:lbv2-wiring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[wip]Lbv2 wiring #2722
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| // `LbChannel` is not constructed by the current P2C wiring (the LoadBalancer | ||
| // tracks load via `ReadyChannel` + `EndpointChannel`); retained for now. | ||
| #[allow(dead_code)] | ||
| pub(crate) mod channel; | ||
| pub(crate) mod channel_state; | ||
| pub(crate) mod errors; | ||
| pub(crate) mod keyed_futures; | ||
| pub(crate) mod loadbalancer; | ||
| pub(crate) mod outlier_detection; | ||
| pub(crate) mod pickers; | ||
| pub(crate) mod service; |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| //! Routing → per-cluster load-balancing service for the `tonic-xds-lb` path. | ||
| //! | ||
| //! [`XdsLoadBalanceService`] is the analogue of the `tower-lb` | ||
| //! `XdsLbService`: it reads the [`RouteDecision`] attached by the routing | ||
| //! layer, looks up (or lazily builds) the target cluster's [`LoadBalancer`], | ||
| //! and dispatches the request to it. | ||
| //! | ||
| //! Each cluster's `LoadBalancer` is not `Clone` and needs `&mut self` to | ||
| //! serve, so it is wrapped in a [`tower::buffer::Buffer`] — giving a cheap, | ||
| //! cloneable handle shared across concurrent requests. The buffer maps the | ||
| //! LB's [`LbError`](crate::client::loadbalance::errors::LbError) to | ||
| //! [`BoxError`] automatically. | ||
| //! | ||
| //! First-cut policy: power-of-two-choices ([`P2cPicker`]) with outlier | ||
| //! detection disabled ([`OutlierDetectionConfig::default`]). Ring-hash | ||
| //! selection and xDS-driven outlier config are follow-ups. | ||
|
|
||
| use std::sync::Arc; | ||
| use std::task::{Context, Poll}; | ||
|
|
||
| use arc_swap::ArcSwap; | ||
| use dashmap::DashMap; | ||
| use http::{Request, Response}; | ||
| use tonic::body::Body as TonicBody; | ||
| use tonic::transport::Channel; | ||
| use tower::buffer::Buffer; | ||
| use tower::{BoxError, Service, ServiceExt}; | ||
|
|
||
| use crate::client::endpoint::EndpointChannel; | ||
| use crate::client::loadbalance::channel_state::ReadyChannel; | ||
| use crate::client::loadbalance::loadbalancer::{LbFuture, LoadBalancer}; | ||
| use crate::client::loadbalance::pickers::ChannelPicker; | ||
| use crate::client::loadbalance::pickers::p2c::P2cPicker; | ||
| use crate::client::route::RouteDecision; | ||
| use crate::common::async_util::BoxFuture; | ||
| use crate::xds::cache::XdsCache; | ||
| #[cfg(feature = "_tls-any")] | ||
| use crate::xds::cert_provider::CertProviderRegistry; | ||
| use crate::xds::lb_discovery::{XdsLbConnector, discover_endpoints}; | ||
| use crate::xds::resource::outlier_detection::OutlierDetectionConfig; | ||
|
|
||
| /// Buffer capacity between callers and a cluster's `LoadBalancer` worker. | ||
| const DEFAULT_BUFFER_CAPACITY: usize = 1024; | ||
|
|
||
| /// The request type flowing into the LB layer (after the routing/retry layers | ||
| /// and the `SharedBody` → `TonicBody` remap). | ||
| type LbRequest = Request<TonicBody>; | ||
| /// The response type produced by an endpoint channel. | ||
| type LbResponse = Response<TonicBody>; | ||
|
|
||
| /// A cloneable handle to one cluster's `LoadBalancer`, buffered so concurrent | ||
| /// callers share a single balancer. `Buffer`'s second type parameter is the | ||
| /// wrapped service's future — here the `LoadBalancer`'s [`LbFuture`]. | ||
| type ClusterChannel = Buffer<LbRequest, LbFuture<LbResponse>>; | ||
|
|
||
| /// Error returned when a request reaches the LB layer without a routing | ||
| /// decision (i.e. the routing layer did not run or produced nothing). | ||
| #[derive(Debug, thiserror::Error)] | ||
| #[error("no routing decision extension from the routing layer available")] | ||
| struct NoRoutingDecision; | ||
|
|
||
| /// Registry of per-cluster [`LoadBalancer`]s, built lazily on first use. | ||
| struct ClusterLbRegistry { | ||
| cache: Arc<XdsCache>, | ||
| #[cfg(feature = "_tls-any")] | ||
| cert_provider_registry: Arc<CertProviderRegistry>, | ||
| clusters: DashMap<String, ClusterChannel>, | ||
| } | ||
|
|
||
| impl ClusterLbRegistry { | ||
| /// Returns a cloneable channel to the cluster's balancer, building it on | ||
| /// first access. | ||
| fn cluster_channel(&self, cluster_name: &str) -> ClusterChannel { | ||
| self.clusters | ||
| .entry(cluster_name.to_string()) | ||
| .or_insert_with(|| self.build_cluster_channel(cluster_name)) | ||
| .clone() | ||
| } | ||
|
|
||
| /// Constructs a fresh `LoadBalancer` for `cluster_name` and wraps it in a | ||
| /// buffer. Discovery yields idle endpoints; [`XdsLbConnector`] resolves the | ||
| /// cluster's CDS security config lazily inside `connect`. | ||
| fn build_cluster_channel(&self, cluster_name: &str) -> ClusterChannel { | ||
| let discover = discover_endpoints(&self.cache, cluster_name); | ||
| let connector = Arc::new(XdsLbConnector::new( | ||
| self.cache.clone(), | ||
| cluster_name.to_string(), | ||
| #[cfg(feature = "_tls-any")] | ||
| self.cert_provider_registry.clone(), | ||
| )); | ||
| let picker: Arc< | ||
| dyn ChannelPicker<ReadyChannel<EndpointChannel<Channel>>, LbRequest> + Send + Sync, | ||
| > = Arc::new(P2cPicker); | ||
| let config = Arc::new(ArcSwap::from_pointee(OutlierDetectionConfig::default())); | ||
| let lb = LoadBalancer::new(discover, connector, picker, config); | ||
| Buffer::new(lb, DEFAULT_BUFFER_CAPACITY) | ||
| } | ||
| } | ||
|
|
||
| /// Tower service that routes each request to its cluster's `LoadBalancer`. | ||
| #[derive(Clone)] | ||
| pub(crate) struct XdsLoadBalanceService { | ||
| registry: Arc<ClusterLbRegistry>, | ||
| } | ||
|
|
||
| impl XdsLoadBalanceService { | ||
| #[cfg(feature = "_tls-any")] | ||
| pub(crate) fn new( | ||
| cache: Arc<XdsCache>, | ||
| cert_provider_registry: Arc<CertProviderRegistry>, | ||
| ) -> Self { | ||
| Self { | ||
| registry: Arc::new(ClusterLbRegistry { | ||
| cache, | ||
| cert_provider_registry, | ||
| clusters: DashMap::new(), | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(feature = "_tls-any"))] | ||
| pub(crate) fn new(cache: Arc<XdsCache>) -> Self { | ||
| Self { | ||
| registry: Arc::new(ClusterLbRegistry { | ||
| cache, | ||
| clusters: DashMap::new(), | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Service<LbRequest> for XdsLoadBalanceService { | ||
| type Response = LbResponse; | ||
| type Error = BoxError; | ||
| type Future = BoxFuture<Result<Self::Response, Self::Error>>; | ||
|
|
||
| fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| // The target cluster is decided per-request by the routing layer, so | ||
| // readiness cannot be determined without the request. | ||
| Poll::Ready(Ok(())) | ||
| } | ||
|
|
||
| fn call(&mut self, request: LbRequest) -> Self::Future { | ||
| let Some(decision) = request.extensions().get::<RouteDecision>().cloned() else { | ||
| return Box::pin(async move { Err(BoxError::from(NoRoutingDecision)) }); | ||
| }; | ||
|
|
||
| let mut channel = self.registry.cluster_channel(&decision.cluster); | ||
|
|
||
| Box::pin(async move { | ||
| // Blocks until the balancer has a ready endpoint. | ||
| channel.ready().await?; | ||
| channel.call(request).await | ||
| }) | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| pub(crate) mod channel; | ||
| pub(crate) mod cluster; | ||
| pub(crate) mod endpoint; | ||
| pub(crate) mod lb; | ||
| #[allow(dead_code)] | ||
| pub(crate) mod loadbalance; | ||
| #[allow(dead_code)] | ||
| pub(crate) mod retry; | ||
| pub(crate) mod route; | ||
|
|
||
| cfg_tower_lb! { | ||
| pub(crate) mod cluster; | ||
| pub(crate) mod lb; | ||
| } | ||
|
|
||
| cfg_tonic_xds_lb! { | ||
| pub(crate) mod loadbalance; | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me understand what's the reason to hardcode
P2cPickerhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be replaced by dynamic xds resolution in the near future. The wire up here is just to unblock those clean up works.