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
161 changes: 152 additions & 9 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ jobs:
for pkg_dir in build/ros2_medkit_cmake/ build/ros2_medkit_*/; do
pkg=$(basename "$pkg_dir")
if [[ " ${pkgs[*]} " == *" $pkg "* ]]; then continue; fi
# graph_watchdog is tested in sanitizer-graph-watchdog, in a job
# of its own.
if [ "$pkg" = "ros2_medkit_graph_watchdog" ]; then continue; fi
# graph_watchdog and fault_manager are tested in their own jobs
# (sanitizer-graph-watchdog, sanitizer-fault-manager).
if [ "$pkg" = "ros2_medkit_graph_watchdog" ] || [ "$pkg" = "ros2_medkit_fault_manager" ]; then continue; fi
while [ "$(jobs -rp | wc -l)" -ge "$max_pkgs" ]; do wait -n || true; done
(cd "$pkg_dir" && run_shard) \
> "$log_dir/$pkg.log" 2>&1 &
Expand All @@ -473,7 +473,7 @@ jobs:
if: always()
run: |
for pkg_dir in build/ros2_medkit_*/; do
if [ "$(basename "$pkg_dir")" = "ros2_medkit_graph_watchdog" ]; then continue; fi
case "$(basename "$pkg_dir")" in ros2_medkit_graph_watchdog | ros2_medkit_fault_manager) continue ;; esac
colcon test-result --test-result-base "$pkg_dir" --verbose 2>/dev/null || true
done

Expand Down Expand Up @@ -636,9 +636,9 @@ jobs:
for pkg_dir in build/ros2_medkit_cmake/ build/ros2_medkit_*/; do
pkg=$(basename "$pkg_dir")
if [[ " ${pkgs[*]} " == *" $pkg "* ]]; then continue; fi
# graph_watchdog is tested in sanitizer-graph-watchdog, in a job
# of its own.
if [ "$pkg" = "ros2_medkit_graph_watchdog" ]; then continue; fi
# graph_watchdog and fault_manager are tested in their own jobs
# (sanitizer-graph-watchdog, sanitizer-fault-manager).
if [ "$pkg" = "ros2_medkit_graph_watchdog" ] || [ "$pkg" = "ros2_medkit_fault_manager" ]; then continue; fi
while [ "$(jobs -rp | wc -l)" -ge "$max_pkgs" ]; do wait -n || true; done
(cd "$pkg_dir" && run_shard) \
> "$log_dir/$pkg.log" 2>&1 &
Expand All @@ -663,7 +663,7 @@ jobs:
if: always()
run: |
for pkg_dir in build/ros2_medkit_*/; do
if [ "$(basename "$pkg_dir")" = "ros2_medkit_graph_watchdog" ]; then continue; fi
case "$(basename "$pkg_dir")" in ros2_medkit_graph_watchdog | ros2_medkit_fault_manager) continue ;; esac
colcon test-result --test-result-base "$pkg_dir" --verbose 2>/dev/null || true
done

Expand Down Expand Up @@ -841,13 +841,156 @@ jobs:
colcon test-result --test-result-base build/ros2_medkit_graph_watchdog \
--verbose 2>/dev/null || true

# The fault manager's own tests, including the launch tests that start the
# node as a process, so a sanitizer report inside it fails the test.
sanitizer-fault-manager:
name: Sanitizer ${{ matrix.name }} (fault_manager)
needs: changes
if: needs.changes.outputs.code == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
sanitizer: [asan, tsan]
include:
- sanitizer: asan
name: ASan + UBSan
cmake_sanitizer: asan,ubsan
# Both sizes are the ones the matching workspace job argued for.
# This job restores that job's cache and must not evict what it
# restored, so it cannot be smaller here.
ccache_size: 2G
- sanitizer: tsan
name: TSan
cmake_sanitizer: tsan
ccache_size: 1.5G
container:
image: ros:jazzy-ros-base
# See sanitizer-asan: the instrumented build tree does not fit the
# container overlay on small GitHub runners, so build on /mnt.
volumes:
- "/mnt:/mnt"
options: ${{ matrix.sanitizer == 'tsan' && '--privileged' || '' }}
# Covers a cold-cache sanitizer build plus the 45-minute test step.
timeout-minutes: 90
defaults:
run:
shell: bash

steps:
# TSan needs at most 28 bits of mmap ASLR entropy. Newer runner kernels use more.
- name: Lower mmap ASLR entropy
if: matrix.sanitizer == 'tsan'
run: |
sysctl -w vm.mmap_rnd_bits=28
cat /proc/sys/vm/mmap_rnd_bits
test "$(cat /proc/sys/vm/mmap_rnd_bits)" = 28

- name: Checkout repository
uses: actions/checkout@v4

- name: Install ccache
run: |
apt-get update
apt-get install -y ccache

- name: Restore ccache
# Restore, never save. This job compiles a subset of the sources
# sanitizer-${{ matrix.sanitizer }} compiles, with the same flags, so
# that job's cache is a superset of what this one needs and serves its
# misses. A second copy would buy nothing and would spend the
# repository's 10 GB Actions cache quota twice for one set of objects.
uses: actions/cache/restore@v4
with:
path: /root/.cache/ccache
key: ccache-jazzy-${{ matrix.sanitizer }}-${{ github.sha }}
restore-keys: |
ccache-jazzy-${{ matrix.sanitizer }}-

- name: Install dependencies
run: |
apt-get update
apt-get install -y ros-jazzy-test-msgs
source /opt/ros/jazzy/setup.bash
for attempt in 1 2 3; do rosdep update && break; [ "$attempt" = 3 ] && exit 1; echo "rosdep update attempt $attempt failed, retrying" >&2; sleep 5; done
rosdep install --from-paths src --ignore-src -y

- name: Redirect heavy build output to /mnt
run: |
# Build on /mnt, as the workspace sanitizer jobs do. The path must match the
# sanitizer job whose ccache this job restores, so ccache can hit.
mkdir -p /mnt/${{ matrix.sanitizer }}/build /mnt/${{ matrix.sanitizer }}/tmp
ln -sfn /mnt/${{ matrix.sanitizer }}/build build
df -h / /mnt

- name: Build with ${{ matrix.name }}
env:
CCACHE_DIR: /root/.cache/ccache
CCACHE_MAXSIZE: ${{ matrix.ccache_size }}
CCACHE_SLOPPINESS: pch_defines,time_macros
TMPDIR: /mnt/${{ matrix.sanitizer }}/tmp
run: |
source /opt/ros/jazzy/setup.bash
ccache -z
# --packages-up-to builds the fault manager and the packages it
# depends on, not the whole workspace.
colcon build --symlink-install \
--packages-up-to ros2_medkit_fault_manager \
--cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DSANITIZER=${{ matrix.cmake_sanitizer }} \
--event-handlers console_direct+
ccache -s
./scripts/ccache_report.sh jazzy-${{ matrix.sanitizer }}-fault-manager
df -h / /mnt

- name: Extend test timeouts for sanitizer overhead
run: |
# The same generic x3 rewrite the workspace sanitizer jobs apply - see
# the comment in sanitizer-asan for why it is not a list of literals.
find build/ -name "CTestTestfile.cmake" -exec \
perl -pi -e 's/\bTIMEOUT "(\d+)"/sprintf(q{TIMEOUT "%d"}, $1 * 3)/ge' {} +
find build/ -name "CTestTestfile.cmake" -exec cat {} + \
| grep -oE 'TIMEOUT "[0-9]+"' | sort -t'"' -k2 -n | uniq -c

- name: Run fault_manager tests with ${{ matrix.name }}
timeout-minutes: 45
env:
# Same factor as the ctest TIMEOUT rewrite above, for the wall-clock
# budgets tests assert internally - ctest's clock cannot reach those.
MEDKIT_TEST_TIME_SCALE: 3
# Lets tests size instrumented-only-expensive resources down.
MEDKIT_TEST_SANITIZED: 1
# Tests the package's ctest runs at once. Sanitizer builds use more
# CPU and memory per test than Release ones.
CTEST_PARALLEL_LEVEL: 2
run: |
if [ "${{ matrix.sanitizer }}" = "tsan" ]; then
TSAN_OPTIONS="halt_on_error=0:history_size=4:suppressions=$(pwd)/tsan_suppressions.txt"
export TSAN_OPTIONS
else
# detect_leaks=0: FastDDS allocator leaks on shutdown (not our code)
# new_delete_type_mismatch=0: ROS 2 DDS scalar/array new/delete mismatch
export ASAN_OPTIONS=halt_on_error=1:detect_leaks=0:new_delete_type_mismatch=0
export UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1
fi
source /opt/ros/jazzy/setup.bash
source install/setup.bash
cd build/ros2_medkit_fault_manager
ctest -LE "linter" --no-tests=error --output-on-failure

- name: Show test results
if: always()
run: |
colcon test-result --test-result-base build/ros2_medkit_fault_manager \
--verbose 2>/dev/null || true

# The one check branch rules require from this workflow. A matrix job skipped by
# its `if` does not report its per-entry checks, so the rules cannot require them.
# always(), not !cancelled(): a skipped gate would count as passed. clang-tidy
# stays out: it is advisory.
quality-ok:
if: always()
needs: [changes, format-lint, sanitizer-asan, sanitizer-tsan, sanitizer-graph-watchdog]
needs: [changes, format-lint, sanitizer-asan, sanitizer-tsan, sanitizer-graph-watchdog, sanitizer-fault-manager]
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
Expand Down
9 changes: 9 additions & 0 deletions src/ros2_medkit_fault_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ endif()
medkit_add_launch_test(test_rosbag_integration test/integration/test_rosbag_integration.test.py TIMEOUT 150
LABELS "integration")

# Rosbag retention pins: a real fault_manager_node, sized payloads, SQLite file.
medkit_add_launch_test(test_rosbag_quota test/integration/test_rosbag_quota.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")
endif()

# Launches without snapshots.rosbag.format, so the parameter default (mcap)
# is what is under test rather than an operator's explicit choice.
medkit_add_launch_test(test_rosbag_default_format test/integration/test_rosbag_default_format.test.py TIMEOUT 150
Expand Down
18 changes: 18 additions & 0 deletions src/ros2_medkit_fault_manager/design/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,21 @@ published, the writer is being created - directory creation plus a storage-backe
Messages arriving in that interval take the buffering path rather than the new bag, so a
post-fault-only recording can miss its first few milliseconds. They are not lost; they
stay in the ring buffer and serve the next fault as pre-history.

Rosbag storage primitive
""""""""""""""""""""""""

``FaultStorage::apply_rosbag_changes(RosbagChangeSet)`` changes the rosbag rows of every backend
in one transaction: ``insert`` upserts on ``(fault_code, file_path)``, ``remove`` drops links, and
``remove_recordings`` drops every link of a recording id. It applies no per-fault cap and never
unlinks a file. ``RosbagApplyResult::orphaned_paths`` lists each path whose last link the call
removed, once, so the caller decides what happens to the directory. A throw leaves every row as
it was: ``BEGIN IMMEDIATE`` in SQLite, one ``run_in_transaction`` in PostgreSQL, a copy swapped in
under the mutex in memory.

A set must not insert and remove the same link, directly or through ``remove_recordings`` of an
inserted row's recording id. The call throws ``std::invalid_argument`` before it changes a row.
With that rule, applying a set twice leaves the same rows and the second call returns no
orphaned paths.

Production code does not call it yet. The six existing rosbag mutators keep serving the node.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ struct RosbagFileInfo {
int64_t created_at_ns{0}; ///< Timestamp when bag was created
};

/// One link between a fault and a recording: the unit a rosbag row stores.
struct RosbagLink {
std::string fault_code;
std::string file_path;
};

/// Rosbag row changes applied together by FaultStorage::apply_rosbag_changes().
struct RosbagChangeSet {
std::vector<RosbagFileInfo> insert; ///< Upsert on (fault_code, file_path)
std::vector<RosbagLink> remove; ///< Remove these links
std::vector<std::string> remove_recordings; ///< Remove every link of these recording ids
};

/// What apply_rosbag_changes() reports back.
struct RosbagApplyResult {
/// Each path whose last link the call removed, once, in ascending order. A path another link
/// still references is not listed. The caller decides what to do with the directory.
std::vector<std::string> orphaned_paths;
};

/// Check the precondition of FaultStorage::apply_rosbag_changes() the same way in every backend.
/// @throws std::invalid_argument when @p changes inserts and removes the same link, directly or
/// through remove_recordings of an inserted row's recording id
void check_rosbag_change_set(const RosbagChangeSet & changes);

/// Abstract interface for fault storage backends
class FaultStorage {
public:
Expand Down Expand Up @@ -396,6 +421,18 @@ class FaultStorage {
}
}

/// Apply a set of rosbag row changes in one transaction.
///
/// - All or nothing: when anything throws, no row has changed.
/// - Applies no per-fault cap.
/// - Never unlinks a file or a directory. orphaned_paths names the paths the caller may unlink.
/// - Precondition: the set never inserts and removes the same link, directly or through
/// remove_recordings of an inserted row's recording id. A violation throws
/// std::invalid_argument before any row changes. With it, applying a set twice leaves the
/// same rows and the second call returns no orphaned paths.
/// An empty recording id in remove_recordings matches nothing.
virtual RosbagApplyResult apply_rosbag_changes(const RosbagChangeSet & changes) = 0;

/// The MOST RECENT recording of a fault, or nullopt.
///
/// A fault can hold several recordings, so "the" recording is a choice: newest
Expand Down Expand Up @@ -530,6 +567,7 @@ class InMemoryFaultStorage : public FaultStorage {
/// All-or-nothing, as the base class requires: the batch is built beside the live
/// map and swapped in, so a throw leaves the store exactly as it was.
void store_rosbag_files(const std::vector<RosbagFileInfo> & infos) override;
RosbagApplyResult apply_rosbag_changes(const RosbagChangeSet & changes) override;
std::optional<RosbagFileInfo> get_rosbag_file(const std::string & fault_code) const override;
std::vector<RosbagFileInfo> get_rosbag_files(const std::string & fault_code) const override;
std::vector<RosbagFileInfo> get_rosbag_files_by_recording(const std::string & recording_id) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class PgFaultStorage : public FaultStorage {

void store_rosbag_file(const RosbagFileInfo & info) override;
void store_rosbag_files(const std::vector<RosbagFileInfo> & infos) override;
RosbagApplyResult apply_rosbag_changes(const RosbagChangeSet & changes) override;
std::optional<RosbagFileInfo> get_rosbag_file(const std::string & fault_code) const override;
std::vector<RosbagFileInfo> get_rosbag_files(const std::string & fault_code) const override;
std::vector<RosbagFileInfo> get_rosbag_files_by_recording(const std::string & recording_id) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class SqliteFaultStorage : public FaultStorage {

void store_rosbag_file(const RosbagFileInfo & info) override;
void store_rosbag_files(const std::vector<RosbagFileInfo> & infos) override;
RosbagApplyResult apply_rosbag_changes(const RosbagChangeSet & changes) override;
std::optional<RosbagFileInfo> get_rosbag_file(const std::string & fault_code) const override;
std::vector<RosbagFileInfo> get_rosbag_files(const std::string & fault_code) const override;
std::vector<RosbagFileInfo> get_rosbag_files_by_recording(const std::string & recording_id) const override;
Expand Down
1 change: 1 addition & 0 deletions src/ros2_medkit_fault_manager/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<test_depend>launch_testing_ament_cmake</test_depend>
<test_depend>launch_testing_ros</test_depend>
<test_depend>python3-pytest</test_depend>
<test_depend>python3-psycopg2</test_depend>
<test_depend>rosbag2_py</test_depend>
<test_depend>rosbag2_storage_mcap</test_depend>
<test_depend>rosbag2_storage_default_plugins</test_depend>
Expand Down
Loading
Loading