View Source ProcessHub.Strategy.Migration.ColdSwap (ProcessHub v0.5.0-beta)

The cold swap migration strategy implements the ProcessHub.Strategy.Migration.Base protocol. It provides a migration strategy where the local process is terminated before starting it on the remote node.

Cold swap is a safe strategy if we want to ensure that the child process is not running on multiple nodes at the same time.

This is the default strategy for process migration.

State Handover

When handover: true is set, ColdSwap will:

  1. Query state from local processes before termination
  2. Store states in ETS with TTL
  3. Terminate local processes
  4. Send start requests to new nodes
  5. When new processes are registered, deliver stored state via hook

To use state handover, your GenServer must use ProcessHub.Strategy.Migration.ColdSwap:

defmodule MyServer do
  use GenServer
  use ProcessHub.Strategy.Migration.ColdSwap

  # 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 cold swap migration struct.

Functions

Options

Callback executed on the originating node to deliver stored handover states.

Post-action callback executed on the target node after children are started.

Types

@type t() :: %ProcessHub.Strategy.Migration.ColdSwap{
  handover: boolean(),
  state_query_timeout: pos_integer(),
  state_ttl: pos_integer()
}

The cold 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 dying 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

deliver_handover_states(hub, target_node, cid_pids)

View Source
@spec deliver_handover_states(ProcessHub.Hub.t(), node(), [
  {ProcessHub.child_id(), pid()}
]) :: :ok

Callback executed on the originating node to deliver stored handover states.

Called via generic :post_action_callback message from the target node.

Link to this function

handle_post_action_state_fetch(hub, results, originating_node, child_ids)

View Source

Post-action callback executed on the target node after children are started.

Sends a generic callback message to the originating node to deliver stored handover states to the newly started processes.