pingora_load_balancing/health_check.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
// Copyright 2024 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Health Check interface and methods.
use crate::Backend;
use arc_swap::ArcSwap;
use async_trait::async_trait;
use pingora_core::connectors::{http::Connector as HttpConnector, TransportConnector};
use pingora_core::upstreams::peer::{BasicPeer, HttpPeer, Peer};
use pingora_error::{Error, ErrorType::CustomCode, Result};
use pingora_http::{RequestHeader, ResponseHeader};
use std::sync::Arc;
use std::time::Duration;
/// [HealthObserve] is an interface for observing health changes of backends,
/// this is what's used for our health observation callback.
#[async_trait]
pub trait HealthObserve {
/// Observes the health of a [Backend], can be used for monitoring purposes.
async fn observe(&self, target: &Backend, healthy: bool);
}
/// Provided to a [HealthCheck] to observe changes to [Backend] health.
pub type HealthObserveCallback = Box<dyn HealthObserve + Send + Sync>;
/// [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<()>;
/// Called when the health changes for a [Backend].
async fn health_status_change(&self, _target: &Backend, _healthy: bool) {}
/// This function defines how many *consecutive* checks should flip the health of a backend.
///
/// For example: with `success``: `true`: this function should return the
/// number of check need to flip from unhealthy to healthy.
fn health_threshold(&self, success: bool) -> usize;
}
/// TCP health check
///
/// This health check checks if a TCP (or TLS) connection can be established to a given backend.
pub struct TcpHealthCheck {
/// Number of successful checks to flip from unhealthy to healthy.
pub consecutive_success: usize,
/// Number of failed checks to flip from healthy to unhealthy.
pub consecutive_failure: usize,
/// How to connect to the backend.
///
/// This field defines settings like the connect timeout and src IP to bind.
/// The SocketAddr of `peer_template` is just a placeholder which will be replaced by the
/// actual address of the backend when the health check runs.
///
/// By default, this check will try to establish a TCP connection. When the `sni` field is
/// set, it will also try to establish a TLS connection on top of the TCP connection.
pub peer_template: BasicPeer,
connector: TransportConnector,
/// A callback that is invoked when the `healthy` status changes for a [Backend].
pub health_changed_callback: Option<HealthObserveCallback>,
}
impl Default for TcpHealthCheck {
fn default() -> Self {
let mut peer_template = BasicPeer::new("0.0.0.0:1");
peer_template.options.connection_timeout = Some(Duration::from_secs(1));
TcpHealthCheck {
consecutive_success: 1,
consecutive_failure: 1,
peer_template,
connector: TransportConnector::new(None),
health_changed_callback: None,
}
}
}
impl TcpHealthCheck {
/// Create a new [TcpHealthCheck] with the following default values
/// * connect timeout: 1 second
/// * consecutive_success: 1
/// * consecutive_failure: 1
pub fn new() -> Box<Self> {
Box::<TcpHealthCheck>::default()
}
/// Create a new [TcpHealthCheck] that tries to establish a TLS connection.
///
/// The default values are the same as [Self::new()].
pub fn new_tls(sni: &str) -> Box<Self> {
let mut new = Self::default();
new.peer_template.sni = sni.into();
Box::new(new)
}
/// Replace the internal tcp connector with the given [TransportConnector]
pub fn set_connector(&mut self, connector: TransportConnector) {
self.connector = connector;
}
}
#[async_trait]
impl HealthCheck for TcpHealthCheck {
fn health_threshold(&self, success: bool) -> usize {
if success {
self.consecutive_success
} else {
self.consecutive_failure
}
}
async fn check(&self, target: &Backend) -> Result<()> {
let mut peer = self.peer_template.clone();
peer._address = target.addr.clone();
self.connector.get_stream(&peer).await.map(|_| {})
}
async fn health_status_change(&self, target: &Backend, healthy: bool) {
if let Some(callback) = &self.health_changed_callback {
callback.observe(target, healthy).await;
}
}
}
type Validator = Box<dyn Fn(&ResponseHeader) -> Result<()> + Send + Sync>;
/// HTTP health check
///
/// This health check checks if it can receive the expected HTTP(s) response from the given backend.
pub struct HttpHealthCheck {
/// Number of successful checks to flip from unhealthy to healthy.
pub consecutive_success: usize,
/// Number of failed checks to flip from healthy to unhealthy.
pub consecutive_failure: usize,
/// How to connect to the backend.
///
/// This field defines settings like the connect timeout and src IP to bind.
/// The SocketAddr of `peer_template` is just a placeholder which will be replaced by the
/// actual address of the backend when the health check runs.
///
/// Set the `scheme` field to use HTTPs.
pub peer_template: HttpPeer,
/// Whether the underlying TCP/TLS connection can be reused across checks.
///
/// * `false` will make sure that every health check goes through TCP (and TLS) handshakes.
/// Established connections sometimes hide the issue of firewalls and L4 LB.
/// * `true` will try to reuse connections across checks, this is the more efficient and fast way
/// to perform health checks.
pub reuse_connection: bool,
/// The request header to send to the backend
pub req: RequestHeader,
connector: HttpConnector,
/// Optional field to define how to validate the response from the server.
///
/// If not set, any response with a `200 OK` is considered a successful check.
pub validator: Option<Validator>,
/// 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>,
/// A callback that is invoked when the `healthy` status changes for a [Backend].
pub health_changed_callback: Option<HealthObserveCallback>,
}
impl HttpHealthCheck {
/// Create a new [HttpHealthCheck] with the following default settings
/// * connect timeout: 1 second
/// * read timeout: 1 second
/// * req: a GET to the `/` of the given host name
/// * consecutive_success: 1
/// * consecutive_failure: 1
/// * reuse_connection: false
/// * validator: `None`, any 200 response is considered successful
pub fn new(host: &str, tls: bool) -> Self {
let mut req = RequestHeader::build("GET", b"/", None).unwrap();
req.append_header("Host", host).unwrap();
let sni = if tls { host.into() } else { String::new() };
let mut peer_template = HttpPeer::new("0.0.0.0:1", tls, sni);
peer_template.options.connection_timeout = Some(Duration::from_secs(1));
peer_template.options.read_timeout = Some(Duration::from_secs(1));
HttpHealthCheck {
consecutive_success: 1,
consecutive_failure: 1,
peer_template,
connector: HttpConnector::new(None),
reuse_connection: false,
req,
validator: None,
port_override: None,
health_changed_callback: None,
}
}
/// Replace the internal http connector with the given [HttpConnector]
pub fn set_connector(&mut self, connector: HttpConnector) {
self.connector = connector;
}
}
#[async_trait]
impl HealthCheck for HttpHealthCheck {
fn health_threshold(&self, success: bool) -> usize {
if success {
self.consecutive_success
} else {
self.consecutive_failure
}
}
async fn check(&self, target: &Backend) -> Result<()> {
let mut peer = self.peer_template.clone();
peer._address = target.addr.clone();
if let Some(port) = self.port_override {
peer._address.set_port(port);
}
let session = self.connector.get_http_session(&peer).await?;
let mut session = session.0;
let req = Box::new(self.req.clone());
session.write_request_header(req).await?;
if let Some(read_timeout) = peer.options.read_timeout {
session.set_read_timeout(read_timeout);
}
session.read_response_header().await?;
let resp = session.response_header().expect("just read");
if let Some(validator) = self.validator.as_ref() {
validator(resp)?;
} else if resp.status != 200 {
return Error::e_explain(
CustomCode("non 200 code", resp.status.as_u16()),
"during http healthcheck",
);
};
while session.read_response_body().await?.is_some() {
// drain the body if any
}
if self.reuse_connection {
let idle_timeout = peer.idle_timeout();
self.connector
.release_http_session(session, &peer, idle_timeout)
.await;
}
Ok(())
}
async fn health_status_change(&self, target: &Backend, healthy: bool) {
if let Some(callback) = &self.health_changed_callback {
callback.observe(target, healthy).await;
}
}
}
#[derive(Clone)]
struct HealthInner {
/// Whether the endpoint is healthy to serve traffic
healthy: bool,
/// Whether the endpoint is allowed to serve traffic independent of its health
enabled: bool,
/// The counter for stateful transition between healthy and unhealthy.
/// When [healthy] is true, this counts the number of consecutive health check failures
/// so that the caller can flip the healthy when a certain threshold is met, and vise versa.
consecutive_counter: usize,
}
/// Health of backends that can be updated atomically
pub(crate) struct Health(ArcSwap<HealthInner>);
impl Default for Health {
fn default() -> Self {
Health(ArcSwap::new(Arc::new(HealthInner {
healthy: true, // TODO: allow to start with unhealthy
enabled: true,
consecutive_counter: 0,
})))
}
}
impl Clone for Health {
fn clone(&self) -> Self {
let inner = self.0.load_full();
Health(ArcSwap::new(inner))
}
}
impl Health {
pub fn ready(&self) -> bool {
let h = self.0.load();
h.healthy && h.enabled
}
pub fn enable(&self, enabled: bool) {
let h = self.0.load();
if h.enabled != enabled {
// clone the inner
let mut new_health = (**h).clone();
new_health.enabled = enabled;
self.0.store(Arc::new(new_health));
};
}
// return true when the health is flipped
pub fn observe_health(&self, health: bool, flip_threshold: usize) -> bool {
let h = self.0.load();
let mut flipped = false;
if h.healthy != health {
// opposite health observed, ready to increase the counter
// clone the inner
let mut new_health = (**h).clone();
new_health.consecutive_counter += 1;
if new_health.consecutive_counter >= flip_threshold {
new_health.healthy = health;
new_health.consecutive_counter = 0;
flipped = true;
}
self.0.store(Arc::new(new_health));
} else if h.consecutive_counter > 0 {
// observing the same health as the current state.
// reset the counter, if it is non-zero, because it is no longer consecutive
let mut new_health = (**h).clone();
new_health.consecutive_counter = 0;
self.0.store(Arc::new(new_health));
}
flipped
}
}
#[cfg(test)]
mod test {
use std::{
collections::{BTreeSet, HashMap},
sync::atomic::{AtomicU16, Ordering},
};
use super::*;
use crate::{discovery, Backends, SocketAddr};
use async_trait::async_trait;
use http::Extensions;
#[tokio::test]
async fn test_tcp_check() {
let tcp_check = TcpHealthCheck::default();
let backend = Backend {
addr: SocketAddr::Inet("1.1.1.1:80".parse().unwrap()),
weight: 1,
ext: Extensions::new(),
};
assert!(tcp_check.check(&backend).await.is_ok());
let backend = Backend {
addr: SocketAddr::Inet("1.1.1.1:79".parse().unwrap()),
weight: 1,
ext: Extensions::new(),
};
assert!(tcp_check.check(&backend).await.is_err());
}
#[cfg(feature = "any_tls")]
#[tokio::test]
async fn test_tls_check() {
let tls_check = TcpHealthCheck::new_tls("one.one.one.one");
let backend = Backend {
addr: SocketAddr::Inet("1.1.1.1:443".parse().unwrap()),
weight: 1,
ext: Extensions::new(),
};
assert!(tls_check.check(&backend).await.is_ok());
}
#[cfg(feature = "any_tls")]
#[tokio::test]
async fn test_https_check() {
let https_check = HttpHealthCheck::new("one.one.one.one", true);
let backend = Backend {
addr: SocketAddr::Inet("1.1.1.1:443".parse().unwrap()),
weight: 1,
ext: Extensions::new(),
};
assert!(https_check.check(&backend).await.is_ok());
}
#[tokio::test]
async fn test_http_custom_check() {
let mut http_check = HttpHealthCheck::new("one.one.one.one", false);
http_check.validator = Some(Box::new(|resp: &ResponseHeader| {
if resp.status == 301 {
Ok(())
} else {
Error::e_explain(
CustomCode("non 301 code", resp.status.as_u16()),
"during http healthcheck",
)
}
}));
let backend = Backend {
addr: SocketAddr::Inet("1.1.1.1:80".parse().unwrap()),
weight: 1,
ext: Extensions::new(),
};
http_check.check(&backend).await.unwrap();
assert!(http_check.check(&backend).await.is_ok());
}
#[tokio::test]
async fn test_health_observe() {
struct Observe {
unhealthy_count: Arc<AtomicU16>,
}
#[async_trait]
impl HealthObserve for Observe {
async fn observe(&self, _target: &Backend, healthy: bool) {
if !healthy {
self.unhealthy_count.fetch_add(1, Ordering::Relaxed);
}
}
}
let good_backend = Backend::new("127.0.0.1:79").unwrap();
let new_good_backends = || -> (BTreeSet<Backend>, HashMap<u64, bool>) {
let mut healthy = HashMap::new();
healthy.insert(good_backend.hash_key(), true);
let mut backends = BTreeSet::new();
backends.extend(vec![good_backend.clone()]);
(backends, healthy)
};
// tcp health check
{
let unhealthy_count = Arc::new(AtomicU16::new(0));
let ob = Observe {
unhealthy_count: unhealthy_count.clone(),
};
let bob = Box::new(ob);
let tcp_check = TcpHealthCheck {
health_changed_callback: Some(bob),
..Default::default()
};
let discovery = discovery::Static::default();
let mut backends = Backends::new(Box::new(discovery));
backends.set_health_check(Box::new(tcp_check));
let result = new_good_backends();
backends.do_update(result.0, result.1, |_backend: Arc<BTreeSet<Backend>>| {});
// the backend is ready
assert!(backends.ready(&good_backend));
// run health check
backends.run_health_check(false).await;
assert!(1 == unhealthy_count.load(Ordering::Relaxed));
// backend is unhealthy
assert!(!backends.ready(&good_backend));
}
// http health check
{
let unhealthy_count = Arc::new(AtomicU16::new(0));
let ob = Observe {
unhealthy_count: unhealthy_count.clone(),
};
let bob = Box::new(ob);
let mut https_check = HttpHealthCheck::new("one.one.one.one", true);
https_check.health_changed_callback = Some(bob);
let discovery = discovery::Static::default();
let mut backends = Backends::new(Box::new(discovery));
backends.set_health_check(Box::new(https_check));
let result = new_good_backends();
backends.do_update(result.0, result.1, |_backend: Arc<BTreeSet<Backend>>| {});
// the backend is ready
assert!(backends.ready(&good_backend));
// run health check
backends.run_health_check(false).await;
assert!(1 == unhealthy_count.load(Ordering::Relaxed));
assert!(!backends.ready(&good_backend));
}
}
}