From e03019694ad40548cb565ab34878c92be791a06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20P=C3=A9dron?= Date: Mon, 22 Jan 2024 11:57:31 +0100 Subject: [PATCH] rabbit_khepri: Fix force_reset/0 to work with stopped Ra system [Why] This function blindly relied on the fact that the Ra `coordination` system was not stopped by `rabbit` when it stopped. That Ra system is finally correctly stopped in the parent commit. This breaks this function and it must be adapted to work with the Ra system stopped. This is even safer because the function deletes the Ra system directory as its last step. [How] The Ra system data directory is queried from RabbitMQ configuration instead of the running Ra system. We add a couple assertions to ensure the conditions are the ones we expect. --- deps/rabbit/src/rabbit_khepri.erl | 17 +++++++++++++++-- deps/rabbit/src/rabbit_ra_systems.erl | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/deps/rabbit/src/rabbit_khepri.erl b/deps/rabbit/src/rabbit_khepri.erl index 5308894982f5..345a16b08e1b 100644 --- a/deps/rabbit/src/rabbit_khepri.erl +++ b/deps/rabbit/src/rabbit_khepri.erl @@ -509,8 +509,21 @@ reset() -> %% @private force_reset() -> - DataDir = maps:get(data_dir, ra_system:fetch(coordination)), - ok = rabbit_file:recursive_delete(filelib:wildcard(DataDir ++ "/*")). + %% The Ra `coordination' system is stopped at this point; we assert that + %% with the `ra_system:fetch/1' call below. Therefore, we take the data + %% directory from the configuration. + RaSystem = coordination, + ?assertEqual(undefined, ra_system:fetch(RaSystem)), + Config = rabbit_ra_systems:get_config(RaSystem), + DataDir = maps:get(data_dir, Config), + ?assert(string:length(DataDir) > 0), %% Assertion to not `rm -rf /'. + ?LOG_INFO( + "Deleting Ra `coordination` system data directory as part of " + "Khepri reset: ~ts", + [DataDir], + #{domain => ?RMQLOG_DOMAIN_GLOBAL}), + Glob = filename:join(DataDir, "*"), + ok = rabbit_file:recursive_delete(filelib:wildcard(Glob)). %% @private diff --git a/deps/rabbit/src/rabbit_ra_systems.erl b/deps/rabbit/src/rabbit_ra_systems.erl index cf2a3b3ad95f..a3f0ade5dda2 100644 --- a/deps/rabbit/src/rabbit_ra_systems.erl +++ b/deps/rabbit/src/rabbit_ra_systems.erl @@ -13,6 +13,7 @@ -export([setup/0, setup/1, + get_config/1, all_ra_systems/0, are_running/0, ensure_ra_system_started/1,