IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

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

系统技术非业余研究 2013-05-19 23:24:24 累计浏览 2,325 次
本机暂存

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. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. gen_tcp发送进程被挂起起因分析及对策 (累计阅读 37,821)
  2. 搜狐闪电邮箱的 Nginx/Postfix 使用模式 (累计阅读 33,896)
  3. libcurl的使用总结(二) (累计阅读 15,083)
  4. Twitter/微博客的学习摘要 (累计阅读 12,262)
  5. 面试题 – 为什么我的朋友圈不见了? (累计阅读 11,953)
  6. 奇怪的 Nginx 的 upstream timed out 引起响应 502 (累计阅读 9,967)
  7. Zookeeper研究和应用 (累计阅读 9,484)
  8. 分布式哈希和一致性哈希 (累计阅读 8,815)
  9. 面试IT业界顶尖企业所应该知道的10道题(1) (累计阅读 8,527)
  10. 在百度的第一年 (累计阅读 6,922)