Stream IPC Policy¶
How the stream's processes talk: the stages and the master over pipes, the master and the GUI over HTTP. What a message may contain, what is dropped, and what a change must preserve.
1. Scope¶
- Pipes and framing:
src/main/stream/shared/simple_stream_pipe.f90. The pipe descriptors are insrc/main/stream/shared/simple_stream_state.f90, and the master's stage table insrc/main/stream/master/simple_stream_master_stage_ids.f90andsrc/main/stream/master/simple_stream_master_stage.f90. - GUI metadata types:
src/utils/gui/metadata/(gui_metadata_baseand its extensions). - The master's store, GUI commands and heartbeat:
src/main/stream/master/simple_stream_master_meta_store.f90;src/main/stream/master/simple_stream_master_gui_commands.f90;src/utils/gui/simple_gui_assembler.f90;- the heartbeat loop of
src/main/commanders/stream/simple_commanders_stream_p00_master.f90.
2. Processes and pipes¶
- The master forks one process per stage. Each stage has two pipes, both made by the master
before the fork: stage to master (
ipc_pipe_<stage>_in) and master to stage (ipc_pipe_<stage>_out). - The master keeps every pipe end open for the whole run. A forked stage closes the ends that
are not its own (
close_other_pipe_ends) before its commander runs. stream_pipewraps the ends a process uses; it never closes the descriptors (they are the master's).- A stage is forked only before the master's listener thread starts, or while holding the
listener's lock (
doc/policies/stream/restart_policy.md).
3. Framing¶
A frame is a C int byte count followed by that many payload bytes.
- Send: retries at once on
EINTR, and with a 10 ms pause onEAGAINup to 200 times. A frame is delivered whole or dropped whole: once a byte is in the pipe, the rest follows. - Receive: returns a frame already assembled from earlier reads before reading again, so a drain loop sees every queued message.
- Resync: a length outside 1..
max_frame_bytesis reported, and the buffered bytes are dropped, so the reader resynchronises on a later frame instead of stopping. - Discard: drops what the pipe and the buffer hold, for a reader whose writer has been replaced (a restarted stage).
- Frame bound:
max_frame_bytesismax_metadata_size(), the largest serialised metadata type.
4. Stage to master: GUI metadata¶
- A message is one serialised GUI metadata object whose first bytes are its type
(
GUI_METADATA_*_TYPE). - Serialisation is a byte copy (
transfer) of the object, so: - an object sent over a pipe has no allocated allocatable or pointer component. A type with
one overrides
serialise: it sends a copy without the component (gui_metadata_vol3Ddrops its reprojection tiles), or stops (gui_metadata_project, jsonised in-process only); - sender and receiver must be the same binary, which forked stages are.
- Capacities are fixed by the types (named constants where callers need them:
MAX_PICKREFS_SELECTION,MAX_SNAPSHOT2D_SELECTION,MAX_SNAPSHOT_FNAME_LEN,MAX_MIC_COORDINATES,MAX_OPTICS_SHIFTS,MAX_TIMEPLOT_POINTS,MAX_HISTOGRAM_BINS,MAX_SIEVE_SELECTION,MAX_FSC_VOL3D,MAX_STATES_SOLVE3D_MULTISTATE). Callers stay within them: the picks drawn on a thumbnail are cut atMAX_MIC_COORDINATES, and a long run's time plots widen their window or keep their newest points (simple_stream_meta_plots). The setters stop on overflow as a guard against programming errors; nothing from outside may reach that guard. - A reused metadata object is reset with
kill: every type extendinggui_metadata_baseoverrideskillto assign its default-initialised self, so no field of the previous message survives into the next. Every component therefore keeps a default initialiser;newsets only the base flags. - The master keeps the latest message per type (
stream_master_meta_store). A frame whose length is not its tag's type's (a desynchronised pipe read as a frame) is dropped with a warning before it is copied into anything (frame_fits). - What it keeps:
- a status replaces the previous one;
- an item of a list (micrograph, optics group, class average, volume, reprojection tile)
goes to slot
iof a list ofi_max, which is remade wheni_maxchanges; - a stage restarted by the GUI has its lists dropped first (
clear_stage), so entries its previous process sent and the new one does not send again (a run's volumes) leave the GUI. - Locking: the master's listener thread drains every stage's pipe into the store while holding the metadata lock. The main loop holds the same lock to assemble a heartbeat, and to discard a stage's pipes and fork it again.
5. Master to stage: updates¶
Sections 5 and 6 are the contract with NICE (nice/nice_lite/data_structures/streamjob.py
points here): NICE answers every heartbeat with its whole state (master_update), never with
deltas; the master forwards to each stage only what changed since its last update to that stage,
nothing to a stopping stage, and acts on a restart_<key> once, again only after the key has left
an answer. A key not listed in section 6 is not part of the contract and is ignored.
- Each GUI answer becomes one
gui_metadata_stream_updateholding that answer's fields. NICE answers every heartbeat with its whole state, so most answers repeat the last one. - The update goes to the running stages that read updates, and only when it differs from the
last update the stage was sent since it was started (
stream_master_stage%send_update). A restarted stage therefore receives the next answer whole: - p01 reads the CTF resolution, astigmatism and ice thresholds;
- p03 reads the picking-reference selection and its cycle;
- p06 reads the 2D mask diameter and 2D snapshot requests;
- p07 reads 3D snapshot requests.
- A stopped stage, and a stage asked to stop, is not sent updates (its pipe would only fill).
- A stage drains its updates once per pass and applies them in order.
- The master's update writer gives up on a part-written frame after
UPDATE_PARTIAL_RETRIES(about 2 s;stream_pipe%limit_partial_frames): a stage that stopped reading must not hold the master. The channel is then broken, later updates are dropped, and the discard before the stage's next start mends it. The stages' writers to the master have no such limit, since the master's listener always drains them.
6. Master and GUI¶
- Heartbeat: every
MASTER_HEARTBEAT_S(5 s) the master POSTs the assembler's JSON document toniceserver. - Each content section is sent only when its hash (FNV-1a) differs from the last one sent.
- A heartbeat the GUI did not accept (no answer, or a status other than 200) clears the hashes, so the next heartbeat sends everything.
- The answer (status 200) is a JSON object; text that is not JSON is logged and ignored. Recognised keys:
| Key | Effect |
|---|---|
terminate |
stop the stream |
terminate_<key>, restart_<key> |
stop, or restart when stopped, one stage (<key> from stage_gui_key); NICE keeps restart_<key> until it sees the stage running, so the master acts on it once, again only after the key has left an answer, and never once the stream is stopping |
ctfresthreshold, astigthreshold, icefracthreshold |
preprocessing thresholds |
pickrefs_selection, pickrefs_cycle |
a picking-reference selection of p03's cycle |
mskdiam2D |
the 2D pool's mask diameter |
snapshot2D {id, iteration, selection, filename} |
a 2D snapshot request; p06 answers each id once with a snapshot report, where 0 particles and no file mean it was not written (an iteration no longer kept). filename is a bare file name ending in .simple, of at most MAX_SNAPSHOT_FNAME_LEN (128) characters: p06 makes a folder of it. NICE sends snapshot_<id>.simple |
snapshot3D {id, selection, filename} |
a 3D snapshot request: the particles of the selected states of multistate 3D's latest result, merged into one state (stream_3D_ingestion_policy.md, section 8). selection holds 1 to MAX_STATES_SOLVE3D_MULTISTATE state numbers, each within 1..MAX_STATES_SOLVE3D_MULTISTATE; filename follows snapshot2D's rule. p07 answers each id once with a 3D snapshot report (tag GUI_METADATA_STREAM_SOLVE3D_SNAPSHOT_TYPE, nested as solve3D_multistate.snapshot, with its states), where 0 particles and no file mean it was not written |
- Dropped selections: a selection larger than the update holds is dropped whole with a
warning. Acting on part of it would act on classes the user did not choose; the rest of the
answer is applied. A snapshot request whose
filenamebreaks its rule is dropped the same way, and so is a 3D snapshot request of no state, of more thanMAX_STATES_SOLVE3D_MULTISTATEstates, or of a state out of range. - Nothing in an answer stops the master.
7. Stopping¶
- Every stage, and the master, handles SIGTERM (the master also SIGINT) by setting a flag only
(
simple_stream_sigterm); the flag is polled between steps. - Ctrl-C stops the stream in order. A forked stage ignores SIGINT (
forked_process), and so does what it execs, so a terminal's Ctrl-C, which reaches the whole process group, stops only the master, which then stops the stages as below. The master flushes its output before every fork, so a child does not write the master's buffered lines again. - The master's stop:
- optics assignment is asked first, with up to
MASTER_OPTICS_STOP_TIMEOUT_S(60 s), during which the heartbeat goes on (restart requests are ignored from the stop on); - then every other stage but the large ones (
STOP_ONE_BY_ONE: multistate 3D, pool 2D, reference picking) is asked together; - the large ones are asked one after another, downstream first, each once the one before has stopped (or been killed), so their final writes do not hold their memory at the same time; one waiting its turn runs on;
- a stage asked and still running is asked again on each pass;
- a stage still running
MASTER_STOP_TIMEOUT_S(600 s) after it was asked is killed (SIGKILL), and the master cancels the jobs recorded in its folder that wrote no exit status (cancel_unfinished_jobs), which the killed stage can no longer cancel. The large stages each get their own 600 s, so a stop can take up to their sum. - Reference picking and pool 2D log their resident memory and the time at each step of their
final write (
log_rss, ">>> RSS ..."), so a stop that runs out of time shows where. - Once none runs, the master sends a last heartbeat, with the stream's stop time and its final
status (finished, or failed when a stage failed), and exits. It is sent again, up to
MASTER_LAST_HEARTBEAT_TRIES(3) timesMASTER_LAST_HEARTBEAT_WAIT_S(1 s) apart, until NICE accepts it (send_last_heartbeat; once without aniceserver): NICE marks a stream it last saw running failed after a minute without a heartbeat, so a lost last one would show a stopped stream as failed. - A start-up failure once stages are forked (a stage not running, the listener thread not
made) stops them before the master stops: asked, then killed with their jobs cancelled after
MASTER_STARTUP_STOP_TIMEOUT_S(60 s). The persistent-worker server ends with the master's process; its workers leave when they lose it. forked_processnever restarts a child by itself: a stage restarts only when the GUI asks.- Every stage but p03 also ends its loop when a
SIMPLE_TERM_STREAMfile exists in its folder (TERM_STREAM). Withasync=yes, a refine2D master and a class-average assembly run exit when the file exists (terminate_stream). The master does not write the file; the only writer is the 2D pool's final write, in its own folder, when it stops mid-iteration.
8. Change rules¶
- A new metadata type: no allocatable or pointer component in what it sends (or a
serialiseoverride), and a named capacity for any array a caller fills from outside. - The GUI metadata types (
src/utils/gui/metadata/) and the assembler import nothing that reachessrc/main, directly or through their imports, so the stages and the master can use them cheaply; a new one keeps it so. Work that needs the domain (reading ansp_project, binning orientations, writing previews) belongs to the caller:simple_gui_project_builder,simple_oris_utils,simple_stream_meta_plots, and the master'sfork_gui_status. - A new GUI answer key: parsed in
stream_master_gui_commands, with oversized arrays dropped, and added to section 6. - Any read of a stage pipe on the master's main thread takes the metadata lock.
- Tests:
unit_ipc"stream pipe": framing, resync, discard, an abandoned part-written frame;unit_stream"stream master": stage names and keys; GUI answers, including invalid and oversized ones, unsafe snapshot names and 3D snapshot selections out of range; the store, including a frame of the wrong length and the 3D snapshot report; a stage's pipes from both sides; an update sent once;unit_stream"solve 3D": a 3D snapshot request through the stage's update pipe, before and after a result;unit_stream"meta plots": the time plots of a run longer than their capacity;unit_ui"GUI metadata": every type a stage sends survives serialisation (a copy received by transfer holds the same fields), a volume without its reprojection tiles;unit_ui"GUI assembler": the sections and the heartbeat, from stage-status records;forked_process"stream heartbeat": a live process read into its heartbeat record (fork_gui_status).
9. Known gaps¶
- The byte-copy format ties the processes to one binary; a stage started by
execwould need a real wire format.