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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Supervisor Behaviour</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="5"><!-- Empty --></A>
<H2>5 Supervisor Behaviour</H2>
<P>This section should be read in conjunction with
<CODE>supervisor(3)</CODE>, where all details about the supervisor
behaviour is given.<A NAME="5.1"><!-- Empty --></A>
<H3>5.1 Supervision Principles</H3>
<P>A supervisor is responsible for starting, stopping and
monitoring its child processes. The basic idea of a supervisor is
that it should keep its child processes alive by restarting them
when necessary.
<P>Which child processes to start and monitor is specified by a
list of <A HREF="#spec">child specifications</A>.
The child processes are started in the order specified by this
list, and terminated in the reversed order.<A NAME="5.2"><!-- Empty --></A>
<H3>5.2 Example</H3>
<P>The callback module for a supervisor starting the server from
the <A HREF="gen_server.html#ex">gen_server chapter</A>
could look like this:<A NAME="ex"><!-- Empty --></A>
<PRE>
-module(ch_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link(ch_sup, []).
init(_Args) ->
{ok, {{one_for_one, 1, 60},
[{ch3, {ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}]}}.
</PRE>
<P><CODE>one_for_one</CODE> is the <A HREF="#strategy">restart
strategy</A>.
<P>1 and 60 defines the <A HREF="#frequency">maximum restart
frequency</A>.
<P>The tuple <CODE>{ch3, ...}</CODE> is a <A HREF="#spec">child
specification</A>.<A NAME="strategy"><!-- Empty --></A><A NAME="5.3"><!-- Empty --></A>
<H3>5.3 Restart Strategy</H3>
<A NAME="5.3.1"><!-- Empty --></A>
<H4>5.3.1 one_for_one</H4>
<P>If a child process terminates, only that process is restarted.
<P>
<CENTER>
<IMG ALT="sup4" SRC="sup4.gif"><BR>
<EM><A NAME="sup4"><!-- Empty --></A>One_For_One Supervision
</EM>
</CENTER>
<A NAME="5.3.2"><!-- Empty --></A>
<H4>5.3.2 one_for_all</H4>
<P>If a child process terminates, all other child processes are
terminated and then all child processes, including
the terminated one, are restarted.
<P>
<CENTER>
<IMG ALT="sup5" SRC="sup5.gif"><BR>
<EM><A NAME="sup5"><!-- Empty --></A>One_For_All Supervision
</EM>
</CENTER>
<A NAME="5.3.3"><!-- Empty --></A>
<H4>5.3.3 rest_for_one</H4>
<P>If a child process terminates, the 'rest' of the child
processes -- i.e. the child processes after the terminated
process in start order -- are terminated. Then the terminated
child process and the rest of the child processes are restarted.
<A NAME="frequency"><!-- Empty --></A><A NAME="5.4"><!-- Empty --></A>
<H3>5.4 Maximum Restart Frequency</H3>
<P>The supervisors have a built-in mechanism to limit the number of
restarts which can occur in a given time interval. This is
determined by the values of the two parameters <CODE>MaxR</CODE> and
<CODE>MaxT</CODE> in the start specification returned by the callback
function <CODE>init</CODE>:
<PRE>
init(...) ->
{ok, {{RestartStrategy, MaxR, MaxT},
[ChildSpec, ...]}}.
</PRE>
<P>If more than <CODE>MaxR</CODE> number of restarts occur in the last
<CODE>MaxT</CODE> seconds, then the supervisor terminates all the child
processes and then itself.
<P>When the supervisor terminates, then the next higher level
supervisor takes some action. It either restarts the terminated
supervisor, or terminates itself.
<P>The intention of the restart mechanism is to prevent a situation
where a process repeatedly dies for the same reason, only to be
restarted again.<A NAME="spec"><!-- Empty --></A><A NAME="5.5"><!-- Empty --></A>
<H3>5.5 Child Specification</H3>
<P>This is the type definition for a child specification:
<PRE>
{Id, StartFunc, Restart, Shutdown, Type, Modules}
Id = term()
StartFunc = {M, F, A}
M = F = atom()
A = [term()]
Restart = permanent | transient | temporary
Shutdown = brutal_kill | integer() &gt;=0 | infinity
Type = worker | supervisor
Modules = [Module] | dynamic
Module = atom()
</PRE>
<P>
<UL>
<LI>
<CODE>Id</CODE> is a name that is used to identify the child
specification internally by the supervisor.<BR>
</LI>
<LI>
<CODE>StartFunc</CODE> defines the function call used to start
the child process. It is a module-function-arguments tuple
used as <CODE>apply(M, F, A)</CODE>.<BR>
It should be (or result in) a call to
<CODE>supervisor:start_link</CODE>, <CODE>gen_server:start_link</CODE>,
<CODE>gen_fsm:start_link</CODE> or <CODE>gen_event:start_link</CODE>.
(Or a function compliant with these functions, see
<CODE>supervisor(3)</CODE> for details.<BR>
</LI>
<LI>
<CODE>Restart</CODE> defines when a terminated child process should
be restarted.<BR>
<UL>
<LI>
A <CODE>permanent</CODE> child process is always restarted.
</LI>
<LI>
A <CODE>temporary</CODE> child process is never restarted.
</LI>
<LI>
A <CODE>transient</CODE> child process is restarted only if it
terminates abnormally, i.e. with another exit reason than
<CODE>normal</CODE>.
</LI>
</UL>
</LI>
<LI>
<A NAME="shutdown"><!-- Empty --></A>
<CODE>Shutdown</CODE> defines how a child process should be
terminated.<BR>
<UL>
<LI>
<CODE>brutal_kill</CODE> means the child process is
unconditionally terminated using <CODE>exit(Child, kill)</CODE>.
</LI>
<LI>
An integer timeout value means that the supervisor tells
the child process to terminate by calling
<CODE>exit(Child, shutdown)</CODE> and then waits for an exit
signal back. If no exit signal is received within
the specified time, the child process is unconditionally
terminated using <CODE>exit(Child, kill)</CODE>.
</LI>
<LI>
If the child process is another supervisor, it should be
set to <CODE>infinity</CODE> to give the subtree enough time to
shutdown.
</LI>
</UL>
</LI>
<LI>
<CODE>Type</CODE> specifies if the child process is a supervisor or
a worker.<BR>
</LI>
<LI>
<CODE>Modules</CODE> should be a list with one element
<CODE>[Module]</CODE>, where <CODE>Module</CODE> is the name of
the callback module, if the child process is a supervisor,
gen_server or gen_fsm. If the child process is a gen_event,
<CODE>Modules</CODE> should be <CODE>dynamic</CODE>.<BR>
This information is used by the release handler during
upgrades and downgrades, see
<A HREF="release_handling.html">Release Handling</A>.
<BR>
</LI>
</UL>
<P>Example: The child specification to start the server <CODE>ch3</CODE>
in the example above looks like:
<PRE>
{ch3,
{ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}
</PRE>
<P>Example: A child specification to start the event manager from
the chapter about
<A HREF="events.html#mgr">gen_event</A>:
<PRE>
{error_man,
{gen_event, start_link, [{local, error_man}]},
permanent, 5000, worker, dynamic}
</PRE>
<P>Both the server and event manager are registered processes which
can be expected to be accessible at all times, thus they are
specified to be <CODE>permanent</CODE>.
<P><CODE>ch3</CODE> does not need to do any cleaning up before
termination, thus no shutdown time is needed but
<CODE>brutal_kill</CODE> should be sufficient. <CODE>error_man</CODE> may
need some time for the event handlers to clean up, thus
<CODE>Shutdown</CODE> is set to 5000 ms.
<P>Example: A child specification to start another supervisor:
<PRE>
{sup,
{sup, start_link, []},
transient, infinity, supervisor, [sup]}
</PRE>
<A NAME="super_tree"><!-- Empty --></A><A NAME="5.6"><!-- Empty --></A>
<H3>5.6 Starting a Supervisor</H3>
<P>In the example above, the supervisor is started by calling
<CODE>ch_sup:start_link()</CODE>:
<PRE>
start_link() ->
supervisor:start_link(ch_sup, []).
</PRE>
<P><CODE>ch_sup:start_link</CODE> calls the function
<CODE>supervisor:start_link/2</CODE>. This function spawns and links to
a new process, a supervisor.
<P>
<UL>
<LI>
The first argument, <CODE>ch_sup</CODE>, is the name of
the callback module, that is the module where the <CODE>init</CODE>
callback function is located.
</LI>
<LI>
The second argument, [], is a term which is passed as-is to
the callback function <CODE>init</CODE>. Here, <CODE>init</CODE> does not
need any indata and ignores the argument.
</LI>
</UL>
<P>In this case, the supervisor is not registered. Instead its pid
must be used. A name can be specified by calling
<CODE>supervisor:start_link({local, Name}, Module, Args)</CODE> or
<CODE>supervisor:start_link({global, Name}, Module, Args)</CODE>.
<P>The new supervisor process calls the callback function
<CODE>ch_sup:init([])</CODE>. <CODE>init</CODE> is expected to return
<CODE>{ok, StartSpec}</CODE>:
<PRE>
init(_Args) ->
{ok, {{one_for_one, 1, 60},
[{ch3, {ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}]}}.
</PRE>
<P>The supervisor then starts all its child processes according to
the child specifications in the start specification. In this case
there is one child process, <CODE>ch3</CODE>.
<P>Note that <CODE>supervisor:start_link</CODE> is synchronous. It does
not return until all child processes have been started.<A NAME="5.7"><!-- Empty --></A>
<H3>5.7 Adding a Child Process</H3>
<P>In addition to the static supervision tree, we can also add
dynamic child processes to an existing supervisor with
the following call:
<PRE>
supervisor:start_child(Sup, ChildSpec)
</PRE>
<P><CODE>Sup</CODE> is the pid, or name, of the supervisor.
<CODE>ChildSpec</CODE> is a <A HREF="#spec">child
specification</A>.
<P>Child processes added using <CODE>start_child/2</CODE> behave in
the same manner as the other child processes, with the following
important exception: If a supervisor dies and is re-created, then
all child processes which were dynamically added to the supervisor
will be lost.<A NAME="5.8"><!-- Empty --></A>
<H3>5.8 Stopping a Child Process</H3>
<P>Any child process, static or dynamic, can be stopped in
accordance with the shutdown specification:
<PRE>
supervisor:terminate_child(Sup, Id)
</PRE>
<P>The child specification for a stopped child process is deleted
with the following call:
<PRE>
supervisor:delete_child(Sup, Id)
</PRE>
<P><CODE>Sup</CODE> is the pid, or name, of the supervisor.
<CODE>Id</CODE> is the id specified in the <A HREF="#spec">child
specification</A>.
<P>As with dynamically added child processes, the effects of
deleting a static child process is lost if the supervisor itself
restarts.<A NAME="5.9"><!-- Empty --></A>
<H3>5.9 Simple-One-For-One Supervisors</H3>
<P>A supervisor with restart strategy <CODE>simple_one_for_one</CODE> is
a simplified one_for_one supervisor, where all child processes are
dynamically added instances of the same process.
<P>Example of a callback module for a simple_one_for_one supervisor:
<PRE>
-module(simple_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link(simple_sup, []).
init(_Args) ->
{ok, {{simple_one_for_one, 0, 1},
[{call, {call, start_link, []},
temporary, brutal_kill, worker, [call]}]}}.
</PRE>
<P>When started, the supervisor will not start any child processes.
Instead, all child processes are added dynamically by calling:
<PRE>
supervisor:start_child(Sup, List)
</PRE>
<P><CODE>Sup</CODE> is the pid, or name, of the supervisor.
<CODE>List</CODE> is an arbitrary list of terms which will be added to
the list of arguments specified in the child specification. If
the start function is specified as <CODE>{M, F, A}</CODE>, then
the child process is started by calling
<CODE>apply(M, F, A++List)</CODE>.
<P>For example, adding a child to <CODE>simple_sup</CODE> above:
<PRE>
supervisor:start_child(Pid, [id1])
</PRE>
<P>results in the child process being started by calling
<CODE>apply(call, start_link, []++[id1])</CODE>, or actually:
<PRE>
call:start_link(id1)
</PRE>
<A NAME="5.10"><!-- Empty --></A>
<H3>5.10 Stopping</H3>
<P>Since the supervisor is part of a supervision tree, it will
automatically be terminated by its supervisor. When asked to
shutdown, it will terminate all child processes in reversed start
order according to the respective shutdown specifications, and
then terminate itself.<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|