A change, step by step

On this page

A TCP server with one process per connection, counting the lines a client sends. This is a sketch for this page. The runnable version, with a checker that verifies nothing broke, is examples/pulse. To set up a node and keys first, see Getting started.

Step 1: the running app

An ordinary GenServer. It knows nothing about Hotcode.

defmodule TcpCounter.Session.State do
  defstruct socket: nil, count: 0
end

defmodule TcpCounter.Session do
  use GenServer

  def start_link(socket), do: GenServer.start_link(__MODULE__, socket)

  @impl true
  def init(socket), do: {:ok, %TcpCounter.Session.State{socket: socket}}

  @impl true
  def handle_info({:tcp, _socket, line}, state) do
    case String.trim(line) do
      "inc" ->
        state = %{state | count: state.count + 1}
        :gen_tcp.send(state.socket, "count=#{state.count}\n")
        {:noreply, state}

      "version" ->
        :gen_tcp.send(state.socket, "v1\n")
        {:noreply, state}
    end
  end
end

A client is connected and has counted twice:

$ nc 127.0.0.1 4040
inc
count=1
inc
count=2

Step 2: change the code

Version 2 remembers what it was asked. The new lines are marked:

defmodule TcpCounter.Session.State do
  defstruct socket: nil, count: 0, history: []          # new: history
end

defmodule TcpCounter.Session do
  use GenServer

  def start_link(socket), do: GenServer.start_link(__MODULE__, socket)

  @impl true
  def init(socket), do: {:ok, %TcpCounter.Session.State{socket: socket}}

  @impl true
  def handle_info({:tcp, _socket, line}, state) do
    case String.trim(line) do
      "inc" ->
        state = %{state | count: state.count + 1, history: [:inc | state.history]}   # new
        :gen_tcp.send(state.socket, "v2 count=#{state.count}\n")                    # new: "v2"
        {:noreply, state}

      "version" ->
        :gen_tcp.send(state.socket, "v2\n")                                         # new: "v2"
        {:noreply, state}
    end
  end
end

There is no code_change/3, no migration function and no Hotcode macro.

Step 3: deploy

The task compiles, asks the node what it runs, ships only what differs and signs it:

$ mix hotcode.deploy https://prod.example.com:4747 --key alice.key
patch 8f2c1d0: 2 changed, 0 new, 0 removed (8185 bytes)
  ~ Elixir.TcpCounter.Session
  ~ Elixir.TcpCounter.Session.State
1 process(es) will be suspended and migrated
deployed: "migrated" => 1, "ms" => 8, "ok" => true

Step 4: the same client, on the same connection

The count is intact and the reply comes from the new code:

inc
v2 count=3

Hotcode suspended the session, loaded the new modules, rebuilt its state with history: [] and resumed it.

Step 5: a change of meaning

A new field is migrated without your help. A field whose meaning changes needs a step from you. Here count changes from units to cents:

defmodule TcpCounter.Session.State do
  defstruct socket: nil, count: 0, history: []
end

defmodule TcpCounter.Session do
  use Hotcode.Versioned, vsn: 3                         # new: a GenServer with a version number

  def start_link(socket), do: GenServer.start_link(__MODULE__, socket)

  @impl true
  def init(socket), do: {:ok, %TcpCounter.Session.State{socket: socket}}

  # new: upgrade, one clause per version step. Deploying from 1 runs upgrade(1), then upgrade(2)
  @impl true
  def upgrade(1, state), do: state                      # v1 -> v2 only added a field: automatic
  def upgrade(2, state), do: %{state | count: state.count * 100}     # v2 -> v3: units -> cents

  # new: and back, for rollback. It runs in THIS module, before the old code returns
  @impl true
  def downgrade(3, state), do: %{state | count: div(state.count, 100)}   # v3 -> v2: cents -> units
  def downgrade(2, state), do: state

  @impl true
  def handle_info({:tcp, _socket, line}, state) do
    case String.trim(line) do
      "inc" ->
        state = %{state | count: state.count + 100, history: [:inc | state.history]}   # cents
        :gen_tcp.send(state.socket, "v3 cents=#{state.count}\n")
        {:noreply, state}

      "version" ->
        :gen_tcp.send(state.socket, "v3\n")
        {:noreply, state}
    end
  end
end

Deploying it moves every live session from 2 to 3: a client at count=3 is at cents=300, and its next inc answers v3 cents=400. A plain GenServer (steps 1 and 2) declares no version. A release that adds use Hotcode.Versioned, vsn: 1 to it is accepted and keeps the state; a first release that jumps straight to vsn: 2 or higher is refused before loading, because Hotcode does not guess a transition from an unversioned module. The rules for version steps are in State migration.

Step 6: roll back

Rollback needs the same signers as a deploy, and runs downgrade/2:

$ mix hotcode.rollback https://prod.example.com:4747 --key alice.key

Where next