-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(tonic-xds): parse RING_HASH lb policy from CDS (gRFC A42) #2701
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
Open
madhurishgupta
wants to merge
5
commits into
grpc:master
Choose a base branch
from
madhurishgupta:madhurishgupta/a42-pr3-cds-ring-hash
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.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2cb5690
feat(tonic-xds): parse RING_HASH lb policy from CDS (gRFC A42)
madhurishgupta a921b5e
refactor(tonic-xds): extract LbPolicy + ring-hash validation into lb_…
madhurishgupta 1af9770
refactor(tonic-xds): hoist HashFunction import to module top
madhurishgupta e46263d
fix(tonic-xds): default unset ring max to 8M before clamping
madhurishgupta 422d7c8
refactor(tonic-xds): enforce RingHashSettings invariant via private f…
madhurishgupta 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
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,199 @@ | ||
| //! Cluster load-balancing policy (CDS) and ring-hash config validation. | ||
|
|
||
| use envoy_types::pb::envoy::config::cluster::v3::cluster::{ | ||
| self, ring_hash_lb_config::HashFunction, | ||
| }; | ||
| use xds_client::Error; | ||
|
|
||
| /// Load balancing policies. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub(crate) enum LbPolicy { | ||
| RoundRobin, | ||
| LeastRequest, | ||
| /// Ring-hash (gRFC A42), carrying the validated ring bounds. | ||
| RingHash(RingHashSettings), | ||
| } | ||
|
|
||
| /// Validated gRFC A42 ring-hash sizing parsed from `ring_hash_lb_config`. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub(crate) struct RingHashSettings { | ||
| pub min_ring_size: u64, | ||
| pub max_ring_size: u64, | ||
| } | ||
|
|
||
| /// gRFC A42 ring-hash sizing. `minimum_ring_size` defaults to 1024 and | ||
| /// `maximum_ring_size` to the xDS default of 8M when unset; both are then | ||
| /// clamped to the local cap of 4096, and any configured value above the 8M | ||
| /// ceiling is rejected. Defaulting an unset max to 8M (rather than directly to | ||
| /// the cap) keeps the `min > max` check from rejecting a `min` in the | ||
| /// `(cap, ceiling]` range when `max` is unset. | ||
| pub(crate) const RING_HASH_DEFAULT_MIN_SIZE: u64 = 1024; | ||
| pub(crate) const RING_HASH_DEFAULT_MAX_SIZE: u64 = 8 * 1024 * 1024; | ||
|
madhurishgupta marked this conversation as resolved.
Outdated
|
||
| pub(crate) const RING_HASH_SIZE_CAP: u64 = 4096; | ||
| pub(crate) const RING_HASH_SIZE_CEILING: u64 = 8 * 1024 * 1024; | ||
|
|
||
| impl RingHashSettings { | ||
| /// Validate a Cluster's `lb_config` oneof as `ring_hash_lb_config` (gRFC | ||
| /// A42). | ||
| /// | ||
| /// Rejects a `hash_function` other than `XX_HASH`, any ring size above the | ||
| /// 8M ceiling, and `min_ring_size > max_ring_size`. Unset sizes take the | ||
| /// defaults (1024 / 8M); the resolved bounds are then clamped to the local | ||
| /// cap. | ||
| pub(crate) fn validate(lb_config: Option<cluster::LbConfig>) -> xds_client::Result<Self> { | ||
| let (min_field, max_field, hash_function) = match lb_config { | ||
| Some(cluster::LbConfig::RingHashLbConfig(c)) => { | ||
| (c.minimum_ring_size, c.maximum_ring_size, c.hash_function) | ||
| } | ||
| // No ring_hash_lb_config → defaults (XX_HASH is the proto default). | ||
| _ => (None, None, HashFunction::XxHash as i32), | ||
| }; | ||
|
|
||
| match HashFunction::try_from(hash_function) { | ||
| Ok(HashFunction::XxHash) => {} | ||
| Ok(other) => { | ||
| return Err(Error::Validation(format!( | ||
| "unsupported ring_hash hash function: {}", | ||
| other.as_str_name() | ||
| ))); | ||
| } | ||
| Err(_) => { | ||
| return Err(Error::Validation(format!( | ||
| "unknown ring_hash hash function: {hash_function}" | ||
| ))); | ||
| } | ||
| } | ||
|
|
||
| let min = min_field.map_or(RING_HASH_DEFAULT_MIN_SIZE, |v| v.value); | ||
| let max = max_field.map_or(RING_HASH_DEFAULT_MAX_SIZE, |v| v.value); | ||
| if min > RING_HASH_SIZE_CEILING || max > RING_HASH_SIZE_CEILING { | ||
| return Err(Error::Validation(format!( | ||
| "ring_hash ring size exceeds the maximum of {RING_HASH_SIZE_CEILING} \ | ||
| (min_ring_size={min}, max_ring_size={max})" | ||
| ))); | ||
| } | ||
| // Checked on the resolved sizes before the cap is applied. | ||
| if min > max { | ||
| return Err(Error::Validation(format!( | ||
| "ring_hash min_ring_size ({min}) is greater than max_ring_size ({max})" | ||
| ))); | ||
| } | ||
|
|
||
| Ok(RingHashSettings { | ||
| min_ring_size: min.min(RING_HASH_SIZE_CAP), | ||
| max_ring_size: max.min(RING_HASH_SIZE_CAP), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use envoy_types::pb::google::protobuf::UInt64Value; | ||
|
|
||
| fn ring_size(value: u64) -> UInt64Value { | ||
| UInt64Value { value } | ||
| } | ||
|
|
||
| fn lb_config(config: cluster::RingHashLbConfig) -> Option<cluster::LbConfig> { | ||
| Some(cluster::LbConfig::RingHashLbConfig(config)) | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_defaults() { | ||
| // No ring_hash_lb_config → min 1024, max defaults to the cap; XX_HASH | ||
| // is the default hash function. | ||
| let settings = RingHashSettings::validate(None).unwrap(); | ||
| assert_eq!( | ||
| settings, | ||
| RingHashSettings { | ||
| min_ring_size: 1024, | ||
| max_ring_size: 4096, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_custom_sizes_within_cap() { | ||
| let settings = RingHashSettings::validate(lb_config(cluster::RingHashLbConfig { | ||
| minimum_ring_size: Some(ring_size(2048)), | ||
| maximum_ring_size: Some(ring_size(3000)), | ||
| ..Default::default() | ||
| })) | ||
| .unwrap(); | ||
| assert_eq!( | ||
| settings, | ||
| RingHashSettings { | ||
| min_ring_size: 2048, | ||
| max_ring_size: 3000, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_sizes_clamped_to_cap() { | ||
| // Sizes within the 8M ceiling but above the local cap clamp to 4096. | ||
| let settings = RingHashSettings::validate(lb_config(cluster::RingHashLbConfig { | ||
| minimum_ring_size: Some(ring_size(100_000)), | ||
| maximum_ring_size: Some(ring_size(100_000)), | ||
| ..Default::default() | ||
| })) | ||
| .unwrap(); | ||
| assert_eq!( | ||
| settings, | ||
| RingHashSettings { | ||
| min_ring_size: 4096, | ||
| max_ring_size: 4096, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_rejects_non_xx_hash() { | ||
| let err = RingHashSettings::validate(lb_config(cluster::RingHashLbConfig { | ||
| hash_function: cluster::ring_hash_lb_config::HashFunction::MurmurHash2 as i32, | ||
| ..Default::default() | ||
| })) | ||
| .unwrap_err(); | ||
| assert!(err.to_string().contains("hash function")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_rejects_size_above_ceiling() { | ||
| let err = RingHashSettings::validate(lb_config(cluster::RingHashLbConfig { | ||
| maximum_ring_size: Some(ring_size(8 * 1024 * 1024 + 1)), | ||
| ..Default::default() | ||
| })) | ||
| .unwrap_err(); | ||
| assert!(err.to_string().contains("exceeds the maximum")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_rejects_min_greater_than_max() { | ||
| let err = RingHashSettings::validate(lb_config(cluster::RingHashLbConfig { | ||
| minimum_ring_size: Some(ring_size(3000)), | ||
| maximum_ring_size: Some(ring_size(2000)), | ||
| ..Default::default() | ||
| })) | ||
| .unwrap_err(); | ||
| assert!(err.to_string().contains("greater than max_ring_size")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ring_hash_min_above_cap_with_unset_max_is_accepted() { | ||
| // min in (cap, ceiling] with max unset must not NACK: max defaults to | ||
| // 8M, so min <= max, and both then clamp to the cap. | ||
| let settings = RingHashSettings::validate(lb_config(cluster::RingHashLbConfig { | ||
| minimum_ring_size: Some(ring_size(5000)), | ||
| ..Default::default() | ||
| })) | ||
| .unwrap(); | ||
| assert_eq!( | ||
| settings, | ||
| RingHashSettings { | ||
| min_ring_size: 4096, | ||
| max_ring_size: 4096, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.