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
36 changes: 36 additions & 0 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,42 @@ A post-fault-only bag on a quiet or heavily filtered system can contain zero
messages. It still finalises normally on both ``mcap`` and ``sqlite3``, is listed
by the bulk-data endpoints and can be downloaded; only its payload is empty.

**Recordings whose directory is gone.** At startup, before it serves requests and also with rosbag
capture off, the fault manager reads every rosbag row once and looks for the directory of each
recording. When it finds at least one of them, or there are no rows, it removes the rows of every
recording whose directory is gone. When there are rows and it finds none of their directories, the
storage path is treated as unavailable, for example a disk that is not mounted: nothing is removed,
one ERROR names the effective storage path (``storage_path``, or the system temp directory when it
is empty) and the number of rows, and the fault manager reports the fault
``ROSBAG_STORAGE_UNAVAILABLE`` (severity ``CRITICAL``, source the fault manager node, description the
path and the number of rows). That fault starts no capture. When the rows cannot be read at startup (a
PostgreSQL server that cannot be reached), one WARN is logged and nothing is checked. A lookup that
fails (no permission, an I/O error, a symbolic link loop) finds nothing and proves nothing: such a
recording is not removed, does not make the storage available and, while the storage is unavailable,
is kept like the recordings that are gone. ``GetRosbag`` answers that the file cannot be looked up,
and ``ListRosbags`` leaves it out with one WARN per call.

After startup, ``GetRosbag`` and ``ListRosbags`` remove the rows of a recording whose directory is
gone when they meet it. While the storage is unavailable they keep those rows: ``ListRosbags`` leaves
them out of the list and ``GetRosbag`` answers that the file is not on disk. To recover, mount the
disk. The first ``GetRosbag`` or ``ListRosbags`` call that finds an existing recorded directory clears
the fault. Rows that this call found missing stay until a later call removes them. A start that finds
a recorded directory, or no rows, clears the fault too. Clearing the fault by hand keeps the rows.

While the storage is unavailable, the recordings it could not find are kept by every rule: the
startup reclassification, a clear, a heal, the cap (``max_bags`` and ``keep_first`` count only the
other recordings), the quota eviction, the age limits and the free-space floor skip them; in the
eviction order, ``keep_first`` ranks the first of the other recordings. They still
count toward ``max_total_storage_mb``: when only they keep the storage over it, the new recording is
kept and a WARN names the bytes they take. Recording goes on: a new recording is
written to the storage path as it is, listed and served, and managed by the rules as usual. The floor
then measures the filesystem the new recordings are written to, not the disk that is not mounted.
Only the directory of a recording that was stored when the fault manager started ends the
unavailable state, so a new recording never clears the fault. The check
compares only the rows with the file system: there is no marker file and no store identity, and a
directory that no row names is not removed. With no rows the storage counts as available, so a disk
that is not mounted while no recording is stored is not detected.

**What a closed recording contains.** The recording directory holds the storage
files rosbag2 wrote, its ``metadata.yaml``, and a ``manifest.json`` the fault
manager writes once the writer is closed and before the recording is listed. The
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 @@ -315,6 +315,11 @@ endif()
medkit_add_launch_test(test_rosbag_free_space test/integration/test_rosbag_free_space.test.py
TIMEOUT 600 LABELS "integration")

# The startup check of the recorded directories: rows of missing recordings, an unavailable storage
# path (no recorded directory found), its recovery and an empty store. Two runs per case.
medkit_add_launch_test(test_rosbag_storage_availability test/integration/test_rosbag_storage_availability.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
12 changes: 12 additions & 0 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ 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.

At startup, also with capture off, the fault manager reads the rosbag rows once and looks for the directory of each
recording. When it finds one, or there are no rows, it removes the rows of the recordings whose directory is gone.
With rows and no directory found, the storage path is treated as unavailable (for example a disk that is not
mounted): nothing is removed, one ERROR names the path (`storage_path`, or the system temp directory when it is
empty) and the row count, and the fault `ROSBAG_STORAGE_UNAVAILABLE` is reported. While it is unavailable,
`GetRosbag`, `ListRosbags` and every rule (status actions, cap, quota eviction, age, floor) keep the recordings it
could not find, and recording goes on into the path as it is, with new recordings managed as usual. Mount the disk:
the first `GetRosbag` or `ListRosbags` call that finds the directory of a recording stored before the start clears
the fault (a recording made since never does), and a later call removes the rows still missing. A start that finds a
directory, or no rows, clears the fault too. A failed read of the rows at startup logs one WARN and checks nothing.
There is no marker file: a disk that is not mounted while no recording is stored is not detected.

#### Rosbag Retention Rules

`snapshots.rosbag.retention.*` sets a `priority` (0-1000), an `over_quota` choice (`keep` or `drop`), a
Expand Down
54 changes: 48 additions & 6 deletions src/ros2_medkit_fault_manager/design/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ Rosbag retention
and posts the directories to the retention worker.
- ``delete_generation``: the code's delete generation, read by ``RosbagCapture`` when a code joins a
recording.
- ``forget_recording``: removes the rows of a recording whose directory is gone, for
``GetRosbag`` and ``ListRosbags``. The node builds the retention also with rosbag capture off,
so this cleanup runs then too.
- ``forget_recording``: removes the rows of a recording whose directory is gone, for the node's
startup storage check, ``GetRosbag`` and ``ListRosbags``. The node builds the retention also with
rosbag capture off, so this cleanup runs then too.
- ``sweep_expired(now_ns)``: one pass of the age sweep. It returns at once unless ``kConfigured``
and ``RetentionPolicy::sweeps()`` (a level sets a ``max_age_sec`` key above 0). Otherwise it reads
``get_all_rosbag_files`` and ``get_all_faults`` once each, and removes in one change set the link of
Expand Down Expand Up @@ -699,6 +699,47 @@ 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.

The report and the clear of the own faults go through ``report_own_fault``, ``ensure_own_fault`` and
``clear_own_fault``, which take the fault code (the first two also the description). Both own faults
record nothing.

**Startup storage check.** ``FaultManagerNode::check_rosbag_storage_at_startup()`` runs once in the
constructor, after the retention and the worker and before the startup reclassification and the
services, also with rosbag capture off, so its result is known before any rule removes a row. It reads ``get_all_rosbag_files`` once, keeps one directory
per recording id and checks each with ``std::filesystem::exists`` outside the retention mutex; an
error of that call counts as neither found nor missing (``look_up_directory``), so it is not
forgotten, does not make the storage available, and is protected when the storage is unavailable.
``GetRosbag`` and ``ListRosbags`` treat a failed lookup the same way: not served, not removed, no
recovery. It logs ``rosbag storage check: rows=<n>
recordings=<n> missing=<n> available=<true|false>`` at DEBUG. A directory found, or no rows: each
missing recording goes through ``forget_recording``, its directories are posted to the retention
worker, and a ``ROSBAG_STORAGE_UNAVAILABLE`` of an earlier start is cleared. Rows but no directory:
``unavailable_recordings_`` takes every recording id and the node passes them to
``RosbagRetention::protect_recordings``, nothing is removed, one ERROR names the effective storage
path and the row count, and the fault is reported. A failed read logs one WARN and sets nothing. The
report or clear runs in the first own-fault pass of the constructor; one the core does not accept
stays pending for the own-fault timer, and a successful clear of the fault drops a pending report.

``unavailable_recordings_`` lives on the executor thread. While it is not empty, ``GetRosbag`` and
``ListRosbags`` do not call ``forget_recording`` for a missing directory. ``ListRosbags`` reads it
once per call, so the call that finds a directory keeps the rows it found missing. Either handler
calls ``recorded_directory_found(recording_id)`` for each existing directory; only an id in the set
empties it, calls ``RosbagRetention::release_recordings`` and clears the fault at once, arming the
own-fault timer when the core does not accept the clear, so a recording stored while the storage is
unavailable never ends that state.

**Protected recordings.** ``protect_recordings`` and ``release_recordings`` set and drop, under the
retention mutex, the recording ids no removal decision of ``RosbagRetention`` removes: the age sweep,
``evict_one_for_space`` and ``has_space_candidate`` (through ``space_candidates``), the quota eviction
(``decide_quota`` and ``evict``; the total still counts their sizes, ``admits_new_recording`` does
not count them as evictable, and when the oldest-first store stays over the quota with protected
bytes, its WARN names those bytes instead of saying the new recording alone exceeds it), the cap (``cap_changes`` neither counts nor removes them, and takes ``keep_first``'s first recording from
the rows it counts; ``decide_quota`` and ``space_candidates`` take it for the eviction order from
``first_paths``, which skips them too), the status
actions (``on_status_change``: heal, clear, startup reclassification; the delete generation still
advances) and ``forget_recording``. A decision skips their rows and, while a set is held, logs
``<decision>: skipped <n> protected rosbag row(s)`` once at DEBUG.

The node publishes the mode and the policy once, with ``RosbagRetention::set_policy``, after it builds
the retention and before capture starts. Each decision loads that snapshot once with ``std::atomic_load``
and uses it throughout.
Expand Down Expand Up @@ -736,9 +777,10 @@ depends on the path:
logged. The executor never runs a store. A bag that never got a row is discarded on the same
thread. A store in doubt (``kInDoubt``) returns no paths: the new directory stays, and a cap
victim's directory can stay with no row when the commit was applied.
- Clear, heal, startup reclassification and stale rows (the executor): the rows go inside the
service call or the constructor. The node's ``RetentionWorker``, one thread with a FIFO queue,
removes the directories, so the clear and report services return before a large bag is removed.
- Clear, heal, startup reclassification and stale rows (the executor; stale rows also in the startup
storage check): the rows go inside the service call or the constructor. The node's
``RetentionWorker``, one thread with a FIFO queue, removes the directories, so the clear and
report services return before a large bag is removed.
- Age sweep (the retention worker): the periodic job calls ``sweep_expired`` with the system clock,
then removes the returned directories on the same thread once the retention mutex is released,
and logs ``retention sweep: now=<ns> removed=<links>`` at DEBUG. ``now`` is the time the pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -155,6 +156,13 @@ class RosbagRetention {
/// decisions run: each decision uses the snapshot it loaded.
void set_policy(RetentionMode mode, std::shared_ptr<const RetentionPolicy> policy);

/// Keep every row of @p recording_ids: each removal decision below skips them, and the quota still counts their
/// sizes. Replaces the set held before. While a set is held, each decision logs the rows it skipped at DEBUG.
void protect_recordings(std::unordered_set<std::string> recording_ids);

/// Drop the set of protect_recordings().
void release_recordings();

/// Priority of a stored recording: the highest priority of the faults it is linked to, 0
/// unless kConfigured.
/// @throws a storage failure
Expand Down Expand Up @@ -251,6 +259,7 @@ class RosbagRetention {
private:
struct Eviction {
std::vector<FreedRecording> unlink_paths;
size_t protected_bytes{0}; ///< Bytes of the protected recordings the eviction skipped
};

struct FaultFacts;
Expand Down Expand Up @@ -297,9 +306,21 @@ class RosbagRetention {
const std::vector<std::string> & fault_codes,
const std::map<std::string, std::string> & first_path_by_code, FactsCache & cache);

/// The recordings the floor may delete at @p priority_limit, in eviction order. Expects mutex_ held.
/// The recordings the floor may delete at @p priority_limit, in eviction order. Counts the protected rows left out
/// in @p skipped. Expects mutex_ held.
/// @throws a failure of a read
std::vector<SpaceCandidate> space_candidates(const RetentionPolicyState & state, int64_t priority_limit);
std::vector<SpaceCandidate> space_candidates(const RetentionPolicyState & state, int64_t priority_limit,
size_t & skipped);

/// The first recording of each fault code among @p rows (read oldest first) that are not protected: what
/// keep_first keeps in the eviction order. Expects mutex_ held.
std::map<std::string, std::string> first_paths(const std::vector<RosbagFileInfo> & rows) const;

/// Whether protect_recordings() holds @p recording_id. Expects mutex_ held.
bool is_protected(const std::string & recording_id) const;

/// One DEBUG line with the protected rows @p decision skipped, only while a set is held. Expects mutex_ held.
void log_protected(const char * decision, size_t skipped) const;

/// Whether a clear of @p fault_code deletes its recordings (on_fault_cleared). Expects mutex_ held.
/// @throws a failure of the fault read
Expand Down Expand Up @@ -355,6 +376,8 @@ class RosbagRetention {
std::mutex mutex_;
/// One entry per fault code that had a delete action. Guarded by mutex_; only looked up by code.
std::unordered_map<std::string, uint64_t> delete_generations_;
/// Recordings no removal decision removes. Guarded by mutex_.
std::unordered_set<std::string> protected_;
};

} // namespace ros2_medkit_fault_manager::core
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,29 @@ class FaultManagerNode : public rclcpp::Node {
/// The \`snapshots.rosbag.retention.*\` overrides, keyed relative to that prefix. Declares nothing.
core::FlatParams read_retention_overrides();

/// Report the retention configuration fault through the core. True when the core accepted it.
bool report_retention_config_fault();
/// Report an own fault (severity CRITICAL, source this node) through the core. True when the core accepted it.
bool report_own_fault(const char * fault_code, const std::string & description);

/// Report the fault unless it is CONFIRMED. True when nothing is left to do.
bool ensure_retention_config_fault();
/// Report the own fault unless it is CONFIRMED. True when nothing is left to do.
bool ensure_own_fault(const char * fault_code, const std::string & description);

/// Clear the fault if it is stored. True when nothing is left to do.
bool clear_retention_config_fault();
/// Clear the own fault if it is stored. True when nothing is left to do.
bool clear_own_fault(const char * fault_code);

/// Run the pending report or clear of the storage fault. True when nothing is left to do.
bool storage_fault_step();

/// One pass of the own-fault work. True while something stays pending.
bool retention_pass();

/// Once at startup, also with rosbag capture off: removes the rows of recordings whose directory is gone, or, when
/// rows exist and no recorded directory does, marks the storage unavailable and reports ROSBAG_STORAGE_UNAVAILABLE.
void check_rosbag_storage_at_startup();

/// GetRosbag or ListRosbags found the directory of @p recording_id. A recording the startup check found missing
/// makes an unavailable storage available again; any other does nothing.
void recorded_directory_found(const std::string & recording_id);

/// A fault moved to CONFIRMED: the own fault is reported again. Runs under the core mutex: it only records the
/// request and arms the timer.
void on_confirmed(const std::string & fault_code);
Expand Down Expand Up @@ -249,6 +260,13 @@ class FaultManagerNode : public rclcpp::Node {
bool retention_reraise_{false};
/// Armed while an own-fault action is pending, cancelled otherwise.
rclcpp::TimerBase::SharedPtr retention_timer_;
/// The recordings stored when the startup check found no directory. While not empty the storage is unavailable:
/// GetRosbag and ListRosbags keep the rows of missing directories until they find a directory of one of these.
std::set<std::string> unavailable_recordings_;
enum class StorageFaultAction { kNone, kReport, kClear };
/// The report or clear of ROSBAG_STORAGE_UNAVAILABLE the core has not accepted yet.
StorageFaultAction storage_fault_action_{StorageFaultAction::kNone};
std::string storage_fault_description_;
std::string storage_type_;
int32_t confirmation_threshold_{-1};
bool healing_enabled_{false};
Expand Down
Loading
Loading