Hot upgrades
A hot upgrade loads new code into a running node. Live sessions, workers and WebRTC peers keep their processes and state, so calls continue. AudioFlow supports hot upgrades through Castle and its build-time part, Forecastle.
A hot upgrade only helps when the node keeps running. A crash, a host reboot or a change of the Erlang VM still stops the calls on that node. For those cases, see durable calls, which keep the media in the SFU and the call state in a store.
What AudioFlow provides
Section titled “What AudioFlow provides”appup.exs: AudioFlow’s upgrade instructions. Forecastle compiles it toaudio_flow.appupin the applicationebindirectory, and a Castle release copies it into the release.@vsnand an explicitcode_change/3in each stateful process:AudioFlow.Session,AudioFlow.Worker,AudioFlow.Duplex,AudioFlow.WebRTC,AudioFlow.WebRTC.SessionandAudioFlow.WebRTC.Transport.test/hot_upgrade_test.exs, which suspends a session and a worker in the middle of a request, loads their code again, runscode_change/3and checks that the request completes on the old code and the next one runs on the new code.
Set up the host release
Section titled “Set up the host release”Add Castle to the application that builds the release, and customize the release:
def deps do [ {:audio_flow, "~> 0.1"}, {:castle, "~> 1.3"} ]end
defp releases do [ my_agent: fn -> [ include_executables_for: [:unix], upgrade_from: ["tar:artifacts/my_agent-1.0.0.tar.gz"] ] |> Castle.customize() end ]endWrite an appup for each application that you own, and point the project at
it with appup: "appup.exs" and compilers: Mix.compilers() ++ [:appup].
mix castle.appup checks that an appup covers the changed modules, and
mix castle.appup.gen drafts missing entries.
Install a new version on the running node:
my_agent/bin/castle unpack 1.1.0my_agent/bin/castle install 1.1.0my_agent/bin/castle commitA version is provisional until commit. A restart before commit returns to
the previous permanent version.
Change process state
Section titled “Change process state”Each stateful module has a state version:
@vsn 1
@impl truedef code_change(_old_vsn, state, _extra), do: {:ok, state}When a change adds, removes or renames a state key, increase @vsn and
migrate the old state:
@vsn 2
@impl truedef code_change(1, state, _extra), do: {:ok, Map.put(state, :new_key, nil)}def code_change({:down, 1}, state, _extra), do: {:ok, Map.delete(state, :new_key)}Then add {:update, Module, {:advanced, []}} to the appup for that version,
in both the upgrade and the downgrade lists.
- Use remote functions in long-lived state. A local
fnstores a reference to the module version that made it. After two upgrades of that module, the fun is purged and a call to it fails with:badfun. Session options such as adapter:runfunctions live as long as the session. In modules that you upgrade often, prefer&Module.function/arityor{module, options}adapter tuples. The session option:on_commitalso takes{pid, tag}(sends{tag, snapshot}) and{module, function, args}; durable calls use the pid form, so saves go on after upgrades. If an:on_commitcallback fails, the session logs it with a stacktrace and the call state is no longer saved. - Turns finish on the old code. A running turn task keeps the code it
started with. The next turn uses the new code. Upgrades with
{:advanced, []}orsoft_purgewait for those tasks. - Restart for native code. A change to a NIF dependency, such as
ex_webrtc,ex_libsrtpor a Membrane plugin, needs a restart. Drain the node first. - Test the transition. Use
Forecastle.UpgradeCaseandForecastle.Deploymentto start the shipped tarball, install the new version and check that live calls continue.