Erlang generic standard behaviours -- summary
gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项目pooler, poolboy, emysql 中的pool 的管理, rabbitmq-server 中的worker_pool 角色等.
lapse request
对于gen_server handle_call callback, 主要处理同步请求,如果gen_server 进程在Timeout 时间内无法处理完成或根本来不及处理,这个时候就会出现失效的请求. 也就是当handle_call 在开始处理某请求时, 该请求的调用者早已经不再需要此次处理.
这样的需求并不一定是需要的,需要还是根据需求场景的不同定. 而避免此种情形出现的方式也比较简单,不着急,慢慢分析:
判断调用者alive
在gen module 处理call 请求的函数 do_call/4, 在将请求发送给目标gen_server 进程后, 请求调用者就会同步receive 目标gen_server 进程的返回, 超时Timeout 之后, 调用进程以exit(timeout) 退出.是的,等gen_server 进程在Timeout 时间内无法完成或根本来不及处理请求, 请求的调用者按照默认的方式, 就会exit. 也就是说,在没有try catch 的情况下,借助erlang:is_alive/0 API , 是可以判断当前handle_call 处理的请求是否是有效的.
但是, 如果使用了try catch 或者是catch 请求的调用进程就很有可能不按照预先的期望exit 退出了,这个时候, 这种方式就已经没有效果了.
monitored_by
还是在gen module 处理call 请求的函数 do_call/4,在发送给目标gen_server 进程请求之前, 请求的调用进程会首先monitor gen_server 进程,并在Timeout 之后demonitor. 也就是, 某请求如果还是有效的话, 在gen_server 进程的 monitored_by lists 中,应该还有请求的调用进程存在.
long-lived
通常情况下,Erlang process 更被期望于以short-lived 的方式存在.
An Erlang process starts with a small stack and heap in order to support a huge number of processes in a system. The size is configurable and the default value is 233 words. In general, Erlang processes are expected to short-lived and have small amounts of live data. When there is not enough free memory in the heap for a pro- cess, it is garbage collected, and if less memory can be freed than required it grows.
但是对于gen_server 来说,一般都是作为long-lived 的进程角色.对于long-lived 的进程角色,需要对其资源利用谨慎处理(通常情况下,是对于GC).
而优化的方案,需要根据应用场景的不同而不同,可供选的大致有:
1, hibernation
2, fullsweep_after
A non-negative integer which indicates how many times generational garbage collections can be done without forcing a fullsweep collection. In low-memory systems (especially without virtual memory), setting the value to
0can help to conserve memory.
Riak 的官方文档中,对fullsweep_after 的参数设置为0.
3, min_heap_size
4, erlang:garbage_collect/1
参见RabbitMQ-server 中的background_gc module.
当然,最为理想的方案, 是尽可能将long-lived 的进程角色改为 short-lived . 参见riak_client 对gen_fsm 的使用.
参考
1, http://erlang.org/pipermail/erlang-questions/2011-October/061871.html
2, http://docs.basho.com/riak/latest/ops/advanced/configs/configuration-files/
3, http://www.erlang.org/doc/man/erlang.html#system_flag-2
4, Characterizing the Scalability of Erlang VM
5, Exploring Alternative Memory Architectures for Erlang- Implementation and Performance Evaluation (A How does the initial heap size affect the runtime? )
6, https://github.com/Eonblast/Emysql/pull/101
Erlang generic standard behaviours -- summary的更多相关文章
- Erlang generic standard behaviours -- gen
在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...
- Erlang generic standard behaviours -- gen_server system msg
这是Erlang generic standard behaviors gen_server 分析的系列的最后一篇,主要分析gen_server module 辅助性的功能函数. 在gen_serve ...
- Erlang generic standard behaviours -- gen_server module
在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...
- Erlang generic standard behaviours -- gen_server hibernate
hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...
- Erlang generic standard behaviours -- gen_server noblock call
在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...
- Erlang generic standard behaviours -- gen_server terminate
gen_server 主体 module 已经分析完了(http://www.cnblogs.com/--00/p/4271982.html),接着,分析下gen_server 中的terminate ...
- jQ1.5源码注释以及解读RE
jQ作为javascript的库( ▼-▼ ), 尽善尽美, 代码优美, 值得学习. 这一周平常上班没啥事也看jQ1.5的代码, 今天周六差不多看完了(Sizzle部分还没看), 重新看了一下, ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- 《C++程序设计语言(英文第四版)》【PDF】下载
<C++程序设计语言(英文第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382177 内容简介 本书是C++领域经典的参 ...
随机推荐
- 搭建Cat笔记01
昨天晚上搭建Cat 时候那叫一个坑b,宝宝心里苦呀! 准备工作: 1.先大众点评Cat的项目源码,https://github.com/dianping/cat.git 2.打包编译: mvn cle ...
- git push问题 objects/pack/tmp_pack_XXXXXX': Permission denied
1.上传时的权限问题 在执行git push origin master之后,上传过程中报出如下错误: objects/pack/tmp_pack_XXXXXX': Permission denied ...
- cloudera impala编译 安装 配置 启动
无论是采用GDB调试impala或者尝试修改impala源码,前提都是需要本地环境编译impala,这篇文章详细的分享一下impala编译方法以及编译过程遇到的棘手的问题: 前言: impala官方的 ...
- Java中byte转换int时与0xff进行与运算的原因
http://w.baike.com/LGAdcWgJBBQxRAHUf.html 转帖 java中byte转换int时为何与0xff进行与运算 在剖析该问题前请看如下代码 public static ...
- python spark
http://blog.jobbole.com/86232/ 1. 安装lib 材料: spark : http://spark.apache.org/downloads.html hadoop : ...
- 【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array
描述: Given an array of integers nums sorted in ascending order, find the starting and ending position ...
- 每天一个Linux命令(2)cd命令
cd命令用来切换工作目录至dirname. 其中dirName表示法可为绝对路径或相对路径.若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录).另外,~也表 ...
- 【leetcode】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 总结:iview(基于vue.js的开源ui组件)学习的一些坑
1.要改变组件的样式 找到这个组件的class名,然后覆盖样式. 举例:修改select框,显示圆角.只需给找到类名并写样 .ivu-select-selection{ border-radius:1 ...
- Hadoop 2.x简介
Hadoop 2.0产生背景 Hadoop1.0中HDFS和MapReduce在高可用.扩展性等方面存在问题 HDFS存在的问题 NameNode单点故障,难以应用于在线场景 NameNode压力过大 ...