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
119 changes: 119 additions & 0 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,125 @@ messages before everything stopped. Note the condition: a single captured topic
goes quiet while others keep publishing is pruned like anything else, because each
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
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``.

.. code-block:: yaml

fault_manager:
ros__parameters:
snapshots:
rosbag:
retention:
defaults:
priority: 10
severity:
ERROR:
priority: 50
fault_codes:
MOTOR-STALL:
priority: 900
patterns:
gnss_fix:
match: 'GNSS\.FIX_LOST'
priority: 300
pattern_order: [gnss_fix]

Grammar, relative to ``snapshots.rosbag.retention.``:

.. list-table::
:header-rows: 1
:widths: 40 60

* - Key
- Meaning
* - ``defaults.<field>``
- Rule of every fault the other levels do not set.
* - ``severity.<INFO|WARN|ERROR|CRITICAL>.<field>``
- Rule of the faults with that severity.
* - ``fault_codes.<CODE>.<field>``
- Rule of one fault code. ``<CODE>`` has no ``.``; hyphens are allowed.
* - ``patterns.<LABEL>.match``
- ECMAScript regular expression that must cover the whole fault code.
* - ``patterns.<LABEL>.<field>``
- 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.

Fields:

.. list-table::
:header-rows: 1
:widths: 20 20 60

* - Field
- Values
- Meaning
* - ``priority``
- integer 0-1000, default ``0``
- Rank of the fault's recordings.
* - ``over_quota``
- ``keep`` (default) or ``drop``
- What happens to a new recording that cannot fit the storage quota.

These two fields are parsed and checked in this release. The rules do not change which recordings
are removed yet.

**Resolution.** Each field of a fault's rule comes from the first level that sets it: its
``fault_codes.<CODE>`` entry, then the first label of ``pattern_order`` whose ``match`` covers the
whole fault code (only that pattern is used, even when a later one matches too), then
``severity.<SEVERITY>`` of the fault's current severity, then ``defaults``, then the built-in value. An
explicit value at a higher level wins also when it equals the built-in value: ``priority: 0`` on a
fault code beats ``priority: 50`` on its severity.

**Dotted fault codes.** A fault code with a ``.`` is configured through a pattern whose ``match``
escapes the dot (``GNSS\.FIX_LOST``), not as a ``fault_codes`` key.

**Keys the loader moves.** The ROS YAML loader mishandles some files: after an empty mapping or after
a dotted mapping key it moves the keys that follow, so ``defaults`` becomes ``fault_codes.defaults`` or
``enabled`` lands under ``retention``. The loaded parameters cannot show this. At startup the fault
manager reads each parameters file that ROS applied to the process or to the node a second time with
its own YAML reader, which names the parameters of every node section the way a correct loader does
(nested node keys joined with ``/``, a key that holds ``ros__parameters`` and child node keys giving
both sections, nested keys below ``ros__parameters`` joined with ``.``, a dotted key as a namespace, an
empty mapping as nothing). The sections that apply to the node are the ones ``rclcpp`` applies to its
full name. In each of them the fault manager compares the names the loader produced with the names the file
says, as sets. The loader merges every genuine repeat (a name in two documents, a name under two
spellings), so a name it produced twice was moved onto an existing name and counts as a difference too. Any difference stops the fault manager with exit code 1; the message
names the file, the differing parameters and the key as written that probably caused it, with its line.
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``;
- 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
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.

Any other error does not stop the fault manager. It keeps running, records as before, does not use
the rules and reports the fault ``ROSBAG_RETENTION_CONFIG_INVALID`` (severity ``CRITICAL``, source the
fault manager node, description the errors). That fault starts no capture. Clearing it does not fix
the configuration: the next fault that confirms reports it again. A start with a valid configuration
(or none) clears it. If the fault storage is unreachable at start, the report or the clear is retried
every second until the storage accepts it.

The retention parameters are read only: a ``SetParameters`` request for one is refused. At startup the
fault manager also warns once, listing every parameter override that no declaration consumed, so a
misspelled key does not pass unnoticed.

.. seealso::

:doc:`/tutorials/snapshots` for detailed snapshot configuration examples.
Expand Down
17 changes: 17 additions & 0 deletions src/ros2_medkit_fault_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include(ROS2MedkitWarnings)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rcl_yaml_param_parser REQUIRED)
find_package(ros2_medkit_fault_detection REQUIRED)
find_package(ros2_medkit_msgs REQUIRED)
find_package(ros2_medkit_serialization REQUIRED)
Expand Down Expand Up @@ -142,6 +143,7 @@ target_include_directories(fault_manager_lib PUBLIC

medkit_target_dependencies(fault_manager_lib PUBLIC
rclcpp
rcl_yaml_param_parser
ros2_medkit_msgs
ros2_medkit_serialization
rosbag2_cpp
Expand Down Expand Up @@ -226,6 +228,11 @@ endif()
target_link_libraries(test_rosbag_retention fault_manager_lib)
medkit_test_needs_no_domain(test_rosbag_retention)

# Retention rule parsing and resolution. Plain C++ core, no ROS node.
medkit_add_gtest(test_retention_policy test/test_retention_policy.cpp)
target_link_libraries(test_retention_policy fault_manager_core)
medkit_test_needs_no_domain(test_retention_policy)

# Fault audit log tests (hash chain, verify, rotation, reopen)
medkit_add_gtest(test_fault_audit_log test/test_fault_audit_log.cpp)
target_link_libraries(test_fault_audit_log fault_manager_lib)
Expand Down Expand Up @@ -291,6 +298,11 @@ endif()
if(POSTGRES_SUPPORT)
medkit_add_launch_test(test_rosbag_quota_pg test/integration/test_rosbag_quota_pg.test.py TIMEOUT 300
LABELS "integration")

# An invalid retention configuration is reported, and a stored report cleared, once a server that
# was unreachable at start answers.
medkit_add_launch_test(test_retention_config_pg test/integration/test_retention_config_pg.test.py TIMEOUT 300
LABELS "integration")
endif()

# Launches without snapshots.rosbag.format, so the parameter default (mcap)
Expand Down Expand Up @@ -339,6 +351,11 @@ endif()
medkit_add_launch_test(test_rosbag_boundary_scope test/integration/test_rosbag_boundary_scope.test.py TIMEOUT 180
LABELS "integration")

# Retention configuration against a real fault_manager_node: a shifted key, every error class,
# the fault the node reports about itself, and the read-only parameters.
medkit_add_launch_test(test_retention_config test/integration/test_retention_config.test.py TIMEOUT 600
LABELS "integration")

# Core orchestration under parallel callers. Plain C++ core, no ROS node.
medkit_add_gtest(test_fault_manager_core_concurrency test/test_fault_manager_core_concurrency.cpp)
target_link_libraries(test_fault_manager_core_concurrency fault_manager_core)
Expand Down
9 changes: 9 additions & 0 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ dependencies of this package, so a normal install pulls in both backends. See
`docs/config/fault-manager.rst` and `docs/tutorials/snapshots.rst` for the full
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; this release does not yet use them to choose which recordings go.
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
recording as before and reports the fault `ROSBAG_RETENTION_CONFIG_INVALID`. See
`config/rosbag_retention.example.yaml` and `docs/config/fault-manager.rst`.

### Storage Backends

This is the persisted fault store (`storage_type`), separate from the rosbag storage
Expand Down
46 changes: 46 additions & 0 deletions src/ros2_medkit_fault_manager/config/rosbag_retention.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Example rosbag retention rules for the fault manager.
#
# 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).
# Any other key under retention is an error: the fault manager keeps running, records as before
# and reports the fault ROSBAG_RETENTION_CONFIG_INVALID. Retention values are read-only at runtime.
#
# Use with: ros2 run ros2_medkit_fault_manager fault_manager_node --ros-args --params-file <this file>
# The rules are read only when snapshots.rosbag.enabled is true.
fault_manager:
ros__parameters:
snapshots:
rosbag:
retention:
defaults:
priority: 10
over_quota: keep
severity:
# Severity names: INFO, WARN, ERROR, CRITICAL.
ERROR:
priority: 50
CRITICAL:
priority: 100
over_quota: keep
fault_codes:
# A fault code has no dot here. Hyphens are allowed.
MOTOR-STALL:
priority: 900
DEBUG_NOISE:
priority: 0
over_quota: drop
patterns:
# A dotted fault code goes through a pattern whose match escapes the dot.
# The match covers the whole fault code (ECMAScript regular expression).
gnss_fix:
match: 'GNSS\.FIX_LOST'
priority: 300
lidar:
match: 'LIDAR_.*'
priority: 200
over_quota: drop
# The first label whose match covers the code wins. Every pattern is listed here.
# With no patterns, leave this key out: an empty list (`[]`) stops the node.
pattern_order: [gnss_fix, lidar]
25 changes: 25 additions & 0 deletions src/ros2_medkit_fault_manager/design/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,31 @@ Rosbag retention
``GetRosbag`` and ``ListRosbags``. The node builds the retention also with rosbag capture off,
so this cleanup runs then too.

``core::RetentionPolicy`` (``core/retention_policy.*``, no ROS types) parses the
``snapshots.rosbag.retention.*`` parameters, flattened by the node into a map of keys relative to the
prefix, and resolves the rule of a fault code and severity field by field: fault code, the first
matching pattern of ``pattern_order``, severity, defaults, built-in. ``parse`` reports every error
and every shifted key (a key with more segments than the grammar allows).

At the start of the constructor the node reads each parameters file rcl lists for the process and the node
twice: with ``rcl_parse_yaml_file``, the loader it used, and with ``core::read_params_file`` (yaml-cpp, no
ROS types), which produces the parameter names of each node section the way a correct loader does. The
sections that apply come from ``rclcpp::parameter_map_from`` with the node's full name, each section offered
alone. ``core::find_differences`` compares, per applying section, the loader's names with the file's as
sets, and counts a name the loader produced twice as moved. Any difference, or a shifted retention key, makes the constructor throw, so the process exits
with code 1, also with rosbag capture off.

The node declares each retention override as a read-only parameter with its own value. An invalid
retention configuration degrades the node: the policy stays unset and the node reports
``ROSBAG_RETENTION_CONFIG_INVALID`` through ``FaultManagerCore::report``. The core calls
``CaptureHooks::on_confirmed`` when a fault moved to CONFIRMED (a report after its re-read, or the tick), not
gated by muting or the capture cooldown: the node records the request and arms a wall timer, which
re-reports. When the re-read of a FAILED report fails the core calls ``on_confirm_candidate`` instead and the timer
reads that fault's status first. A successful clear of the own fault, or of a root cause that auto-clears it,
drops both. The timer is created cancelled and runs on the
executor thread only while something is pending: the flag, or the startup report or clear that the core
has not accepted yet. Nothing reads the policy yet.

Lock order: the caller's outer locks (the node's rosbag mutex on the inline store path, the core
mutex on the clear path), then the retention mutex, then the storage's own lock. Under the
retention mutex only storage calls and logging run. No other lock is taken and no file system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ class CaptureHooks {
virtual CaptureResult capture_now(const std::string & code) = 0;
virtual void on_prefailed(const std::string & code) = 0;
virtual void on_cleared(const std::string & code) = 0;
/// A fault that just moved to CONFIRMED, muted or not and inside the recapture cooldown or not. Not gated by
/// either.
virtual void on_confirmed(const std::string & code) {
(void)code;
}
/// A FAILED report reached a fault that was not CONFIRMED and the core could not read the result back. The
/// receiver checks the status itself.
virtual void on_confirm_candidate(const std::string & code) {
(void)code;
}
};

/// Wall-clock source, nanoseconds since the Unix epoch.
Expand Down Expand Up @@ -109,6 +119,7 @@ struct ReportOutcome {

struct ClearOutcome {
bool success{false};
bool not_found{false}; ///< No such fault is stored.
std::string message;
std::vector<std::string> auto_cleared_codes;
};
Expand Down Expand Up @@ -173,6 +184,7 @@ inline constexpr const char * kSnapshotRosbag = "rosbag";
/// GetFault.srv response field for field.
struct FaultDetail {
bool success{false};
bool not_found{false}; ///< No such fault is stored; set only with success false.
std::string error_message;
fd::FaultRecord fault;
int64_t first_occurrence_ns{0};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2026 bburda
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <map>
#include <set>
#include <string>
#include <vector>

namespace ros2_medkit_fault_manager::core {

/// A parameter our reading of a file produces, and the line of its last key.
struct ReadParam {
std::string name;
int line{0}; ///< 1-based.
};

/// A mapping key the loader may mishandle: an empty mapping, or a key with a `.` and a mapping value.
struct SuspectKey {
std::string key; ///< As written.
int line{0};
};

/// What a correct loader produces from a ROS parameters file, per node section. A section is named the
/// way rcl names it: nested node keys joined with `/`. Below `ros__parameters` nested keys are joined
/// with `.`, a key with a dot is a namespace, and an empty mapping produces nothing.
struct ParamsFileReading {
bool readable{true};
std::string error; ///< Set when the file could not be read or parsed.
std::map<std::string, std::vector<ReadParam>> sections;
std::map<std::string, std::vector<SuspectKey>> suspects;
};

/// Read every document of the file with yaml-cpp.
ParamsFileReading read_params_file(const std::string & path);

/// Parameter names per section as the rcl loader produced them, in its order and with repeats.
using RclSections = std::map<std::string, std::vector<std::string>>;

/// A name only one of the loader and a correct reading of the file produces in a section, or that the loader
/// produced more than once.
struct Difference {
std::string section;
std::string name;
int loader_count{0};
int file_count{0};
std::string key; ///< The key as written that probably caused it; empty when none was found.
int line{0};
};

/// Compare, for each section in @p applying, the names the loader produced with those of @p reading as sets.
/// rcl merges every genuine repeat (two documents, two spellings), so a name it produced twice was moved onto a
/// name its section already had.
std::vector<Difference> find_differences(const ParamsFileReading & reading, const RclSections & loader,
const std::set<std::string> & applying);

} // namespace ros2_medkit_fault_manager::core
Loading
Loading