Skip to content

Commit 572cd3c

Browse files
Merge pull request #11834 from rabbitmq/mergify/bp/v4.0.x/pr-11706
Handle timeouts possible in Khepri minority in `rabbit_db_vhost` (backport #11706)
2 parents 94942f6 + f040510 commit 572cd3c

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

deps/rabbit/src/rabbit_db_vhost.erl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
VHostName :: vhost:name(),
6363
Limits :: vhost:limits(),
6464
Metadata :: vhost:metadata(),
65-
Ret :: {existing | new, VHost},
65+
Ret :: {existing | new, VHost} | no_return(),
6666
VHost :: vhost:vhost().
6767
%% @doc Writes a virtual host record if it doesn't exist already or returns
6868
%% the existing one.
@@ -117,7 +117,9 @@ create_or_get_in_khepri(VHostName, VHost) ->
117117
-spec merge_metadata(VHostName, Metadata) -> Ret when
118118
VHostName :: vhost:name(),
119119
Metadata :: vhost:metadata(),
120-
Ret :: {ok, VHost} | {error, {no_such_vhost, VHostName}},
120+
Ret :: {ok, VHost} |
121+
{error, {no_such_vhost, VHostName}} |
122+
rabbit_khepri:timeout_error(),
121123
VHost :: vhost:vhost().
122124
%% @doc Updates the metadata of an existing virtual host record.
123125
%%
@@ -188,7 +190,7 @@ merge_metadata_in_khepri(VHostName, Metadata) ->
188190
-spec set_tags(VHostName, Tags) -> VHost when
189191
VHostName :: vhost:name(),
190192
Tags :: [vhost:tag() | binary() | string()],
191-
VHost :: vhost:vhost().
193+
VHost :: vhost:vhost() | no_return().
192194
%% @doc Sets the tags of an existing virtual host record.
193195
%%
194196
%% @returns the updated virtual host record if the record existed and the
@@ -347,7 +349,7 @@ list_in_khepri() ->
347349
-spec update(VHostName, UpdateFun) -> VHost when
348350
VHostName :: vhost:name(),
349351
UpdateFun :: fun((VHost) -> VHost),
350-
VHost :: vhost:vhost().
352+
VHost :: vhost:vhost() | no_return().
351353
%% @doc Updates an existing virtual host record using the result of
352354
%% `UpdateFun'.
353355
%%
@@ -439,9 +441,10 @@ with_fun_in_khepri_tx(VHostName, Thunk) ->
439441
%% delete().
440442
%% -------------------------------------------------------------------
441443

442-
-spec delete(VHostName) -> Existed when
444+
-spec delete(VHostName) -> Ret when
443445
VHostName :: vhost:name(),
444-
Existed :: boolean().
446+
Existed :: boolean(),
447+
Ret :: Existed | rabbit_khepri:timeout_error().
445448
%% @doc Deletes a virtual host record from the database.
446449
%%
447450
%% @returns a boolean indicating if the vhost existed or not. It throws an
@@ -468,7 +471,7 @@ delete_in_khepri(VHostName) ->
468471
case rabbit_khepri:delete_or_fail(Path) of
469472
ok -> true;
470473
{error, {node_not_found, _}} -> false;
471-
_ -> false
474+
{error, _} = Err -> Err
472475
end.
473476

474477
%% -------------------------------------------------------------------

deps/rabbit/src/rabbit_definitions.erl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ add_policy(VHost, Param, Username) ->
773773
exit(rabbit_data_coercion:to_binary(rabbit_misc:escape_html_tags(E ++ S)))
774774
end.
775775

776-
-spec add_vhost(map(), rabbit_types:username()) -> ok.
776+
-spec add_vhost(map(), rabbit_types:username()) -> ok | no_return().
777777

778778
add_vhost(VHost, ActingUser) ->
779779
Name = maps:get(name, VHost, undefined),
@@ -783,7 +783,12 @@ add_vhost(VHost, ActingUser) ->
783783
Tags = maps:get(tags, VHost, maps:get(tags, Metadata, [])),
784784
DefaultQueueType = maps:get(default_queue_type, Metadata, undefined),
785785

786-
rabbit_vhost:put_vhost(Name, Description, Tags, DefaultQueueType, IsTracingEnabled, ActingUser).
786+
case rabbit_vhost:put_vhost(Name, Description, Tags, DefaultQueueType, IsTracingEnabled, ActingUser) of
787+
ok ->
788+
ok;
789+
{error, _} = Err ->
790+
throw(Err)
791+
end.
787792

788793
add_permission(Permission, ActingUser) ->
789794
rabbit_auth_backend_internal:set_permissions(maps:get(user, Permission, undefined),

deps/rabbit/src/rabbit_vhost.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ delete(VHost, ActingUser) ->
311311
[{name, VHost},
312312
{user_who_performed_action, ActingUser}]);
313313
false ->
314-
{error, {no_such_vhost, VHost}}
314+
{error, {no_such_vhost, VHost}};
315+
{error, _} = Err ->
316+
Err
315317
end,
316318
%% After vhost was deleted from the database, we try to stop vhost
317319
%% supervisors on all the nodes.

deps/rabbitmq_management/src/rabbit_mgmt_wm_vhost.erl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,22 @@ accept_content(ReqData0, Context = #context{user = #user{username = Username}})
9292

9393
delete_resource(ReqData, Context = #context{user = #user{username = Username}}) ->
9494
VHost = id(ReqData),
95-
_ = rabbit_vhost:delete(VHost, Username),
96-
{true, ReqData, Context}.
95+
case rabbit_vhost:delete(VHost, Username) of
96+
ok ->
97+
{true, ReqData, Context};
98+
{error, timeout} ->
99+
rabbit_mgmt_util:internal_server_error(
100+
timeout,
101+
"Timed out waiting for the vhost to be deleted",
102+
ReqData, Context);
103+
{error, E} ->
104+
Reason = iolist_to_binary(
105+
io_lib:format(
106+
"Error occurred while deleting vhost: ~tp",
107+
[E])),
108+
rabbit_mgmt_util:internal_server_error(
109+
Reason, ReqData, Context)
110+
end.
97111

98112
is_authorized(ReqData, Context) ->
99113
rabbit_mgmt_util:is_authorized_admin(ReqData, Context).

0 commit comments

Comments
 (0)