Aiur

zellux 的博客

Live Code Update in Erlang

Permalink

http://bc.tech.coop/blog/070713.html

Joe Armstrong的原话 Live code upgrading is one of the things Erlang was designed for.

一个最简单的例子

1
2
3
4
5
6
7
8
9
loop(Fun, State)
    receive
        {From, {rpc, Tag, Q}} ->
               {Reply, State1} = Fun(Q, State),
               From !  {Tag, Reply},
               loop(Fun, State1);
        {code_upgrade, Fun1} ->
               loop(Fun1, State)
     end.

使用时只要发送一个{code_update, Fun1}消息即可 在实际的项目中,live code update需要在一个进程的稳定状态中进行。好在有Erlang天生的分布式特性,结点i进行更新时其他结点可以接管这个结点的任务,直到结点i更新成功,再进行下一个结点的更新。这个形式和结点的crash处理有点类似。于是Erlang中live updating的实际难点在于 1. How to suspend the system and put it into a stable state 2. How to replicate the stable state 3. How to restart from a stable state 4. How to detect failure 5. How to upgrade and downgrade the stable state

另外http://www.erlang.org/doc/man/code.html#12介绍了Erlang中的Code Server模块。

评论