Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.0 KB
Raw Blame
use std::sync::Arc;

use k8s_awareness::PeerTracker;

use crate::aperture;
use crate::routing::{Candidate, RoutingStrategy};
use crate::worker_registry::{WorkerId, WorkerRegistry};

/// The routable workers for one action.
#[derive(Clone, Debug, Default)]
pub struct WorkerPool {
    pub healthy: Vec<WorkerId>,
    /// Candidates for fresh requests: the weighted aperture ring slice when
    /// narrowing applies, otherwise the healthy pool with uniform weights.
    pub candidates: Vec<Candidate>,
}

/// Captures a [`WorkerPool`] from the registry and the peer set.
#[derive(Clone)]
pub struct WorkerPoolSource {
    registry: Arc<WorkerRegistry>,
    strategy: RoutingStrategy,
    aperture: Option<(Arc<PeerTracker>, usize)>,
}

impl WorkerPoolSource {
    pub fn new(registry: Arc<WorkerRegistry>, strategy: RoutingStrategy) -> Self {
        Self {
            registry,
            strategy,
            aperture: None,
        }
    }

    /// Narrow fresh candidates to this consumer's slice of the worker ring,
    /// at least `min_aperture` wide. Only consulted under [`RoutingStrategy::Aperture`].
    pub fn set_aperture(&mut self, tracker: Arc<PeerTracker>, min_aperture: usize) {
        self.aperture = Some((tracker, min_aperture.max(1)));
    }

    pub fn registry(&self) -> &Arc<WorkerRegistry> {
        &self.registry
    }

    pub fn strategy(&self) -> RoutingStrategy {
        self.strategy
    }

    pub fn min_aperture(&self) -> Option<usize> {
        self.aperture.as_ref().map(|(_, width)| *width)
    }

    /// The ring slice of `healthy`, or `None` when narrowing does not apply.
    /// The slice falls back to `None` while the peer set is unknown, at
    /// startup or with peer awareness disabled.
    pub fn slice(&self, ring: &[WorkerId], healthy: &[WorkerId]) -> Option<Vec<Candidate>> {
        let (tracker, width) = self.active_aperture()?;
        let peers = tracker.snapshot();
        aperture::ring_slice(ring, healthy, peers.self_index, peers.peer_count(), *width)
    }

    pub fn pool(&self) -> WorkerPool {
        let healthy = self.registry.healthy_workers();
        let candidates = self
            .narrow(&healthy)
            .unwrap_or_else(|| uniform(healthy.iter().cloned()));
        WorkerPool {
            healthy,
            candidates,
        }
    }

    pub fn candidates(&self) -> Vec<Candidate> {
        let healthy = self.registry.healthy_workers();
        self.narrow(&healthy)
            .unwrap_or_else(|| uniform(healthy.into_iter()))
    }

    fn active_aperture(&self) -> Option<&(Arc<PeerTracker>, usize)> {
        if self.strategy == RoutingStrategy::Aperture {
            self.aperture.as_ref()
        } else {
            None
        }
    }

    fn narrow(&self, healthy: &[WorkerId]) -> Option<Vec<Candidate>> {
        self.active_aperture()?;
        let ring = aperture::sorted_ring(self.registry.members());
        self.slice(&ring, healthy)
    }
}

fn uniform(workers: impl Iterator<Item = WorkerId>) -> Vec<Candidate> {
    workers.map(Candidate::uniform).collect()
}