~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to system/doc/design_principles/sup_princ.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2010-03-09 17:34:57 UTC
  • mfrom: (10.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100309173457-4yd6hlcb2osfhx31
Tags: 1:13.b.4-dfsg-3
Manpages in section 1 are needed even if only arch-dependent packages are
built. So, re-enabled them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
 
3
 
 
4
<chapter>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>1997</year><year>2009</year>
 
8
      <holder>Ericsson AB. All Rights Reserved.</holder>
 
9
    </copyright>
 
10
    <legalnotice>
 
11
      The contents of this file are subject to the Erlang Public License,
 
12
      Version 1.1, (the "License"); you may not use this file except in
 
13
      compliance with the License. You should have received a copy of the
 
14
      Erlang Public License along with this software. If not, it can be
 
15
      retrieved online at http://www.erlang.org/.
 
16
    
 
17
      Software distributed under the License is distributed on an "AS IS"
 
18
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
19
      the License for the specific language governing rights and limitations
 
20
      under the License.
 
21
    
 
22
    </legalnotice>
 
23
 
 
24
    <title>Supervisor Behaviour</title>
 
25
    <prepared></prepared>
 
26
    <docno></docno>
 
27
    <date></date>
 
28
    <rev></rev>
 
29
    <file>sup_princ.xml</file>
 
30
  </header>
 
31
  <p>This section should be read in conjunction with
 
32
    <c>supervisor(3)</c>, where all details about the supervisor
 
33
    behaviour is given.</p>
 
34
 
 
35
  <section>
 
36
    <title>Supervision Principles</title>
 
37
    <p>A supervisor is responsible for starting, stopping and
 
38
      monitoring its child processes. The basic idea of a supervisor is
 
39
      that it should keep its child processes alive by restarting them
 
40
      when necessary.</p>
 
41
    <p>Which child processes to start and monitor is specified by a
 
42
      list of <seealso marker="#spec">child specifications</seealso>.
 
43
      The child processes are started in the order specified by this
 
44
      list, and terminated in the reversed order.</p>
 
45
  </section>
 
46
 
 
47
  <section>
 
48
    <title>Example</title>
 
49
    <p>The callback module for a supervisor starting the server from
 
50
      the <seealso marker="gen_server_concepts#ex">gen_server chapter</seealso>
 
51
      could look like this:</p>
 
52
    <marker id="ex"></marker>
 
53
    <code type="none">
 
54
-module(ch_sup).
 
55
-behaviour(supervisor).
 
56
 
 
57
-export([start_link/0]).
 
58
-export([init/1]).
 
59
 
 
60
start_link() ->
 
61
    supervisor:start_link(ch_sup, []).
 
62
 
 
63
init(_Args) ->
 
64
    {ok, {{one_for_one, 1, 60},
 
65
          [{ch3, {ch3, start_link, []},
 
66
            permanent, brutal_kill, worker, [ch3]}]}}.</code>
 
67
    <p><c>one_for_one</c> is the <seealso marker="#strategy">restart strategy</seealso>.</p>
 
68
    <p>1 and 60 defines the <seealso marker="#frequency">maximum restart frequency</seealso>.</p>
 
69
    <p>The tuple <c>{ch3, ...}</c> is a <seealso marker="#spec">child specification</seealso>.</p>
 
70
  </section>
 
71
 
 
72
  <section>
 
73
    <marker id="strategy"></marker>
 
74
    <title>Restart Strategy</title>
 
75
 
 
76
    <section>
 
77
      <title>one_for_one</title>
 
78
      <p>If a child process terminates, only that process is restarted.</p>
 
79
      <marker id="sup4"></marker>
 
80
      <image file="../design_principles/sup4.gif">
 
81
        <icaption>One_For_One Supervision</icaption>
 
82
      </image>
 
83
    </section>
 
84
 
 
85
    <section>
 
86
      <title>one_for_all</title>
 
87
      <p>If a child process terminates, all other child processes are
 
88
        terminated and then all child processes, including
 
89
        the terminated one, are restarted.</p>
 
90
      <marker id="sup5"></marker>
 
91
      <image file="../design_principles/sup5.gif">
 
92
        <icaption>One_For_All Supervision</icaption>
 
93
      </image>
 
94
    </section>
 
95
 
 
96
    <section>
 
97
      <title>rest_for_one</title>
 
98
      <p>If a child process terminates, the 'rest' of the child
 
99
        processes -- i.e. the child processes after the terminated
 
100
        process in start order -- are terminated. Then the terminated
 
101
        child process and the rest of the child processes are restarted.</p>
 
102
    </section>
 
103
  </section>
 
104
 
 
105
  <section>
 
106
    <marker id="frequency"></marker>
 
107
    <title>Maximum Restart Frequency</title>
 
108
    <p>The supervisors have a built-in mechanism to limit the number of
 
109
      restarts which can occur in a given time interval. This is
 
110
      determined by the values of the two parameters <c>MaxR</c> and
 
111
      <c>MaxT</c> in the start specification returned by the callback
 
112
      function <c>init</c>:</p>
 
113
    <code type="none">
 
114
init(...) ->
 
115
    {ok, {{RestartStrategy, MaxR, MaxT},
 
116
          [ChildSpec, ...]}}.</code>
 
117
    <p>If more than <c>MaxR</c> number of restarts occur in the last
 
118
      <c>MaxT</c> seconds, then the supervisor terminates all the child
 
119
      processes and then itself.</p>
 
120
    <p>When the supervisor terminates, then the next higher level
 
121
      supervisor takes some action. It either restarts the terminated
 
122
      supervisor, or terminates itself.</p>
 
123
    <p>The intention of the restart mechanism is to prevent a situation
 
124
      where a process repeatedly dies for the same reason, only to be
 
125
      restarted again.</p>
 
126
  </section>
 
127
 
 
128
  <section>
 
129
    <marker id="spec"></marker>
 
130
    <title>Child Specification</title>
 
131
    <p>This is the type definition for a child specification:</p>
 
132
    <code type="none"><![CDATA[
 
133
{Id, StartFunc, Restart, Shutdown, Type, Modules}
 
134
    Id = term()
 
135
    StartFunc = {M, F, A}
 
136
        M = F = atom()
 
137
        A = [term()]
 
138
    Restart = permanent | transient | temporary
 
139
    Shutdown = brutal_kill | integer() &gt;=0 | infinity
 
140
    Type = worker | supervisor
 
141
    Modules = [Module] | dynamic
 
142
        Module = atom()]]></code>
 
143
    <list type="bulleted">
 
144
      <item>
 
145
        <p><c>Id</c> is a name that is used to identify the child
 
146
          specification internally by the supervisor.</p>
 
147
      </item>
 
148
      <item>
 
149
        <p><c>StartFunc</c> defines the function call used to start
 
150
          the child process. It is a module-function-arguments tuple
 
151
          used as <c>apply(M, F, A)</c>.</p>
 
152
        <p>It should be (or result in) a call to
 
153
          <c>supervisor:start_link</c>, <c>gen_server:start_link</c>,
 
154
          <c>gen_fsm:start_link</c> or <c>gen_event:start_link</c>.
 
155
          (Or a function compliant with these functions, see
 
156
          <c>supervisor(3)</c> for details.</p>
 
157
      </item>
 
158
      <item>
 
159
        <p><c>Restart</c> defines when a terminated child process should
 
160
          be restarted.</p>
 
161
        <list type="bulleted">
 
162
          <item>A <c>permanent</c> child process is always restarted.</item>
 
163
          <item>A <c>temporary</c> child process is never restarted.</item>
 
164
          <item>A <c>transient</c> child process is restarted only if it
 
165
           terminates abnormally, i.e. with another exit reason than
 
166
          <c>normal</c>.</item>
 
167
        </list>
 
168
      </item>
 
169
      <item>
 
170
        <marker id="shutdown"></marker>
 
171
        <p><c>Shutdown</c> defines how a child process should be
 
172
          terminated.</p>
 
173
        <list type="bulleted">
 
174
          <item><c>brutal_kill</c> means the child process is
 
175
           unconditionally terminated using <c>exit(Child, kill)</c>.</item>
 
176
          <item>An integer timeout value means that the supervisor tells
 
177
           the child process to terminate by calling
 
178
          <c>exit(Child, shutdown)</c> and then waits for an exit
 
179
           signal back. If no exit signal is received within
 
180
           the specified time, the child process is unconditionally
 
181
           terminated using <c>exit(Child, kill)</c>.</item>
 
182
          <item>If the child process is another supervisor, it should be
 
183
           set to <c>infinity</c> to give the subtree enough time to
 
184
           shutdown.</item>
 
185
        </list>
 
186
      </item>
 
187
      <item>
 
188
        <p><c>Type</c> specifies if the child process is a supervisor or
 
189
          a worker.</p>
 
190
      </item>
 
191
      <item>
 
192
        <p><c>Modules</c> should be a list with one element
 
193
          <c>[Module]</c>, where <c>Module</c> is the name of
 
194
          the callback module, if the child process is a supervisor,
 
195
          gen_server or gen_fsm. If the child process is a gen_event,
 
196
          <c>Modules</c> should be <c>dynamic</c>.</p>
 
197
        <p>This information is used by the release handler during
 
198
          upgrades and downgrades, see
 
199
          <seealso marker="release_handling">Release Handling</seealso>.</p>
 
200
      </item>
 
201
    </list>
 
202
    <p>Example: The child specification to start the server <c>ch3</c>
 
203
      in the example above looks like:</p>
 
204
    <code type="none">
 
205
{ch3,
 
206
 {ch3, start_link, []},
 
207
 permanent, brutal_kill, worker, [ch3]}</code>
 
208
    <p>Example: A child specification to start the event manager from
 
209
      the chapter about
 
210
      <seealso marker="events#mgr">gen_event</seealso>:</p>
 
211
    <code type="none">
 
212
{error_man,
 
213
 {gen_event, start_link, [{local, error_man}]},
 
214
 permanent, 5000, worker, dynamic}</code>
 
215
    <p>Both the server and event manager are registered processes which
 
216
      can be expected to be accessible at all times, thus they are
 
217
      specified to be <c>permanent</c>.</p>
 
218
    <p><c>ch3</c> does not need to do any cleaning up before
 
219
      termination, thus no shutdown time is needed but
 
220
      <c>brutal_kill</c> should be sufficient. <c>error_man</c> may
 
221
      need some time for the event handlers to clean up, thus
 
222
      <c>Shutdown</c> is set to 5000 ms.</p>
 
223
    <p>Example: A child specification to start another supervisor:</p>
 
224
    <code type="none">
 
225
{sup,
 
226
 {sup, start_link, []},
 
227
 transient, infinity, supervisor, [sup]}</code>
 
228
  </section>
 
229
 
 
230
  <section>
 
231
    <marker id="super_tree"></marker>
 
232
    <title>Starting a Supervisor</title>
 
233
    <p>In the example above, the supervisor is started by calling
 
234
      <c>ch_sup:start_link()</c>:</p>
 
235
    <code type="none">
 
236
start_link() ->
 
237
    supervisor:start_link(ch_sup, []).</code>
 
238
    <p><c>ch_sup:start_link</c> calls the function
 
239
      <c>supervisor:start_link/2</c>. This function spawns and links to
 
240
      a new process, a supervisor.</p>
 
241
    <list type="bulleted">
 
242
      <item>The first argument, <c>ch_sup</c>, is the name of
 
243
       the callback module, that is the module where the <c>init</c>
 
244
       callback function is located.</item>
 
245
      <item>The second argument, [], is a term which is passed as-is to
 
246
       the callback function <c>init</c>. Here, <c>init</c> does not
 
247
       need any indata and ignores the argument.</item>
 
248
    </list>
 
249
    <p>In this case, the supervisor is not registered. Instead its pid
 
250
      must be used. A name can be specified by calling
 
251
      <c>supervisor:start_link({local, Name}, Module, Args)</c> or
 
252
      <c>supervisor:start_link({global, Name}, Module, Args)</c>.</p>
 
253
    <p>The new supervisor process calls the callback function
 
254
      <c>ch_sup:init([])</c>. <c>init</c> is expected to return
 
255
      <c>{ok, StartSpec}</c>:</p>
 
256
    <code type="none">
 
257
init(_Args) ->
 
258
    {ok, {{one_for_one, 1, 60},
 
259
          [{ch3, {ch3, start_link, []},
 
260
            permanent, brutal_kill, worker, [ch3]}]}}.</code>
 
261
    <p>The supervisor then starts all its child processes according to
 
262
      the child specifications in the start specification. In this case
 
263
      there is one child process, <c>ch3</c>.</p>
 
264
    <p>Note that <c>supervisor:start_link</c> is synchronous. It does
 
265
      not return until all child processes have been started.</p>
 
266
  </section>
 
267
 
 
268
  <section>
 
269
    <title>Adding a Child Process</title>
 
270
    <p>In addition to the static supervision tree, we can also add
 
271
      dynamic child processes to an existing supervisor with
 
272
      the following call:</p>
 
273
    <code type="none">
 
274
supervisor:start_child(Sup, ChildSpec)</code>
 
275
    <p><c>Sup</c> is the pid, or name, of the supervisor.
 
276
      <c>ChildSpec</c> is a <seealso marker="#spec">child specification</seealso>.</p>
 
277
    <p>Child processes added using <c>start_child/2</c> behave in
 
278
      the same manner as the other child processes, with the following
 
279
      important exception: If a supervisor dies and is re-created, then
 
280
      all child processes which were dynamically added to the supervisor
 
281
      will be lost.</p>
 
282
  </section>
 
283
 
 
284
  <section>
 
285
    <title>Stopping a Child Process</title>
 
286
    <p>Any child process, static or dynamic, can be stopped in
 
287
      accordance with the shutdown specification:</p>
 
288
    <code type="none">
 
289
supervisor:terminate_child(Sup, Id)</code>
 
290
    <p>The child specification for a stopped child process is deleted
 
291
      with the following call:</p>
 
292
    <code type="none">
 
293
supervisor:delete_child(Sup, Id)</code>
 
294
    <p><c>Sup</c> is the pid, or name, of the supervisor.
 
295
      <c>Id</c> is the id specified in the <seealso marker="#spec">child specification</seealso>.</p>
 
296
    <p>As with dynamically added child processes, the effects of
 
297
      deleting a static child process is lost if the supervisor itself
 
298
      restarts.</p>
 
299
  </section>
 
300
 
 
301
  <section>
 
302
    <title>Simple-One-For-One Supervisors</title>
 
303
    <p>A supervisor with restart strategy <c>simple_one_for_one</c> is
 
304
      a simplified one_for_one supervisor, where all child processes are
 
305
      dynamically added instances of the same process.</p>
 
306
    <p>Example of a callback module for a simple_one_for_one supervisor:</p>
 
307
    <code type="none">
 
308
-module(simple_sup).
 
309
-behaviour(supervisor).
 
310
 
 
311
-export([start_link/0]).
 
312
-export([init/1]).
 
313
 
 
314
start_link() ->
 
315
    supervisor:start_link(simple_sup, []).
 
316
 
 
317
init(_Args) ->
 
318
    {ok, {{simple_one_for_one, 0, 1},
 
319
          [{call, {call, start_link, []},
 
320
            temporary, brutal_kill, worker, [call]}]}}.</code>
 
321
    <p>When started, the supervisor will not start any child processes.
 
322
      Instead, all child processes are added dynamically by calling:</p>
 
323
    <code type="none">
 
324
supervisor:start_child(Sup, List)</code>
 
325
    <p><c>Sup</c> is the pid, or name, of the supervisor.
 
326
      <c>List</c> is an arbitrary list of terms which will be added to
 
327
      the list of arguments specified in the child specification. If
 
328
      the start function is specified as <c>{M, F, A}</c>, then
 
329
      the child process is started by calling
 
330
      <c>apply(M, F, A++List)</c>.</p>
 
331
    <p>For example, adding a child to <c>simple_sup</c> above:</p>
 
332
    <code type="none">
 
333
supervisor:start_child(Pid, [id1])</code>
 
334
    <p>results in the child process being started by calling
 
335
      <c>apply(call, start_link, []++[id1])</c>, or actually:</p>
 
336
    <code type="none">
 
337
call:start_link(id1)</code>
 
338
  </section>
 
339
 
 
340
  <section>
 
341
    <title>Stopping</title>
 
342
    <p>Since the supervisor is part of a supervision tree, it will
 
343
      automatically be terminated by its supervisor. When asked to
 
344
      shutdown, it will terminate all child processes in reversed start
 
345
      order according to the respective shutdown specifications, and
 
346
      then terminate itself.</p>
 
347
  </section>
 
348
</chapter>
 
349