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:

  1. Query state from local processes before sending start requests
  2. Store states in ETS with TTL (along with old process reference)
  3. Send start requests to new nodes
  4. When new processes start on the target node, the post-action callback notifies the originating node
  5. 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
end

State Handover with Replication

State handover is not supported when using the ProcessHub.Strategy.Redundancy.Replication strategy. With replication, multiple instances of a process run across the cluster, making state handover semantics undefined. If you attempt to use handover: true with replication, the hub will fail to start with {:error, {:invalid_config, :handover_with_replication_not_supported}}.

Summary

Types

t()

The hot swap migration struct.

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

Link to this macro

__using__(opts)

View Source (macro)

Options:

  • :declare_behaviour - When true (default), declares the ProcessHub.Strategy.Migration.HandoverBehaviour behaviour and provides default implementations. Set to false when using both HotSwap and ColdSwap macros in the same module to avoid duplicate declarations.
Link to this function

complete_migration(hub, target_node, cid_pids)

View Source
@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.

Link to this function

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).