Detailed Design for Component: Launch Manager, Graph Functionality#
Description#
This detailed design focuses on the graph functionality including:
Run Target Transitions
Component activation & deactivation
Dynamic Diagrams for Unit Interactions#
Graph State Machine#
The graph is the internal state machine of the Launch Manager and tracks the process of a Run Target transition. The Launch Manager main thread runs a cyclic loop and the state of the graph state machine defines how events are processed.
Component Activation#
The Launch Manager main loop does not directly activate any component, but the activation is only scheduled on a thread pool.
A component activation consists of:
starting the process (possibly multiple times)
registering the pid in the SafeProcessMap
waiting for the ready condition to be fulfilled or ready timeout
posting the activation result to the event queue
The component activation sequence is executed synchronously on a worker thread.
Component Deactivation#
The Launch Manager main loop does not directly deactivate any component, but the deactivation is only scheduled on a thread pool.
A component deactivation consists of:
stopping the process (if running)
waiting for the process to terminate or until shutdown timeout
escalating to SIGKILL in case the process did not terminate within the shutdown timeout
posting the deactivation result to the event queue. Process crashes during deactivation are ignored.
The component deactivation sequence is executed synchronously on a worker thread.
Component State Machine#
A component manages one resource, currently only a POSIX process, and is always in one of four states:
Terminated: no resource is running. This is the initial state and the state every failure ends in, so a component can always be activated again.
Starting: the resource has been started and the component waits for its ready condition.
Ready: the resource has finished its startup and, if configured, is supervised.
Terminating: the resource has been asked to stop and the component waits for its termination, first gracefully up to the shutdown timeout, then forcefully terminating it.
Component Recovery#
A component can be configured with a restart recovery action. If it terminates unexpectedly outside Run Target activation, the graph moves to kComponentRecovery and restarts only this component on the thread pool; its dependents keep running and the Run Target stays active. When the last recovery succeeds, the graph returns to kSuccess.
If a restart fails, a component without a restart recovery action fails, a different Run Target is requested or the Launch Manager shuts down, the ongoing recoveries are stopped and the graph continues via kAborting to the pending or fallback Run Target. A switch Run Target recovery action currently always activates the fallback Run Target.
Process Termination#
The process termination may happen during component activation or deactivation but also at any time after activation in case of a process crash or a self-terminating process. The latter is the focus of the diagram below.
Supervision Failure#
A component activates the alive supervision once the Running state is reached. The supervision failure may be detected at any time and is reported to the component event queue. The graph forwards the failure to the affected component, which kills its process unless the failure is outdated (the component is no longer ready, component was restarted since, or the process has already terminated). The resulting unexpected termination then triggers the fallback Run Target as described in Process Termination.
Run Target transition#
A Run Target transition may be initiated either by an external request or as an error reaction (e.g. fallback_run_target).
A transition runs in two phases: first every component that is running and is not needed by the target is stopped, then every component of the target that is not active yet is started. The graph dispatches only the components whose predecessors have finished and never waits for them; each result that arrives in the main loop advances the transition. A component that fails ends the transition and moves the graph to kAborting.
Transition cancellation / preemption#
When cancelling an ongoing transition it is crucial to leave every component in a well-defined state.
The diagram below defines the point in time a component activation / deactivation may be safely cancelled.
A cancellation is observed at three points, each of which leaves the component in a state the next transition can reason about:
Before the task is executed. A worker that picks up a task for which cancellation was already requested does not run it and reports it as skipped. The component keeps the state it had before the transition.
While waiting for the ready condition. Every ready condition is abortable: the wait for the Running report, a configured file, the termination of the process, or a sequence of these is interrupted and the startup attempt fails. Its process is stopped (SIGTERM, then SIGKILL after the shutdown timeout), no further attempt is started and the activation returns. The component is left stopped.
Between two startup attempts. Every failed attempt is stopped before the cancellation is checked. No further attempt is started and the activation returns. The component is left stopped.
Stopping a process is not abortable, whether after a failed attempt or for a deactivation, and always runs to its end. The process may be writing its state to disk, so it always keeps its full shutdown timeout before SIGKILL follows.
A component that reaches its ready condition despite the cancellation is reported as activated and left running. The next transition stops it if its target does not need it. The time a cancellation needs is therefore bounded by the shutdown timeouts of the components still in flight.
Units within the Component#
The Launch Manager is composed of many units. This detailed design focuses on the central dependency graph logic and its interaction with surrounding units.
ProcessGroupManager: Implements the main loop of the Launch Manager
Graph: Implements the state machine for the Run Target transition logic
OSHandler: Waits for any child process to terminate, then forwards the process termination to process monitor
ComponentEventQueue: Synchronizes the component events
SafeProcessMap: Maintains a thread-safe mapping PID to component. Mapping is used to forward process termination event to the right component.
IComponentController: Executes Component Tasks and enqueues the result to an Event Queue
IComponent: The component interface
Transition: Manages a RunTarget transition by determining which components are ready to start/stop