Skip to content
Draft
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
24 changes: 22 additions & 2 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ Basic Snapshot Settings
the range is clamped to the nearest end with a warning).
* - ``snapshots.capture_queue_full_policy``
- ``reject_newest``
- Policy when the queue is full: ``reject_newest`` or ``drop_oldest``.
- Which capture a full queue drops among the captures with the lowest priority, the new one
included: ``reject_newest`` the newest (the new capture when it ties), ``drop_oldest`` the
oldest. Each drop logs one WARN with the fault code and the priority. Without valid retention
rules every priority is ``0``, so ``reject_newest`` drops the new capture and ``drop_oldest``
the oldest waiting one. See the capture queue under Rosbag Retention Rules.

Rosbag Recording
~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -908,7 +912,7 @@ Fields:
- Meaning
* - ``priority``
- integer 0-1000, default ``0``
- Rank of the fault's recordings.
- Rank of the fault's recordings and of its captures waiting in the capture queue.
* - ``over_quota``
- ``keep`` (default) or ``drop``
- What happens to a new recording that cannot fit the storage quota.
Expand Down Expand Up @@ -1085,6 +1089,22 @@ or unreadable file is a failed measurement. The fault manager logs one WARN at s
line ends with ``source=test-capacity`` instead of ``source=filesystem``. It is not a parameter and
cannot be set over the API. Never set it on a robot.

**The capture queue.** A capture waits in the queue (``snapshots.capture_queue_depth``) until a capture
worker is free. Its priority is the ``priority`` of its fault's rule, resolved with the fault's severity
as it is stored when a worker picks a capture and when a full queue drops one: a fault reported again,
while its capture waits, at a severity whose rule has a higher priority moves up (a priority need not
follow the severity). A free worker runs the waiting capture with the highest priority, the oldest first
among equal priorities. The order is strict and has no aging: a low-priority capture can wait as long as
captures of a higher priority keep arriving. A full queue drops the capture with the
lowest priority, the new one included; among equal lowest priorities ``capture_queue_full_policy``
decides: ``reject_newest`` drops the newest (the new capture when it ties), ``drop_oldest`` the oldest.
When the waiting captures keep changing while their priorities are read (three reads in a row), that one
pick or drop uses arrival order and ``capture_queue_full_policy`` alone and logs a DEBUG line.
Each dropped capture logs one WARN, not throttled: ``Capture queue full (<policy>): dropped the
[pending ]capture of '<code>' (priority <n>)``. A fault that cannot be read counts as priority ``0``.
Without valid rules (none, or an invalid configuration) every priority is ``0``: the queue runs oldest
first and the policy alone decides the drop.

**The priority in the list.** ``~/list_rosbags`` and the gateway's ``GET .../bulk-data/rosbags``
show each recording's priority (``priorities``, ``x-medkit.priority``): the rank above, read when the
list is served. It is ``0`` without rules and while the configuration is invalid.
Expand Down
4 changes: 3 additions & 1 deletion docs/tutorials/snapshots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ Configure snapshot capture via fault manager parameters:
- Max pending captures before the full-queue policy applies (>= 1).
* - ``snapshots.capture_queue_full_policy``
- ``reject_newest``
- Policy when the queue is full: ``reject_newest`` or ``drop_oldest``.
- Which capture a full queue drops among those with the lowest retention priority:
``reject_newest`` (the newest) or ``drop_oldest`` (the oldest). Without retention rules
every priority is equal.

Advanced Configuration
----------------------
Expand Down
5 changes: 5 additions & 0 deletions src/ros2_medkit_fault_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ endif()
medkit_add_launch_test(test_rosbag_storage_availability test/integration/test_rosbag_storage_availability.test.py
TIMEOUT 300 LABELS "integration")

# The capture queue by retention priority: the pick order, the full-queue drop and its WARN, without
# rules and with an invalid configuration.
medkit_add_launch_test(test_capture_queue_priority test/integration/test_capture_queue_priority.test.py
TIMEOUT 300 LABELS "integration")

if(POSTGRES_SUPPORT)
medkit_add_launch_test(test_rosbag_quota_pg test/integration/test_rosbag_quota_pg.test.py TIMEOUT 300
LABELS "integration")
Expand Down
6 changes: 4 additions & 2 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Snapshots capture topic data when faults are confirmed for post-mortem debugging

Each confirm also writes a **freeze-frame**: a single compact JSON object mapping every captured topic to its value at confirmation time, keyed by fault code. It differs from per-topic snapshots in two ways: snapshots are deleted when the fault is cleared, while the freeze-frame is retained across `clear_fault` (once the snapshots are gone, `~/get_fault` serves the retained frame so the confirmed-state record stays available after acknowledgement); and a re-confirm that captures nothing (e.g. source publishers down) never overwrites an existing non-empty frame. A fault code with no configured capture set gets no freeze-frame row; a configured capture that samples nothing on its first run records an empty `{}` frame. Freeze-frame storage is bounded by the number of distinct fault codes (one row per code, replaced in place) and rows are never evicted.

Under a fault storm, captures are bounded by a worker pool (`capture_pool_size`) draining a bounded queue (`capture_queue_depth`); excess captures are dropped per `capture_queue_full_policy` and logged (throttled). The pool is shared and is created when snapshots **or** rosbag is enabled, so these parameters bound both. `capture_pool_size` parallelizes freeze-frame snapshot capture only - rosbag stays single-writer regardless of pool size, and correlated faults confirming inside one post-roll window share a single recording.
Under a fault storm, captures are bounded by a worker pool (`capture_pool_size`) draining a bounded queue (`capture_queue_depth`). A free worker runs the waiting capture with the highest retention priority, oldest first among equals; a full queue drops the capture with the lowest priority, the new one included, and `capture_queue_full_policy` decides among equal priorities. Each drop logs one WARN with the fault code and the priority. Without valid retention rules every priority is 0: oldest first, and the policy alone decides the drop. The pool is shared and is created when snapshots **or** rosbag is enabled, so these parameters bound both. `capture_pool_size` parallelizes freeze-frame snapshot capture only - rosbag stays single-writer regardless of pool size, and correlated faults confirming inside one post-roll window share a single recording.

That single-writer property also shapes what each fault of a burst gets. Nothing is buffered while a post-fault window is open (messages go straight into the open bag), and the flush that opened that bag already emptied the ring buffer, so a fault confirming right _after_ the window closes has no pre-fault history available. What a confirmation gets is decided by the buffer, so a fault arriving before any captured topic has published lands the same way. It gets a **post-fault-only bag**: its own recording holding just its `duration_after_sec` window, entered through the same post-roll state machine, so later faults of the burst attach to it normally. With `duration_after_sec: 0` there is no window to record into and such a fault gets no bag; if the bag cannot be written at all, no recording is opened and no metadata row is stored. The `duration_sec` on a stored bag is the span the recording was open rather than the configured windows, so a post-fault-only bag usually reports roughly `duration_after_sec` where a full one reports its buffered history too. It is a recording span, not a content span: a window during which nothing was published still reports the seconds it covered. It can also exceed `duration_sec + duration_after_sec`, because the ring buffer is pruned only when a message arrives - a topic that stops publishing keeps its last window buffered until the next confirmation flushes it, which is deliberate for a black box. See `docs/config/fault-manager.rst` for the full lifecycle.

Expand All @@ -97,7 +97,7 @@ That single-writer property also shapes what each fault of a burst gets. Nothing
| `snapshots.max_per_fault` | int | `10` | Max snapshots retained per fault. |
| `snapshots.capture_pool_size` | int | `2` | Max concurrent capture threads under a fault storm (>= 1). Parallelizes snapshot capture only; rosbag stays single-writer. |
| `snapshots.capture_queue_depth` | int | `16` | Max pending captures before the full-queue policy applies (>= 1). |
| `snapshots.capture_queue_full_policy` | string | `reject_newest` | Policy when the queue is full: `reject_newest` or `drop_oldest`. |
| `snapshots.capture_queue_full_policy` | string | `reject_newest` | Drop of a full queue among the lowest priorities: `reject_newest` (the newest) or `drop_oldest` (the oldest). |

**Topic Resolution Priority:**

Expand Down Expand Up @@ -174,6 +174,8 @@ periodic job with no priority limit. A new recording below the floor with nothin
delete is not started. `over_quota: keep` does not keep a recording below the floor. The floor is a target, not a
reservation: other writers can change the free space between two measurements. With PostgreSQL on the same disk
it is also its headroom.
A capture waiting in the capture queue has its fault's priority, read with the fault's current severity when a
worker picks a capture and when a full queue drops one: the highest runs first, the lowest is dropped first.
A new recording evicts only recordings of equal or lower priority, lowest priority first, then cleared
before healed before active, then a recording `keep_first` protects after the others, then oldest. A recording
that still does not fit follows `over_quota`: `keep` (default) stores it over the quota, `drop` drops it and
Expand Down
29 changes: 25 additions & 4 deletions src/ros2_medkit_fault_manager/design/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ The following diagram shows the relationships between the main components of the
+shutdown()
+dropped_captures() : uint64_t
-workers_ : vector<thread>
-queue_ : deque<string>
-queue_ : deque<Job>
-priority_of_ : PriorityFn
}

enum QueueFullPolicy {
Expand Down Expand Up @@ -201,8 +202,21 @@ Main Components
5. **CaptureThreadPool** - Bounded worker pool for snapshot capture
Under a fault storm the node enqueues capture jobs into a bounded ``CaptureThreadPool``
(``pool_size`` workers draining a ``queue_depth``-bounded queue) rather than spawning one
thread per fault. The full-queue policy (``reject_newest``/``drop_oldest``) bounds memory,
and the destructor joins the pool before rosbag teardown.
thread per fault. The bounded queue bounds memory, and the destructor joins the pool before
rosbag teardown.

Each job's priority comes from ``RosbagRetention::capture_priorities``, read when a worker picks
a job and when a full queue chooses its victim: one call per decision with every code it
compares, so one decision uses one policy snapshot. A worker runs the highest priority,
FIFO among equals. A full queue drops the lowest, the incoming job included; the full-queue policy
(``reject_newest``/``drop_oldest``) picks the newest or the oldest among equal lowest priorities.
``EnqueueOutcome`` names the dropped code and the priority the decision used; the node logs one
WARN per drop, not throttled. The reads run outside the queue mutex on a copy of the pending
jobs. Each job carries a unique sequence number, and a decision applies only when the pending
jobs are unchanged after the read; else the read repeats, and after ``kMaxPriorityReads`` (3)
that decision is FIFO with one DEBUG line. ``enqueue`` runs in the core's capture hook, so its
reads run under the core mutex, like the storage writes of a report; a worker's reads hold no
core lock. Without valid rules every priority is 0: FIFO and the policy alone.

6. **FaultManagerCore** - ROS-free report, clear, list, get and tick logic (``core/fault_manager_core.hpp``)

Expand Down Expand Up @@ -564,7 +578,8 @@ It is the only write of the rosbag rows. The retention below makes every decisio
Rosbag retention
""""""""""""""""

``core::RosbagRetention`` holds every rosbag retention decision, each under one retention mutex:
``core::RosbagRetention`` holds every rosbag retention decision, each under one retention mutex
except ``capture_priorities``:

- ``store``: drops the rows of codes whose delete generation moved since they joined the
recording (see below) and of faults the storage holds as cleared with a deleting clear
Expand Down Expand Up @@ -652,6 +667,12 @@ Rosbag retention
- ``has_space_candidate(priority_limit)``: the same choice without the removal, for the open-time
floor check. A failed read answers true and logs nothing.
- ``fault_priority(fault_code)``: the priority of the fault's rule, 0 unless ``kConfigured``.
- ``capture_priorities(fault_codes)``: the priorities of captures in the capture queue, without the
retention mutex, so a capture pick does not wait behind a store, a floor pass or a sweep that
holds it during its storage calls. It loads the policy snapshot once with ``std::atomic_load``,
reads each fault with one ``get_fault`` and resolves its rule with the fault's severity. 0 unless
``kConfigured``, for a fault that cannot be read and for one that does not exist. It logs nothing
and never throws.

**Delete generation.** A recording closes on the executor and is stored later on the store worker,
so a fault can heal or be cleared, and fail again, between the two. The status at the store then
Expand Down
Loading
Loading