From 42a0387e886ddbf60b0e2cb977758e2ca74954ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 12 Mar 2015 15:35:13 +0100 Subject: Update Design Principles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Language cleaned up by the technical writers xsipewe and tmanevik from Combitech. Proofreading and corrections by Björn Gustavsson. --- system/doc/design_principles/sup_princ.xml | 141 +++++++++++++++-------------- 1 file changed, 75 insertions(+), 66 deletions(-) (limited to 'system/doc/design_principles/sup_princ.xml') diff --git a/system/doc/design_principles/sup_princ.xml b/system/doc/design_principles/sup_princ.xml index 3d7b53e339..9583ca5c55 100644 --- a/system/doc/design_principles/sup_princ.xml +++ b/system/doc/design_principles/sup_princ.xml @@ -28,15 +28,16 @@ sup_princ.xml -

This section should be read in conjunction with - supervisor(3), where - all details about the supervisor behaviour are described.

+

This section should be read with the + supervisor(3) manual page + in STDLIB, where all details about the supervisor + behaviour is given.

Supervision Principles

A supervisor is responsible for starting, stopping, and monitoring its child processes. The basic idea of a supervisor is - that it shall keep its child processes alive by restarting them + that it is to keep its child processes alive by restarting them when necessary.

Which child processes to start and monitor is specified by a list of child specifications. @@ -47,8 +48,8 @@

Example

The callback module for a supervisor starting the server from - the gen_server chapter - could look like this:

+ gen_server Behaviour + can look as follows:

-module(ch_sup). @@ -79,6 +80,7 @@ init(_Args) ->
Supervisor Flags +

This is the type definition for the supervisor flags:

strategy(), % optional @@ -136,9 +138,9 @@ SupFlags = #{strategy => Strategy, ...}
rest_for_one -

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 +

If a child process terminates, the rest of the child + processes (that is, 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.

@@ -162,7 +164,7 @@ SupFlags = #{intensity => MaxR, period => MaxT, ...}

If more than MaxR number of restarts occur in the last MaxT seconds, the supervisor terminates all the child processes and then itself.

-

When the supervisor terminates, the next higher level +

When the supervisor terminates, then the next higher-level supervisor takes some action. It either restarts the terminated supervisor or terminates itself.

The intention of the restart mechanism is to prevent a situation @@ -176,14 +178,14 @@ SupFlags = #{intensity => MaxR, period => MaxT, ...}

Child Specification -

This is the type definition for a child specification:

+

The type definition for a child specification is as follows:

child_id(), % mandatory start => mfargs(), % mandatory restart => restart(), % optional shutdown => shutdown(), % optional type => worker(), % optional - modules => modules()} % optional + modules => modules()} % optional child_id() = term() mfargs() = {M :: module(), F :: atom(), A :: [term()]} modules() = [module()] | dynamic @@ -195,7 +197,7 @@ child_spec() = #{id => child_id(), % mandatory

id is used to identify the child specification internally by the supervisor.

The id key is mandatory.

-

Note that this identifier on occations has been called +

Note that this identifier occasionally has been called "name". As far as possible, the terms "identifier" or "id" are now used but in order to keep backwards compatibility, some occurences of "name" can still be found, for example @@ -205,24 +207,28 @@ child_spec() = #{id => child_id(), % mandatory

start defines the function call used to start the child process. It is a module-function-arguments tuple used as apply(M, F, A).

-

It should be (or result in) a call to - supervisor:start_link, gen_server:start_link, - gen_fsm:start_link, or gen_event:start_link. - (Or a function compliant with these functions, see - supervisor(3) for details.

+

It is to be (or result in) a call to any of the following:

+ + supervisor:start_link + gen_server:start_link + gen_fsm:start_link + gen_event:start_link + A function compliant with these functions. For details, + see the supervisor(3) manual page. +

The start key is mandatory.

-

restart defines when a terminated child process shall +

restart defines when a terminated child process is to be restarted.

A permanent child process is always restarted. A temporary child process is never restarted - (not even when the supervisor's restart strategy - is rest_for_one or one_for_all and a sibling's + (not even when the supervisor restart strategy + is rest_for_one or one_for_all and a sibling death causes the temporary process to be terminated). A transient child process is restarted only if it - terminates abnormally, i.e. with another exit reason than + terminates abnormally, that is, with another exit reason than normal, shutdown, or {shutdown,Term}.

The restart key is optional. If it is not given, the @@ -230,27 +236,27 @@ child_spec() = #{id => child_id(), % mandatory -

shutdown defines how a child process shall be +

shutdown defines how a child process is to be terminated.

- brutal_kill means the child process is + brutal_kill means that the child process is unconditionally terminated using exit(Child, kill). - An integer timeout value means that the supervisor tells + An integer time-out value means that the supervisor tells the child process to terminate by calling exit(Child, shutdown) 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 exit(Child, kill). - If the child process is another supervisor, it should be + If the child process is another supervisor, it is to be set to infinity to give the subtree enough time to shut down. It is also allowed to set it to infinity, - if the child process is a worker. + if the child process is a worker. See the warning below:

Be careful when setting the shutdown time to infinity when the child process is a worker. Because, in this situation, the termination of the supervision tree depends on the - child process, it must be implemented in a safe way and its cleanup + child process; it must be implemented in a safe way and its cleanup procedure must always return.

The shutdown key is optional. If it is not given, @@ -266,7 +272,7 @@ child_spec() = #{id => child_id(), % mandatory default value worker will be used.

-

modules should be a list with one element +

modules are to be a list with one element [Module], where Module 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, @@ -279,8 +285,8 @@ child_spec() = #{id => child_id(), % mandatory child's start {M,F,A}.

-

Example: The child specification to start the server ch3 - in the example above looks like:

+

Example: The child specification to start the server + ch3 in the previous example look as follows:

#{id => ch3, start => {ch3, start_link, []}, @@ -301,11 +307,11 @@ child_spec() = #{id => child_id(), % mandatory start => {gen_event, start_link, [{local, error_man}]}, modules => dynamic}

Both server and event manager are registered processes which - can be expected to be accessible at all times, thus they are + can be expected to be always accessible. Thus they are specified to be permanent.

ch3 does not need to do any cleaning up before - termination, thus no shutdown time is needed but - brutal_kill should be sufficient. error_man may + termination. Thus, no shutdown time is needed, but + brutal_kill is sufficient. error_man can need some time for the event handlers to clean up, thus the shutdown time is set to 5000 ms (which is the default value).

@@ -320,19 +326,20 @@ child_spec() = #{id => child_id(), % mandatory
Starting a Supervisor -

In the example above, the supervisor is started by calling +

In the previous example, the supervisor is started by calling ch_sup:start_link():

start_link() -> supervisor:start_link(ch_sup, []). -

ch_sup:start_link calls the function - supervisor:start_link/2. This function spawns and links to - a new process, a supervisor.

+

ch_sup:start_link calls function + supervisor:start_link/2, which spawns and links to a new + process, a supervisor.

The first argument, ch_sup, is the name of - the callback module, that is the module where the init + the callback module, that is, the module where the init callback function is located. - The second argument, [], is a term which is passed as-is to + The second argument, [], is a term that is passed + as is to the callback function init. Here, init does not need any indata and ignores the argument. @@ -351,26 +358,27 @@ init(_Args) -> shutdown => brutal_kill}], {ok, {SupFlags, ChildSpecs}}.

The supervisor then starts all its child processes according to - the given child specifications. In this case there, is one child - process, ch3.

-

Note that supervisor:start_link is synchronous. It does + the child specifications in the start specification. In this case + there is one child process, ch3.

+

supervisor:start_link is synchronous. It does not return until all child processes have been started.

Adding a Child Process -

In addition to the static supervision tree, we can also add - dynamic child processes to an existing supervisor with - the following call:

+

In addition to the static supervision tree, dynamic child + processes can be added to an existing supervisor with the following + call:

supervisor:start_child(Sup, ChildSpec)

Sup is the pid, or name, of the supervisor. - ChildSpec is a child specification.

+ ChildSpec is a + child specification.

Child processes added using start_child/2 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.

+ the same way as the other child processes, with the an important + exception: if a supervisor dies and is recreated, then + all child processes that were dynamically added to the supervisor + are lost.

@@ -393,11 +401,12 @@ supervisor:delete_child(Sup, Id)
- Simple-One-For-One Supervisors + Simplified one_for_one Supervisors

A supervisor with restart strategy simple_one_for_one is - a simplified one_for_one supervisor, where all child processes are - dynamically added instances of the same child specification.

-

Example of a callback module for a simple_one_for_one supervisor:

+ a simplified one_for_one supervisor, where all child + processes are dynamically added instances of the same process.

+

The following is an example of a callback module for a + simple_one_for_one supervisor:

-module(simple_sup). -behaviour(supervisor). @@ -416,12 +425,12 @@ init(_Args) -> start => {call, start_link, []}, shutdown => brutal_kill}], {ok, {SupFlags, ChildSpecs}}. -

When started, the supervisor will not start any child processes. +

When started, the supervisor does not start any child processes. Instead, all child processes are added dynamically by calling:

supervisor:start_child(Sup, List)

Sup is the pid, or name, of the supervisor. - List is an arbitrary list of terms which will be added to + List is an arbitrary list of terms, which are added to the list of arguments specified in the child specification. If the start function is specified as {M, F, A}, the child process is started by calling @@ -429,17 +438,17 @@ supervisor:start_child(Sup, List)

For example, adding a child to simple_sup above:

supervisor:start_child(Pid, [id1]) -

results in the child process being started by calling +

The result is that the child process is started by calling apply(call, start_link, []++[id1]), or actually:

call:start_link(id1) -

A child under a simple_one_for_one supervisor can be terminated - with

+

A child under a simple_one_for_one supervisor can be + terminated with the following:

supervisor:terminate_child(Sup, Pid) -

where Sup is the pid, or name, of the supervisor and +

Sup is the pid, or name, of the supervisor and Pid is the pid of the child.

-

Because a simple_one_for_one supervisor could have many +

Because a simple_one_for_one supervisor can have many children, it shuts them all down asynchronously. This means that the children will do their cleanup in parallel and therefore the order in which they are stopped is not defined.

@@ -447,11 +456,11 @@ supervisor:terminate_child(Sup, Pid)
Stopping -

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 +

Since the supervisor is part of a supervision tree, it is + automatically terminated by its supervisor. When asked to + shut down, it terminates all child processes in reversed start order according to the respective shutdown specifications, and - then terminate itself.

+ then terminates itself.

-- cgit v1.2.3