Skip to content

Make rabbit_vhost:add/2 idempotent #1714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2018
Merged
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
11 changes: 9 additions & 2 deletions src/rabbit_vhost.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,22 @@ recover(VHost) ->

-define(INFO_KEYS, [name, tracing, cluster_state]).

add(VHostPath, ActingUser) ->
add(VHost, ActingUser) ->
case exists(VHost) of
true -> ok;
false -> do_add(VHost, ActingUser)
end.

do_add(VHostPath, ActingUser) ->
rabbit_log:info("Adding vhost '~s'~n", [VHostPath]),
R = rabbit_misc:execute_mnesia_transaction(
fun () ->
case mnesia:wread({rabbit_vhost, VHostPath}) of
[] -> ok = mnesia:write(rabbit_vhost,
#vhost{virtual_host = VHostPath},
write);
[_] -> mnesia:abort({vhost_already_exists, VHostPath})
%% the vhost already exists
[_] -> ok
end
end,
fun (ok, true) ->
Expand Down
16 changes: 14 additions & 2 deletions test/vhost_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ groups() ->
ClusterSize1Tests = [
single_node_vhost_deletion_forces_connection_closure,
vhost_failure_forces_connection_closure,
dead_vhost_connection_refused
dead_vhost_connection_refused,
vhost_creation_idempotency
],
ClusterSize2Tests = [
cluster_vhost_deletion_forces_connection_closure,
Expand All @@ -43,7 +44,8 @@ groups() ->
vhost_failure_forces_connection_closure_on_failure_node,
dead_vhost_connection_refused_on_failure_node,
node_starts_with_dead_vhosts,
node_starts_with_dead_vhosts_and_ignore_slaves
node_starts_with_dead_vhosts_and_ignore_slaves,
vhost_creation_idempotency
],
[
{cluster_size_1_network, [], ClusterSize1Tests},
Expand Down Expand Up @@ -373,6 +375,16 @@ node_starts_with_dead_vhosts_and_ignore_slaves(Config) ->
true = rabbit_ct_broker_helpers:rpc(Config, 1,
rabbit_vhost_sup_sup, is_vhost_alive, [VHost2]).

vhost_creation_idempotency(Config) ->
VHost = <<"idempotency-test">>,
try
?assertEqual(ok, rabbit_ct_broker_helpers:add_vhost(Config, VHost)),
?assertEqual(ok, rabbit_ct_broker_helpers:add_vhost(Config, VHost)),
?assertEqual(ok, rabbit_ct_broker_helpers:add_vhost(Config, VHost))
after
rabbit_ct_broker_helpers:delete_vhost(Config, VHost)
end.

%% -------------------------------------------------------------------
%% Helpers
%% -------------------------------------------------------------------
Expand Down