技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 系统运维 --> Erlang集群全联通问题及解决方案

Erlang集群全联通问题及解决方案

浏览:1691次  出处信息

Erlang的集群默认情况下是全联通的,也就是当一个节点加入集群的时候,介绍人会推荐集群里面所有的节点主动来和新加入的节点建立联系,

效果如下图:

erlang_connect

具体点讲那就是net_kernel模块负责节点间的通道的建立、检查、断开并提供monitor_node语义。

摘抄 http://www.erlang.org/doc/man/erlang.html 如下:

monitor_node(Node, Flag) -> true

Types:

Node = node()

Flag = boolean()

Monitors the status of the node Node. If Flag is true, monitoring is turned on; if Flag is false, monitoring is turned off.

Making several calls to monitor_node(Node, true) for the same Node is not an error; it results in as many, completely independent, monitorings.

If Node fails or does not exist, the message {nodedown, Node} is delivered to the process. If a process has made two calls to monitor_node(Node, true) and Node terminates, two nodedown messages are delivered to the process. If there is no connection to Node, there will be an attempt to create one. If this fails, a nodedown message is delivered.

Nodes connected through hidden connections can be monitored as any other node.

Failure: badargif the local node is not alive.

其他模块如global, pg2, mnesia都利用这个monitor_node提供的语义来实现更上层的逻辑。那么上面提到的引荐机制正是global模块实现的,它的目的是提供集群层面的名称和进程的映射关系,所以它需要全联通。

学过中学数学的同学都知道系统总的通道的数目是N*(N-1)/2, 随着N的增长,这个数目会急速上升,见下图。

hidden_visible

这个对集群的规模有致命的破坏作用。 这么多链接需要耗用很多资源,更坏的是,erlang为了检测节点的存活,需要定期发心跳包来检查,一分钟一个tick, 这会造成大量的网络风暴。

那么我们如何来避免这个事情呢?


很简单,避免全联通,摘抄 http://www.erlang.org/doc/man/erl.html 如下:

hidden

Starts the Erlang runtime system as a hidden node, if it is run as a distributed node. Hidden nodes always establish hidden connections to all other nodes except for nodes in the same global group. Hidden connections are not published on either of the connected nodes, i.e. neither of the connected nodes are part of the result from nodes/0 on the other node. See also hidden global groups, global_group(3).

哈哈,一个参数就解决这个问题了。

但是且慢,这个参数的行为需要注意下。 hidden是or语义,也就是说当参与方只要有一个是hidden, 那么global模块就不会介绍别人来认识新加入的节点。 只要2个都不是hidden, 才会有正常的社交。

leofs 摘抄一段配置:

## set up the node with the -hidden flag

-hidden

社区已经开始注意,并回避这个问题了,你呢?

小结:有些问题很小,但是影响很坏!

建议继续学习:

  1. Erlang match_spec引擎介绍和应用    (阅读:4448)
  2. whatsapp深度使用Erlang有感    (阅读:4422)
  3. php-erlang    (阅读:4250)
  4. gen_tcp调用进程收到{empty_out_q, Port}消息奇怪行为分析    (阅读:3488)
  5. hibernate使用注意事项    (阅读:3151)
  6. Erlang linkin driver用port_control方式时的一些经验分享    (阅读:2911)
  7. Erlang如何限制节点对集群的访问之net_kernel:allow    (阅读:2839)
  8. ERLANG OTP源码分析 – gen_server    (阅读:2824)
  9. erlang学习手记    (阅读:2656)
  10. gen_tcp容易误用的一点解释    (阅读:2575)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1