use std::{
fmt::{Debug, Display},
sync::Arc,
time::Duration,
};
use arc_swap::{ArcSwap, ArcSwapOption};
use tokio::{select, sync::watch::Receiver};
use tokio_util::{sync::CancellationToken, task::TaskTracker};
use super::{
ChecksTarget, ExecutesRequest, TargetState,
distributor::{self, Distributor, Strategy},
health_check::{self, HealthChecker},
};
#[derive(thiserror::Error)]
pub enum Error<E> {
#[error("No healthy nodes")]
NoHealthyNodes,
#[error("{0}")]
Inner(E),
}
struct Actor<T, RQ = (), RS = (), E = ()> {
weights: Vec<usize>,
health_checker: Arc<HealthChecker<T>>,
strategy: Strategy,
executor: Arc<dyn ExecutesRequest<T, Request = RQ, Response = RS, Error = E>>,
distributor: Arc<ArcSwapOption<Distributor<T, RQ, RS, E>>>,
distributor_metrics: distributor::Metrics,
healthy: Arc<ArcSwap<Vec<T>>>,
}
impl<T, RQ, RS, E> Actor<T, RQ, RS, E>
where
T: Clone + Display + Debug + Send + Sync + 'static,
RQ: Send + 'static,
RS: Send + 'static,
E: Send + 'static,
{
async fn process(&self, backends: Arc<Vec<(T, TargetState)>>) {
let healthy = backends
.iter()
.zip(&self.weights)
.filter(|x| x.0.1 == TargetState::Healthy)
.map(|x| (*x.1, x.0.0.clone()))
.collect::<Vec<_>>();
if healthy.is_empty() {
self.distributor.store(None);
return;
}
let distributor = Distributor::new(
&healthy,
self.strategy,
self.executor.clone(),
self.distributor_metrics.clone(),
);
self.distributor.store(Some(Arc::new(distributor)));
self.healthy
.store(Arc::new(healthy.into_iter().map(|x| x.1).collect()));
}
async fn run(&self, token: CancellationToken) {
let mut rx = self.health_checker.subscribe();
loop {
select! {
biased;
_ = token.cancelled() => {
self.health_checker.stop().await;
return;
}
Ok(()) = rx.changed() => {
let backends = rx.borrow_and_update().clone();
self.process(backends).await;
}
}
}
}
}
#[derive(Debug)]
pub struct BackendRouter<T, RQ = (), RS = (), E = ()> {
token: CancellationToken,
tracker: TaskTracker,
distributor: Arc<ArcSwapOption<Distributor<T, RQ, RS, E>>>,
notify: Receiver<Arc<Vec<(T, TargetState)>>>,
healthy: Arc<ArcSwap<Vec<T>>>,
}
impl<T, RQ, RS, E> BackendRouter<T, RQ, RS, E>
where
T: Clone + Display + Debug + Send + Sync + 'static,
RQ: Send + 'static,
RS: Send + 'static,
E: Send + 'static,
{
pub fn new(
backends: &[(T, usize)],
executor: Arc<dyn ExecutesRequest<T, Request = RQ, Response = RS, Error = E>>,
checker: Arc<dyn ChecksTarget<T>>,
strategy: Strategy,
check_interval: Duration,
health_check_metrics: health_check::Metrics,
distributor_metrics: distributor::Metrics,
) -> Self {
let weights = backends.iter().map(|x| x.1).collect();
let backends = backends.iter().map(|x| x.0.clone()).collect::<Vec<_>>();
let health_checker = Arc::new(HealthChecker::new(
&backends,
checker,
check_interval,
health_check_metrics,
));
let notify = health_checker.subscribe();
let distributor = Arc::new(ArcSwapOption::empty());
let healthy = Arc::new(ArcSwap::new(Arc::new(vec![])));
let actor = Actor {
weights,
health_checker,
strategy,
executor,
distributor: distributor.clone(),
distributor_metrics,
healthy: healthy.clone(),
};
let token = CancellationToken::new();
let tracker = TaskTracker::new();
let child_token = token.child_token();
tracker.spawn(async move {
actor.run(child_token).await;
});
Self {
token,
tracker,
distributor,
notify,
healthy,
}
}
pub async fn execute(&self, request: RQ) -> Result<RS, Error<E>> {
let Some(distributor) = self.distributor.load_full() else {
return Err(Error::NoHealthyNodes);
};
distributor
.execute(request)
.await
.map_err(|e| Error::Inner(e))
}
pub fn subscribe(&self) -> Receiver<Arc<Vec<(T, TargetState)>>> {
self.notify.clone()
}
pub fn get_healthy(&self) -> Arc<Vec<T>> {
self.healthy.load_full()
}
pub async fn stop(&self) {
self.token.cancel();
self.tracker.close();
self.tracker.wait().await;
}
}
#[cfg(test)]
mod test {
use std::{
collections::HashMap,
sync::{
Mutex,
atomic::{AtomicBool, Ordering},
},
};
use async_trait::async_trait;
use prometheus::Registry;
use tokio::sync::Notify;
use super::distributor::test::{
FailingExecutor, GateExecutor, TestExecutor, counting_executor,
};
use super::*;
struct TestChecker;
#[async_trait]
impl ChecksTarget<String> for TestChecker {
async fn check(&self, target: &String) -> TargetState {
if ["foo", "bar"].contains(&target.as_str()) {
TargetState::Healthy
} else {
TargetState::Degraded
}
}
}
#[tokio::test(start_paused = true)]
async fn test_request_router_somewhat_healthy() {
let executor = Arc::new(TestExecutor(Duration::ZERO, Mutex::new(HashMap::new())));
let router = BackendRouter::new(
&[
("foo".to_string(), 1),
("bar".to_string(), 2),
("baz".to_string(), 3),
],
executor.clone(),
Arc::new(TestChecker),
Strategy::WeightedRoundRobin,
Duration::from_millis(1),
health_check::Metrics::new(&Registry::new()),
distributor::Metrics::new(&Registry::new()),
);
tokio::time::sleep(Duration::from_millis(100)).await;
for _ in 0..900 {
assert!(router.execute(()).await.is_ok());
}
let h = executor.1.lock().unwrap();
assert_eq!(h["foo"], 300);
assert_eq!(h["bar"], 600);
assert!(!h.contains_key("baz"));
drop(h)
}
#[tokio::test(start_paused = true)]
async fn test_request_router_unhealthy() {
let executor = Arc::new(TestExecutor(Duration::ZERO, Mutex::new(HashMap::new())));
let router = BackendRouter::new(
&[("baz".to_string(), 3)],
executor.clone(),
Arc::new(TestChecker),
Strategy::WeightedRoundRobin,
Duration::from_millis(1),
health_check::Metrics::new(&Registry::new()),
distributor::Metrics::new(&Registry::new()),
);
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(matches!(
router.execute(()).await.unwrap_err(),
Error::NoHealthyNodes
));
}
struct SetChecker(Vec<String>);
impl SetChecker {
fn new(healthy: &[&str]) -> Self {
Self(healthy.iter().map(|x| (*x).to_string()).collect())
}
}
#[async_trait]
impl ChecksTarget<String> for SetChecker {
async fn check(&self, target: &String) -> TargetState {
if self.0.contains(target) {
TargetState::Healthy
} else {
TargetState::Degraded
}
}
}
struct FlipChecker {
flaky: String,
healthy: Arc<AtomicBool>,
}
#[async_trait]
impl ChecksTarget<String> for FlipChecker {
async fn check(&self, target: &String) -> TargetState {
if *target == self.flaky && !self.healthy.load(Ordering::SeqCst) {
TargetState::Degraded
} else {
TargetState::Healthy
}
}
}
struct GatedChecker {
gate: Arc<Notify>,
open: Arc<AtomicBool>,
}
impl GatedChecker {
fn new() -> (Self, Arc<Notify>) {
let gate = Arc::new(Notify::new());
(
Self {
gate: gate.clone(),
open: Arc::new(AtomicBool::new(false)),
},
gate,
)
}
fn never() -> Self {
Self::new().0
}
}
#[async_trait]
impl ChecksTarget<String> for GatedChecker {
async fn check(&self, _target: &String) -> TargetState {
if !self.open.load(Ordering::SeqCst) {
self.gate.notified().await;
self.open.store(true, Ordering::SeqCst);
}
TargetState::Healthy
}
}
fn backends(v: &[(&str, usize)]) -> Vec<(String, usize)> {
v.iter().map(|(n, w)| ((*n).to_string(), *w)).collect()
}
fn names(v: &[&str]) -> Vec<String> {
v.iter().map(|x| (*x).to_string()).collect()
}
fn router(
backends: &[(String, usize)],
executor: Arc<TestExecutor>,
checker: Arc<dyn ChecksTarget<String>>,
strategy: Strategy,
) -> BackendRouter<String> {
BackendRouter::new(
backends,
executor,
checker,
strategy,
Duration::from_millis(1),
health_check::Metrics::new(&Registry::new()),
distributor::Metrics::new(&Registry::new()),
)
}
#[test]
fn test_error_display() {
assert_eq!(
Error::<String>::NoHealthyNodes.to_string(),
"No healthy nodes"
);
assert_eq!(Error::Inner("boom".to_string()).to_string(), "boom");
}
#[tokio::test(start_paused = true)]
async fn test_router_no_backends_before_first_check_result() {
let (checker, gate) = GatedChecker::new();
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1)]),
executor.clone(),
Arc::new(checker),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(router.get_healthy().is_empty());
assert!(matches!(
router.execute(()).await.unwrap_err(),
Error::NoHealthyNodes
));
gate.notify_waiters();
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(*router.get_healthy(), names(&["foo"]));
assert!(router.execute(()).await.is_ok());
assert_eq!(executor.1.lock().unwrap()["foo"], 1);
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_weights_follow_backend_positions() {
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1), ("bar", 2), ("baz", 3)]),
executor.clone(),
Arc::new(SetChecker::new(&["bar", "baz"])),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(*router.get_healthy(), names(&["bar", "baz"]));
for _ in 0..1000 {
assert!(router.execute(()).await.is_ok());
}
let h = executor.counts();
assert_eq!(h["bar"], 400);
assert_eq!(h["baz"], 600);
assert!(!h.contains_key("foo"));
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_zero_weight_backend_is_starved() {
let executor = counting_executor();
let router = router(
&backends(&[("off", 0), ("on", 1)]),
executor.clone(),
Arc::new(SetChecker::new(&["off", "on"])),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(*router.get_healthy(), names(&["off", "on"]));
for _ in 0..20 {
assert!(router.execute(()).await.is_ok());
}
let h = executor.counts();
assert_eq!(h["on"], 20);
assert!(!h.contains_key("off"));
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_lor_strategy() {
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1), ("bar", 5), ("baz", 5)]),
executor.clone(),
Arc::new(SetChecker::new(&["foo", "bar", "baz"])),
Strategy::LeastOutstandingRequests,
);
tokio::time::sleep(Duration::from_millis(100)).await;
for _ in 0..20 {
assert!(router.execute(()).await.is_ok());
}
let h = executor.counts();
assert_eq!(h["foo"], 20);
assert_eq!(h.len(), 1);
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_subscribe_propagates_all_target_states() {
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1), ("bar", 1)]),
executor,
Arc::new(SetChecker::new(&["foo"])),
Strategy::WeightedRoundRobin,
);
let mut rx = router.subscribe();
assert!(rx.borrow_and_update().is_empty());
tokio::time::sleep(Duration::from_millis(100)).await;
rx.changed().await.unwrap();
assert_eq!(
**rx.borrow_and_update(),
[
("foo".to_string(), TargetState::Healthy),
("bar".to_string(), TargetState::Degraded),
]
);
let mut late = router.subscribe();
late.changed().await.unwrap();
assert_eq!(*late.borrow_and_update(), *rx.borrow());
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_picks_up_recovered_backend() {
let flag = Arc::new(AtomicBool::new(false));
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1), ("bar", 1)]),
executor.clone(),
Arc::new(FlipChecker {
flaky: "bar".to_string(),
healthy: flag.clone(),
}),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(*router.get_healthy(), names(&["foo"]));
for _ in 0..10 {
assert!(router.execute(()).await.is_ok());
}
{
let h = executor.counts();
assert_eq!(h["foo"], 10);
assert!(!h.contains_key("bar"));
}
flag.store(true, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(*router.get_healthy(), names(&["foo", "bar"]));
for _ in 0..10 {
assert!(router.execute(()).await.is_ok());
}
let h = executor.counts();
assert_eq!(h["foo"], 15);
assert_eq!(h["bar"], 5);
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_stops_routing_when_last_backend_fails() {
let flag = Arc::new(AtomicBool::new(true));
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1)]),
executor.clone(),
Arc::new(FlipChecker {
flaky: "foo".to_string(),
healthy: flag.clone(),
}),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(router.execute(()).await.is_ok());
flag.store(false, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(matches!(
router.execute(()).await.unwrap_err(),
Error::NoHealthyNodes
));
assert_eq!(executor.1.lock().unwrap()["foo"], 1);
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_propagates_executor_error() {
let router: BackendRouter<String, (), (), String> = BackendRouter::new(
&backends(&[("foo", 1)]),
Arc::new(FailingExecutor),
Arc::new(SetChecker::new(&["foo"])),
Strategy::WeightedRoundRobin,
Duration::from_millis(1),
health_check::Metrics::new(&Registry::new()),
distributor::Metrics::new(&Registry::new()),
);
tokio::time::sleep(Duration::from_millis(50)).await;
let err = router.execute(()).await.err().unwrap();
assert!(matches!(err, Error::Inner(_)));
assert_eq!(err.to_string(), "boom: foo");
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_stop() {
let flag = Arc::new(AtomicBool::new(false));
let executor = counting_executor();
let router = router(
&backends(&[("foo", 1), ("bar", 1)]),
executor.clone(),
Arc::new(FlipChecker {
flaky: "bar".to_string(),
healthy: flag.clone(),
}),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(*router.get_healthy(), names(&["foo"]));
router.stop().await;
router.stop().await;
flag.store(true, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(*router.get_healthy(), names(&["foo"]));
assert!(router.execute(()).await.is_ok());
let h = executor.counts();
assert_eq!(h["foo"], 1);
assert!(!h.contains_key("bar"));
}
#[tokio::test(start_paused = true)]
async fn test_router_with_no_backends_at_all() {
let router = router(
&[],
counting_executor(),
Arc::new(SetChecker::new(&["foo"])),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(router.get_healthy().is_empty());
let rx = router.subscribe();
assert!(rx.borrow().is_empty());
assert!(matches!(
router.execute(()).await.unwrap_err(),
Error::NoHealthyNodes
));
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_inflight_request_outlives_distributor_swap() {
let flag = Arc::new(AtomicBool::new(true));
let executor = Arc::new(GateExecutor::default());
let router: Arc<BackendRouter<String>> = Arc::new(BackendRouter::new(
&backends(&[("foo", 1)]),
executor.clone(),
Arc::new(FlipChecker {
flaky: "foo".to_string(),
healthy: flag.clone(),
}),
Strategy::WeightedRoundRobin,
Duration::from_millis(1),
health_check::Metrics::new(&Registry::new()),
distributor::Metrics::new(&Registry::new()),
));
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(*router.get_healthy(), names(&["foo"]));
let r = router.clone();
let handle = tokio::spawn(async move { r.execute(()).await });
while executor.started.load(Ordering::SeqCst) == 0 {
tokio::task::yield_now().await;
}
flag.store(false, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(matches!(
router.execute(()).await.unwrap_err(),
Error::NoHealthyNodes
));
assert!(router.get_healthy().contains(&"foo".to_string()));
assert_eq!(executor.started.load(Ordering::SeqCst), 1);
executor.gate.notify_waiters();
assert!(handle.await.unwrap().is_ok());
router.stop().await;
}
#[tokio::test(start_paused = true)]
async fn test_router_stop_hangs_while_a_check_is_in_flight() {
let router = router(
&backends(&[("foo", 1)]),
counting_executor(),
Arc::new(GatedChecker::never()),
Strategy::WeightedRoundRobin,
);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(router.get_healthy().is_empty());
assert!(
tokio::time::timeout(Duration::from_secs(30), router.stop())
.await
.is_err()
);
}
}