Skip to content

Commit c07b7ca

Browse files
rabbit_nodes: move a few more functions to rabbit_nodes_common
they are required by or can be useful to CLI tools.
1 parent 5d45fc9 commit c07b7ca

File tree

1 file changed

+7
-143
lines changed

1 file changed

+7
-143
lines changed

src/rabbit_nodes.erl

Lines changed: 7 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
-include_lib("kernel/include/inet.hrl").
2626

27-
-define(EPMD_TIMEOUT, 30000).
28-
-define(TCP_DIAGNOSTIC_TIMEOUT, 5000).
29-
-define(ERROR_LOGGER_HANDLER, rabbit_error_logger_handler).
30-
3127
%%----------------------------------------------------------------------------
3228
%% Specs
3329
%%----------------------------------------------------------------------------
@@ -45,135 +41,10 @@
4541
%%----------------------------------------------------------------------------
4642

4743
names(Hostname) ->
48-
Self = self(),
49-
Ref = make_ref(),
50-
{Pid, MRef} = spawn_monitor(
51-
fun () -> Self ! {Ref, net_adm:names(Hostname)} end),
52-
timer:exit_after(?EPMD_TIMEOUT, Pid, timeout),
53-
receive
54-
{Ref, Names} -> erlang:demonitor(MRef, [flush]),
55-
Names;
56-
{'DOWN', MRef, process, Pid, Reason} -> {error, Reason}
57-
end.
44+
rabbit_nodes_common:names(Hostname).
5845

5946
diagnostics(Nodes) ->
60-
verbose_erlang_distribution(true),
61-
NodeDiags = [{"~nDIAGNOSTICS~n===========~n~n"
62-
"attempted to contact: ~p~n", [Nodes]}] ++
63-
[diagnostics_node(Node) || Node <- Nodes] ++
64-
current_node_details(),
65-
verbose_erlang_distribution(false),
66-
rabbit_misc:format_many(lists:flatten(NodeDiags)).
67-
68-
verbose_erlang_distribution(true) ->
69-
net_kernel:verbose(1),
70-
error_logger:add_report_handler(?ERROR_LOGGER_HANDLER);
71-
verbose_erlang_distribution(false) ->
72-
net_kernel:verbose(0),
73-
error_logger:delete_report_handler(?ERROR_LOGGER_HANDLER).
74-
75-
current_node_details() ->
76-
[{"~ncurrent node details:~n- node name: ~w", [node()]},
77-
case init:get_argument(home) of
78-
{ok, [[Home]]} -> {"- home dir: ~s", [Home]};
79-
Other -> {"- no home dir: ~p", [Other]}
80-
end,
81-
{"- cookie hash: ~s", [cookie_hash()]}].
82-
83-
diagnostics_node(Node) ->
84-
{Name, Host} = parts(Node),
85-
[{"~s:", [Node]} |
86-
case names(Host) of
87-
{error, Reason} ->
88-
[{" * unable to connect to epmd (port ~s) on ~s: ~s~n",
89-
[epmd_port(), Host, rabbit_misc:format_inet_error(Reason)]}];
90-
{ok, NamePorts} ->
91-
[{" * connected to epmd (port ~s) on ~s",
92-
[epmd_port(), Host]}] ++
93-
case net_adm:ping(Node) of
94-
pong -> dist_working_diagnostics(Node);
95-
pang -> dist_broken_diagnostics(Name, Host, NamePorts)
96-
end
97-
end].
98-
99-
epmd_port() ->
100-
case init:get_argument(epmd_port) of
101-
{ok, [[Port | _] | _]} when is_list(Port) -> Port;
102-
error -> "4369"
103-
end.
104-
105-
dist_working_diagnostics(Node) ->
106-
case is_process_running(Node, rabbit) of
107-
true -> [{" * node ~s up, 'rabbit' application running", [Node]}];
108-
false -> [{" * node ~s up, 'rabbit' application not running~n"
109-
" * running applications on ~s: ~p~n"
110-
" * suggestion: start_app on ~s",
111-
[Node, Node, remote_apps(Node), Node]}]
112-
end.
113-
114-
remote_apps(Node) ->
115-
%% We want a timeout here because really, we don't trust the node,
116-
%% the last thing we want to do is hang.
117-
case rpc:call(Node, application, which_applications, [5000]) of
118-
{badrpc, _} = E -> E;
119-
Apps -> [App || {App, _, _} <- Apps]
120-
end.
121-
122-
dist_broken_diagnostics(Name, Host, NamePorts) ->
123-
case [{N, P} || {N, P} <- NamePorts, N =:= Name] of
124-
[] ->
125-
{SelfName, SelfHost} = parts(node()),
126-
Others = [list_to_atom(N) || {N, _} <- NamePorts,
127-
N =/= case SelfHost of
128-
Host -> SelfName;
129-
_ -> never_matches
130-
end],
131-
OthersDiag = case Others of
132-
[] -> [{" no other nodes on ~s",
133-
[Host]}];
134-
_ -> [{" other nodes on ~s: ~p",
135-
[Host, Others]}]
136-
end,
137-
[{" * epmd reports: node '~s' not running at all", [Name]},
138-
OthersDiag, {" * suggestion: start the node", []}];
139-
[{Name, Port}] ->
140-
[{" * epmd reports node '~s' running on port ~b", [Name, Port]} |
141-
case diagnose_connect(Host, Port) of
142-
ok ->
143-
connection_succeeded_diagnostics();
144-
{error, Reason} ->
145-
[{" * can't establish TCP connection, reason: ~s~n"
146-
" * suggestion: blocked by firewall?",
147-
[rabbit_misc:format_inet_error(Reason)]}]
148-
end]
149-
end.
150-
151-
connection_succeeded_diagnostics() ->
152-
case gen_event:call(error_logger, ?ERROR_LOGGER_HANDLER, get_connection_report) of
153-
[] ->
154-
[{" * TCP connection succeeded but Erlang distribution "
155-
"failed~n"
156-
" * suggestion: hostname mismatch?~n"
157-
" * suggestion: is the cookie set correctly?~n"
158-
" * suggestion: is the Erlang distribution using TLS?", []}];
159-
Report ->
160-
[{" * TCP connection succeeded but Erlang distribution "
161-
"failed~n", []}]
162-
++ Report
163-
end.
164-
165-
diagnose_connect(Host, Port) ->
166-
case inet:gethostbyname(Host) of
167-
{ok, #hostent{h_addrtype = Family}} ->
168-
case gen_tcp:connect(Host, Port, [Family],
169-
?TCP_DIAGNOSTIC_TIMEOUT) of
170-
{ok, Socket} -> gen_tcp:close(Socket),
171-
ok;
172-
{error, _} = E -> E
173-
end;
174-
{error, _} = E ->
175-
E
176-
end.
47+
rabbit_nodes_common:diagnostics(Nodes).
17748

17849
make(NodeStr) ->
17950
rabbit_nodes_common:make(NodeStr).
@@ -182,30 +53,23 @@ parts(NodeStr) ->
18253
rabbit_nodes_common:parts(NodeStr).
18354

18455
cookie_hash() ->
185-
base64:encode_to_string(erlang:md5(atom_to_list(erlang:get_cookie()))).
56+
rabbit_nodes_common:cookie_hash().
18657

18758
is_running(Node, Application) ->
188-
case rpc:call(Node, rabbit_misc, which_applications, []) of
189-
{badrpc, _} -> false;
190-
Apps -> proplists:is_defined(Application, Apps)
191-
end.
59+
rabbit_nodes_common:is_running(Node, Application).
19260

19361
is_process_running(Node, Process) ->
194-
case rpc:call(Node, erlang, whereis, [Process]) of
195-
{badrpc, _} -> false;
196-
undefined -> false;
197-
P when is_pid(P) -> true
198-
end.
62+
rabbit_nodes_common:is_process_running(Node, Process).
19963

20064
cluster_name() ->
20165
rabbit_runtime_parameters:value_global(
20266
cluster_name, cluster_name_default()).
20367

20468
cluster_name_default() ->
205-
{ID, _} = rabbit_nodes:parts(node()),
69+
{ID, _} = parts(node()),
20670
{ok, Host} = inet:gethostname(),
20771
{ok, #hostent{h_name = FQDN}} = inet:gethostbyname(Host),
208-
list_to_binary(atom_to_list(rabbit_nodes:make({ID, FQDN}))).
72+
list_to_binary(atom_to_list(make({ID, FQDN}))).
20973

21074
set_cluster_name(Name, Username) ->
21175
%% Cluster name should be binary

0 commit comments

Comments
 (0)