View Source ProcessHub.Strategy.Migration.HotSwap (ProcessHub v0.3.2-alpha)
The hot swap migration strategy implements the ProcessHub.Strategy.Migration.Base
protocol.
It provides a migration strategy where the local process is terminated after the new one is
started on the remote node.
In the following text, we will refer to the process that is being terminated after the migration as the peer process.
Hotswap migration can also handle process state migration when nodes are leaving the cluster. The process states are stored in the local storage and are sent to the remote node before the local process is terminated. This will only work if the node is being shut down gracefully.
The hot swap strategy is useful when we want to ensure that there is no downtime when migrating the child process to the remote node. It also provides a way to ensure that the state of the process is synchronized before terminating the peer process.
To pass the process state to the newly started process on the remote node, the
:handover
option must be set to true
in the migration strategy configuration, and
necessary messages must be handled in the processes.
Using the handover option
When the
:handover
option is set totrue
, the peer process must handle the following message:{:process_hub, :send_handover_state, receiver, child_id, opts}
.
Example migration with handover using GenServer
:
def handle_info({:process_hub, :send_handover_state, receiver, child_id, opts}, state) do
if is_pid(receiver) do
Process.send(receiver, {:process_hub, :handover, child_id, state}, [])
end
if is_pid(opts[:retention_receiver]) do
Process.send(opts[:retention_receiver], {:process_hub, :retention_handled, child_id}, [])
end
{:noreply, state}
end
def handle_info({:process_hub, :handover, _child_id, handover_state}, _state) do
{:noreply, handover_state}
end
Use HotSwap macro to provide the handover callbacks automatically.
It's convenient to use the
HotSwap
macro to provide the handover callbacks automatically.use ProcessHub.Strategy.Migration.HotSwap
Summary
Types
@type t() :: %ProcessHub.Strategy.Migration.HotSwap{ child_migration_timeout: pos_integer(), handover: boolean(), handover_data_wait: pos_integer(), retention: pos_integer() }
Hot-swap migration strategy configuration. Available options:
:retention
- An integer value in milliseconds is used to specify how long the peer process should be kept alive after a new child process has been started on the remote node. This option is used to ensure that the peer process has had enough time to perform any cleanup or state synchronization before the local process is terminated. Keep in mind that process will be terminated before the retention time has passed if the peer process has notified the handler process that the state transfer has been handled. The default value is5000
.:handover
- A boolean value. If set totrue
, the processes involved in the migration must handle the following message:{:process_hub, :send_handover_state, receiver_pid, child_id, opts}
. This message will be sent to the peer process that will be terminated after the migration. The variablereceiver_pid
is the PID of the process who is expecting to receive the message containing the state of the peer process. If the:retention
option is used, then the peer process has to signal the handler process that the state has been handled; otherwise, the handler will wait until the default retention time has passed. The handler PID is passed in thefrom
variable. Theretention_receiver
pid can be accessed from theopts
variable. The default value isfalse
.:handover_data_wait
- An integer value in milliseconds is used to specify how long the handler process should wait for the state of the remote process to be sent to the local node. The default value is3000
.:child_migration_timeout
- An integer value in milliseconds is used to specify the timeout for single child process migration. If the child process migration does not complete within this time, the migration for this child process will be considered failed but the migration for other child processes will continue. The default value is10000
.