Skip to main content

liminal_server/server/
runtime.rs

1use crate::ServerError;
2use crate::auth_pass::PassVerifier;
3use crate::cluster::ClusterHandle;
4use crate::config::file::load_config;
5use crate::config::types::ServiceProfile;
6use crate::health::{ReadinessState, SharedReadinessState, start_health_server};
7use crate::server::connection::WebSocketListener;
8use crate::server::connection::services::{LiminalConnectionServices, build_connection_services};
9use crate::server::connection::{
10    ConnectionSupervisor, LysInstall, configured_lys, start_context_watch,
11};
12use crate::server::listener::ServerListener;
13use crate::server::self_streams::owner::PublisherRuntime;
14use crate::server::shutdown::{ShutdownHandle, register_signal_handlers};
15use crate::services::production::ServiceBindings;
16
17use std::fmt;
18use std::path::Path;
19use std::sync::Arc;
20use std::time::Instant;
21
22#[path = "runtime_links.rs"]
23pub(in crate::server) mod runtime_links;
24use runtime_links::ConfiguredLinks;
25#[path = "link_udp_endpoint.rs"]
26pub mod link_udp_endpoint;
27
28#[cfg(feature = "test-support")]
29pub use runtime_links::test_support as link_test_support;
30
31fn configured_authentication(
32    config: &crate::config::types::ServerConfig,
33) -> Result<(Option<Vec<u8>>, Option<PassVerifier>), ServerError> {
34    let auth_token = config
35        .auth
36        .as_ref()
37        .map(|auth| auth.token.clone().into_bytes());
38    let pass_verifier = config
39        .auth
40        .as_ref()
41        .map(PassVerifier::from_auth)
42        .transpose()?
43        .flatten();
44    Ok((auth_token, pass_verifier))
45}
46
47/// Rewrites every participant conversation log under the configured
48/// persistence path into the current row grammar, offline.
49///
50/// The store is opened exclusively, so this refuses while a server has it
51/// open. Every conversation is replayed once, rewritten into its next log
52/// generation, proved to replay to the same state, and only then switched.
53///
54/// # Errors
55///
56/// Returns [`ServerError`] when the configuration names no persistence path
57/// or no `[participant]` section, the store cannot be opened, or a
58/// conversation refuses the migration (named in the error).
59pub fn run_participant_log_migration(
60    config_path: &Path,
61) -> Result<crate::server::participant::ParticipantLogMigration, ServerError> {
62    let config = load_config(config_path)?;
63    let Some(persistence_path) = config.persistence_path.as_deref() else {
64        return Err(ServerError::ConfigValidation {
65            message: "the participant log migration needs persistence_path; an ephemeral store has no logs to migrate".to_owned(),
66        });
67    };
68    let Some(participant) = config.participant.as_ref() else {
69        return Err(ServerError::ConfigValidation {
70            message: "the participant log migration needs the [participant] section".to_owned(),
71        });
72    };
73    let store = crate::server::connection::services::open_persistent_store(persistence_path)?;
74    Ok(crate::server::participant::migrate_participant_logs(
75        &store,
76        participant,
77    )?)
78}
79
80/// Starts the server deployment wrapper for the supplied configuration path.
81///
82/// # Errors
83///
84/// Returns [`ServerError`] when a later server lifecycle phase fails.
85pub fn run(config_path: &Path) -> Result<(), ServerError> {
86    let mut stages = StartStages::new();
87    let config = load_config(config_path)?;
88    stages.mark("config");
89
90    // Enable metrics for this process before the health server accepts scrapes,
91    // so `/metrics` renders the server families. Standalone liminal library users
92    // never call this, so the registry gate stays off for them.
93    crate::metrics::init();
94
95    let readiness = SharedReadinessState::new(ReadinessState::default());
96    let health_server = start_health_server(config.health_listen_address, readiness.clone())?;
97    stages.mark("health");
98    let shutdown_handle = ShutdownHandle::new();
99    let signal_registration = register_signal_handlers(shutdown_handle.clone())?;
100
101    // The configured [auth] token must ride along here: these call sites build
102    // services themselves (full mode reaches the shared channel cluster first;
103    // the worker front door builds no cluster at all) and so cannot use
104    // `from_config`, which is the only other place the token is wired.
105    let (auth_token, pass_verifier) = configured_authentication(&config)?;
106    stages.mark("auth");
107    // ACCESS-003 R1 FOUND: the production server built its supervisor by a
108    // path that installed no Lys, so `[auth.lys]` never reached a connection,
109    // and it started with no tokio runtime, so a live ask had nothing to run
110    // on. Both are built here, once, and the Lys install takes the same path
111    // for every profile.
112    let (lys_ask, lys) = runtime_links::lys(&config)?;
113    stages.mark("lys");
114    let mut context_watch = None;
115
116    // D2: the service profile selects which connection-services stack is built.
117    // Full mode is byte-for-byte the previous construction path (build services,
118    // reach the shared channel cluster, start clustering when configured). The
119    // worker front door constructs the connection supervisor over the
120    // capability-scoped adapter and NOTHING else — no channel/conversation/haematite
121    // services, and therefore no distribution cluster (config validation rejects a
122    // `[cluster]` section under this profile, so none can be present here).
123    let mut self_stream_publisher = None;
124    let mut spine = None;
125    let (connection_supervisor, cluster_handle, mut links) = match config.services.profile()? {
126        ServiceProfile::Full => {
127            let full = start_full_profile(
128                &config,
129                (auth_token, pass_verifier),
130                (lys, lys_ask.as_ref()),
131                &shutdown_handle,
132                &health_server,
133                &readiness,
134                &mut stages,
135            )?;
136            context_watch = full.context_watch;
137            self_stream_publisher = full.self_stream_publisher;
138            spine = full.spine;
139            (full.connection_supervisor, full.cluster_handle, full.links)
140        }
141        ServiceProfile::WorkerFrontDoor => {
142            let services = build_connection_services(&config)?;
143            // The front door admits and decides on the pass; it serves no
144            // channel operation, so the service bindings have no publisher
145            // here and are not built.
146            let connection_supervisor = ConnectionSupervisor::with_lys_and_fatal_shutdown(
147                services,
148                auth_token,
149                pass_verifier,
150                lys,
151                config.limits,
152                shutdown_handle.clone(),
153            )?;
154            readiness.set_cluster_configured(false);
155            stages.mark("supervisor");
156            (connection_supervisor, None, ConfiguredLinks::default())
157        }
158    };
159
160    // P0 #56 R4: readiness now reports whether the server can ADMIT, not just
161    // whether it finished starting. It is installed here rather than at
162    // `SharedReadinessState::new` above because the health endpoint binds before
163    // the supervisor exists — liveness has to be answerable while the rest of
164    // the server is still being built — so the authority that owns the answer
165    // is not available until now.
166    // SRV-028: installed before any connection is admitted; its processes
167    // stop when the guard drops.
168    let services =
169        super::services_boot::install(&config, &connection_supervisor, lys_ask.as_ref())?;
170    let (mut listener, mut websocket_listener) = open_listeners(
171        &config,
172        connection_supervisor,
173        &readiness,
174        &mut stages,
175        spine.as_ref(),
176    )?;
177
178    // SRV-028 R5: the address and the live restart after the bind.
179    health_server.install_listen_addresses(listener.local_addresses())?;
180    services.listening(listener.local_addr())?;
181    links.start(&health_server.link_bridges(), &listener.supervisor())?;
182    stages.mark("services_registry");
183
184    tracing::debug!(
185        config_path = %config_path.display(),
186        listen_address = %config.listen_address,
187        health_listen_address = %health_server.local_addr(),
188        "liminal server configuration validated"
189    );
190
191    listening::report_started(&listener, &health_server, &stages);
192
193    shutdown_handle.wait();
194    readiness.set_listener_bound(false);
195    // SRV-028: before any connection drains, so a worker leaving with its
196    // connection keeps its service live for the next start.
197    services.shutdown_begun();
198
199    // Tear the cluster down before draining connections: stop accepting peer
200    // links and halt the membership poll loop. Each node shuts down independently
201    // (no cluster-wide coordinated shutdown — that boundary belongs to SRV-004).
202    if let Some(mut cluster_handle) = cluster_handle {
203        cluster_handle.shutdown();
204    }
205
206    let supervisor = listener.supervisor();
207    let shutdown_result = runtime_links::shutdown(
208        &supervisor,
209        config.drain_timeout(),
210        || crate::server::shutdown::stop_accepting(&mut listener, websocket_listener.as_mut()),
211        (&mut links, || {
212            finish_self_stream_publisher(&mut self_stream_publisher)
213        }),
214    );
215    let participant_fatal = supervisor.participant_service_fatal();
216    let publisher_shutdown = finish_self_stream_publisher(&mut self_stream_publisher);
217    // The watch stops before the Lys ask runtime it reports through goes.
218    drop(context_watch);
219    drop(lys_ask);
220    drop(websocket_listener);
221    drop(signal_registration);
222    // The health endpoint is torn down before the shutdown-sequence result is
223    // raised: the teardown has to happen either way.
224    raise_in_precedence(
225        health_server.shutdown(),
226        raise_in_precedence(publisher_shutdown, shutdown_result),
227    )?;
228    participant_fatal?.map_or(Ok(()), |fatal| {
229        Err(ServerError::ParticipantServiceFatal { fatal })
230    })
231}
232
233/// What the full profile starts: the connection supervisor over the
234/// production services, and the cluster, context watch and self-stream
235/// publisher beside it when configured.
236struct FullProfile {
237    links: runtime_links::ConfiguredLinks,
238    connection_supervisor: ConnectionSupervisor,
239    /// The spine store opened held (DUR-021), released after the listeners bind.
240    spine: Option<Arc<haematite::EventStore>>,
241    cluster_handle: Option<ClusterHandle>,
242    context_watch: Option<crate::services::context_watch::ContextWatch>,
243    self_stream_publisher: Option<PublisherRuntime>,
244}
245
246/// Starts the full profile: the production connection services, the health
247/// records they publish, the Lys install with its service bindings, the
248/// connection supervisor, the context watch, the self-stream publisher and
249/// clustering when configured.
250fn start_full_profile(
251    config: &crate::config::types::ServerConfig,
252    (auth_token, pass_verifier): (Option<Vec<u8>>, Option<PassVerifier>),
253    (lys, lys_ask): (Option<LysInstall>, Option<&tokio::runtime::Runtime>),
254    shutdown_handle: &ShutdownHandle,
255    health_server: &crate::health::HealthServerHandle,
256    readiness: &SharedReadinessState,
257    stages: &mut StartStages,
258) -> Result<FullProfile, ServerError> {
259    let services = Arc::new(LiminalConnectionServices::from_config(config)?);
260    stages.mark("services");
261    // DUR-021 R4: the health endpoint reads the spine's reclaim status on each
262    // request and offers the one operator route that quiesces it.
263    let spine = services.durable_store().reclaim_spine();
264    if let Some(spine) = &spine {
265        health_server.install_spine(spine);
266    }
267    install_ephemeral_sweep_report(&services, health_server)?;
268    // Publish the participant's refused-load record onto the health
269    // endpoint. It happens HERE and not at `start_health_server`
270    // because the endpoint binds before the participant exists —
271    // liveness has to be answerable while the rest of the server is
272    // still being built. Construction restores no conversation
273    // (DUR-020): the record fills as conversations restore, on first
274    // touch or in the warm, and is complete once the warm has finished.
275    // The worker-front-
276    // door profile configures no participant and installs nothing, and
277    // the route reports that rather than an empty refusal set.
278    if let Some(record) = services.unloadable_conversation_record() {
279        health_server.install_unloadable_record(record);
280    }
281    // R18 amendment A7 (§0.18): the operator credential-re-issue
282    // authority, published on the same line of reasoning and at the
283    // same moment. A profile with no participant installs nothing and
284    // the route says so.
285    if let Some(reissuer) = services.credential_reissuer() {
286        health_server.install_credential_reissuer(reissuer);
287    }
288    let channel_cluster = services.channel_cluster().clone();
289    let signals_owner = Arc::clone(&services);
290    let lys = lys
291        .map(|install| configured_service_bindings(install, config, &services, lys_ask))
292        .transpose()?;
293    let bindings = lys.as_ref().and_then(|install| install.services.clone());
294    let links = runtime_links::prepare(config, lys.as_ref(), &signals_owner)?;
295    let connection_supervisor = ConnectionSupervisor::with_lys_and_fatal_shutdown(
296        services,
297        auth_token,
298        pass_verifier,
299        lys,
300        config.limits,
301        shutdown_handle.clone(),
302    )?;
303    stages.mark("supervisor");
304    let mut context_watch = None;
305    if let Some(bindings) = bindings.as_deref() {
306        context_watch = start_context_watch(signals_owner.as_ref(), bindings, &config.limits)?;
307        stages.mark("services");
308    }
309
310    let self_stream_publisher = start_self_stream_publisher(
311        &signals_owner,
312        &connection_supervisor,
313        config,
314        shutdown_handle,
315    )?;
316    stages.mark("incarnation");
317
318    // SRV-005: start clustering on the channel-supervisor scheduler when a
319    // [cluster] section is configured. The returned handle owns the inbound
320    // distribution listener and the membership poll loop; it must outlive the
321    // server and is torn down in the shutdown sequence below.
322    let cluster_handle =
323        start_configured_cluster(&channel_cluster, config.cluster.as_ref(), readiness)?;
324    stages.mark("cluster");
325    Ok(FullProfile {
326        links,
327        connection_supervisor,
328        spine,
329        cluster_handle,
330        context_watch,
331        self_stream_publisher,
332    })
333}
334
335/// Binds the wire listener and, when configured, its WebSocket sibling.
336/// Readiness reports the listeners bound once both are.
337/// Builds the ACCESS-003 service bindings `[auth.lys.services]` configures
338/// into the Lys install: made as this machine, on the Lys ask runtime,
339/// publishing through the server's own connection services.
340fn configured_service_bindings(
341    mut install: LysInstall,
342    config: &crate::config::types::ServerConfig,
343    services: &Arc<LiminalConnectionServices>,
344    lys_ask: Option<&tokio::runtime::Runtime>,
345) -> Result<LysInstall, ServerError> {
346    let Some(lys) = config.auth.as_ref().and_then(|auth| auth.lys.as_ref()) else {
347        return Ok(install);
348    };
349    let Some(runtime) = lys_ask else {
350        return Err(ServerError::ConfigValidation {
351            message: "auth.lys.services needs the Lys ask runtime; none was started".to_owned(),
352        });
353    };
354    let now = std::time::SystemTime::now()
355        .duration_since(std::time::UNIX_EPOCH)
356        .map_or(0, |elapsed| elapsed.as_secs());
357    install.services =
358        ServiceBindings::configure(lys, Arc::clone(services), runtime.handle().clone(), now)
359            .map_err(|error| ServerError::ConfigValidation {
360                message: format!("auth.lys.services: {error}"),
361            })?
362            .map(Arc::new);
363    Ok(install)
364}
365
366/// The Lys install for the configured `[auth.lys]`, on the ask runtime's
367/// handle when one runs; none when no `[auth]` is configured.
368fn lys_for(
369    config: &crate::config::types::ServerConfig,
370    lys_ask: Option<&tokio::runtime::Runtime>,
371) -> Result<Option<LysInstall>, ServerError> {
372    config
373        .auth
374        .as_ref()
375        .map(|auth| configured_lys(auth, lys_ask.map(|runtime| runtime.handle().clone())))
376        .transpose()
377        .map(Option::flatten)
378}
379
380/// Binds the listeners and, once they are bound, starts the release of the
381/// spine's held reclaim (DUR-021 R1 and R3) on its own thread, with what each
382/// shard resumed on the log: the start does not wait for a shard to finish
383/// its reclaim step to answer.
384fn open_listeners(
385    config: &crate::config::types::ServerConfig,
386    supervisor: ConnectionSupervisor,
387    readiness: &SharedReadinessState,
388    stages: &mut StartStages,
389    spine: Option<&Arc<haematite::EventStore>>,
390) -> Result<(ServerListener, Option<WebSocketListener>), ServerError> {
391    readiness.track_admission(supervisor.admission_readiness());
392    let listeners = bind_listeners(config, supervisor, readiness)?;
393    stages.mark("listeners");
394    if let Some(spine) = spine {
395        let release =
396            crate::server::connection::reclaim_placement::release_after_bind(spine, stages.began)?;
397        listeners.0.supervisor().retain_reclaim_release(release)?;
398    }
399    Ok(listeners)
400}
401
402fn bind_listeners(
403    config: &crate::config::types::ServerConfig,
404    supervisor: ConnectionSupervisor,
405    readiness: &SharedReadinessState,
406) -> Result<(ServerListener, Option<WebSocketListener>), ServerError> {
407    let listener = ServerListener::bind(config, supervisor)?;
408    // LP-WS-TRANSPORT R1.1: the sibling WebSocket acceptor is explicit opt-in.
409    // Absent `[websocket]` binds nothing — no HTTP surface exists at all — and
410    // a bind failure fails startup BEFORE readiness reports the listeners
411    // bound, exactly like the main listener.
412    let websocket_listener = match config.websocket.as_ref() {
413        Some(websocket_config) => Some(WebSocketListener::bind(
414            websocket_config,
415            listener.supervisor(),
416        )?),
417        None => None,
418    };
419    readiness.set_config_loaded(true);
420    readiness.set_listener_bound(true);
421    Ok((listener, websocket_listener))
422}
423
424/// The start path's stages, each as milliseconds since `run` began, so a
425/// slow start says where its time went. Logged once, on the started line.
426struct StartStages {
427    began: Instant,
428    marks: Vec<(&'static str, u128)>,
429}
430
431impl StartStages {
432    fn new() -> Self {
433        Self {
434            began: Instant::now(),
435            marks: Vec::with_capacity(8),
436        }
437    }
438
439    /// The stage `name` has ended, now.
440    fn mark(&mut self, name: &'static str) {
441        self.marks.push((name, self.began.elapsed().as_millis()));
442    }
443}
444
445/// Each stage with its own length and where it ended: `config 3 (3),
446/// health 1 (4), ...` in milliseconds.
447impl fmt::Display for StartStages {
448    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
449        let mut before = 0;
450        for (index, (name, at)) in self.marks.iter().enumerate() {
451            if index > 0 {
452                formatter.write_str(", ")?;
453            }
454            write!(formatter, "{name} {} ({at})", at.saturating_sub(before))?;
455            before = *at;
456        }
457        Ok(())
458    }
459}
460
461mod bootstrap;
462#[cfg(test)]
463pub(in crate::server) use bootstrap::bind_self_streams;
464pub(in crate::server) use bootstrap::start_self_stream_publisher;
465use bootstrap::{
466    finish_self_stream_publisher, install_ephemeral_sweep_report, raise_in_precedence,
467    start_configured_cluster,
468};
469#[cfg(test)]
470#[path = "runtime/tests.rs"]
471mod tests;
472
473mod listening;