1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
27
<prepared>Martin Björklund</prepared>
29
<date>1997-11-17</date>
32
<module>global</module>
33
<modulesummary>A Global Name Registration Facility</modulesummary>
35
<p>This documentation describes the Global module which consists
36
of the following functionalities:
38
<list type="bulleted">
39
<item>registration of global names;</item>
40
<item>global locks;</item>
41
<item>maintenance of the fully connected network.</item>
43
<p>These services are controlled via the process
44
<c>global_name_server</c> which exists on every node. The global
45
name server is started automatically when a node is started.
46
With the term <em>global</em> is meant over a system consisting
47
of several Erlang nodes.</p>
48
<p>The ability to globally register names is a central concept in
49
the programming of distributed Erlang systems. In this module,
50
the equivalent of the <c>register/2</c> and <c>whereis/1</c>
51
BIFs (for local name registration) are implemented, but for a
52
network of Erlang nodes. A registered name is an alias for a
53
process identifier (pid). The global name server monitors
54
globally registered pids. If a process terminates, the name will
55
also be globally unregistered.</p>
56
<p>The registered names are stored in replica global name tables on
57
every node. There is no central storage point. Thus,
58
the translation of a name to a pid is fast, as it is always done
59
locally. When any action in taken which results in a change to
60
the global name table, all tables on other nodes are automatically
62
<p>Global locks have lock identities and are set on a specific
63
resource. For instance, the specified resource could be a pid.
64
When a global lock is set, access to the locked resource is
65
denied for all other resources other than the lock requester.</p>
66
<p>Both the registration and lock functionalities are atomic. All
67
nodes involved in these actions will have the same view of
69
<p>The global name server also performs the critical task of
70
continuously monitoring changes in node configuration: if a node
71
which runs a globally registered process goes down, the name
72
will be globally unregistered. To this end the global name
73
server subscribes to <c>nodeup</c> and <c>nodedown</c> messages
74
sent from the <c>net_kernel</c> module. Relevant Kernel
75
application variables in this context are <c>net_setuptime</c>,
76
<c>net_ticktime</c>, and <c>dist_auto_connect</c>. See also
77
<seealso marker="kernel_app#net_setuptime">kernel(6)</seealso>.
79
<p>The server will also maintain a fully connected network. For
80
example, if node <c>N1</c> connects to node <c>N2</c> (which is
81
already connected to <c>N3</c>), the global name servers on the
82
nodes <c>N1</c> and <c>N3</c> will make sure that also <c>N1</c>
83
and <c>N3</c> are connected. If this is not desired, the command
84
line flag <c>-connect_all false</c> can be used (see also
85
<seealso marker="erts:erl#connect_all">erl(1)</seealso>). In
86
this case the name registration facility cannot be used, but the
87
lock mechanism will still work.
89
<p>If the global name server fails to connect nodes (<c>N1</c> and
90
<c>N3</c> in the example above) a warning event is sent to the
91
error logger. The presence of such an event does not exclude the
92
possibility that the nodes will later connect--one can for
93
example try the command <c>rpc:call(N1, net_adm, ping, [N2]) in the Erlang shell</c>--but it indicates some kind of problem with
97
<p>If the fully connected network is not set up properly, the
98
first thing to try is to increase the value of
99
<c>net_setuptime</c>.</p>
104
<name>del_lock(Id)</name>
105
<name>del_lock(Id, Nodes) -> void()</name>
106
<fsummary>Delete a lock</fsummary>
108
<v>Id = {ResourceId, LockRequesterId}</v>
109
<v> ResourceId = term()</v>
110
<v> LockRequesterId = term()</v>
111
<v>Nodes = [node()]</v>
114
<p>Deletes the lock <c>Id</c> synchronously.</p>
118
<name>notify_all_name(Name, Pid1, Pid2) -> none</name>
119
<fsummary>Name resolving function that notifies both pids</fsummary>
122
<v>Pid1 = Pid2 = pid()</v>
125
<p>This function can be used as a name resolving function for
126
<c>register_name/3</c> and <c>re_register_name/3</c>. It
127
unregisters both pids, and sends the message
128
<c>{global_name_conflict, Name, OtherPid}</c> to both
133
<name>random_exit_name(Name, Pid1, Pid2) -> Pid1 | Pid2</name>
134
<fsummary>Name resolving function that kills one pid</fsummary>
137
<v>Pid1 = Pid2 = pid()</v>
140
<p>This function can be used as a name resolving function for
141
<c>register_name/3</c> and <c>re_register_name/3</c>. It
142
randomly chooses one of the pids for registration and kills
147
<name>random_notify_name(Name, Pid1, Pid2) -> Pid1 | Pid2</name>
148
<fsummary>Name resolving function that notifies one pid</fsummary>
151
<v>Pid1 = Pid2 = pid()</v>
154
<p>This function can be used as a name resolving function for
155
<c>register_name/3</c> and <c>re_register_name/3</c>. It
156
randomly chooses one of the pids for registration, and sends
157
the message <c>{global_name_conflict, Name}</c> to the other
162
<name>register_name(Name, Pid)</name>
163
<name>register_name(Name, Pid, Resolve) -> yes | no</name>
164
<fsummary>Globally register a name for a pid</fsummary>
168
<v>Resolve = fun() or {Module, Function} where</v>
169
<v> Resolve(Name, Pid, Pid2) -> Pid | Pid2 | none</v>
172
<p>Globally associates the name <c>Name</c> with a pid, that is,
173
Globally notifies all nodes of a new global name in a network
175
<p>When new nodes are added to the network, they are informed
176
of the globally registered names that already exist.
177
The network is also informed of any global names in newly
178
connected nodes. If any name clashes are discovered,
179
the <c>Resolve</c> function is called. Its purpose is to
180
decide which pid is correct. If the function crashes, or
181
returns anything other than one of the pids, the name is
182
unregistered. This function is called once for each name
184
<p>There are three pre-defined resolve functions:
185
<c>random_exit_name/3</c>, <c>random_notify_name/3</c>, and
186
<c>notify_all_name/3</c>. If no <c>Resolve</c> function is
187
defined, <c>random_exit_name</c> is used. This means that one
188
of the two registered processes will be selected as correct
189
while the other is killed.</p>
190
<p>This function is completely synchronous. This means that
191
when this function returns, the name is either registered on
192
all nodes or none.</p>
193
<p>The function returns <c>yes</c> if successful, <c>no</c> if
194
it fails. For example, <c>no</c> is returned if an attempt
195
is made to register an already registered process or to
196
register a process with a name that is already in use.</p>
198
<p>Releases up to and including OTP R10 did not check if
199
the process was already registered. As a consequence the
200
global name table could become inconsistent. The old (buggy)
201
behavior can be chosen by giving the Kernel application
202
variable <c>global_multi_name_action</c> the value
205
<p>If a process with a registered name dies, or the node goes
206
down, the name is unregistered on all nodes.</p>
210
<name>registered_names() -> [Name]</name>
211
<fsummary>All globally registered names</fsummary>
216
<p>Returns a lists of all globally registered names.</p>
220
<name>re_register_name(Name, Pid)</name>
221
<name>re_register_name(Name, Pid, Resolve) -> void()</name>
222
<fsummary>Atomically re-register a name</fsummary>
226
<v>Resolve = fun() or {Module, Function} where</v>
227
<v> Resolve(Name, Pid, Pid2) -> Pid | Pid2 | none</v>
230
<p>Atomically changes the registered name <c>Name</c> on all
231
nodes to refer to <c>Pid</c>.</p>
232
<p>The <c>Resolve</c> function has the same behavior as in
233
<c>register_name/2,3</c>.</p>
237
<name>send(Name, Msg) -> Pid</name>
238
<fsummary>Send a message to a globally registered pid</fsummary>
245
<p>Sends the message <c>Msg</c> to the pid globally registered
247
<p>Failure: If <c>Name</c> is not a globally registered
248
name, the calling function will exit with reason
249
<c>{badarg, {Name, Msg}}</c>.</p>
253
<name>set_lock(Id)</name>
254
<name>set_lock(Id, Nodes)</name>
255
<name>set_lock(Id, Nodes, Retries) -> boolean()</name>
256
<fsummary>Set a lock on the specified nodes</fsummary>
258
<v>Id = {ResourceId, LockRequesterId}</v>
259
<v> ResourceId = term()</v>
260
<v> LockRequesterId = term()</v>
261
<v>Nodes = [node()]</v>
262
<v>Retries = int() >= 0 | infinity</v>
265
<p>Sets a lock on the specified nodes (or on all nodes if none
266
are specified) on <c>ResourceId</c> for
267
<c>LockRequesterId</c>. If a lock already exists on
268
<c>ResourceId</c> for another requester than
269
<c>LockRequesterId</c>, and <c>Retries</c> is not equal to 0,
270
the process sleeps for a while and will try to execute
271
the action later. When <c>Retries</c> attempts have been made,
272
<c>false</c> is returned, otherwise <c>true</c>. If
273
<c>Retries</c> is <c>infinity</c>, <c>true</c> is eventually
274
returned (unless the lock is never released).</p>
275
<p>If no value for <c>Retries</c> is given, <c>infinity</c> is
277
<p>This function is completely synchronous.</p>
278
<p>If a process which holds a lock dies, or the node goes
279
down, the locks held by the process are deleted.</p>
280
<p>The global name server keeps track of all processes sharing
281
the same lock, that is, if two processes set the same lock,
282
both processes must delete the lock.</p>
283
<p>This function does not address the problem of a deadlock. A
284
deadlock can never occur as long as processes only lock one
285
resource at a time. But if some processes try to lock two or
286
more resources, a deadlock may occur. It is up to the
287
application to detect and rectify a deadlock.</p>
289
<p>Some values of <c>ResourceId</c> should be avoided or
290
Erlang/OTP will not work properly. A list of
291
resources to avoid: <c>global</c>, <c>dist_ac</c>,
292
<c>mnesia_table_lock</c>, <c>mnesia_adjust_log_writes</c>,
298
<name>sync() -> void()</name>
299
<fsummary>Synchronize the global name server</fsummary>
301
<p>Synchronizes the global name server with all nodes known to
302
this node. These are the nodes which are returned from
303
<c>erlang:nodes()</c>. When this function returns,
304
the global name server will receive global information from
305
all nodes. This function can be called when new nodes are
306
added to the network.</p>
310
<name>trans(Id, Fun)</name>
311
<name>trans(Id, Fun, Nodes)</name>
312
<name>trans(Id, Fun, Nodes, Retries) -> Res | aborted</name>
313
<fsummary>Micro transaction facility</fsummary>
315
<v>Id = {ResourceId, LockRequesterId}</v>
316
<v> ResourceId = term()</v>
317
<v> LockRequesterId = term()</v>
318
<v>Fun = fun() | {M, F}</v>
319
<v>Nodes = [node()]</v>
320
<v>Retries = int() >= 0 | infinity</v>
324
<p>Sets a lock on <c>Id</c> (using <c>set_lock/3</c>). If this
325
succeeds, <c>Fun()</c> is evaluated and the result <c>Res</c>
326
is returned. Returns <c>aborted</c> if the lock attempt
327
failed. If <c>Retries</c> is set to <c>infinity</c>,
328
the transaction will not abort.</p>
329
<p><c>infinity</c> is the default setting and will be used if
330
no value is given for <c>Retries</c>.</p>
334
<name>unregister_name(Name) -> void()</name>
335
<fsummary>Remove a globally registered name for a pid</fsummary>
340
<p>Removes the globally registered name <c>Name</c> from
341
the network of Erlang nodes.</p>
345
<name>whereis_name(Name) -> pid() | undefined</name>
346
<fsummary>Get the pid with a given globally registered name</fsummary>
351
<p>Returns the pid with the globally registered name
352
<c>Name</c>. Returns <c>undefined</c> if the name is not
353
globally registered.</p>
359
<title>See Also</title>
360
<p><seealso marker="global_group">global_group(3)</seealso>,
361
<seealso marker="net_kernel">net_kernel(3)</seealso>,</p>