Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions backend/crates/station/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub trait TcpLink: Send + Sync {
pub struct EventConfig {
/// Slots in each of `net`'s error queues (①, one per transport).
pub errors_queue: usize,
/// Slots in ② (problems from station modules).
pub problems_queue: usize,
/// Slots in ② (notices from station modules).
pub notices_queue: usize,
/// Slots in ③ (TCP notifications).
pub tcp_queue: usize,
/// Slots in ④ (UDP notifications). The only stretch of queue between the
Expand All @@ -94,7 +94,7 @@ impl Default for EventConfig {
fn default() -> Self {
Self {
errors_queue: 16,
problems_queue: 16,
notices_queue: 16,
tcp_queue: 1024,
udp_queue: 8192,
logging_queue: 8192,
Expand All @@ -106,23 +106,25 @@ impl Default for EventConfig {
}
}

/// Something the operator has to see.
/// Something the operator is told: an error, or just news ("recording
/// started"). Its [`NoticeKind`] says which, and what the backend does about
/// it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Problem {
/// When it was detected.
pub struct Notice {
/// When it happened.
pub at: SystemTime,
/// The board, if known.
/// The board, if it concerns one.
pub board: Option<BoardId>,
/// What it means for the backend.
pub kind: ProblemKind,
/// The message with all its causes: "could not X: because Y: because Z"
/// (see [`full_message`]).
/// How it is shown, and what it means for the backend.
pub kind: NoticeKind,
/// The text. For an error, the message with all its causes: "could not
/// X: because Y: because Z" (see [`full_message`]).
pub text: String,
}

impl Problem {
/// A problem detected now.
pub fn now(kind: ProblemKind, board: Option<BoardId>, text: impl Into<String>) -> Self {
impl Notice {
/// A notice of something that happened now.
pub fn now(kind: NoticeKind, board: Option<BoardId>, text: impl Into<String>) -> Self {
Self {
at: SystemTime::now(),
board,
Expand All @@ -132,11 +134,14 @@ impl Problem {
}
}

/// What a [`Problem`] means for the backend.
/// How a [`Notice`] is shown, and what it means for the backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProblemKind {
/// Shown on the front; the backend keeps running.
Shown,
pub enum NoticeKind {
/// News, not an error: shown on the front as information; the backend
/// keeps running.
Info,
/// Shown on the front as a warning; the backend keeps running.
Warning,
/// The fault is sent, it is shown on the front and the backend stops.
Fatal,
/// Only written to the trace: not shown, the backend keeps running.
Expand Down Expand Up @@ -167,8 +172,9 @@ pub enum ToPipeline {
/// A notification, TCP or UDP, as soon as it arrives. `logging` gets the
/// same copy.
Received(Arc<Notification>),
/// Something the operator has to see.
Problem(Problem),
/// Something the operator is told, shown on the front as a message from
/// the backend: as information, a warning or a fatal error, by its kind.
Notice(Notice),
/// An order the front sent that went out: shown on the front as a message
/// from the backend.
OrderSent {
Expand All @@ -195,8 +201,9 @@ pub enum ToPipeline {
/// Why `event` ended. `main` reads it and shuts everything down in order.
#[derive(Debug)]
pub enum Stop {
/// A fatal error: shown on the front, fault sent (or attempted).
Fatal(Problem),
/// A fatal error, with the notice that told it: fault sent (or
/// attempted), shown on the front.
Fatal(Notice),
/// `net`'s queues closed: UDP and TCP were stopped.
NetGone,
}
Expand All @@ -220,8 +227,8 @@ pub struct Channels {
pub event: EventEnds,
/// ⑦, for websocket's order forwarder.
pub orders: mpsc::Sender<OrderPacket>,
/// ②, cloned for every station module that reports problems.
pub problems: mpsc::Sender<Problem>,
/// ②, cloned for every station module that reports notices.
pub notices: mpsc::Sender<Notice>,
/// ⑤, for `logging`.
pub logging: mpsc::Receiver<ToLogging>,
/// ⑥, for `pipeline`.
Expand Down Expand Up @@ -252,7 +259,7 @@ pub fn channels(config: &EventConfig) -> Channels {
// Channel creation
let (udp, udp_sinks) = Reporter::channels(udp_capacity, errors);
let (tcp, tcp_sinks) = Reporter::channels(tcp_capacity, errors);
let (problems_tx, problems_rx) = mpsc::channel(config.problems_queue.max(1));
let (notices_tx, notices_rx) = mpsc::channel(config.notices_queue.max(1));
let (orders_tx, orders_rx) = mpsc::channel(config.orders_queue.max(1));
let (logging_tx, logging_rx) = mpsc::channel(config.logging_queue.max(1));
let (pipeline_tx, pipeline_rx) = mpsc::channel(config.pipeline_queue.max(1));
Expand All @@ -264,7 +271,7 @@ pub fn channels(config: &EventConfig) -> Channels {
router: RouterEnds {
udp: udp_sinks,
tcp: tcp_sinks,
problems: problems_rx,
notices: notices_rx,
logging: logging_tx,
pipeline: pipeline_tx,
tcp_capacity,
Expand All @@ -273,7 +280,7 @@ pub fn channels(config: &EventConfig) -> Channels {
orders: orders_rx,
},
orders: orders_tx,
problems: problems_tx,
notices: notices_tx,
logging: logging_rx,
pipeline: pipeline_rx,
}
Expand Down
86 changes: 49 additions & 37 deletions backend/crates/station/src/event/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ the boards.
┌─ task ─────┐ ─① errors──────► │ reads in this order: │
│ net::tcp │ ─③ TCP notif.──► │ 1. ① ② errors │ ─⑥─► ┌─ task ───┐
└────────────┘ │ 2. ③ TCP notifications │ │ pipeline │ → websocket → front
any station ─② problems─────► │ 3. ④ UDP notifications │ └──────────┘
any station ─② notices─────► │ 3. ④ UDP notifications │ └──────────┘
module └────────────┬─────────────┘
│ ⑧ (fatal error, board fault)
▼
Expand Down Expand Up @@ -127,7 +127,7 @@ event/
| # | Queue | From → to | Carries |
|---|---|---|---|
| ① | net errors (one queue per transport) | `net::udp`, `net::tcp` → routing loop | `ErrorReport` |
| ② | problems | any station module → routing loop | `Problem` |
| ② | notices | any station module → routing loop | `Notice` |
| ③ | TCP notifications | `net::tcp` → routing loop | `Notification` (protections, messages, connected/disconnected) |
| ④ | UDP notifications | `net::udp` → routing loop | `Notification` (data) |
| ⑤ | to logging | routing loop, both workers → `logging` | `ToLogging` |
Expand Down Expand Up @@ -162,7 +162,7 @@ let event = event::spawn(config, channels.event, pod_data, index, tcp); // Arc<P
logging::spawn(channels.logging, ...); // ⑤
pipeline::spawn(channels.pipeline, broker_client, ...); // ⑥
OrderForwarder::new(index, channels.orders); // ⑦ (websocket)
// channels.problems is cloned for every station module that reports errors ②
// channels.notices is cloned for every station module that reports errors or news ②
```

Why one place, instead of each module creating its own input queue:
Expand All @@ -189,7 +189,7 @@ Why one place, instead of each module creating its own input queue:
|---|---|---|---|
| TCP notification (③) | yes | yes | — |
| UDP notification (④) | yes | yes | — |
| Problem to show (②, or a non-fatal error from ①) | no (trace only) | yes | — |
| Notice to show (②, or a non-fatal error from ①) | no (trace only) | yes | — |
| Fatal error | no (trace only) | yes, before stopping | yes |
| Fault from a board (packet id 0, ①) | no (trace only) | yes, the first one from each board | yes, every time, if `propagate_fault` is on |
| Protection with severity `Fault`, or message `Fault`/`Panic` (③) | yes | yes, as the notification it is | yes, every time, if `propagate_fault` is on |
Expand Down Expand Up @@ -238,7 +238,7 @@ is expected to keep up with the incoming rate (about 15–25 Mbit/s).
every board), or `OrderFailed` with the reason. Nothing else.
- **Boards it did not reach**: a fault that only half landed is exactly what
the operator has to know. For an automatic fault they go to `pipeline` as a
`Problem`; for the front's, they are the reason in `OrderFailed`.
`Notice`; for the front's, they are the reason in `OrderFailed`.
- **Without TCP** (no TCP client running), every order fails with "TCP not
available", shown on the front, and an automatic fault is only traced.

Expand All @@ -256,12 +256,18 @@ There are three procedures, and every one of them ends up in the trace:

1. **Not fatal, not shown**: trace only. Usually traced where it happens,
without reaching `event` (e.g. a failed reconnection with backoff); a
problem that does reach `event` this way is a `ProblemKind::Silent`, and
notice that does reach `event` this way is a `NoticeKind::Silent`, and
`event` only traces it.
2. **Not fatal, shown on the front**: e.g. an order to a board that is not
connected.
3. **Fatal, shown on the front**: send the fault, close TCP, shut the backend
down safely.
2. **Not fatal, shown on the front** (`NoticeKind::Warning`): e.g. an order to
a board that is not connected.
3. **Fatal, shown on the front** (`NoticeKind::Fatal`): send the fault, close
TCP, shut the backend down safely.

**Not every notice is an error.** A station module can also send news, such as
"recording started", as a `NoticeKind::Info`: it is shown on the front as
information and the backend keeps running. It is not one of the procedures,
since nothing went wrong; it only shares the queue (②) and the way to the front
(⑥).

| Error | What it means | Procedure |
|---|---|---|
Expand All @@ -274,7 +280,7 @@ There are three procedures, and every one of them ends up in the trace:
| TCP connect or read failure | a board not up yet, or a connection that dropped | **1**, trace only; the `Disconnected` notification already tells the front |
| TCP keep-alive write failure | the keep-alive could not be written | **2, shown** |
| An order that could not be written | reported by the orders worker, not by `net` | **2, shown** |
| `Problem` from a station module (②) | whatever that module reports | what the module says: **1** (`Silent`), **2** (`Shown`) or **3** (`Fatal`) |
| `Notice` from a station module (②) | whatever that module reports | what the module says: **1** (`Silent`), **2** (`Warning`) or **3** (`Fatal`); an `Info` is not an error and is only shown |
| `logging` or `pipeline` stops (⑤ or ⑥ closed) | nothing is recorded, or nothing is shown | **3, fatal** |

Every decode error is fatal, not only an unknown id: a truncated datagram or an
Expand All @@ -293,7 +299,7 @@ the address.

**Board faults on the front.** A fault packet (id 0) arrives as an error, not
as a notification, so the front only sees it through `event`: the first one
from each board is shown, as a `Shown` problem whose text names the board;
from each board is shown, as a `Warning` notice whose text names the board;
repeats are not, since `net` already traces them (at most one line a second,
with how many were skipped). A `Fault` protection or a `Fault`/`Panic` message
reaches the front as the notification it is, so nothing extra is shown for it.
Expand All @@ -303,22 +309,22 @@ on by default, and it is sent each time one arrives.
**Traces.** Every error ends up in the trace (`tracing`), but never twice: the
module where it happens traces it, and `event` only traces what it decides
itself ("fatal, stopping", "fault sent to every board", "first fault from
HVSCU") and the `Silent` problems it is sent.
HVSCU") and the `Silent` notices it is sent.

## Stopping

**On a fatal error**, the routing loop:

1. Asks the fault worker for the fault, without waiting for it to go out: the
fault comes first, and the next steps happen while it is being written.
2. Sends the `Problem` to `pipeline`, so the operator sees it.
2. Sends the `Notice` to `pipeline`, so the operator sees it.
3. Empties whatever was already in ③ and ④ (at most their capacity) and passes
it on. Errors are read first, so the fatal error has overtaken the data that
arrived just before it, and that data is exactly what you want to understand
what happened.
4. Waits for the fault to have gone out, within `fault_timeout` from step 1
(if it already has, the answer is immediate).
5. Ends by returning `Stop::Fatal(problem)`.
5. Ends by returning `Stop::Fatal(notice)`.

If what stopped is `logging` or `pipeline`, step 3 is skipped. If it is
`logging`, `event` still tells `pipeline`.
Expand Down Expand Up @@ -356,8 +362,8 @@ pub struct Channels {
pub event: EventEnds,
/// ⑦, for websocket's OrderForwarder.
pub orders: mpsc::Sender<OrderPacket>,
/// ②, cloned for every station module that reports problems.
pub problems: mpsc::Sender<Problem>,
/// ②, cloned for every station module that reports notices.
pub notices: mpsc::Sender<Notice>,
/// ⑤, for logging.
pub logging: mpsc::Receiver<ToLogging>,
/// ⑥, for pipeline.
Expand Down Expand Up @@ -396,28 +402,32 @@ pub enum ToLogging {
pub enum ToPipeline {
/// A notification, the same copy logging gets.
Received(Arc<Notification>),
Problem(Problem),
Notice(Notice),
/// An order the front sent that went out, as in `ToLogging`.
OrderSent { at: SystemTime, board: Option<BoardId>, order: OrderPacket },
/// An order the front sent that did not go out, and why.
OrderFailed { at: SystemTime, order: OrderPacket, reason: String },
}

/// Something the operator has to see. `Problem::now(kind, board, text)`
/// builds one stamped with the current time.
pub struct Problem {
/// Something the operator is told: an error, or just news. Its kind says
/// which. `Notice::now(kind, board, text)` builds one stamped with the
/// current time.
pub struct Notice {
pub at: SystemTime,
/// The board, if known.
/// The board, if it concerns one.
pub board: Option<BoardId>,
pub kind: ProblemKind,
/// The message with all its causes: "could not X: because Y: because Z"
/// (`event::full_message` builds it from any error).
pub kind: NoticeKind,
/// The text. For an error, the message with all its causes: "could not
/// X: because Y: because Z" (`event::full_message` builds it from any
/// error).
pub text: String,
}

pub enum ProblemKind {
/// Procedure 2: shown, the backend keeps running.
Shown,
pub enum NoticeKind {
/// News, not an error: shown as information, the backend keeps running.
Info,
/// Procedure 2: shown as a warning, the backend keeps running.
Warning,
/// Procedure 3: the fault is sent, it is shown and the backend stops.
Fatal,
/// Procedure 1: only traced, not shown; the backend keeps running.
Expand All @@ -426,7 +436,7 @@ pub enum ProblemKind {

/// Why event ended. `main` reads it and shuts down in order.
pub enum Stop {
Fatal(Problem),
Fatal(Notice),
/// `net`'s queues closed: UDP and TCP were stopped.
NetGone,
}
Expand All @@ -435,7 +445,7 @@ pub enum Stop {
/// clamped to at least 1.
pub struct EventConfig {
pub errors_queue: usize, // each of net's two error queues, ① (16)
pub problems_queue: usize, // ② (16)
pub notices_queue: usize, // ② (16)
pub tcp_queue: usize, // ③ (1,024)
pub udp_queue: usize, // ④ (8,192)
pub logging_queue: usize, // ⑤ (8,192)
Expand All @@ -454,11 +464,11 @@ these two calls only. `FaultRequest` (⑧) is private to `event`.

```text
loop, select! in fixed order (closed queues are no longer watched):
1. ① UDP errors, ① TCP errors, then ② problems -> policy:
1. ① UDP errors, ① TCP errors, then ② notices -> policy:
fatal -> stop (see "Stopping")
board fault -> propagate_fault? request to ⑧ (every time)
first from that board? Problem to ⑥
shown -> Problem to ⑥
first from that board? Notice to ⑥
shown -> Notice to ⑥
silent -> trace only
2. ③ TCP notifications.recv() -> fault protection or message?
propagate_fault? request to ⑧
Expand Down Expand Up @@ -513,13 +523,15 @@ notifications a second through `event`, over 20 times the requirement.
(`logger/enable`, variable selection) go straight to `logging`, not through
`event`.
- **`logging`** reads ⑤, picks which measurements to store following its own
selection, records the orders that went out, and reports its own failures
(e.g. the disk) on ②.
selection, records the orders that went out, and reports on ② its own
failures (e.g. the disk, as a `Fatal` notice) and its news (an `Info`, e.g.
recording started).
- **`pipeline`** reads ⑥ and turns it into what the front shows: a notification
into per-packet updates (counts, cycle time, averages) or protection and
message updates; a `Problem`, and an order that went out or failed
message updates; a `Notice`, and an order that went out or failed
(`OrderSent`, `OrderFailed`), into a message from the backend
(`message/update`). It waits instead of dropping and does not decide anything
(`message/update`). A notice keeps its kind there: `Info` as info, `Warning`
as warning, `Fatal` as fault. It waits instead of dropping and does not decide anything
about errors.

## Implementation notes
Expand Down
6 changes: 3 additions & 3 deletions backend/crates/station/src/event/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::{mpsc, oneshot};
use tracing::{error, info, warn};

use super::orders::TCP_NOT_AVAILABLE;
use super::{Problem, ProblemKind, TcpLink, ToLogging, ToPipeline, board_name};
use super::{Notice, NoticeKind, TcpLink, ToLogging, ToPipeline, board_name};

/// The fault's id on the wire: what a board sends when it detects an error,
/// and what the backend sends to every board to put the pod in fault.
Expand Down Expand Up @@ -82,8 +82,8 @@ pub(super) async fn run(
error!(%text);
// A fault that only half landed is exactly what the
// operator has to know.
let problem = Problem::now(ProblemKind::Shown, None, text);
let _ = pipeline.send(ToPipeline::Problem(problem)).await;
let notice = Notice::now(NoticeKind::Warning, None, text);
let _ = pipeline.send(ToPipeline::Notice(notice)).await;
}
}
if let Some(done) = done {
Expand Down
4 changes: 2 additions & 2 deletions backend/crates/station/src/event/manual_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ async fn manual_event_throughput() -> Result<(), Box<dyn Error>> {
tcp,
event,
orders,
problems,
notices,
mut logging,
mut pipeline,
} = channels(&config);
drop((tcp, orders, problems));
drop((tcp, orders, notices));
let event = spawn(config, event, Arc::default(), Arc::default(), None);

let logged = tokio::spawn(async move {
Expand Down
Loading
Loading