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
60 changes: 47 additions & 13 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,16 @@ Capture continuous rosbag recordings around fault events.
unlinked, so the default reproduces the historical behaviour exactly: a
new recording replaces the previous one. ``0`` means unlimited, bounded
only by ``max_total_storage_mb``. ``3`` is a reasonable value for a fault
that flaps - see the note below before raising it.
that flaps - see the note below before raising it. A retention rule's
``max_bags`` replaces it for the fault codes the rule covers (see Rosbag
Retention Rules below).
* - ``rosbag.auto_cleanup``
- ``true``
- Delete a fault's bags when the fault is cleared. A recording shared by a
burst survives until the last fault referencing it clears. Has no effect
once ``max_bags_per_fault`` is anything other than ``1``: a history someone
configured must not be what an acknowledgement takes away, so the cap
governs retention there instead.
burst survives until the last fault referencing it clears. Deletes only
for a fault whose cap is exactly ``1`` (its rule's ``max_bags``, else
``max_bags_per_fault``): a history someone configured must not be what
an acknowledgement takes away, so the cap governs retention there instead.

.. note::

Expand All @@ -683,7 +685,9 @@ Capture continuous rosbag recordings around fault events.
a fault that returns sooner than the cooldown keeps ONE recording however high
this cap is. That is the fast-flapping fault the cap exists for, so lower the
cooldown when you raise the cap. The fault manager logs a warning at startup
when the two are configured against each other.
when the two are configured against each other: a cooldown above 0 with
``max_bags_per_fault`` or, with a valid retention configuration, a rule's
``max_bags`` other than 1 (the line names one such rule).

.. _rosbag-recording-lifecycle:

Expand Down Expand Up @@ -784,8 +788,9 @@ arrival prunes the whole buffer by age.
Rosbag Retention Rules
~~~~~~~~~~~~~~~~~~~~~~

The parameters under ``snapshots.rosbag.retention.`` set a rule per fault: a ``priority`` and an
``over_quota`` choice. They are read at startup, validated and logged. They are read only while
The parameters under ``snapshots.rosbag.retention.`` set a rule per fault: a ``priority``, an
``over_quota`` choice, a ``max_bags`` cap and ``keep_first``. 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
before. An example file is ``config/rosbag_retention.example.yaml``.
Expand All @@ -805,6 +810,8 @@ before. An example file is ``config/rosbag_retention.example.yaml``.
fault_codes:
MOTOR-STALL:
priority: 900
max_bags: 5
keep_first: true
patterns:
gnss_fix:
match: 'GNSS\.FIX_LOST'
Expand Down Expand Up @@ -847,10 +854,34 @@ Fields:
* - ``over_quota``
- ``keep`` (default) or ``drop``
- What happens to a new recording that cannot fit the storage quota.
* - ``max_bags``
- integer ``0`` or more, default ``rosbag.max_bags_per_fault``
- Recordings kept per fault code. ``0`` means no cap, only the quota applies.
* - ``keep_first``
- ``true`` or ``false`` (default)
- With a ``max_bags`` of 2 or more, keep the first recording plus the ``max_bags - 1`` newest.
Ignored with a warning when ``max_bags`` is below 2.

**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
oldest recording of the code is removed. With ``keep_first`` and a ``max_bags`` of 2 or more the first
recording is never removed by the cap: the code keeps it plus the ``max_bags - 1`` newest, and the
second oldest goes. "First" is the oldest recording of the fault code still stored: once the quota has
removed it, the next oldest is first. ``keep_first`` with a ``max_bags`` of 0 or 1 is ignored, and each
store of that fault code logs a warning. A fault that cannot be read when a recording is stored keeps
all its recordings in that store, with a warning; the next store decides again. A fault that is
reported again at a higher severity takes the cap of the higher severity.

**The clear.** With ``rosbag.auto_cleanup: true`` a clear deletes the fault's recordings only when its
``max_bags`` is 1. ``max_bags: 3`` on a rule keeps the history of that fault also when
``max_bags_per_fault`` is 1, and ``max_bags: 1`` deletes it also when ``max_bags_per_fault`` is 3.

**What the rules do.** A new recording evicts only recordings whose priority is less than or equal to
its own, and evicts them in this order: lowest ``priority`` first, then the recordings whose faults are
all cleared, then healed, then active (any fault PREFAILED, PREPASSED or CONFIRMED), then the oldest. A
all cleared, then healed, then active (any fault PREFAILED, PREPASSED or CONFIRMED); inside one
priority and status, a recording that ``keep_first`` protects goes after the others; then the oldest.
A recording is protected when it is the first recording of at least one of its fault codes whose rule
has ``keep_first`` with a ``max_bags`` of 2 or more. Priority and status still go before protection. A
recording shared by several faults (a burst) ranks by its highest-priority fault, with each fault's
severity as it is stored now, so a fault that was reported again at a higher severity moves its
recordings up. The ranking reads the links as they are after the new recording and the per-fault cap
Expand All @@ -870,7 +901,8 @@ list is served. It is ``0`` without rules and while the configuration is invalid

**Open-time check.** When a fault whose rule says ``over_quota: drop`` confirms, and the recording
is predicted not to fit, the recording is not started. The prediction is the stored bytes, less what the
per-fault cap frees for that fault and what the fault's priority allows to evict, plus the bytes of
per-fault cap frees for that fault (with that fault code's ``max_bags`` and ``keep_first``; the codes
that join the recording later are not known yet) and what the fault's priority allows to evict, plus the bytes of
recordings that are closed but not stored yet, plus the buffered bytes times
``1 + duration_after_sec / duration_sec``. The term for recordings not stored yet is an estimate of their payload (container and metadata overhead
are not included), counted only while no stored row names them; the store still decides with the
Expand All @@ -881,7 +913,8 @@ it in its post-fault window is dropped when it is stored.
**Invalid configuration.** While the retention configuration is invalid, the quota is a hard stop for
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.
a cleared fault's recording (``auto_cleanup``) work as without rules, with ``max_bags_per_fault`` for
every fault code.

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 @@ -912,8 +945,9 @@ A file the fault manager cannot read is reported with a warning and skipped.

**Errors.** The fault manager reports every error it finds:

- a value of the wrong type (``priority: 5.0`` is a double, not an integer), ``priority`` outside 0-1000,
``over_quota`` other than ``keep`` or ``drop``;
- 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``;
- 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.
Expand Down
18 changes: 11 additions & 7 deletions docs/tutorials/snapshots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ configuration always overrides the zero-config fallback when present.
across all of it, and only those. The rosbag recording is not covered by that
setting. A clear deletes the recording under
``snapshots.rosbag.auto_cleanup``, unless that is off or
``snapshots.rosbag.max_bags_per_fault`` keeps a history. The cap then governs
retention, except at ``0`` (unlimited), where only
``snapshots.rosbag.max_bags_per_fault`` keeps a history. A retention rule's
``max_bags`` replaces ``max_bags_per_fault`` for the faults it covers. The cap
then governs retention, except at ``0`` (unlimited), where only
``snapshots.rosbag.max_total_storage_mb`` bounds it.

Quick Start
Expand Down Expand Up @@ -505,7 +506,8 @@ style recording. This continuously buffers messages in memory and flushes them
to a bag file when a fault is confirmed. The recording is deleted when the fault
is cleared, unless ``snapshots.rosbag.auto_cleanup`` is off or
``snapshots.rosbag.max_bags_per_fault`` keeps a history, which takes over the
retention decision.
retention decision. A retention rule's ``max_bags`` replaces
``max_bags_per_fault`` for the faults it covers.

**Key differences from JSON snapshots:**

Expand Down Expand Up @@ -646,9 +648,10 @@ Rosbag Configuration Options
- Automatically delete a fault's bag when it is cleared. A recording
shared by a burst of faults is deleted when the last fault referencing
it clears. Set to ``false`` to retain bags for manual analysis. Has no
effect once ``max_bags_per_fault`` is anything other than ``1``: a
history someone configured must not be what an acknowledgement takes
away, so the cap governs retention there instead.
effect for a fault whose cap is anything other than ``1`` (its retention
rule's ``max_bags``, else ``max_bags_per_fault``): a history someone
configured must not be what an acknowledgement takes away, so the cap
governs retention there instead.
* - ``snapshots.rosbag.lazy_start``
- ``false``
- Controls when the ring buffer starts recording. See diagram below.
Expand All @@ -675,7 +678,8 @@ Rosbag Configuration Options
chasing. This is a fairness knob rather than a depth knob:
``max_total_storage_mb`` is the real disk bound, and a high value lets
one flapping fault consume the budget and evict every other fault's
recording.
recording. A retention rule's ``max_bags`` replaces it for the faults
the rule covers.

Bursts and the Window Boundary
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions src/ros2_medkit_fault_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ endif()
medkit_add_launch_test(test_rosbag_quota test/integration/test_rosbag_quota.test.py TIMEOUT 600
LABELS "integration")

# max_bags and keep_first per retention rule: the cap, the clear and the eviction order.
medkit_add_launch_test(test_rosbag_max_bags test/integration/test_rosbag_max_bags.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
15 changes: 10 additions & 5 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ parameter list and recording lifecycle.

#### Rosbag Retention Rules

`snapshots.rosbag.retention.*` sets a `priority` (0-1000) and an `over_quota` choice (`keep` or `drop`)
per fault, from four levels: fault code, the first matching pattern of `pattern_order`, severity, defaults.
They are read at startup and validated. A new recording evicts only recordings of equal or lower priority,
lowest priority first, then cleared before healed before active, 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.
`snapshots.rosbag.retention.*` sets a `priority` (0-1000), an `over_quota` choice (`keep` or `drop`), a
`max_bags` cap and `keep_first` 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
the fault code still stored. With `auto_cleanup`, a clear deletes a fault's recordings only when its `max_bags`
is 1. 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.
Expand Down
17 changes: 14 additions & 3 deletions src/ros2_medkit_fault_manager/config/rosbag_retention.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
# A field of a fault's rule comes from the first level that sets it:
# fault_codes.<CODE>, then the first label of pattern_order whose match covers the whole
# fault code, then severity.<SEVERITY> of the fault, then defaults, then the built-in value.
# Fields: priority (integer 0-1000, built-in 0) and over_quota (keep or drop, built-in keep).
# 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) and keep_first (true or
# false, built-in false).
# 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
# with a max_bags below 2 is ignored with a warning. A clear deletes the fault's recordings when
# snapshots.rosbag.auto_cleanup is true and its max_bags is 1.
# A new recording evicts only recordings of equal or lower priority: lowest priority first, then
# cleared, healed, active, then oldest. If it still does not fit, keep stores it over the quota and
# drop discards it alone. With drop, a recording predicted not to fit is not started.
# 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.
# With drop, a recording predicted not to fit is not started.
# Any other key under retention is an error: the fault manager keeps running without the rules, the
# quota becomes a hard stop for new recordings (nothing is evicted for it) and it reports the fault
# ROSBAG_RETENTION_CONFIG_INVALID. Retention values are read-only at runtime.
Expand All @@ -28,10 +36,13 @@ fault_manager:
CRITICAL:
priority: 100
over_quota: keep
max_bags: 5
fault_codes:
# A fault code has no dot here. Hyphens are allowed.
MOTOR-STALL:
priority: 900
max_bags: 10
keep_first: true
DEBUG_NOISE:
priority: 0
over_quota: drop
Expand Down
6 changes: 4 additions & 2 deletions src/ros2_medkit_fault_manager/config/snapshots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ rosbag:
# Think of this as fairness rather than depth: the disk is bounded by
# max_total_storage_mb either way, and a high value lets one flapping fault consume
# the budget and evict every other fault's recording.
# A retention rule's max_bags (snapshots.rosbag.retention) replaces it for its faults.
max_bags_per_fault: 1

# Maximum in-memory ring buffer size in MB (default: 256)
Expand All @@ -195,8 +196,9 @@ rosbag:
# Auto-cleanup bag files when fault is cleared (default: true)
# When true, bag files are deleted when ClearFault is called
# When false, bag files persist until manually deleted or storage limit hit
# No effect once max_bags_per_fault is anything other than 1: a configured
# history governs retention instead, and an acknowledgement leaves it alone
# No effect for a fault whose cap is anything other than 1 (its retention rule's
# max_bags, else max_bags_per_fault): a configured history governs retention
# instead, and an acknowledgement leaves it alone
auto_cleanup: true

# =============================================================================
Expand Down
Loading
Loading