OTP 设计原则 是一套教你如何运用进程,模块和目录等条件来组织Erlang代码的原则.
A basic concept in Erlang/OTP is the supervision tree. This is a process structuring model based on the idea of workers and supervisors.
Erlang/OTP中的一个基本概念就是 管理树. 这是一个以工作者和管理者思想为基础的进程结构模型.
In the figure above, square boxes represents supervisors and circles represent workers.
上图中, 方块代表管理者, 圆代表工作者.
In a supervision tree, many of the processes have similar structures, they follow similar patterns. For example, the supervisors are very similar in structure. The only difference between them is which child processes they supervise. Also, many of the workers are serves in a server-client relation, finite state machines, or event handlers such as error loggers.
在一棵管理树中, 许多进程都有相似的结构,它们遵循相似的模式.例如, 所有管理者的结构非常相似.它们之间唯一不同点就是具体管理哪些子进程.同样, 许多工作者都在服务-客户关系, 有限状态机或事件处理中提供服务.
Behaviours are formalizations of these common patterns. The idea is to divide the code for a process in a generic part (a behaviour module) and a specific part (a callback module).
行为形式化了这些共同模式.它的思想是把进程的代码分离成一普通部分(一个行为模块)和一特殊部分(一个回调模块).
The behaviour module is part of Erlang/OTP. To implement a process such as a supervisor, the user only has to implement the callback module which should export a pre-defined set of functions, the callback functions.
行为模块是Erlang/OTP的一部分。 实现像管理者这样的进程, 用户只须实现回调模块.回调模块要求输出一套预定义函数,这些函数叫做回调函数。
An example to illustrate how code can be divided into a generic and a specific part: Consider the following code (written in plain Erlang) for a simple server, which keeps track of a number of "channels". Other processes can allocate and free the channels by calling the functions alloc/0
and free/1
, respectively.
The code for the server can be rewritten into a generic part server.erl
:
服务的代码可以写入到一普通部分server.erl
:
and a callback module ch2.erl
:
加上一个加调模块 ch2.erl
:
Note the following:
注解:
server
can be re-used to build many different servers.server
代码可以重用到任何其它不同的服务中. ch2
, is hidden from the users of the client functions. This means the name can be changed without affecting them.是ch2原子.它对其它客户函数的使用来说是不可见的.这意味着名称的修改将不会影响到其它客户函数.
server
, without having to change ch2
or any other callback module.server
的功能,而不必改变ch2
或其它任何回调模块. (In ch1.erl
and ch2.erl
above, the implementation of channels/0
, alloc/1
and free/2
has been intentionally left out, as it is not relevant to the example. For completeness, one way to write these functions are given below. Note that this is an example only, a realistic implementation must be able to handle situations like running out of channels to allocate etc.)
Code written without making use of behaviours may be more efficient, but the increased efficiency will be at the expense of generality. The ability to manage all applications in the system in a consistent manner is very important.
Using behaviours also makes it easier to read and understand code written by other programmers. Ad hoc programming structures, while possibly more efficient, are always more difficult to understand.
The module server
corresponds, greatly simplified, to the Erlang/OTP behaviour gen_server
.
The standard Erlang/OTP behaviours are:
The compiler understands the module attribute -behaviour(Behaviour)
and issues warnings about missing callback functions. Example:
编译器能识别模块属生-behaviour(Behaviour)
,并会在缺少回调函数时发出警告.例如:
Erlang/OTP comes with a number of components, each implementing some specific functionality. Components are with Erlang/OTP terminology called applications. Examples of Erlang/OTP applications are Mnesia, which has everything needed for programming database services, and Debugger which is used to debug Erlang programs. The minimal system based on Erlang/OTP consists of the applications Kernel and STDLIB.
Erlang/OTP带有一系列组件,它们分别实现了特定的功能。组件在Erlang/OTP术语中叫做应用.Erlang/OTP应用有众多例子。命例如: Mnesia,它有所有数据库服务编程所需的功能; 调试器,它用来调试Erlang程序;最小系统,它基于Erlang/OTP,由Kernel 与STDLIB应用组成.
The application concept applies both to program structure (processes) and directory structure (modules).
应用概念既适用于程序结构(进程),也适用于目录结构(模块).
The simplest kind of application does not have any processes, but consists of a collection of functional modules. Such an application is called a library application. An example of a library application is STDLIB.
最简单的应用没有任何进程,而是由一套功能模块组成. 这种应用叫做库应用.库应用的一个典型例子就是STDLIB.
An application with processes is easiest implemented as a supervision tree using the standard behaviours.
最容易实现且带有进程的应用,就是用标准行为实现的管理树.
How to program applications is described in Applications.
怎么编写应用,在 Applications中有描述.
A release is a complete system made out from a subset of the Erlang/OTP applications and a set of user-specific applications.
一个release 是一个由Erlang/OTP 子集与一些用户特定的应用组成的完整系统
How to program releases is described in Releases.
怎么编写releases请参考 Releases.
How to install a release in a target environment is described in the chapter about Target Systems in System Principles.
怎么在目标环境中安装一个release,Target Systems in System Principles相关的章节有描述.
Release handling is upgrading and downgrading between different versions of a release, in a (possibly) running system. How to do this is described in Release Handling.
Release handling 是在一个运行系统中,不同版本release之间的升级和降级 ,具体怎么做,Release Handling.有描述.