View Source ProcessHub.Strategy.Migration.HotSwap (ProcessHub v0.5.0-beta)
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 and registered.
Hot swap is useful when we want to ensure that there is no downtime when migrating the child process to the remote node. The old process remains alive until the new process is successfully registered, at which point the state is delivered and the old process is terminated.
This is the key difference from ColdSwap, which terminates the local process before starting the remote one.
State Handover
When handover: true is set, HotSwap will:
- Query state from local processes before sending start requests
- Store states in ETS with TTL (along with old process reference)
- Send start requests to new nodes
- When new processes start on the target node, the post-action callback notifies the originating node
- Originating node delivers stored state and terminates old local process
To use state handover, your GenServer must use ProcessHub.Strategy.Migration.HotSwap:
defmodule MyServer do
use GenServer
use ProcessHub.Strategy.Migration.HotSwap
# Optionally override these callbacks:
# def prepare_handover_state(state), do: state
# def alter_handover_state(_current, handover), do: handover
endState Handover with Replication
State handover is not supported when using the
ProcessHub.Strategy.Redundancy.Replicationstrategy. With replication, multiple instances of a process run across the cluster, making state handover semantics undefined. If you attempt to usehandover: truewith replication, the hub will fail to start with{:error, {:invalid_config, :handover_with_replication_not_supported}}.
Summary
Functions
Options
Callback executed on the originating node to complete migration.
Post-action callback executed on the target node after children are started.
Types
@type t() :: %ProcessHub.Strategy.Migration.HotSwap{ handover: boolean(), state_query_timeout: pos_integer(), state_ttl: pos_integer() }
The hot swap migration struct.
Options:
:handover- Enable state handover before termination (default: false):state_ttl- TTL for stored states in milliseconds (default: 30000):state_query_timeout- Timeout for querying state from local process (default: 5000)
Functions
Options:
:declare_behaviour- Whentrue(default), declares theProcessHub.Strategy.Migration.HandoverBehaviourbehaviour and provides default implementations. Set tofalsewhen using both HotSwap and ColdSwap macros in the same module to avoid duplicate declarations.
@spec complete_migration(ProcessHub.Hub.t(), node(), [{ProcessHub.child_id(), pid()}]) :: :ok
Callback executed on the originating node to complete migration.
Called via generic :post_action_callback message from the target node.
Delivers stored handover states (if any) to new processes, then terminates
old local processes.
handle_post_action_migrate_complete(hub, results, originating_node, child_ids)
View Source@spec handle_post_action_migrate_complete( ProcessHub.Hub.t(), [ProcessHub.Request.Handler.StartChildrenRequest.PostStartData.t()], node(), [ProcessHub.child_id()] ) :: :ok
Post-action callback executed on the target node after children are started.
Filters successfully started children and sends a callback message to the originating node to complete the migration (deliver state + terminate old).