分布式、并行计算语言Erlang 学习笔记(第三部分)

Error Handling 错误处理

--------------------------------------------------------------------------------

Definitions 定义
Exit signals are sent when processes crash 进程出错时的退出信号
Exit Signals propagate through Links 退出信号通过链接传播
Processes can trap exit signals 进程可以捕获退出信号
Complex Exit signal Propagation 复杂的退出信号传递
Robust Systems can be made by Layering 鲁棒系统的分层实现
Primitives For Exit Signal Handling 简单的退出信号处理
A Robust Server 一个鲁棒服务
Allocator with Error Recovery 带有错误恢复的分配器
Allocator Utilities
分配器工具

--------------------------------------------------------------------------------

Definitions 定义
Link A bi-directional propagation path for exit signals.
链接 退出信号的一个双向的传输路径。
Exit Signal - Transmit process termination information.
退出信号 发送的进程终止信息。
Error trapping - The ability of a process to process exit signals as if they were mssages. 错误捕获 进程间传递退出信号消息的一种能力。

--------------------------------------------------------------------------------

Exit Signals are Sent when Processes Crash
进程出错时的退出信号


When a process crashes (e.g. failure of a BIF or a pattern match) Exit Signals are sent to all processes to which the failing process is currently linked.
当一个进程挂掉了(例如BIF失败或者模式匹配失败),退出信号将发送给所有和该失效进程相关的其他进程。

--------------------------------------------------------------------------------

Exit Signals propagate through Links
退出信号通过链接传播


Suppose we have a number of processes which are linked together, as in the following diagram. Process A is linked to B, B is linked to C (The links are shown by the arrows).
Now suppose process A fails - exit signals start to propogate through the links:
假设我们有一组进程,它们之间是相互联系在一起的,如图所示。进程A链接到B,B链接到C(链接使用箭头表示)。现在假定进程A失效了,退出信号就开始以下面的方式传播:

These exit signals eventuall reach all the processes which are linked together.
退出信号可能到达所有互相链接在一起的进程。

The rule for propagating errors is: If the process which receives an exit signal, caused by an error, is not trapping exits then the process dies and sends exit signals to all its linked processes.
传播错误的规则是:如果一个进程接收到退出信号(可能是由于一个错误引起的),而没有加以捕获,这个进程失效并且发送退出信号给和它相链接的其他进程。

--------------------------------------------------------------------------------

Processes can trap exit signals
进程可以捕获退出信号


In the following diagram P1 is linked to P2 and P2 is linked to P3. An error occurs in P1 - the error propagates to P2. P2 traps the error and the error is not propagated to P3.
下图中,P1和P2链接,P2和P3链接。一个错误出现在P1,并且传递到P2。P2捕获该错误,该错误就不会传递到P3。

P2 has the following code:
P2的代码如下:

receive
{'EXIT', P1, Why} ->
... exit signals ...
{P3, Msg} ->
... normal messages ...
end

--------------------------------------------------------------------------------

Complex Exit signal Propagation
复杂的退出信号传递

Suppose we have the following set of processes and links:
假定我们有以下进程和链接关系:

The process marked with a double ring is an error trapping process.
使用双环标记的进程为错误捕获进程。

If an error occurs in any of A, B, or C then All of these process will die (through propagation of errors). Process D will be unaffected.
如果一个错误发生在A或者B或者C中,所有的进程都将失效(通过错误的传递)。进程D则不受影响。

--------------------------------------------------------------------------------

Exit Signal Propagation Semantics
退出信号传播语义

When a process terminates it sends an exit signal, either normal or non-normal, to the processes in its link set.
当一个进程终止的时候,它发出一个退出信号(不论是否正常退出)到和它链接在一起的所有进程。
A process which is not trapping exit signals (a normal process) dies if it receives a non-normal exit signal. When it dies it sends a non-normal exit signal to the processes in its link set.
一个没有捕获退出信号的进程(普通进程)在它接收到一个非正常退出信号的时候失效。它发出非正常退出信号给和它链接在一起的进程。
A process which is trapping exit signals converts all incoming exit signals to conventional messages which it can receive in a receive statement.
捕获了退出信号的进程将所有进入的退出信号转化为它可以接受的一般消息。
Errors in BIFs or pattern matching errors send automatic exit signals to the link set of the process where the error occured.
在BIF或者模式匹配中发生的错误将自动发送退出信号到该出错进程的链接进程集合。

--------------------------------------------------------------------------------

Robust Systems can be made by Layering

鲁棒系统的分层实现

By building a system in layers we can make a robust system. Level1 traps and corrects errors occuring in Level2. Level2 traps and corrects errors ocuring in the application level.
In a well designed system we can arrange that application programers will not have to write any error handling code since all error handling is isolated to deper levels in the system.
通过构造分层系统我们可以得到一个鲁棒系统。Level1捕获和纠正发生在Level2的错误。Level2捕获和纠正发生在应用层次上的错误。良好设计的系统我们可以安排程序不必写任何错误处理的代码,所有的错误处理都是由不同深度的层次独立负责的。

--------------------------------------------------------------------------------

Primitives For Exit Signal Handling
简单的退出信号处理


link(Pid) - Set a bi-directional link between the current process and the process Pid
link(Pid) 在当前的进程之间设置一个双向的链接。

process_flag(trap_exit, true) - Set the current process to convert exit signals to exit messages, these messages can then be received in a normal receive statement.
process_flag(trap_exit, true) 使用当前进程将退出信号转化为退出消息,这些消息可以通过一般的消息接受语句所接受。

exit(Reason) - Terminates the process and generates an exit signal where the process termination information is Reason.
exit(Reason) 终止进程,并且生成一个退出信号,包含的进程终止信息就是参数Reason。

What really happens is as follows: Each process has an associated mailbox - Pid?燤sg sends the message Msg to the mailbox associated with the process Pid.
The receive .. end construct attempts to remove messages from the mailbox of the current process. Exit signals which arrive at a process either cause the process to crash (if the process is not trapping exit signals) or are treated as normal messages and placed in the process mailbox (if the process is trapping exit signals). Exit signals are sent implicitly (as a result of evaluating a BIF with incorrect arguments) or explicitly (using exit(Pid, Reason), or exit(Reason) ).
到底真正发生了什么:每个进程都有一个关联着的邮箱(mailbox)-Pid,向邮箱内发消息就是与Pid所标识的进程进行通讯。 receive...end结构尝试从当前进程的邮箱中删除消息。抵达进程的退出信号将导致进程失效(如果进程不捕获该退出信号的话)或者如同一般消息一 样对待退出信号并且放入进程的邮箱中(如果进程捕获了退出信号)。退出信号被隐式的发送(执行带有错误参数的BIF导致的)或者显式的(使用 exit(Pid,Reason)或者exit(Reason))。

If Reason is the atom normal - the receiving process ignores the signal (if it is not trapping exits). When a process terminates without an error it sends normal exit signals to all linked processes. Don't say you didn't ask!
如果Reason是一个字符串,一般情况下接受进程会忽略该信号。当一个没有发生错误的进程终止时发送正常退出信号到所有链接上的进程。

--------------------------------------------------------------------------------

A Robust Server
一个鲁棒服务


The following server assumes that a client process will send an alloc message to allocate a resource and then send a release message to deallocate the resource.
下面的server为一个client进程服务,通过发送请求分配的消息分配资源,发送释放消息来释放资源。
This is unreliable - What happens if the client crashes before it sends the release message?
这是不可靠的,如果client在发送释放消息之前就挂掉了呢?

top(Free, Allocated) ->
receive
{Pid, alloc} ->
top_alloc(Free, Allocated, Pid);
{Pid ,{release, Resource}} ->
Allocated1 = delete({Resource,Pid},燗llocated),
top([Resource|Free], Allocated1)
end.

top_alloc([], Allocated, Pid) ->
Pid ! no,
top([], Allocated);

top_alloc([Resource|Free], Allocated, Pid) ->
Pid ! {yes, Resource},
top(Free, [{Resource,Pid}|Allocated]).

This is the top loop of an allocator with no error recovery. Free is a list of unreserved resources. Allocated is a list of pairs {Resource, Pid} - showing which resource has been allocated to which process.
top没有带有错误恢复的进行循环的分配工作。Free是一个可以使用的资源的列表。分配的情况通过二元组来表示{Resource,Pid},表示哪个资源被哪个进程所使用了。
--------------------------------------------------------------------------------

Allocator with Error Recovery
带有错误恢复的分配器


The following is a reliable server. If a client craches after it has allocated a resource and before it has released the resource, then the server will automatically release the resource.
下面是一个可靠的服务,如果一个client在释放资源之前挂掉了,server将自动的释放该资源。
The server is linked to the client during the time interval when the resource is allocted. If an exit message comes from the client during this time the resource is released.
当进行资源分配的时候,该server链接到client。如果一个退出消息从client发出,资源也是被释放。

top_recover_alloc([], Allocated, Pid) ->
Pid ! no,
top_recover([], Allocated);

top_recover_alloc([Resource|Free], Allocated, Pid) ->
%% No need to unlink.
Pid ! {yes, Resource},
link(Pid),
top_recover(Free, [{Resource,Pid}|Allocated]).

top_recover(Free, Allocated) ->
receive
{Pid , alloc}?>
top_recover_alloc(Free, Allocated, Pid);
{Pid, {release, Resource}}?>
unlink(Pid),
Allocated1 = delete({Resource, Pid}, Allocated),
top_recover([Resource|Free], Allocated1);
{'EXIT', Pid, Reason}?>
%% No need to unlink.
Resource = lookup(Pid, Allocated),
Allocated1 = delete({Resource, Pid}, Allocated),
top_recover([Resource|Free], Allocated1)
end.

Not done -- multiple allocation to same process. i.e. before doing the unlink(Pid) we should check to see that the process has not allocated more than one device.
还没完~同一个进程的多次分配~在执行unlink(Pid)之前,我们应该检查改进成有没有分配多于1个的资源。

--------------------------------------------------------------------------------

Allocator Utilities
分配器工具


delete(H, [H|T]) ->
T;
delete(X, [H|T]) ->
[H|delete(X, T)].

lookup(Pid, [{Resource,Pid}|_]) ->
Resource;
lookup(Pid, [_|Allocated]) ->

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值