MaeBPF.Migration (maebpf v0.1.0)

Copy Markdown View Source

Transactional live hot-swap map migration planner and executor.

Plans map transitions between old and new MaeBPF.Schema.MapSpec sets, failing closed before touching any kernel object whenever a schema change cannot be migrated safely, and executes hot-swaps with:

  • Pre-load FD reuse (bpf_map__reuse_fd) for identical maps
  • Kernel load verification before any live mutation
  • Pre-swap entry copying with schema decoding, transformation, and encoding for normal and per-CPU maps
  • Atomic program replacement (BPF_LINK_UPDATE)
  • Optional post-swap delta reconciliation (:merge_counters or :overwrite_missing) to preserve updates written during the swap window

Summary

Functions

Executes a validated %MaeBPF.Migration.Plan{} using injectable callbacks.

Computes a transactional migration plan from old_specs to new_specs.

Types

action()

@type action() ::
  {:reuse_fd, String.t()}
  | {:create_new, String.t()}
  | {:drop_old, String.t()}
  | {:reopen_ringbuf, String.t()}
  | {:migrate, String.t(), :identity | transform_fn()}

key_transform_fn()

@type key_transform_fn() :: (term() -> {:ok, term()} | :skip | {:error, term()})

reconcile_mode()

@type reconcile_mode() :: :none | :merge_counters | :overwrite_missing

transform_fn()

@type transform_fn() :: (term(), term() -> {:ok, term()} | :skip | {:error, term()})

Functions

execute(plan, callbacks \\ %{}, opts \\ [])

@spec execute(MaeBPF.Migration.Plan.t(), map(), keyword()) ::
  {:ok, map()} | {:error, term()}

Executes a validated %MaeBPF.Migration.Plan{} using injectable callbacks.

Callbacks map (missing entries default to real MaeBPF.Map, MaeBPF.Link, and internal NIF runtime adapters):

  • :reuse_fd - fn(new_obj, map_name, old_obj) -> :ok | {:error, term()}

  • :load_new_obj - fn(new_obj) -> :ok | {:error, term()}

  • :dump_entries - fn(obj, map_name) -> {:ok, [{key_bin, val_bin_or_percpu_list}]} | {:error, term()}

  • :put_entry - fn(obj, map_name, key_bin, val_bin_or_percpu_list) -> :ok | {:error, term()}

  • :lookup_entry - optional fn(obj, map_name, key_bin) -> {:ok, val_bin_or_percpu_list} | :not_found | {:error, term()}

  • :swap_links - fn(new_obj) -> :ok | {:error, term()}

  • :reopen_ringbuf - optional fn(new_obj, map_name) -> :ok | {:error, term()}

Options:

  • :old_obj - source eBPF object handle (also accepted in callbacks[:old_obj])
  • :new_obj - candidate eBPF object handle (also accepted in callbacks[:new_obj])
  • :link / :program_name - used to build a default :swap_links callback
  • :reconcile_mode - :none | :merge_counters | :overwrite_missing or %{map_name => mode} overriding plan defaults

  • :counter_fields - optional list of struct field atoms (or %{map_name => [atom()]}) restricting which numeric fields accumulate deltas under :merge_counters
  • :batch_size - batch size for default MaeBPF.Map.lookup_batch/4 adapter (default 64)

plan(old_specs, new_specs, opts \\ [])

@spec plan(
  [MaeBPF.Schema.MapSpec.t()]
  | %{required(String.t()) => MaeBPF.Schema.MapSpec.t()},
  [MaeBPF.Schema.MapSpec.t()]
  | %{required(String.t()) => MaeBPF.Schema.MapSpec.t()},
  keyword()
) :: {:ok, MaeBPF.Migration.Plan.t()} | {:error, term()}

Computes a transactional migration plan from old_specs to new_specs.

Both old_specs and new_specs may be lists of %MaeBPF.Schema.MapSpec{} or maps of %{map_name => %MaeBPF.Schema.MapSpec{}}.

Options:

  • :defaults - explicit defaults for newly added fields in additive struct migrations, either scoped per map (%{"stats" => %{drops: 0}}) or flat (%{drops: 0})
  • :migrate_fns - map of %{map_name => fn(key, old_val) -> {:ok, new_val} | :skip end}

  • :migrate_key_fns (or :migrate_key_fn) - map of %{map_name => fn(old_key) -> {:ok, new_key} | :skip end} (or a single 1-arity function)
  • :reconcile_mode - default post-swap reconciliation mode (:none | :merge_counters | :overwrite_missing or %{map_name => mode})

Fails closed with {:error, {:map_schema_incompatible, name, diff}} on map type changes, key schema changes without a key migration function, removed or type-changed value fields without an explicit migrate function, or added struct fields lacking explicit defaults.