Skip to content
Merged
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
29 changes: 26 additions & 3 deletions src/rabbit_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,21 @@

-endif.

ensure_cli_distribution() ->
case start_distribution() of
{ok, _} ->
ok;
{error, Error} ->
print_error("Failed to initialize erlang distribution: ~p.",
[Error]),
rabbit_misc:quit(?EX_TEMPFAIL)
end.

%%----------------------------------------------------------------------------

main(ParseFun, DoFun, UsageMod) ->
error_logger:tty(false),
start_distribution(),
ensure_cli_distribution(),
{ok, [[NodeStr|_]|_]} = init:get_argument(nodename),
{Command, Opts, Args} =
case ParseFun(init:get_plain_arguments(), NodeStr) of
Expand Down Expand Up @@ -125,9 +135,22 @@ main(ParseFun, DoFun, UsageMod) ->
rabbit_misc:quit(?EX_SOFTWARE)
end.

start_distribution_anon(0, LastError) ->
{error, LastError};
start_distribution_anon(TriesLeft, _) ->
NameCandidate = list_to_atom(rabbit_misc:format("rabbitmq-cli-~2..0b", [rabbit_misc:random(100)])),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rabbit_misc:random is undefined.
random:uniform can be used in this case, because it just retries in same process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you'll need rabbitmq/rabbitmq-common#48
And also you can't simply run ./scripts/rabbitmqctl from rabbitmq-server checkout - rabbit_common will be missing from code path (but on installed server it will be available through rabbit_common.ez archive).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One can run make package-generic-unix UNOFFICIAL_RELEASE=true from the umbrella to produce a tarball. It should take a couple of minutes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then every sequence of "random" numbers will be the same - and it's exactly the thing that we want to avoid, to reduce contention.

case net_kernel:start([NameCandidate, name_type()]) of
{ok, _} = Result ->
Result;
{error, Reason} ->
start_distribution_anon(TriesLeft - 1, Reason)
end.

%% Tries to start distribution with randonm name choosen from limited list of candidates - to
%% prevent atom table pollution on target nodes.
start_distribution() ->
start_distribution(list_to_atom(
rabbit_misc:format("rabbitmq-cli-~s", [os:getpid()]))).
rabbit_nodes:ensure_epmd(),
start_distribution_anon(10, undefined).

start_distribution(Name) ->
rabbit_nodes:ensure_epmd(),
Expand Down