Getting Started with Erlang: Building application Exercise 5 (Supervisory tree)

Source: Internet
Author: User

1, preface in "Erlang Get started: Build application Exercise 4 (Process link role)" In the article,
A question was raised:
What happens if an error occurs inside the system process and exits unexpectedly?
In application, a system process such as Bank_center, if terminated, the entire system will not function properly, in order to let the Bank_center unexpectedly terminate the automatic restart, here will ask Erlang's supervisory tree to help.
Demo Project BANK3 directory structure:



2. Write the supervisory tree callback module Bank_sup.erl The callback module of the supervisory tree has only one function init/1
Percent @author Rolong<[email protected]>-module (bank_sup).-behaviour (Supervisor).-export ([start_link/0]).- Export ([INIT/1]). Percent percent defines a apistart_link (),    Supervisor:start_link ({local), that initiates this oversight tree. MODULE},? MODULE, []). Init ([]), with    a zero start of 2 sub-processes    Bankcenterspec = {Center percent percent      specifies the name of this process (unique in the child process)      , {bank_center,  Start_link, []} percent process start function: {m,f,a}      , transient-percent restart policy: permanent | transient | Temporary      , 5000% off mode: Brutal_kill | int () >0 | Infinity      , worker percent process type: Worker or supervisor      , [Bank_center] percent of the callback module name: [Module] | dynamic     },    BANKCENTERSPEC2 = {      Center2      , {bank_center2, Start_link, []}      , transient      ,,      worker      , [Bank_center2]     }, percent percent    {OK, {{restartstrategy, MaxR, Maxt}, [Childspec]}}    {OK, {{one_for_one, 5, [Bankcenterspec, BANKCENTERSPEC2]}}.

Restart policy:
(1), permanent: will always be restarted after termination
(2), transient: will be restarted after accidental termination
What's the accident?
Is the process exit reason is not one of the following 3 types:
normal | Shutdown | {Shutdown, term}
(3), temporary: will not be restarted after termination

Closing method:
(1), Brutal_kill: Savage Way, direct exit (Pid,kill)
(2), integer greater than 0: first exit (Child,shutdown), if more than the specified number of milliseconds has not received the exit signal {' exit ', PiD, Reason}, then exit (Pid,kill)
(3), Infinity:exit (Child,shutdown), and wait until the exit signal {' exit ', PiD, Reason}

Problem: If the restart policy for a child process is permanent, when this sub-process is abnormal, it will end immediately after startup, and then immediately be restarted by the supervised tree, so what is the best way to proceed?

The Childspec of the return value of INIT/1 {OK, {{restartstrategy, MaxR, Maxt}, [MaxR]}}, Maxt was born to solve this problem.
In the example maxr=5, maxt=30, which represents 30 seconds, allows the total number of reboots to be 5,
If the total number of restarts more than 5 times, the entire monitoring tree process will be terminated, and finally the monitoring tree itself will be terminated, the whole application also stopped running.

The childspec of the INIT/1 return value {OK, {{restartstrategy, MaxR, Maxt}, [Restartstrategy]}} is a restart strategy for the supervisory tree, which has the following 4 types:
(1), One_for_one: This is the most commonly used strategy. When a sub-process terminates, only the child process is restarted
(2), Simple_one_for_one: and One_for_one, just as the child process is the monitoring tree after the start of dynamic increase. In the game, suppose a player is a process, and after each player process is created, the process is added to the supervisory tree, which is simple_one_for_one
(3), One_for_all: Not commonly used, you need to see the documentation
(4), Rest_for_one: Not commonly used, you need to see the documentation

3. The following message processing is added in the Monitoring tree experiment Bank_center/bank_center2:
Handle_info (Error, Money)->%% error Return ... {OK, money};handle_info (force_stop, Money)->{stop, ' Force Stop ', Money};handle_info (Normal_stop, Money)->{stop , Normal, Money};handle_info (shutdown, money)->{stop, shutdown, Money};handle_info ({shutdown,term}, Money)->{ Stop, {shutdown,term}, money};

3.1. Test {restartstrategy, MaxR, Maxt}={one_for_one, 5, 30}
Eshell V5.10.4 (abort with ^g) 1> Appmon:start (). {ok,<0.40.0>}2> Bank_center! NORMAL_STOP.##[&LT;0.38.0&GT;BANK_CENTER:59] terminate:normalnormal_stop##[<0.51.0>bank_center:23] Start Bank_center ... 3> Bank_center! NORMAL_STOP.##[&LT;0.51.0&GT;BANK_CENTER:59] terminate:normalnormal_stop##[<0.53.0>bank_center:23] Start Bank_center ... 4> Bank_center! NORMAL_STOP.##[&LT;0.53.0&GT;BANK_CENTER:59] terminate:normalnormal_stop##[<0.55.0>bank_center:23] Start Bank_center ... 5> Bank_center2! NORMAL_STOP.##[&LT;0.39.0&GT;BANK_CENTER2:59] terminate:normalnormal_stop##[<0.57.0>bank_center2:23] Start Bank_center2 ... 6> Bank_center2! NORMAL_STOP.##[&LT;0.57.0&GT;BANK_CENTER2:59] terminate:normalnormal_stop##[<0.59.0>bank_center2:23] Start Bank_center2 ... 7> Bank_center2! NORMAL_STOP.##[&LT;0.59.0&GT;BANK_CENTER2:59] terminate:normal##[<0.55.0>bank_center:59] Terminate: Shutdownnormal_stop8> ##[<0.34.0>bank_app:16] Stop Bank_app!=info report==== 17-jan-2015::16:19:15 ===application:bankexited:shutdowntype:temporary 


As can be seen from the above, when a supervised process terminates, it is restarted immediately and the other processes are unaffected, which is one_for_one.
In 30 seconds, the Normal_stop message was sent to Bank_center 3 times,
Sent 3 normal_stop messages to Bank_center2,
After sending the NORMAL_STOP message for the sixth time, Application:bank has shutdown

3.2, test transient restart strategy bank_sup:init/1 in the permanent changed to transient
Eshell V5.10.4 (abort with ^g) 1> Bank_center! ERROR.##[&LT;0.38.0&GT;BANK_CENTER:59] Terminate: {bad_return_value,{ok,0}}error##[<0.40.0>bank_center:23] Start Bank_center ... 2> =error report==== 17-jan-2015::17:09:15 ===** Generic Server bank_center terminating * * Last message in was error** When the Server state = = 0** Reason for termination = = * {bad_return_value,{ok,0}}2> bank_center! FORCE_STOP.##[&LT;0.40.0&GT;BANK_CENTER:59] Terminate: "Force stop" =error report==== 17-jan-2015::17:09:28 ===**  Generic Server bank_center Terminating * * Last message in is force_stop** when server state = = 0** Reason for termination = = * * "Force STOP" force_stop##[<0.42.0>bank_center:23] Start bank_center ... 3> 3> Bank_center! NORMAL_STOP.##[&LT;0.42.0&GT;BANK_CENTER:59] terminate:normalnormal_stop4> 4> whereis (bank_center). Undefined5> Supervisor:restart_child (bank_sup, center). ##[<0.46.0>bank_center:23] Start bank_center ... {ok,<0.46.0>}6> Whereis(Bank_center). <0.46.0>7> Bank_center! Shutdown. ##[<0.46.0>bank_center:59] terminate:shutdownshutdown8> Whereis (bank_center). Undefined9> Supervisor:restart_child (bank_sup, center). ##[<0.51.0>bank_center:23] Start bank_center ... {ok,<0.51.0>}10> Whereis (bank_center). <0.51.0>11> Bank_center! {shutdown, test}. ##[<0.51.0>bank_center:59] Terminate: {shutdown,test}{shutdown,test}12> whereis (Bank_center). Undefined

From the visible, the process exits the reason is normal or shutdown or {shutdown, term}, Bank_center will not be restarted, other reason will be restarted.


4. Full Demo code download

Address: http://download.csdn.net/detail/u011471961/8371361

Getting Started with Erlang: Building application Exercise 5 (Supervisory tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.