The request middleware, the backend worker and the metrics handler now share one RequestMetrics value that Services holds, so the LazyLock registry of request_log.rs and its architecture.json exception are gone. The middleware gets it through Observe, the worker through BackendWorkerConfig, and prometheus::render takes it as an argument; the counters, histograms, labels and log lines are unchanged, and the unit tests now count into a registry of their own instead of one shared by the whole test process.
148 lines
5.0 KiB
Rust
148 lines
5.0 KiB
Rust
//! The control-plane stack the replay drives, started over the instant backend.
|
|
|
|
use super::*;
|
|
|
|
pub(super) struct Stack {
|
|
pub(super) addr: std::net::SocketAddr,
|
|
pub(super) marks: Arc<EngineMarks>,
|
|
pub(super) stop: watch::Sender<bool>,
|
|
pub(super) worker: tokio::task::JoinHandle<()>,
|
|
pub(super) server: tokio::task::JoinHandle<()>,
|
|
}
|
|
|
|
fn worker_config(tokenizer: Arc<Tokenizer>) -> BackendWorkerConfig {
|
|
BackendWorkerConfig {
|
|
tokenizer,
|
|
model: leleka_backend_api::scripted::ScriptedEngine::fixture_descriptor(),
|
|
runtime: RuntimeConfig {
|
|
load_config_version: leleka_backend_api::LOAD_CONFIG_VERSION,
|
|
inference_mode: InferenceMode::Native,
|
|
gpu_memory_limit_bytes: None,
|
|
allow_experimental: false,
|
|
},
|
|
max_context: 262_144,
|
|
max_output_tokens: 32768,
|
|
health_interval: Duration::from_secs(3600),
|
|
generation_timeout: None,
|
|
stall_timeout: None,
|
|
attach_retry: Duration::from_millis(20),
|
|
cancel_drain_timeout: Duration::from_secs(5),
|
|
prefix_view: None,
|
|
memory_view: None,
|
|
request_metrics: Default::default(),
|
|
}
|
|
}
|
|
|
|
fn app_state(
|
|
status: &Arc<RwLock<WorkerStatus>>,
|
|
requests: mpsc::Sender<leleka_control_plane::worker::GenerationCommand>,
|
|
control: mpsc::Sender<leleka_control_plane::worker::Control>,
|
|
tokenizer: &Arc<Tokenizer>,
|
|
) -> AppState {
|
|
AppState {
|
|
engine: EngineLink {
|
|
worker: status.clone(),
|
|
client: Some(leleka_control_plane::adapters::ChannelEngine::shared(requests)),
|
|
control: Some(control),
|
|
..Default::default()
|
|
},
|
|
model: ModelView {
|
|
name: Some("fixture".into()),
|
|
tokenizer: Some(Arc::new(leleka_control_plane::codec::qwen_codec_shared(tokenizer.clone()))),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Serves `app` on a loopback port; returns the address and the server task.
|
|
async fn serve(app: axum::Router) -> (std::net::SocketAddr, tokio::task::JoinHandle<()>) {
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let addr = listener.local_addr().unwrap();
|
|
let server = tokio::spawn(async move {
|
|
use axum::serve::ListenerExt as _;
|
|
let listener = listener.tap_io(|tcp| {
|
|
let _ = tcp.set_nodelay(true);
|
|
});
|
|
let _ = axum::serve(listener, app).await;
|
|
});
|
|
(addr, server)
|
|
}
|
|
|
|
async fn await_ready(status: &Arc<RwLock<WorkerStatus>>) {
|
|
tokio::time::timeout(Duration::from_secs(10), async {
|
|
while status.read().await.state != WorkerState::Ready {
|
|
tokio::time::sleep(Duration::from_millis(5)).await;
|
|
}
|
|
})
|
|
.await
|
|
.expect("worker ready");
|
|
}
|
|
|
|
type SpawnedWorker = (
|
|
watch::Sender<bool>,
|
|
mpsc::Sender<leleka_control_plane::worker::GenerationCommand>,
|
|
mpsc::Sender<leleka_control_plane::worker::Control>,
|
|
tokio::task::JoinHandle<()>,
|
|
);
|
|
|
|
/// Runs the backend worker over `backend`; returns its stop switch, its two channels and the task.
|
|
fn spawn_worker(
|
|
backend: Box<dyn Backend>,
|
|
config: BackendWorkerConfig,
|
|
status: Arc<RwLock<WorkerStatus>>,
|
|
) -> SpawnedWorker {
|
|
let (stop, stop_rx) = watch::channel(false);
|
|
let (requests, requests_rx) = mpsc::channel(8);
|
|
let (control, control_rx) = mpsc::channel(8);
|
|
let worker = tokio::spawn(run_backend_worker(
|
|
backend,
|
|
config,
|
|
status,
|
|
stop_rx,
|
|
WorkerInbox { requests: requests_rx, control: control_rx },
|
|
));
|
|
(stop, requests, control, worker)
|
|
}
|
|
|
|
pub(super) async fn start_stack<F>(tokenizer_dir: &Path, make: &F) -> Stack
|
|
where
|
|
F: AsyncFn(Arc<EngineMarks>) -> Box<dyn Backend>,
|
|
{
|
|
let tokenizer = Arc::new(Tokenizer::from_assets(tokenizer_dir).expect("tokenizer assets"));
|
|
let marks = Arc::new(EngineMarks::default());
|
|
let backend = make(marks.clone()).await;
|
|
let config = worker_config(tokenizer.clone());
|
|
let status = Arc::new(RwLock::new(WorkerStatus::default()));
|
|
let (stop, requests, control, worker) = spawn_worker(backend, config, status.clone());
|
|
let state = app_state(&status, requests, control, &tokenizer);
|
|
let (addr, server) = serve(router(state)).await;
|
|
await_ready(&status).await;
|
|
Stack { addr, marks, stop, worker, server }
|
|
}
|
|
|
|
impl Stack {
|
|
pub(super) async fn stop(self) {
|
|
let _ = self.stop.send(true);
|
|
let _ = tokio::time::timeout(Duration::from_secs(5), self.worker).await;
|
|
self.server.abort();
|
|
}
|
|
}
|
|
|
|
pub(super) struct Reply {
|
|
pub(super) status: u16,
|
|
pub(super) body_len: usize,
|
|
pub(super) t_sent: u64,
|
|
pub(super) t_first_byte: u64,
|
|
pub(super) t_created: u64,
|
|
pub(super) t_first_delta: u64,
|
|
pub(super) t_completed: u64,
|
|
pub(super) saw_function_call: bool,
|
|
pub(super) saw_completed: bool,
|
|
pub(super) sent_unix_ms: u128,
|
|
pub(super) done_unix_ms: u128,
|
|
/// `input_tokens` / `cached_tokens` of the terminal event's usage, when present.
|
|
pub(super) usage: (u64, u64),
|
|
pub(super) terminal: String,
|
|
}
|