- 浏览: 576823 次
- 性别:
- 来自: 广州杭州
文章分类
最新评论
-
bohc:
谢谢,搞了两天了,现在才算是找到问题所在,解决了。
文件在使用FileChannel.map后不能被删除(Windows上) -
zhang0000jun:
在jdk1.8中执行正好和楼主的结果相反,请指教
从Java视角理解CPU缓存(CPU Cache) -
在世界的中心呼喚愛:
forenroll 写道请问楼主的那个分析工具cachemis ...
从Java视角理解CPU缓存(CPU Cache) -
xgj1988:
我这里打出的结果是: 0 L1-dcache-load-mis ...
从Java视角理解CPU缓存(CPU Cache) -
thebye85:
请教下大神,为什么频繁的park会导致大量context sw ...
从Java视角理解CPU上下文切换(Context Switch)
5 Supervisor Behaviour
This section should be read in conjunction with supervisor(3)
, where all details about the supervisor behaviour is given.
supervisor(3)结合来读,所有关于supervisor行为的细节都在那儿。
5.1 Supervision Principles
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.
supervisor的职责是启动,停止和监视它的子进程。最简单的supervisor就是必要时通过重启它的子进程,来保证它们一直活着.
Which child processes to start and monitor is specified by a list of child specifications. The child processes are started in the order specified by this list, and terminated in the reversed order.
重启和监视哪些子进程,由子进程说明列表来定义。子进程按列表顺序来启动,而终止时是相反的顺序。5.2 Example
The callback module for a supervisor starting the server from the gen_server chapter could look like this:
java 代码
- -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]}]}}.
one_for_one
is the restart strategy.
one_for_one是重启策略.
1 and 60 defines the maximum restart frequency.
1和60定义了最大重启频率
The tuple {ch3, ...}
is a child specification.
元组 {ch3, ...}
是一个子进程说明.
5.3 Restart Strategy
5.3 重启策略
5.3.1 one_for_one
If a child process terminates, only that process is restarted.
如果一个子进程终止了, 只重启此子进程自身。
One_For_One Supervision
5.3.2 one_for_all
If a child process terminates, all other child processes are terminated and then all child processes, including the terminated one, are restarted.
如果一个子进程终止了,其它子进程全部被终止。然后所有子进程,包括终止了的那个,全部重启。
One_For_All Supervision
5.3.3 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 child process and the rest of the child processes are restarted.
5.4 Maximum Restart Frequency
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 MaxR
and MaxT
in the start specification returned by the callback function init
:
java 代码init(...) ->
- init(...) ->
- {ok, {{RestartStrategy, MaxR, MaxT},
- [ChildSpec, ...]}}.
{ok, {{RestartStrategy, MaxR, MaxT},
[ChildSpec, ...]}}.
If more than MaxR
number of restarts occur in the last MaxT
seconds, then the supervisor terminates all the child processes and then itself.
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 where a process repeatedly dies for the same reason, only to be restarted again.
5.5 Child Specification
This is the type definition for a child specification:
{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() >=0 | infinity
Type = worker | supervisor
Modules = [Module] | dynamic
Module = atom()
-
Id
is a name that is used to identify the child specification internally by the supervisor.
Id
是一个被supervisor用来区分子进程的名称。
-
StartFunc
defines the function call used to start the child process. It is a module-function-arguments tuple used asapply(M, F, A)
.
It should be (or result in) a call tosupervisor:start_link
,gen_server:start_link
,gen_fsm:start_link
orgen_event:start_link
. (Or a function compliant with these functions, seesupervisor(3)
for details.
-
Restart
defines when a terminated child process should be restarted.
- A
permanent
child process is always restarted. - A
temporary
child process is never restarted. - A
transient
child process is restarted only if it terminates abnormally, i.e. with another exit reason thannormal
.
- A
-
Shutdown
defines how a child process should be terminated.
-
brutal_kill
means the child process is unconditionally terminated usingexit(Child, kill)
. - An integer timeout 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 usingexit(Child, kill)
. - If the child process is another supervisor, it should be set to
infinity
to give the subtree enough time to shutdown.
如子进程是另外一个管理者,应该把它设为infinity
,让它的子树有足够的时间来关闭。
-
-
Type
specifies if the child process is a supervisor or a worker.
-
Modules
should be a list with one element[Module]
, whereModule
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,Modules
should bedynamic
.
This information is used by the release handler during upgrades and downgrades, see Release Handling.
Example: The child specification to start the server ch3
in the example above looks like:
{ch3,
{ch3, start_link, []},
permanent, brutal_kill, worker, [ch3]}
Example: A child specification to start the event manager from the chapter about gen_event:
{error_man,
{gen_event, start_link, [{local, error_man}]},
permanent, 5000, worker, dynamic}
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 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 need some time for the event handlers to clean up, thus Shutdown
is set to 5000 ms.
Example: A child specification to start another supervisor:
{sup,
{sup, start_link, []},
transient, infinity, supervisor, [sup]}
5.6 Starting a Supervisor
In the example above, the supervisor is started by calling ch_sup:start_link()
:
java 代码
- 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.
- The first argument,
ch_sup
, is the name of the callback module, that is the module where theinit
callback function is located. - The second argument, [], is a term which is passed as-is to the callback function
init
. Here,init
does not need any indata and ignores the argument.
In this case, the supervisor is not registered. Instead its pid must be used. A name can be specified by calling supervisor:start_link({local, Name}, Module, Args)
or supervisor:start_link({global, Name}, Module, Args)
.
The new supervisor process calls the callback function ch_sup:init([])
. init
is expected to return {ok, StartSpec}
:
java 代码
- init(_Args) ->
- {ok, {{one_for_one, 1, 60},
- [{ch3, {ch3, start_link, []},
- permanent, brutal_kill, worker, [ch3]}]}}.
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, ch3
.
Note that supervisor:start_link
is synchronous. It does not return until all child processes have been started.
5.7 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:
java 代码
- supervisor:start_child(Sup, ChildSpec)
Sup
is the pid, or name, of the supervisor. 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.
5.8 Stopping a Child Process
Any child process, static or dynamic, can be stopped in accordance with the shutdown specification:
java 代码
- supervisor:terminate_child(Sup, Id)
The child specification for a stopped child process is deleted with the following call:
java 代码
- supervisor:delete_child(Sup, Id)
Sup
is the pid, or name, of the supervisor. Id
is the id specified in the child specification.
As with dynamically added child processes, the effects of deleting a static child process is lost if the supervisor itself restarts.
5.9 Simple-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 process.
Example of a callback module for a simple_one_for_one supervisor:
java 代码
- -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]}]}}.
When started, the supervisor will not start any child processes. Instead, all child processes are added dynamically by calling:
java 代码
- 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 the list of arguments specified in the child specification. If the start function is specified as {M, F, A}
, then the child process is started by calling apply(M, F, A++List)
.
For example, adding a child to simple_sup
above:
java 代码
- supervisor:start_child(Pid, [id1])
results in the child process being started by calling apply(call, start_link, []++[id1])
, or actually:
java 代码
- call:start_link(id1)
5.10 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 order according to the respective shutdown specifications, and then terminate itself.
评论
init(_Args) -> {ok, {{one_for_one, 1, 60}, [{ch3, {ch3, start_link, []}, permanent, brutal_kill, worker, [ch3]}]}}.
发表评论
-
ubuntu安装otp R11B 的一些记录
2007-11-16 12:30 2828新的ubuntu系统会缺少一些工具 和lib. 用apt-ge ... -
emulator调试日志: driver篇
2007-10-08 16:35 2318--------- driver篇 ------------- ... -
修正Programming Erlang中linked driver实例的小问题
2007-10-08 14:50 2485也许很多人碰上过, 用example1_lid:sta ... -
emulator调试日志: port篇
2007-10-06 16:14 2408------------------ port 篇 ----- ... -
supervisor一小技巧
2007-09-04 13:20 1833simple_one_for_one可以让supervisor ... -
gen_server
2007-08-29 21:52 1944State用来存数据, 任何erlang term都行 ge ... -
application
2007-08-29 02:01 1760用pman 可以看出application controlle ... -
epmd源码学习
2007-07-26 10:14 2046注: 此处节点是指分布式中分布在各终端的点, 而结点是指存在数 ... -
Tracing和dbg
2007-07-15 21:49 2573代码不必用特殊的标记(比如debug_info)来编译,也可以 ... -
ets,dets与大数据存储
2007-07-15 12:49 4970ets与dets都是用来存大数据的机制 ets是Erl ... -
用telnet来与ejabberd交互
2007-07-11 15:41 3243看了一篇文章,觉得用telnet来调试ejabberd也是一种 ... -
ejabberd管理页面和客户端
2007-07-11 00:23 9784转战到97机器。在ejabber.config加上这么一行. ... -
ejabberd在linux平台的安装与配置
2007-07-05 21:17 11971这些天捣鼓了下ejabberd,准备研究它的代码,做为榜样~ ... -
mnesia相关笔记
2007-06-29 12:17 2361当前版本OTP 5.5的mensia建表的表名可以和记录名不一 ... -
OTP设计原则:应用
2007-06-27 00:32 19647 Applications This chapter sh ... -
erlang网络编程的几个性能调优和注意点
2007-06-26 09:56 17882前些天给echo_server写了 ... -
erlc
2007-06-24 15:08 3854erlc 命令 erlc 概要 编译器 描述 Th ... -
echo_server
2007-06-23 14:45 2470代码 -module(echo_server ... -
OTP设计原则:Gen_Event 行为
2007-06-22 11:59 20414 Gen_Event 行为 这一章应该与gen_event ... -
OTP设计原则:Gen_Fsm 行为
2007-06-22 11:56 27643 Gen_Fsm 行为 This chapter shou ...
相关推荐
Erlang OTP(Open Telephony Platform)是Erlang编程语言的一个核心部分,它提供了...通过深入理解这些文档和概念,开发者能够构建出符合OTP原则的、高度可靠的Erlang系统,有效地处理并发、故障恢复和系统扩展性问题。
2. OTP的设计原则:理解微服务、状态机和行为(Behaviours)的概念,如Supervisor用于管理进程树,GenServer实现状态管理,以及GenEvent处理事件流。 3. 分布式系统构建:学习如何在Erlang节点间进行通信,以及如何...
5. **Supervisor和gen_server**:OTP的监控和行为模式,帮助构建有弹性的系统,能够自动检测和恢复错误。 6. **Distributed OTP**:支持Erlang节点间的通信和分布式应用,确保即使在网络故障或节点失败后,系统也能...
Erlang OTP 设计原理涉及到的核心概念包括监督树、行为模式以及应用的设计和管理。Erlang/OTP 提供了一种组织代码的高级原则,它基于进程、模块和目录的层次结构来构建可靠的、可容错的分布式系统。本文档将详细介绍...
### OTP设计原则详解 #### 概览 OTP(Open Telecom Platform)是Erlang/OTP框架的核心组成部分之一,它提供了一套成熟的、可扩展的、容错的应用程序设计模式。OTP设计原则指导开发者如何构建稳定可靠的分布式系统...
4. **模块化**:OTP包含一组预定义的行为模式(如GenServer、GenEvent、Supervisor等),这些行为模式是可复用的组件,帮助开发者快速构建复杂系统。 5. **热升级**:Erlang OTP支持代码热更新,这意味着可以在不...
Erlang的这种设计哲学在Erlang/OTP设计原理中得到了充分的体现。接下来我将详细解释文档中提到的相关知识点。 1. 监督树(Supervision Trees):监督树是Erlang/OTP的核心概念之一,它是一种分层的组织结构,用于...
- 模块化:OTP 提供了一系列预先设计好的行为模式(Behaviours),如GenServer、GenEvent、Supervisor等,使得代码结构清晰,易于维护。 - 监督树(Supervision Tree):OTP的监督树模型确保系统在组件失败时能够...
2. OTP 库:OTP 提供了一系列预定义的模式和行为,如GenServer、GenEvent、Supervisor、Worker等,这些模式可以帮助开发者快速构建健壮的分布式系统。它们包含了一套设计原则和库,以帮助开发者实现容错、监控、恢复...
Supervisor行为展示了监督器的设计,包括如何使用重启策略和子进程规格来管理子进程的生命周期。 Sys和Proc_Lib部分讲解了Erlang系统的基本调试方法和特殊进程的处理。应用章节则阐述了如何定义和组织应用,包括...
例如,Supervisor行为模式允许构建有监督的进程树,当子进程崩溃时,Supervisor可以自动重启它们。 3. **热代码升级**:Erlang支持在运行时进行代码替换,无需停止系统服务。这对于维护和更新生产环境中的应用程序...
这份《Erlang/OTP 中文手册(R11B)》是针对Erlang初学者和进阶者的重要参考资料,旨在帮助读者理解和掌握Erlang语言的基础语法以及OTP的设计原则。 ### 1. Erlang语言基础 Erlang语言以其独特的语法和特性而闻名...
- **OTP设计原则**:介绍OTP的行为模式、模块和最佳实践。 - **库函数参考**:详述Erlang标准库和OTP库中的各种模块和函数。 - **系统架构**:描述Erlang系统是如何组织和运行的。 - **错误处理和调试**:指导如何...
在Erlang OTP中,"OTP"一词不仅代表开放电信平台,还代表了一组设计原则和库,用于构建可靠和可维护的软件系统。这些库提供了进程通信、错误恢复、分布式计算等功能。Erlang OTP的特性包括: 1. **进程模型**:...
10. **系统架构**:OTP鼓励使用微服务和模块化的设计,手册将指导如何组织和构建符合OTP原则的系统架构。 这份离线手册的R14B03版本可能相对较旧,但依然具有很高的参考价值,尤其是对于初学者和在旧系统上工作的...
2. **OTP库**:OTP包含了大量的预构建模块和行为(Behaviours),如GenServer、GenEvent、Supervisor等,这些可以帮助开发者快速构建出遵循Erlang并发模型的可靠服务。理解这些库的使用方式和设计原则对于提高代码...
OTP(Open Telecom Platform)是一套框架、库和设计原则的集合,旨在简化Erlang应用程序的开发。它提供了许多模块和工具,如: 1. **Mnesia**:一个分布式数据库管理系统,支持事务和实时数据访问,特别适合在...
OTP是Erlang生态系统的核心部分,提供了一系列的设计原则、库和工具,帮助开发者构建可靠、可扩展的系统。OTP包含以下几个关键组件: 1. **行为(Behaviours)**:如GenServer、GenEvent和Supervisor等,它们定义了...