ExMCP.Transport.Beam.PartitionDetector (ex_mcp v0.9.2)

View Source

Network partition detection and healing for BEAM transport clustering.

Monitors cluster connectivity and detects network partitions (split-brain scenarios). When partitions are detected, applies configured merge strategies to reconcile conflicting service registrations.

Partition Detection Methods

  • Node Monitoring: Monitor BEAM node connections
  • Heartbeat: Regular heartbeat messages between cluster members
  • Service Registry Comparison: Compare service states across nodes
  • Clock Synchronization: Detect time drift that may indicate partitions

Merge Strategies

  • :last_writer_wins - Keep services with most recent timestamps
  • :first_writer_wins - Keep services with earliest timestamps
  • :manual - Require manual intervention to resolve conflicts

Example Usage

{:ok, detector} = PartitionDetector.start_link(%{
  cluster: cluster_pid,
  detection_interval: 10000,
  merge_strategy: :last_writer_wins,
  partition_threshold: 3
})

Summary

Functions

Returns a specification to start this module under a supervisor.

Manually triggers partition detection.

Gets current partition status.

Gets partition detector statistics.

Manually heals a detected partition.

Simulates a network partition for testing purposes.

Starts the partition detector with the given configuration.

Stops the partition detector.

Updates the merge strategy.

Types

config()

@type config() :: %{
  cluster: GenServer.server(),
  registry: GenServer.server(),
  detection_interval: non_neg_integer(),
  merge_strategy: merge_strategy(),
  partition_threshold: non_neg_integer(),
  heartbeat_timeout: non_neg_integer()
}

merge_strategy()

@type merge_strategy() :: :last_writer_wins | :first_writer_wins | :manual

partition_state()

@type partition_state() :: :normal | :partitioned | :healing

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

detect_partitions(detector)

@spec detect_partitions(GenServer.server()) ::
  {:ok, partition_state()} | {:error, term()}

Manually triggers partition detection.

get_partition_status(detector)

@spec get_partition_status(GenServer.server()) :: {:ok, partition_state()}

Gets current partition status.

get_stats(detector)

@spec get_stats(GenServer.server()) :: {:ok, map()}

Gets partition detector statistics.

heal_partition(detector)

@spec heal_partition(GenServer.server()) :: :ok | {:error, term()}

Manually heals a detected partition.

simulate_partition(detector, partition_a, partition_b)

@spec simulate_partition(GenServer.server(), [atom()], [atom()]) :: :ok

Simulates a network partition for testing purposes.

start_link(config)

@spec start_link(config()) :: GenServer.on_start()

Starts the partition detector with the given configuration.

stop(detector)

@spec stop(GenServer.server()) :: :ok

Stops the partition detector.

update_merge_strategy(detector, strategy)

@spec update_merge_strategy(GenServer.server(), merge_strategy()) :: :ok

Updates the merge strategy.