0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · 课时

热代码替换与在线升级

学习 Erlang 如何在不停止系统的情况下升级正在运行的代码,以及 OTP 行为如何支持有状态的在线升级。

热代码替换与在线升级 是 CoddyKit 上的免费 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Zero-Downtime Upgrades

One of Erlang's signature features is hot code swapping: replacing a module's code while the system keeps running, with no restart and no dropped connections.

Two Code Versions

The VM keeps up to two versions of each module: current and old. Loading a third version purges the oldest.

Local vs External Calls

A local call (foo()) stays in the running version. An external/qualified call (?MODULE:foo()) jumps to the newest loaded version. This distinction drives upgrades.

loop(State) ->
    receive
      Msg ->
        NewState = handle(Msg, State),
        ?MODULE:loop(NewState)  % picks up new code
    end.

Loading New Code

You compile and load the new version with the code module or via the shell.

c(my_module).
code:load_file(my_module).

The code_change Callback

Stateful OTP behaviors like GenServer support a code_change/3 callback. OTP calls it during an upgrade so you can transform old state into the new format.

code_change(_OldVsn, State, _Extra) ->
    {ok, migrate_state(State)}.

Why State Migration Matters

If your new version stores state differently (say a tuple becomes a map), code_change bridges the gap so live processes do not crash on the new code.

migrate_state({old, A, B}) ->
    #{a => A, b => B}.

Release Upgrades

For whole-system upgrades, OTP uses appup and relup files that describe the steps to move from one release version to the next.

{"1.1.0",
 [{"1.0.0", [{update, my_server, {advanced, []}}]}],
 [{"1.0.0", [{update, my_server, {advanced, []}}]}]}.

Suspending Processes

During an advanced upgrade, OTP suspends the affected processes, applies code_change, then resumes them, ensuring no message is handled with mismatched state.

Purging Old Code

After an upgrade, processes still running old code can be checked and the old version purged once safe.

code:soft_purge(my_module).
% returns false if any process still runs old code

When to Use It

Hot upgrades shine for telecom-grade always-on systems. For many apps, a rolling restart of nodes is simpler. Choose based on your uptime requirements.

Best Practices

Key rules:

  • Always make the recursive loop call qualified (?MODULE:loop)
  • Implement code_change for any stateful behavior
  • Test upgrades in staging before production

Quick Check

Test your hot-swap knowledge.

Recap

You learned Erlang's live upgrade model.

  • The VM keeps two code versions
  • Qualified calls adopt new code
  • code_change/3 migrates state during OTP upgrades
  • appup/relup drive full release upgrades

常见问题解答

「热代码替换与在线升级」课时是免费的吗?

是的 — 「热代码替换与在线升级」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

「热代码替换与在线升级」这节课中我会学到什么?

学习 Erlang 如何在不停止系统的情况下升级正在运行的代码,以及 OTP 行为如何支持有状态的在线升级。 你通过在浏览器中直接运行的动手代码来练习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「热代码替换与在线升级」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课中编写并运行代码吗?

能。每节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 GenStatem 管理状态
  2. 使用 GenEvent 处理事件
  3. 自定义 OTP 行为
  4. 热代码替换与在线升级
← 返回 Erlang OTP: Distributed & Fault-Tolerant Systems Programming