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
82 changes: 73 additions & 9 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,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``, 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
when it heals (``on_healed``) or is cleared (``on_cleared``), how long they are kept in each status
(``max_age_sec``), and how much free space stays on the disk (``min_free_disk_mb``). 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 @@ -806,6 +807,7 @@ before. An example file is ``config/rosbag_retention.example.yaml``.
snapshots:
rosbag:
retention:
min_free_disk_mb: 1024
sweep_interval_sec: 60
defaults:
priority: 10
Expand Down Expand Up @@ -851,9 +853,13 @@ Grammar, relative to ``snapshots.rosbag.retention.``:
- 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.
* - ``min_free_disk_mb``
- Free space in MiB that must stay on the filesystem of ``storage_path``, integer ``0`` or more,
default ``0`` (off). Below it, recordings are deleted in eviction order.
* - ``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``.
- Seconds between two runs of the periodic job (the age sweep and the free-space floor), integer
1-86400, default ``60``. The job runs only when a level sets a ``max_age_sec`` key above ``0``
or ``min_free_disk_mb`` is above ``0``.

Fields:

Expand Down Expand Up @@ -943,9 +949,11 @@ that key: ``fault_codes.<CODE>.max_age_sec.active: 0`` keeps that code's recordi
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,
The periodic job runs on the retention worker every ``sweep_interval_sec``, only while a level sets a
``max_age_sec`` key above ``0`` or ``min_free_disk_mb`` is above ``0``; without either there is no
timer. The age sweep runs in it only while a ``max_age_sec`` key is above ``0``. The policy line at
startup ends with ``sweep: off`` or ``sweep: every <n> s``, then ``floor: off`` or
``floor: <n> MB``. Each age 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
Expand Down Expand Up @@ -987,6 +995,60 @@ quota and logs a warning; ``keep`` also deletes the recordings it was allowed to
the new recording alone and deletes nothing. The log line of a drop names the path, the size, the
quota and the reason.

**The free-space floor.** With ``min_free_disk_mb`` above ``0`` the fault manager keeps that much
free space on the filesystem of the effective storage path: ``storage_path``, or the system temp
directory when it is empty. All recordings are written under it, so they sit on that one
filesystem. A path not created yet is measured on its nearest existing parent directory. The free
space is what an unprivileged process may still write (``statvfs`` ``f_bavail``).

The floor is enforced by measuring, never by estimating. A pass measures the free space; while it is
below the floor, it deletes one whole recording in the eviction order above, removes its directory,
and measures again. It stops when the floor is met or nothing more can be deleted; the second case
logs one WARN that names the floor and the free space. The size a recording is listed with is not
used: a file held open by a download, hard links and block rounding make it differ from what the
deletion frees. Each removed recording is logged as ``Recording removed: <id> (min_free_disk_mb)``.
Every pass ends with one DEBUG line ``floor pass: removed=<n> free=<bytes> floor=<bytes>
met=<true|false> source=<filesystem|test-capacity>``.

A pass runs at two moments, and one pass runs at a time, on the retention worker thread:

- after each store that kept the new recording, at its priority. A store whose per-fault cap removed
every new row kept nothing and runs no pass. It never deletes a recording
with a higher priority. The new recording goes only when nothing else of its priority or lower is
left; then it is dropped and logged like a quota drop, with ``min_free_disk_mb`` as the reason.
``over_quota: keep`` does not keep a recording that leaves the free space below the floor.
- in the periodic job, after the age sweep, with no priority limit: other data that fills the disk
can then delete any recording, the highest priority last.

The floor is a target, not a reservation: other writers can change the free space between two
measurements. When PostgreSQL shares the disk (``storage_type: postgres``), ``min_free_disk_mb`` is
also its headroom.

**Open-time floor check.** When a new recording is about to open (not when a fault joins the post-fault
window of an open one) and the free space is already below the floor, the recording starts only when
a recording of the fault's priority or lower can be deleted. Otherwise it is not started: one INFO line
names the floor, and the ring buffer stays for the next fault. The quota estimate of the open-time check
below is unchanged.

A failed read of the free space logs one WARN, and that pass or check skips the floor. A storage
failure during a pass logs one WARN and stops the pass with no directory removed; a removal whose
commit outcome is unknown logs one WARN, leaves the directory and stops the pass. Each of these ends the
pass with that one WARN. A recording directory whose removal fails stays on disk with no row, as on every
removal path; the floor still sees its space and can remove other recordings.

A configuration that sets only ``min_free_disk_mb`` (or only ``sweep_interval_sec``) is a configured
retention: quota eviction then follows the eviction order above, with every priority ``0``.
``min_free_disk_mb: 0`` keeps the floor off and is still a configured retention. While the
configuration is invalid, the floor is off.

**Test capacity (tests only).** When the environment variable
``ROS2_MEDKIT_TEST_STORAGE_CAPACITY_FILE`` names a file when the fault manager starts, the free space
is not measured: it is the decimal capacity in that file, read again at each measurement, less the
sizes of the regular files under the storage path (symlinks not followed), and at least 0. A missing
or unreadable file is a failed measurement. The fault manager logs one WARN at startup, and each pass
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 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 All @@ -1006,7 +1068,8 @@ 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``). Nothing is deleted by age.
every fault code. A heal keeps the recordings (``on_healed`` is ``keep``). Nothing is deleted by age,
and the free-space floor is off.

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 @@ -1041,7 +1104,8 @@ A file the fault manager cannot read is reported with a warning and skipped.
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), a ``max_age_sec`` value below 0 or not an
integer, ``sweep_interval_sec`` outside 1-86400 or not an integer;
integer, ``sweep_interval_sec`` outside 1-86400 or not an integer, ``min_free_disk_mb`` below 0 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;
Expand Down
6 changes: 6 additions & 0 deletions src/ros2_medkit_fault_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ endif()
medkit_add_launch_test(test_rosbag_max_age test/integration/test_rosbag_max_age.test.py
TIMEOUT 600 LABELS "integration")

# min_free_disk_mb: the floor pass after a store and in the periodic job, the open-time check, a
# storage path not created yet, the invalid values. The cases set the free space with the test
# capacity file; one case measures the real filesystem.
medkit_add_launch_test(test_rosbag_free_space test/integration/test_rosbag_free_space.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
30 changes: 21 additions & 9 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,29 @@ only with an explicit `on_cleared: delete`. A recording made before such a delet
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.
resolves on its own, an explicit `0` wins. The periodic job runs on the retention worker every
`sweep_interval_sec` (default 60), only when a `max_age_sec` key or `min_free_disk_mb` is above 0; the startup
policy line says `sweep: off` or `sweep: every <n> s`, then `floor: off` or `floor: <n> MB`. 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.
`min_free_disk_mb` (default 0 = off) is the free space kept on the filesystem of `storage_path` (the system temp
directory when it is empty; a path not created yet is measured on its nearest existing parent). Below it, whole
recordings are deleted one at a time in eviction order, and the free space is measured again after each one:
the size a recording is listed with is not used. A pass runs on the retention worker after each store that kept its recording,
at its priority (never deleting a higher one; the new recording goes last and is then dropped), and in the
periodic job with no priority limit. A new recording below the floor with nothing of its priority or lower to
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 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, and nothing is deleted by age. Without any retention parameter the quota evicts the oldest
recordings first.
evicted for it, nothing is deleted by age and the floor is off. A configuration with only `min_free_disk_mb` or
only `sweep_interval_sec` is a configured retention (every priority 0). 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 @@ -611,8 +622,9 @@ and some of them build an `rclcpp::Clock`.
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, 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.
age sweep, the removal step of the free-space floor and the stale-row cleanup) on top of
`FaultStorage::apply_rosbag_changes`. It returns the directories to remove and never removes one itself,
and never reads the free space.

The core reaches the outside through four interfaces. The ROS adapter implements them.

Expand All @@ -638,7 +650,7 @@ 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 and runs the age
sweep. It
sweep and the free-space floor passes. 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
13 changes: 10 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 @@ -19,9 +19,15 @@
# 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.
# starts the age again. A shared recording loses only that fault's link. The periodic job runs every
# sweep_interval_sec (1-86400, default 60), only when a max_age_sec key or min_free_disk_mb is above
# 0; the age sweep runs in it only with a max_age_sec key. The age uses the system clock: start the
# fault manager after time synchronisation.
# min_free_disk_mb (MiB, default 0 = off) is the free space kept on the filesystem of storage_path.
# Below it, whole recordings are deleted one at a time in the eviction order below, and the free
# space is measured again after each one. After a store the new recording deletes only recordings of
# its priority or lower, itself last (then it is dropped); the periodic job has no priority limit.
# A recording below the floor with nothing of its priority or lower to delete is not started.
# 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 @@ -37,6 +43,7 @@ fault_manager:
snapshots:
rosbag:
retention:
min_free_disk_mb: 1024
sweep_interval_sec: 60
defaults:
priority: 10
Expand Down
Loading
Loading