-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(tonic-xds): add OutlierDetectionConfig types (gRFC A50) #2604
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
Merged
gu0keno0
merged 3 commits into
hyperium:master
from
LYZJU2019:lyzju2019/a50-outlier-detection-config
Apr 30, 2026
+160
−0
Merged
Changes from all commits
Commits
Show all changes
3 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
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,159 @@ | ||
| //! Validated configuration types for [gRFC A50] outlier detection. | ||
| //! | ||
| //! [`OutlierDetectionConfig`] is the input to the outlier-detection | ||
| //! algorithm. The two sub-configs gate which ejection algorithms run. | ||
| //! | ||
| //! Note: A50 specifies outlier detection as a load-balancing policy | ||
| //! wrapping a `child_policy`. `tonic-xds` currently runs P2C as its only | ||
| //! load balancer and integrates outlier detection as a filter on the | ||
| //! `Discover` stream feeding it, so there is no `child_policy` field | ||
| //! here yet. It will be added when more balancers are supported. | ||
| //! | ||
| //! [gRFC A50]: https://github.com/grpc/proposal/blob/master/A50-xds-outlier-detection.md | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| /// A 0–100 percentage. Construction is fallible; once held, every | ||
| /// `Percentage` is guaranteed to be in range, so the algorithm never | ||
| /// has to re-validate. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub(crate) struct Percentage(u8); | ||
|
|
||
| impl Percentage { | ||
| /// Construct from a raw value, returning `Err` if it exceeds 100. | ||
| /// Accepts `u32` to match the proto wire type without forcing callers | ||
| /// to cast at every site. | ||
| pub(crate) fn new(value: u32) -> Result<Self, PercentageError> { | ||
| if value > 100 { | ||
| Err(PercentageError(value)) | ||
| } else { | ||
| Ok(Self(value as u8)) | ||
| } | ||
| } | ||
|
|
||
| /// The contained value, in `0..=100`. | ||
| pub(crate) fn get(self) -> u8 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, PartialEq, Eq)] | ||
| #[error("percentage must be in 0..=100, got {0}")] | ||
| pub(crate) struct PercentageError(u32); | ||
|
|
||
| /// Validated A50 outlier-detection configuration for a cluster. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub(crate) struct OutlierDetectionConfig { | ||
| /// How often the ejection sweep runs. | ||
| pub interval: Duration, | ||
| /// Base duration for a single ejection; actual ejection time is | ||
| /// `base_ejection_time * multiplier`, capped by `max_ejection_time`. | ||
| pub base_ejection_time: Duration, | ||
| /// Upper bound on `base_ejection_time * multiplier`. The spec guarantees | ||
| /// this is at least `base_ejection_time`. | ||
| pub max_ejection_time: Duration, | ||
| /// Maximum percentage of endpoints that may be ejected at any time. | ||
| pub max_ejection_percent: Percentage, | ||
| /// Success-rate ejection parameters. `None` if the algorithm is disabled. | ||
| pub success_rate: Option<SuccessRateConfig>, | ||
| /// Failure-percentage ejection parameters. `None` if the algorithm is | ||
| /// disabled. | ||
| pub failure_percentage: Option<FailurePercentageConfig>, | ||
| } | ||
|
LYZJU2019 marked this conversation as resolved.
|
||
|
|
||
| /// Success-rate ejection parameters (gRFC A50). | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub(crate) struct SuccessRateConfig { | ||
| /// Ejection threshold factor, scaled by 1000 (so `1900` means `1.9`). | ||
| /// An endpoint is a candidate for ejection when its success rate falls | ||
| /// below `mean - stdev * (stdev_factor / 1000.0)`. | ||
| pub stdev_factor: u32, | ||
| /// Probability that a candidate is actually ejected. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a threshold for ejecting a host if RPC success rate drops below it? |
||
| pub enforcing_success_rate: Percentage, | ||
| /// Minimum number of candidate endpoints required to run the algorithm. | ||
|
LYZJU2019 marked this conversation as resolved.
|
||
| pub minimum_hosts: u32, | ||
| /// Minimum number of requests an endpoint must have seen in the last | ||
| /// interval to be considered a candidate. | ||
| pub request_volume: u32, | ||
| } | ||
|
|
||
| /// Failure-percentage ejection parameters (gRFC A50). | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub(crate) struct FailurePercentageConfig { | ||
| /// Failure rate at or above which an endpoint is a candidate for | ||
| /// ejection. | ||
| pub threshold: Percentage, | ||
| /// Probability that a candidate is actually ejected. | ||
| pub enforcing_failure_percentage: Percentage, | ||
| /// Minimum number of candidate endpoints required to run the algorithm. | ||
| pub minimum_hosts: u32, | ||
| /// Minimum number of requests an endpoint must have seen in the last | ||
| /// interval to be considered a candidate. | ||
| pub request_volume: u32, | ||
| } | ||
|
|
||
| impl OutlierDetectionConfig { | ||
| /// True when at least one ejection algorithm is enabled and the detector | ||
| /// should do work. If false, the cluster can skip instantiating detection. | ||
| pub(crate) fn is_enabled(&self) -> bool { | ||
| self.success_rate.is_some() || self.failure_percentage.is_some() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn base_config() -> OutlierDetectionConfig { | ||
| OutlierDetectionConfig { | ||
| interval: Duration::from_secs(10), | ||
| base_ejection_time: Duration::from_secs(30), | ||
| max_ejection_time: Duration::from_secs(300), | ||
| max_ejection_percent: Percentage::new(10).unwrap(), | ||
| success_rate: None, | ||
| failure_percentage: None, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn is_enabled_false_when_both_algorithms_disabled() { | ||
| assert!(!base_config().is_enabled()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn is_enabled_true_when_success_rate_present() { | ||
| let mut c = base_config(); | ||
| c.success_rate = Some(SuccessRateConfig { | ||
| stdev_factor: 1900, | ||
| enforcing_success_rate: Percentage::new(100).unwrap(), | ||
| minimum_hosts: 5, | ||
| request_volume: 100, | ||
| }); | ||
| assert!(c.is_enabled()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn is_enabled_true_when_failure_percentage_present() { | ||
| let mut c = base_config(); | ||
| c.failure_percentage = Some(FailurePercentageConfig { | ||
| threshold: Percentage::new(85).unwrap(), | ||
| enforcing_failure_percentage: Percentage::new(100).unwrap(), | ||
| minimum_hosts: 5, | ||
| request_volume: 50, | ||
| }); | ||
| assert!(c.is_enabled()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn percentage_accepts_zero_to_one_hundred() { | ||
| for v in [0, 1, 50, 99, 100] { | ||
| assert_eq!(Percentage::new(v).unwrap().get() as u32, v); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn percentage_rejects_values_above_one_hundred() { | ||
| assert_eq!(Percentage::new(101), Err(PercentageError(101))); | ||
| assert_eq!(Percentage::new(u32::MAX), Err(PercentageError(u32::MAX))); | ||
| } | ||
| } | ||
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.
This is not entirely accurate. Tonic-xds outlier detection will likely be implemented as a stream / mpsc channel that can be polled by LoadBalancer layer in #2607 . The implementation will need to leverage the EjectedChannel type added in #2587
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.
Thanks for this comment @gu0keno0! Will update the module description in subsequent PRs when #2607 is merged.