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
61 changes: 56 additions & 5 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,9 @@ Rosbag Retention Rules
~~~~~~~~~~~~~~~~~~~~~~

The parameters under ``snapshots.rosbag.retention.`` set a rule per fault: a ``priority``, an
``over_quota`` choice, a ``max_bags`` cap, ``keep_first``, and what happens to the fault's recordings
when it heals (``on_healed``) or is cleared (``on_cleared``). They are read at startup, validated and
``over_quota`` choice, a ``max_bags`` cap, ``keep_first``, what happens to the fault's recordings
when it heals (``on_healed``) or is cleared (``on_cleared``), and how long they are kept in each status
(``max_age_sec``). They are read at startup, validated and
logged. They are read only while
rosbag capture is on (``snapshots.rosbag.enabled: true``); with it off, the fault manager warns once
and ignores them. Without any ``snapshots.rosbag.retention.*`` parameter the fault manager behaves as
Expand All @@ -805,8 +806,12 @@ before. An example file is ``config/rosbag_retention.example.yaml``.
snapshots:
rosbag:
retention:
sweep_interval_sec: 60
defaults:
priority: 10
max_age_sec:
healed: 604800
cleared: 86400
severity:
INFO:
on_healed: delete
Expand Down Expand Up @@ -844,6 +849,11 @@ Grammar, relative to ``snapshots.rosbag.retention.``:
- Rule of the faults the pattern matches.
* - ``pattern_order``
- List of every pattern label, in the order they are tried. With no patterns, leave the key out.
* - ``<level>.max_age_sec.<active|healed|cleared>``
- The ``max_age_sec`` field at any of the four levels above, one key per status.
* - ``sweep_interval_sec``
- Seconds between two passes of the age sweep, integer 1-86400, default ``60``. The sweep runs
only when a level sets a ``max_age_sec`` key above ``0``.

Fields:

Expand Down Expand Up @@ -875,6 +885,12 @@ Fields:
- ``keep`` or ``delete``; default ``delete`` when ``rosbag.auto_cleanup`` is true and the fault's
``max_bags`` is 1, else ``keep``
- What happens to the fault's recordings when it becomes CLEARED.
* - ``max_age_sec``
- keys ``active``, ``healed``, ``cleared``; each an integer ``0`` or more in seconds, default
``0`` (no limit)
- Delete the fault's recordings when the fault is in that status and its last FAILED report is
older than the limit. ``active`` means PREFAILED, PREPASSED or CONFIRMED: a fault reported
once and never again ages out while it is still CONFIRMED.

**The cap per fault code.** When a recording is stored, each of its fault codes keeps at most its rule's
``max_bags`` recordings, resolved with the fault's severity as it is stored now. Over the cap the
Expand Down Expand Up @@ -916,6 +932,36 @@ code becomes CLEARED. Its recordings are deleted only when its rule says ``on_cl
explicitly; the default of ``on_cleared`` does not apply there. A restart with healing turned off thus
deletes nothing that no rule asks to delete.

**The age.** ``max_age_sec`` counts from the fault's last FAILED report (``last_occurred``). A new
FAILED report starts the age again, in every status: a heal or a clear stores no new time, so a
fault that healed or was cleared long after its last FAILED report can lose its recordings at the
first pass after the change. A recording goes when its age in whole seconds is above the limit of
the fault's current status, resolved with the fault's current severity. Each ``max_age_sec`` key
resolves on its own across the four levels, and an explicit ``0`` at a more specific level wins for
that key: ``fault_codes.<CODE>.max_age_sec.active: 0`` keeps that code's recordings while
``defaults.max_age_sec.active`` is set. The age removes the link of the fault concerned, in any
status and regardless of ``keep_first``: a recording shared by a burst loses only that link, and the
directory goes with its last link. A link whose fault the storage no longer holds is not aged.

The age sweep runs on the retention worker every ``sweep_interval_sec``, only while a level sets a
``max_age_sec`` key above ``0``; without one there is no timer. The policy line at startup ends
with ``sweep: off`` or ``sweep: every <n> s``. Each pass reads the rosbag rows and the faults once,
removes every expired link in one transaction, then removes the directories no row names any more
and logs ``Recording removed: <recording_id> (max_age_sec.active)`` (``.healed``, ``.cleared``) for
each. Each pass logs ``retention sweep: now=<ns> removed=<links>`` at DEBUG, ``now`` being the
wall-clock time the pass decided with. A storage failure logs one WARN
and the next pass tries again. A removal whose commit outcome is unknown logs one WARN and leaves
the directories; a committed removal then leaves a directory with no row. A FAILED report that
arrives while a pass runs, between its read and its delete, can lose the old recording the report
would have kept; the next confirmation records again. A recording stored after its fault's age
expired is removed by the next pass. While the configuration is invalid there is no age deletion.

The age uses the system clock and is not paused after a start or a clock jump. On a robot without a
battery-backed clock the fault manager must start after time synchronisation (with systemd:
``After=time-sync.target`` together with ``chrony-wait.service`` or
``systemd-time-wait-sync.service``), or recordings made before it can be deleted after it. A last
FAILED time in the future (a negative age) never deletes.

With ``audit_log.fail_closed: true``, a failed audit append stops the status actions that follow it:
a heal stops before its ``on_healed``, a startup reclassification before its ``on_cleared``, and a
symptom auto-cleared with its root cause stops its own ``on_cleared`` and that of the root cause. The
Expand Down Expand Up @@ -960,7 +1006,7 @@ it in its post-fault window is dropped when it is stored.
new recordings: a recording that does not fit after the per-fault cap is dropped, and nothing is
evicted for the quota. The open-time check runs for every fault. The per-fault cap and the removal of
a cleared fault's recording (``auto_cleanup``) work as without rules, with ``max_bags_per_fault`` for
every fault code. A heal keeps the recordings (``on_healed`` is ``keep``).
every fault code. A heal keeps the recordings (``on_healed`` is ``keep``). Nothing is deleted by age.

A failed attempt to open a recording keeps the ring buffer, so the next fault within ``duration_sec``
still gets its history.
Expand Down Expand Up @@ -994,13 +1040,18 @@ A file the fault manager cannot read is reported with a warning and skipped.
- a value of the wrong type (``priority: 5.0`` is a double, not an integer, ``max_bags: "3"`` is a
string, ``keep_first: "yes"`` is a string, not a boolean), ``priority`` outside 0-1000, ``max_bags``
below 0, ``over_quota`` other than ``keep`` or ``drop``, ``on_healed`` or ``on_cleared`` other than
``keep`` or ``delete`` (``on_cleared: 1`` is an integer);
``keep`` or ``delete`` (``on_cleared: 1`` is an integer), a ``max_age_sec`` value below 0 or not an
integer, ``sweep_interval_sec`` outside 1-86400 or not an integer;
- ``max_age_sec`` with a value instead of its keys (``max_age_sec: 5``), or with a key other than
``active``, ``healed`` and ``cleared``;
- an unknown severity name, an unknown field or an unknown key under the prefix;
- ``match`` outside ``patterns``, a pattern without ``match``, a ``match`` that is not a valid regular expression;
- a pattern missing from ``pattern_order``, a label in ``pattern_order`` that names no pattern, or twice.

A key with more segments than the grammar allows (``fault_codes.A.B.priority``), from a parameters
file or from ``-p``, stops the fault manager with exit code 1 too, also with rosbag capture off. An
file or from ``-p``, stops the fault manager with exit code 1 too, also with rosbag capture off. Only
``max_age_sec`` takes one more segment, for its status: ``fault_codes.A.B.max_age_sec.active`` (a
dotted code) and ``fault_codes.A.B.max_age_sec`` are such keys. An
empty list (``pattern_order: []``, leave the key out) fails while the node is built: the fault manager
logs "Fault manager did not start" and exits with code 1. An empty key segment (``fault_codes..priority``
through ``-p``, or an empty YAML key) aborts inside ROS initialisation, before the fault manager runs.
Expand Down
8 changes: 6 additions & 2 deletions docs/tutorials/snapshots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ configuration always overrides the zero-config fallback when present.
``snapshots.rosbag.max_total_storage_mb`` bounds it. A retention rule's
``on_cleared`` (``keep`` or ``delete``) overrides all of this for the faults it
covers, and its ``on_healed: delete`` removes the fault's links when the
fault heals.
fault heals. Its ``max_age_sec`` (``active``, ``healed``, ``cleared``) removes the
fault's link to the recording once the fault's last FAILED report is older than
the limit of its status; the recording goes with its last link.

Quick Start
-----------
Expand Down Expand Up @@ -513,7 +515,9 @@ off or ``snapshots.rosbag.max_bags_per_fault`` keeps a history, which takes over
the retention decision. A recording is deleted with its last link. A retention
rule's ``max_bags`` replaces ``max_bags_per_fault`` for the faults it covers, and
its ``on_cleared`` (``keep`` or ``delete``) overrides the clear decision. A rule's
``on_healed: delete`` removes the fault's links when the fault heals.
``on_healed: delete`` removes the fault's links when the fault heals, and its
``max_age_sec`` removes the fault's link to a recording once the fault's last
FAILED report is older than the limit of its status.

**Key differences from JSON snapshots:**

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 @@ -304,6 +304,11 @@ endif()
medkit_add_launch_test(test_rosbag_status_actions test/integration/test_rosbag_status_actions.test.py
TIMEOUT 600 LABELS "integration")

# max_age_sec per retention rule and the age sweep: each status, a new FAILED report, a shared
# recording, a time in the future, the invalid values.
medkit_add_launch_test(test_rosbag_max_age test/integration/test_rosbag_max_age.test.py
TIMEOUT 600 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
19 changes: 14 additions & 5 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ parameter list and recording lifecycle.
#### Rosbag Retention Rules

`snapshots.rosbag.retention.*` sets a `priority` (0-1000), an `over_quota` choice (`keep` or `drop`), a
`max_bags` cap, `keep_first`, `on_healed` and `on_cleared` (`keep` or `delete`) per fault, from four levels: fault code, the first matching pattern of
`max_bags` cap, `keep_first`, `on_healed` and `on_cleared` (`keep` or `delete`) and `max_age_sec` (`active`,
`healed`, `cleared`) per fault, from four levels: fault code, the first matching pattern of
`pattern_order`, severity, defaults. They are read at startup and validated. Each fault code keeps at most its
rule's `max_bags` recordings (`max_bags_per_fault` when no level sets it, `0` = no cap); with `keep_first` and a
`max_bags` of 2 or more the first recording stays and the second oldest goes. "First" is the oldest recording of
Expand All @@ -145,13 +146,20 @@ needs `healing_enabled: true`). A clear deletes them with `on_cleared: delete` a
is 1. A fault the startup turns from HEALED into CLEARED, because healing is now disabled, loses its recordings
only with an explicit `on_cleared: delete`. A recording made before such a delete never survives it, also when
the fault fails again before the recording is stored; a burst's shared recording loses only that fault's link.
`max_age_sec.<status>` deletes a fault's link to its recordings when the fault is in that status and its last
FAILED report is older than the limit (`0` = no limit; a new FAILED report starts the age again). Each key
resolves on its own, an explicit `0` wins. The age sweep runs on the retention worker every `sweep_interval_sec`
(default 60), only when a key is above 0; the startup policy line says `sweep: off` or `sweep: every <n> s`. The
age uses the system clock: on a robot without a battery-backed clock, start the fault manager after time
synchronisation. A time in the future never deletes.
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
deletes nothing.
A recording that is predicted not to fit is not started when its fault says `drop` (the ring buffer stays for
the next fault). With an invalid configuration the quota is a hard stop for new recordings and nothing is
evicted for it. Without any retention parameter the quota evicts the oldest recordings first.
evicted for it, and nothing is deleted by age. Without any retention parameter the quota evicts the oldest
recordings first.
A parameter the ROS YAML loader moved (found by reading each parameters file a second time) or that has too many key segments stops the node with exit code 1. Any other error keeps the node
running without the rules, with the quota as a hard stop for new recordings, and reports the fault `ROSBAG_RETENTION_CONFIG_INVALID`. See
`config/rosbag_retention.example.yaml` and `docs/config/fault-manager.rst`.
Expand Down Expand Up @@ -602,8 +610,8 @@ and some of them build an `rclcpp::Clock`.
- `core::FaultManagerCore`: `report`, `clear`, `list`, `get`, `list_for_entity`, `record_transition`
and `tick`. These calls run under one internal mutex.
- `core::RosbagRetention`: every rosbag retention decision (the per-fault cap, the storage quota with
the retention rules, the open-time quota check, the clear, the heal, the startup reclassification and the
stale-row cleanup) on top of `FaultStorage::apply_rosbag_changes`. It returns
the retention rules, the open-time quota check, the clear, the heal, the startup reclassification, the
age sweep and the stale-row cleanup) on top of `FaultStorage::apply_rosbag_changes`. It returns
the directories to remove and never removes one itself.

The core reaches the outside through four interfaces. The ROS adapter implements them.
Expand All @@ -629,7 +637,8 @@ nanosecond times.

**ROS adapter** (`fault_manager_lib` and `fault_manager_node`): the services, the `FaultEvent`
publisher, wall timers that call `tick`, the capture thread pool, snapshot and rosbag capture, and
the `RetentionWorker` thread that removes the directories of cleared and stale recordings. It
the `RetentionWorker` thread that removes the directories of cleared and stale recordings and runs the age
sweep. It
implements the four interfaces: `ros2::RosEventSink`, `ros2::RosCaptureHooks`,
`ros2::SystemWallClock` and `ros2::RosLogSink`. It converts between the core values and the
messages in `ros2/fault_msg_conversions.hpp`.
Expand Down
17 changes: 15 additions & 2 deletions src/ros2_medkit_fault_manager/config/rosbag_retention.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
# fault code, then severity.<SEVERITY> of the fault, then defaults, then the built-in value.
# Fields: priority (integer 0-1000, built-in 0), over_quota (keep or drop, built-in keep),
# max_bags (integer 0 or more, built-in snapshots.rosbag.max_bags_per_fault), keep_first (true or
# false, built-in false), on_healed (keep or delete, built-in keep) and on_cleared (keep or delete,
# built-in: the clear default below).
# false, built-in false), on_healed (keep or delete, built-in keep), on_cleared (keep or delete,
# built-in: the clear default below) and max_age_sec (keys active, healed, cleared; seconds, built-in
# 0 = no limit; each key falls through on its own, an explicit 0 wins).
# max_bags is the number of recordings kept per fault code, 0 = no cap. Over the cap the oldest
# recording goes; with keep_first and a max_bags of 2 or more the first recording stays and the
# second oldest goes. "First" is the oldest recording of the fault code still stored. keep_first
Expand All @@ -16,6 +17,11 @@
# snapshots.rosbag.auto_cleanup is true and its max_bags is 1. A fault turned from HEALED into
# CLEARED at startup, because healing is now disabled, loses its recordings only with an explicit
# on_cleared: delete. A recording shared by a burst loses only the link of that fault.
# max_age_sec deletes a fault's recordings when the fault is in that status (active: PREFAILED,
# PREPASSED or CONFIRMED) and its last FAILED report is older than the limit; a new FAILED report
# starts the age again. A shared recording loses only that fault's link. The age sweep runs every
# sweep_interval_sec (1-86400, default 60), only when a max_age_sec key is above 0. The age uses
# the system clock: start the fault manager after time synchronisation.
# A new recording evicts only recordings of equal or lower priority: lowest priority first, then
# cleared, healed, active; inside that, a recording kept by keep_first after the others, then
# oldest first. If it still does not fit, keep stores it over the quota and drop discards it alone.
Expand All @@ -31,9 +37,13 @@ fault_manager:
snapshots:
rosbag:
retention:
sweep_interval_sec: 60
defaults:
priority: 10
over_quota: keep
max_age_sec:
healed: 604800
cleared: 86400
severity:
# Severity names: INFO, WARN, ERROR, CRITICAL.
INFO:
Expand All @@ -44,6 +54,9 @@ fault_manager:
priority: 100
over_quota: keep
max_bags: 5
max_age_sec:
healed: 0
cleared: 0
fault_codes:
# A fault code has no dot here. Hyphens are allowed.
MOTOR-STALL:
Expand Down
Loading
Loading