From 6add4591c1a2ee42904fdc2e2aa6e88b9c7af891 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 13 May 2024 16:13:42 -0400 Subject: [PATCH] rabbit_runtime_parameters: Handle timeout failures in clear functions --- deps/rabbit/src/rabbit_db_rtparams.erl | 12 ++-- deps/rabbit/src/rabbit_runtime_parameters.erl | 59 +++++++++++++++---- .../src/rabbit_ct_broker_helpers.erl | 4 +- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/deps/rabbit/src/rabbit_db_rtparams.erl b/deps/rabbit/src/rabbit_db_rtparams.erl index c14a303a5c96..e420ca831156 100644 --- a/deps/rabbit/src/rabbit_db_rtparams.erl +++ b/deps/rabbit/src/rabbit_db_rtparams.erl @@ -224,8 +224,9 @@ get_all_in_khepri(VHostName, Comp) -> %% delete(). %% ------------------------------------------------------------------- --spec delete(Key) -> ok when - Key :: atom(). +-spec delete(Key) -> Ret when + Key :: atom(), + Ret :: ok | rabbit_khepri:timeout_error(). %% @doc Deletes the global runtime parameter named `Key'. %% %% @private @@ -235,10 +236,11 @@ delete(Key) when is_atom(Key) -> #{mnesia => fun() -> delete_in_mnesia(Key) end, khepri => fun() -> delete_in_khepri(Key) end}). --spec delete(VHostName, Comp, Name) -> ok when +-spec delete(VHostName, Comp, Name) -> Ret when VHostName :: vhost:name() | '_', Comp :: binary() | '_', - Name :: binary() | atom() | '_'. + Name :: binary() | atom() | '_', + Ret :: ok | rabbit_khepri:timeout_error(). %% @doc Deletes the non-global runtime parameter named `Name' for the given %% virtual host and component. %% @@ -281,7 +283,7 @@ delete_matching_in_mnesia_tx(VHostName, Comp, Name) -> delete_in_khepri(Key) -> Path = khepri_rp_path(Key), - ok = rabbit_khepri:delete(Path). + rabbit_khepri:delete(Path). delete_matching_in_khepri(VHostName, Comp, Name) -> Key = {?any(VHostName), ?any(Comp), ?any(Name)}, diff --git a/deps/rabbit/src/rabbit_runtime_parameters.erl b/deps/rabbit/src/rabbit_runtime_parameters.erl index 0e54bf08571e..ed802e540ba7 100644 --- a/deps/rabbit/src/rabbit_runtime_parameters.erl +++ b/deps/rabbit/src/rabbit_runtime_parameters.erl @@ -218,10 +218,19 @@ clear_global(Key, ActingUser) -> not_found -> {error_string, "Parameter does not exist"}; _ -> - ok = rabbit_db_rtparams:delete(KeyAsAtom), - event_notify(parameter_cleared, none, global, - [{name, KeyAsAtom}, - {user_who_performed_action, ActingUser}]) + case rabbit_db_rtparams:delete(KeyAsAtom) of + ok -> + event_notify(parameter_cleared, none, global, + [{name, KeyAsAtom}, + {user_who_performed_action, ActingUser}]), + ok; + {error, timeout} -> + Msg = rabbit_misc:format( + "Could not delete global runtime parameter '~ts' " + "because the operation timed out", + [Key]), + {error_string, Msg} + end end. clear_vhost(VHostName, ActingUser) when is_binary(VHostName) -> @@ -247,10 +256,24 @@ clear_vhost(VHostName, ActingUser) when is_binary(VHostName) -> Err end. +-spec clear_component(Component, ActingUser) -> Ret when + Component :: binary(), + ActingUser :: rabbit_types:username(), + Ret :: ok_or_error_string(). + clear_component(<<"policy">>, _) -> {error_string, "policies may not be cleared using this method"}; clear_component(Component, _ActingUser) -> - ok = rabbit_db_rtparams:delete('_', Component, '_'). + case rabbit_db_rtparams:delete('_', Component, '_') of + ok -> + ok; + {error, timeout} -> + Msg = rabbit_misc:format( + "Could not delete component '~ts' because the operation " + "timed out", + [Component]), + {error_string, Msg} + end. -spec clear_any(rabbit_types:vhost(), binary(), binary(), rabbit_types:username()) -> ok_thunk_or_error_string(). @@ -259,14 +282,24 @@ clear_any(VHost, Component, Name, ActingUser) -> case lookup(VHost, Component, Name) of not_found -> {error_string, "Parameter does not exist"}; _ -> - rabbit_db_rtparams:delete(VHost, Component, Name), - case lookup_component(Component) of - {ok, Mod} -> event_notify( - parameter_cleared, VHost, Component, - [{name, Name}, - {user_who_performed_action, ActingUser}]), - Mod:notify_clear(VHost, Component, Name, ActingUser); - _ -> ok + case rabbit_db_rtparams:delete(VHost, Component, Name) of + ok -> + case lookup_component(Component) of + {ok, Mod} -> + event_notify( + parameter_cleared, VHost, Component, + [{name, Name}, + {user_who_performed_action, ActingUser}]), + Mod:notify_clear(VHost, Component, Name, ActingUser); + _ -> + ok + end; + {error, timeout} -> + Msg = rabbit_misc:format( + "Could not delete parameter '~ts' because the " + "operation timed out", + [Name]), + {error_string, Msg} end end. diff --git a/deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl b/deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl index 982c31d4a83f..2e1adfc29190 100644 --- a/deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl +++ b/deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl @@ -2081,8 +2081,8 @@ clear_policy(Config, Node, Name) -> clear_policy(Config, Node, Name, <<"acting-user">>). clear_policy(Config, Node, Name, Username) -> - rpc(Config, Node, - rabbit_policy, delete, [<<"/">>, Name, Username]). + ok = rpc(Config, Node, + rabbit_policy, delete, [<<"/">>, Name, Username]). set_operator_policy(Config, Node, Name, Pattern, ApplyTo, Definition) -> ok = rpc(Config, Node,