File: distributed.html

package info (click to toggle)
erlang-doc-html 1%3A11.b.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 23,284 kB
  • ctags: 10,724
  • sloc: erlang: 505; ansic: 323; makefile: 62; perl: 61; sh: 45
file content (401 lines) | stat: -rw-r--r-- 12,730 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
  <TITLE>Distributed Erlang</TITLE>
  <SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
      ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="11"><!-- Empty --></A>
<H2>11 Distributed Erlang</H2>
<A NAME="11.1"><!-- Empty --></A>
<H3>11.1 Distributed Erlang System</H3>

<P>A <STRONG>distributed Erlang system</STRONG> consists of a number of
Erlang runtime systems communicating with each other. Each such
runtime system is called a <STRONG>node</STRONG>. Message passing between
processes at different nodes, as well as links and monitors, are
transparent when pids are used. Registered names, however, are
local to each node. This means the node must be specified as well
when sending messages etc. using registered names.
<P>The distribution mechanism is implemented using TCP/IP sockets.
How to implement an alternative carrier is described in <STRONG>ERTS
        User's Guide</STRONG>.<A NAME="11.2"><!-- Empty --></A>
<H3>11.2 Nodes</H3>

<P>A <STRONG>node</STRONG> is an executing Erlang runtime system which has
been given a name, using the command line flag <CODE>-name</CODE>
(long names) or <CODE>-sname</CODE> (short names).
<P>The format of the node name is an atom <CODE>name@host</CODE> where
<CODE>name</CODE> is the name given by the user and <CODE>host</CODE> is
the full host name if long names are used, or the first part of
the host name if short names are used. <CODE>node()</CODE> returns
the name of the node. Example:
<PRE>
% <STRONG>erl -name dilbert</STRONG>
(dilbert@uab.ericsson.se)1&#62; <STRONG>node().</STRONG>
'dilbert@uab.ericsson.se'

% <STRONG>erl -sname dilbert</STRONG>
(dilbert@uab)1&#62; <STRONG>node().</STRONG>
dilbert@uab
    
</PRE>

<P>
<TABLE CELLPADDING=4>
  <TR>
    <TD VALIGN=TOP><IMG ALT="Note!" SRC="note.gif"></TD>
    <TD>

<P>A node with a long node name cannot communicate with a node
        with a short node name.    </TD>
  </TR>
</TABLE>
<A NAME="11.3"><!-- Empty --></A>
<H3>11.3 Node Connections</H3>

<P>The nodes in a distributed Erlang system are loosely connected.
The first time the name of another node is used, for example if
<CODE>spawn(Node,M,F,A)</CODE> or <CODE>net_adm:ping(Node)</CODE> is called,
a connection attempt to that node will be made.
<P>Connections are by default transitive. If a node A connects to
node B, and node B has a connection to node C, then node A will
also try to connect to node C. This feature can be turned off by
using the command line flag <CODE>-connect_all false</CODE>, see
<CODE>erl(1)</CODE>.
<P>If a node goes down, all connections to that node are removed.
Calling <CODE>erlang:disconnect(Node)</CODE> will force disconnection
of a node.
<P>The list of (visible) nodes currently connected to is returned by
<CODE>nodes()</CODE>.<A NAME="11.4"><!-- Empty --></A>
<H3>11.4 epmd</H3>

<P>The Erlang Port Mapper Daemon <STRONG>epmd</STRONG> is automatically
started at every host where an Erlang node is started. It is
responsible for mapping the symbolic node names to machine
addresses. See <CODE>epmd(1)</CODE>.<A NAME="11.5"><!-- Empty --></A>
<H3>11.5 Hidden Nodes</H3>

<P>In a distributed Erlang system, it is sometimes useful to
connect to a node without also connecting to all other nodes.
An example could be some kind of O&#38;M functionality used to
inspect the status of a system without disturbing it. For this
purpose, a <STRONG>hidden node</STRONG> may be used.
<P>A hidden node is a node started with the command line flag
<CODE>-hidden</CODE>. Connections between hidden nodes and other nodes
are not transitive, they must be set up explicitly. Also, hidden
nodes does not show up in the list of nodes returned by
<CODE>nodes()</CODE>. Instead, <CODE>nodes(hidden)</CODE> or
<CODE>nodes(connected)</CODE> must be used. This means, for example,
that the hidden node will not be added to the set of nodes that
<CODE>global</CODE> is keeping track of.
<P>This feature was added in Erlang 5.0/OTP R7.<A NAME="11.6"><!-- Empty --></A>
<H3>11.6 C Nodes</H3>

<P>A <STRONG>C node</STRONG> is a C program written to act as a hidden node
in a distributed Erlang system. The library <STRONG>Erl_Interface</STRONG>
contains functions for this purpose. Refer to the documentation
for Erl_Interface and <STRONG>Interoperability Tutorial</STRONG> for more
information about C nodes.<A NAME="11.7"><!-- Empty --></A>
<H3>11.7 Security</H3>

<P>Authentication determines which nodes are allowed to communicate
with each other. In a network of different Erlang nodes, it is
built into the system at the lowest possible level. Each node has
its own <STRONG>magic cookie</STRONG>, which is an Erlang atom.
<P>When a nodes tries to connect to another node, the magic cookies
are compared. If they do not match, the connected node rejects
the connection.
<P>At start-up, a node has a random atom assigned as its magic
cookie and the cookie of other nodes is assumed to be
<CODE>nocookie</CODE>. The first action of the Erlang network
authentication server (<CODE>auth</CODE>) is then to read a file named
<CODE>$HOME/.erlang.cookie</CODE>. If the file does not exist, it is
created. The UNIX permissions mode of the file is set to octal
400 (read-only by user) and its contents are a random string. An
atom <CODE>Cookie</CODE> is created from the contents of the file and
the cookie of the local node is set to this using
<CODE>erlang:set_cookie(node(), Cookie)</CODE>. This also makes
the local node assume that all other nodes have the same cookie
<CODE>Cookie</CODE>.
<P>Thus, groups of users with identical cookie files get Erlang
nodes which can communicate freely and without interference from
the magic cookie system. Users who want run nodes on separate
file systems must make certain that their cookie files are
identical on the different file systems.
<P>For a node <CODE>Node1</CODE> with magic cookie <CODE>Cookie</CODE> to be
able to connect to, or accept a connection from, another node
<CODE>Node2</CODE> with a different cookie <CODE>DiffCookie</CODE>,
the function <CODE>erlang:set_cookie(Node2, DiffCookie)</CODE> must
first be called at <CODE>Node1</CODE>. Distributed systems with
multiple user IDs can be handled in this way.
<P>The default when a connection is established between two nodes,
is to immediately connect all other visible nodes as well. This
way, there is always a fully connected network. If there are
nodes with different cookies, this method might be inappropriate
and the command line flag <CODE>-connect_all false</CODE> must be set,
see <A HREF="javascript:erlhref('../../', 'erts', 'erl.html');">erl(1)</A>.
<P>The magic cookie of the local node is retrieved by calling
<CODE>erlang:get_cookie()</CODE>.<A NAME="11.8"><!-- Empty --></A>
<H3>11.8 Distribution BIFs</H3>

<P>Some useful BIFs for distributed programming, see
        <CODE>erlang(3)</CODE> for more information:
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
  <CAPTION ALIGN=BOTTOM><EM>Distribution BIFs.</EM></CAPTION>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>erlang:disconnect_node(Node)</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Forces the disconnection of a node.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>erlang:get_cookie()</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Returns the magic cookie of the current node.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_alive()</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Returns <CODE>true</CODE> if the runtime system is a node and
         can connect to other nodes, <CODE>false</CODE> otherwise.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>monitor_node(Node, true|false)</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Monitor the status of <CODE>Node</CODE>. A message
         <CODE>{nodedown, Node}</CODE> is received if the connection to it
         is lost.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>node()</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Returns the name of the current node. Allowed in guards.
        
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>node(Arg)</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Returns the node where <CODE>Arg</CODE>, a pid, reference, or
         port, is located.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>nodes()</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Returns a list of all visible nodes this node is connected
         to.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>nodes(Arg)</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Depending on <CODE>Arg</CODE>, this function can return a list
         not only of visible nodes, but also hidden nodes and
         previously known nodes, etc.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>set_cookie(Node, Cookie)</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Sets the magic cookie used when connecting to <CODE>Node</CODE>.
         If <CODE>Node</CODE> is the current node, <CODE>Cookie</CODE> will be used
         when connecting to all new nodes.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>spawn[_link|_opt](Node, Fun)</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Creates a process at a remote node.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>spawn[_link|opt](Node, Module, FunctionName, Args)</CODE>
        
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Creates a process at a remote node.
    </TD>

  </TR>

</TABLE>
</CENTER>
<A NAME="11.9"><!-- Empty --></A>
<H3>11.9 Distribution Command Line Flags</H3>

<P>Examples of command line flags used for distributed programming,
see <CODE>erl(1)</CODE> for more information:
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
  <CAPTION ALIGN=BOTTOM><EM>Distribution Command Line Flags.</EM></CAPTION>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-connect_all false</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Only explicit connection set-ups will be used.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-hidden</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Makes a node into a hidden node.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-name Name</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Makes a runtime system into a node, using long node names.
        
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-setcookie Cookie</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Same as calling <CODE>erlang:set_cookie(node(), Cookie)</CODE>.
        
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-sname Name</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Makes a runtime system into a node, using short node
         names.
    </TD>

  </TR>

</TABLE>
</CENTER>
<A NAME="11.10"><!-- Empty --></A>
<H3>11.10 Distribution Modules</H3>

<P>Examples of modules useful for distributed programming:
<P>In Kernel:
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
  <CAPTION ALIGN=BOTTOM><EM>Kernel Modules Useful For Distribution.</EM></CAPTION>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>global</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
A global name registration facility.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>global_group</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Grouping nodes to global name registration groups.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>net_adm</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Various Erlang net administration routines.
    </TD>

  </TR>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>net_kernel</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Erlang networking kernel.
    </TD>

  </TR>

</TABLE>
</CENTER>

<P>In STDLIB:
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
  <CAPTION ALIGN=BOTTOM><EM>STDLIB Modules Useful For Distribution.</EM></CAPTION>
  <TR>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>slave</CODE>
    </TD>
    <TD ALIGN="LEFT" VALIGN="MIDDLE">
Start and control of slave nodes.
    </TD>

  </TR>

</TABLE>
</CENTER>
<CENTER>
<HR>
<SMALL>
Copyright &copy; 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>