Skip to main content

a3s_box_runtime/scale/
mod.rs

1//! Scale Manager — Tracks instances per service and processes scale requests.
2//!
3//! Manages the mapping between services and their running instances,
4//! handles scale-up/scale-down decisions, and emits instance state events.
5
6use a3s_box_core::scale::{InstanceHealth, InstanceState};
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9
10mod api;
11mod authority;
12mod catalog;
13mod endpoints;
14mod manager;
15mod reconciler;
16mod registry;
17
18#[cfg(test)]
19mod tests;
20
21// Re-export public types
22pub use api::{scale_router, serve_scale_api, ScaleApiState, SharedScaleAuthority};
23pub use authority::{DurableScaleAuthority, ScaleAuthorityError};
24pub use catalog::{
25    ScaleCatalogError, ScaleServiceCatalog, SCALE_GUEST_PORT_LABEL, SCALE_MANAGED_LABEL,
26    SCALE_SERVICE_LABEL, SCALE_SLOT_LABEL, SCALE_TEMPLATE_DIGEST_LABEL,
27};
28pub use endpoints::{ScaleEndpointConfig, ScaleEndpointConfigError};
29pub use manager::ScaleManager;
30pub use reconciler::{
31    LocalScaleReconciler, ScaleReconcileError, ScaleReconcileObservation, ScaleReconcileReport,
32};
33pub use registry::InstanceRegistry;
34
35/// Instances belonging to a single service.
36#[derive(Debug, Clone)]
37pub(super) struct ServiceInstances {
38    /// Target replica count
39    pub(super) target_replicas: u32,
40    /// Active instances
41    pub(super) instances: Vec<TrackedInstance>,
42}
43
44/// A tracked instance with its current state.
45#[derive(Debug, Clone)]
46pub(super) struct TrackedInstance {
47    pub(super) id: String,
48    pub(super) state: InstanceState,
49    pub(super) created_at: DateTime<Utc>,
50    pub(super) ready_at: Option<DateTime<Utc>>,
51    pub(super) endpoint: Option<String>,
52    pub(super) health: InstanceHealth,
53}
54
55/// Aggregated health metrics for a service.
56#[derive(Debug, Clone, Default, Serialize, Deserialize)]
57pub struct ServiceHealth {
58    /// Number of active instances (Ready + Busy)
59    pub active_instances: u32,
60    /// Number of Ready instances
61    pub ready_instances: u32,
62    /// Number of Busy instances
63    pub busy_instances: u32,
64    /// Average CPU usage across active instances
65    pub avg_cpu_percent: Option<f32>,
66    /// Total memory usage across all active instances
67    pub total_memory_bytes: u64,
68    /// Total in-flight requests across all active instances
69    pub total_inflight_requests: u32,
70    /// Number of unhealthy instances
71    pub unhealthy_instances: u32,
72}