Skip to content
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

feat: Support observe backend health status #225 #325

Closed
wants to merge 6 commits into from

Conversation

vicanso
Copy link
Contributor

@vicanso vicanso commented Jul 13, 2024

This PR supports add callback function for observing upstream backend health status.
Issue: #225

@vicanso
Copy link
Contributor Author

vicanso commented Jul 19, 2024

@eaufavor
I want to know how to get PR accepted?

@eaufavor eaufavor added the enhancement New feature or request label Jul 26, 2024
@andrewhavck andrewhavck self-assigned this Jul 26, 2024
#[async_trait]
pub trait HealthObserve {
/// This function is called When health status changes
async fn health_check_callback(&self, _target: &Backend, _healthy: bool);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind removing the underscores in the parameters?

@@ -24,13 +24,24 @@ use pingora_http::{RequestHeader, ResponseHeader};
use std::sync::Arc;
use std::time::Duration;

#[async_trait]
pub trait HealthObserve {
/// This function is called When health status changes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the doc comment to be /// Observes the health of a [Backend], can be used for monitoring purposes..

#[async_trait]
pub trait HealthObserve {
/// This function is called When health status changes
async fn health_check_callback(&self, _target: &Backend, _healthy: bool);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this function from health_check_callback to observe.

/// [HealthCheck] is the interface to implement health check for backends
#[async_trait]
pub trait HealthCheck {
/// Check the given backend.
///
/// `Ok(())`` if the check passes, otherwise the check fails.
async fn check(&self, target: &Backend) -> Result<()>;

/// This function is called When health status changes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update doc comment to /// Called when the health changes for a [Backend]..

@@ -56,6 +67,7 @@ pub struct TcpHealthCheck {
/// set, it will also try to establish a TLS connection on top of the TCP connection.
pub peer_template: BasicPeer,
connector: TransportConnector,
callback: Option<HealthCheckCallback>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be pub and renamed to health_changed_callback. Doc comment can be /// A callback that is invoked when the `healthy` status changes for a [Backend].

@@ -93,6 +106,11 @@ impl TcpHealthCheck {
pub fn set_connector(&mut self, connector: TransportConnector) {
self.connector = connector;
}

/// Set the health observe callback function.
pub fn set_callback(&mut self, callback: HealthCheckCallback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove this if we change the callback to be pub.

}
}

/// Replace the internal http connector with the given [HttpConnector]
pub fn set_connector(&mut self, connector: HttpConnector) {
self.connector = connector;
}
/// Set the health observe callback function.
pub fn set_callback(&mut self, callback: HealthCheckCallback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove this if we change the callback to be pub.

@@ -147,6 +171,7 @@ pub struct HttpHealthCheck {
/// Sometimes the health check endpoint lives one a different port than the actual backend.
/// Setting this option allows the health check to perform on the given port of the backend IP.
pub port_override: Option<u16>,
callback: Option<HealthCheckCallback>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be pub and renamed to health_changed_callback. Doc comment can be /// A callback that is invoked when the `healthy` status changes for a [Backend]...

/// This function is called When health status changes
async fn health_check_callback(&self, _target: &Backend, _healthy: bool);
}
pub type HealthCheckCallback = Box<dyn HealthObserve + Send + Sync>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this to HealthObserveCallback and add doc comment /// Provided to a [HealthCheck] to observe changes to [Backend] health.

@@ -226,8 +226,10 @@ impl Backends {
h.observe_health(errored.is_none(), check.health_threshold(errored.is_none()));
if flipped {
if let Some(e) = errored {
check.health_status_change(backend, false).await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check.health_status_change(backend, errored.is_none()).await; before the if let check, then we can remove the redundant call.

@vicanso
Copy link
Contributor Author

vicanso commented Jul 28, 2024

@andrewhavck I have modified the code, please review.

@vicanso vicanso requested a review from andrewhavck July 30, 2024 14:14
@andrewhavck
Copy link
Contributor

@vicanso thanks for addressing the review comments, looks like there are some merge conflicts now? Once those are resolved I'll get this in internal review.

@vicanso
Copy link
Contributor Author

vicanso commented Aug 8, 2024

@andrewhavck Is any adjustment needed?

andrewhavck pushed a commit that referenced this pull request Aug 9, 2024
---
test: add test for upstream health observe
---
renamed the function and added doc to make it intelligible
---
fix clippy error
---
Merge branch 'main' into main
---
test: fix test for backend do update

Co-authored-by: Tree Xie <tree.xie@outlook.com>
Includes-commit: 1421c26
Includes-commit: 695d549
Includes-commit: 6a09b52
Includes-commit: 72d6ee0
Includes-commit: e6c2af0
Includes-commit: fb62869
Replicated-from: #325
andrewhavck pushed a commit that referenced this pull request Aug 9, 2024
---
test: add test for upstream health observe
---
renamed the function and added doc to make it intelligible
---
fix clippy error
---
Merge branch 'main' into main
---
test: fix test for backend do update

Co-authored-by: Tree Xie <tree.xie@outlook.com>
Includes-commit: 1421c26
Includes-commit: 695d549
Includes-commit: 6a09b52
Includes-commit: 72d6ee0
Includes-commit: e6c2af0
Includes-commit: fb62869
Replicated-from: #325
@andrewhavck
Copy link
Contributor

@vicanso thanks for your contribution, this was included in our latest sync!

@andrewhavck andrewhavck closed this Aug 9, 2024
escoffier pushed a commit to escoffier/pingora that referenced this pull request Sep 6, 2024
---
test: add test for upstream health observe
---
renamed the function and added doc to make it intelligible
---
fix clippy error
---
Merge branch 'main' into main
---
test: fix test for backend do update

Co-authored-by: Tree Xie <tree.xie@outlook.com>
Includes-commit: 1421c26
Includes-commit: 695d549
Includes-commit: 6a09b52
Includes-commit: 72d6ee0
Includes-commit: e6c2af0
Includes-commit: fb62869
Replicated-from: cloudflare#325
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants