Distributed Erlang
A node is one running Erlang VM. Without a name it is a closed world: it runs, it answers, and it cannot reach a process on another machine. With a name it joins a cluster where the same send and receive work across the network, and a pid on another node is a value the program can keep. This lesson starts at the closed world and adds the name.
A node without a name
A VM started with a plain erl has no name and no distribution. It is a complete Erlang system for one machine: processes, ETS and the standard library all work, and every call that needs a peer answers a value instead of raising. The program asks what such a node can say about the world:
nonode.erl
-module(nonode). -export([main/0]). main() -> io:format("node: ~p~n", [node()]), io:format("nodes: ~p~n", [nodes()]), io:format("ping: ~p~n", [net_adm:ping('nobody@127.0.0.1')]), io:format("rpc: ~p~n", [rpc:call('nobody@127.0.0.1', erlang, node, [])]), io:format("cookie: ~p~n", [is_atom(erlang:get_cookie())]), io:format("connect: ~p~n", [net_kernel:connect_node('nobody@127.0.0.1')]).
erlc nonode.erl
erl -noshell -eval 'nonode:main(), halt().'
node: nonode@nohost nodes: [] ping: pang rpc: {badrpc,nodedown} cookie: true connect: ignored
The atom nonode@nohost is the name the runtime gives an unnamed node, and pang is the normal answer of a node that cannot be reached, not an exception. net_kernel:connect_node/1 answers ignored on a node that never started distribution, which is a third answer a caller has to know: true, false and ignored. The cookie exists even here, because the runtime reads it at boot whether or not it is used.
A second node
The name comes from the command line. -sname takes a short name and builds name@host from it, while -name takes a long name whose host contains a dot; two nodes on one machine must differ in the name part. The program below starts the second node itself with the peer module, which is the modern replacement for the deprecated slave module, and stops it at the end:
peer_demo.erl
-module(peer_demo). -export([run/0]). run() -> logger:set_primary_config(level, none), ok = net_kernel:monitor_nodes(true), {ok, Peer, Node} = peer:start_link(#{name => peer_one}), io:format("ping: ~p~n", [net_adm:ping(Node)]), io:format("nodeup: ~p~n", [nodeup(Node)]), io:format("remote call: ~p~n", [rpc:call(Node, erlang, node, []) =:= Node]), io:format("visible: ~p~n", [lists:member(Node, nodes())]), {ok, Hidden, HiddenNode} = peer:start_link(#{name => peer_two, args => ["-hidden"]}), io:format("hidden ping: ~p~n", [net_adm:ping(HiddenNode)]), io:format("hidden in nodes(): ~p~n", [lists:member(HiddenNode, nodes())]), io:format("hidden in nodes(hidden): ~p~n", [lists:member(HiddenNode, nodes(hidden))]), {Ok, Bad} = rpc:multicall([Node, HiddenNode], erlang, node, []), io:format("multicall: ~p~n", [{length(Ok), length(Bad)}]), peer:stop(Hidden), peer:stop(Peer). nodeup(Node) -> receive {nodeup, Node} -> nodeup after 1000 -> timeout end.
erlc peer_demo.erl
erl -sname short_node -noshell -eval 'peer_demo:run(), halt().'
ping: pong nodeup: nodeup remote call: true visible: true hidden ping: pong hidden in nodes(): false hidden in nodes(hidden): true multicall: {2,0}
The node name never appears in the output the page shows, because it would include the host name of the machine that ran it; the booleans carry the proof instead. The first line connects, and the second receives the {nodeup, Node} message that net_kernel:monitor_nodes/1 asked for, so a supervisor can react to a node as it can to a process. A connection is symmetric: once one side pings, both see each other in nodes/0.
Calls across nodes
rpc:call/4 runs a function on another node and answers its value, or {badrpc, Reason} when the call failed. rpc:multicall/4 takes a list of nodes and answers the values that succeeded together with the nodes that did not, which is the shape of a fan-out: three of five nodes answered, and the caller decides what to do about the other two. A spawn(Node, Fun) starts a process on the other node, and the code of the fun must be loadable there, which is the job of a release or of the same code path on every machine.
A call through rpc is synchronous and copies the arguments and the answer between the nodes, so a large term costs a serialisation on both sides. For high-volume traffic, keep the work on the node that owns the data and send a small request, which is what a message protocol does with a registered name on the other side.
Cookies and hidden nodes
Two nodes connect only when they share a cookie: a secret read from ~/.erlang.cookie or given with -setcookie. The cookie is not a password and it is not encrypted: anyone who can read the file is inside the cluster, so a cluster is one trust domain. OTP 25 added distribution over TLS, which authenticates and encrypts the link, and that is the option for a cluster that crosses a network the operator does not control.
A node started with -hidden connects without joining the visible set: nodes/0 does not list it, and nodes(hidden) does. The program above uses that to show both views, and it is how a monitoring or management tool attaches to a cluster without becoming part of the work the application distributes. Distribution is off by default for exactly this reason: a node that does not need it cannot be reached.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
node()answer on a VM started without a name?Answer
nonode@nohostAn unnamed node is
nonode@nohostand cannot connect to other nodes. -
Which flag gives a node a short name?
Answer
-sname-snamebuildsname@hostfrom the short host name, and-nametakes the long form. -
What does
net_adm:ping/1answer when the peer is not running?Answer
pangpang is the normal answer for a node that cannot be reached.
-
What does
nodes()list?Answer
The visible nodes this node is connected to
It lists the visible connected nodes; hidden ones are asked for with
nodes(hidden). -
What must two nodes share before they can connect?
Answer
The same cookie
The cookie authenticates the connection, and a mismatch is refused.
-
What does
rpc:call/4answer when the node is down?Answer
{badrpc, nodedown}The failed call is reported as
{badrpc, Reason}withnodedownas the reason. -
What does this module print?
-module(q1). -export([main/0]). main() -> io:format("~p~n", [node()]).Answer
nonode@nohostA VM without a name is
nonode@nohost, and a plainerl -noshellrun has no name. -
What does this module print?
-module(q2). -export([main/0]). main() -> io:format("~p~n", [rpc:call('nobody@127.0.0.1', erlang, node, [])]).Answer
{badrpc,nodedown}The local node has no distribution, so the remote call fails with
nodedown. -
What does this module print?
-module(q3). -export([main/0]). main() -> io:format("~p~n", [net_kernel:connect_node('other@127.0.0.1')]), io:format("~p~n", [nodes()]).Answer
ignored/[]Distribution was never started, so the call is ignored instead of failing, and no node is connected.
-
What does this module print?
-module(q4). -export([main/0]). main() -> io:format("~p~n", [is_atom(erlang:get_cookie())]).Answer
trueThe cookie is read from the cookie file at boot and is an atom even when distribution is off.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container, with the second node on the same machine.
Автор статьи: Arthur Isaev