barrel_p2p (barrel_p2p v0.1.0)

View Source

Summary

Functions

Cancel a pending reminder cluster-wide. Stability: beta.

Stop a map on this node (node-local; not a cluster-wide erase). Stability: beta.

This node's fencing token for Name, valid only while it leads. Stability: beta.

Whether this node currently holds leadership for Name. Stability: beta.

Whether this node currently owns Key. Stability: beta.

Campaign for leadership of the singleton Name. The calling process becomes a candidate and is monitored; if it dies it stops being a candidate. The return value is the caller's initial role: - {ok, {leader, Fence}} if it holds leadership now - {ok, follower} otherwise On every later transition the caller is sent one of: - {barrel_p2p_leader, Name, {elected, Fence}} - {barrel_p2p_leader, Name, revoked}

As lead/1 with options. #{priority => integer()} (default 0) biases the election: higher priority wins, ties fall back to the lowest node atom. Stability: beta.

The current leader for Name cluster-wide, if any. Stability: beta.

The current live member set (sorted). Stability: beta.

Trigger RFC 9000 §9 path migration on the QUIC connection backing the dist channel to Node. The connection rebinds to a new local 4-tuple via PATH_CHALLENGE/PATH_RESPONSE; keys, streams, and any open circuits ride through transparently. Useful when the local network changes (NIC/IP swap, tethering, multi-link policy).

Start a replicated map named Name on this node. A map is node-local: to be cluster-wide it must run on every participating node (declare it in the replicated_maps env, or call this on each node). Idempotent. See barrel_p2p_map for the full API and caveats. Stability: beta.

The top-N distinct owner nodes for Key (best first), for replicated placement. Stability: beta.

The ring partition Key falls in (0..ring_size-1). Use it to map keys to partitions when reacting to ownership events. Stability: beta.

The node that should own Key cluster-wide. Agreement is eventual: under churn nodes can briefly disagree until the member set converges. Stability: beta.

Set a reminder for Key to fire at absolute wall-clock FireAtMs (erlang:system_time(millisecond) scale), delivering Payload to subscribers on the owning node. Re-setting a Key replaces it. Fires exactly once in steady state; best-effort under churn or a crash at the fire instant. Stability: beta.

Like remind/3 but DelayMs from now, converted to an absolute target so all nodes agree. Stability: beta.

Stop campaigning for Name and yield leadership if held. No revoked message is sent (the caller asked to step down). Stability: beta.

Subscribe the caller to a map's change events: {barrel_p2p_map, Name, {put, Key, Value} | {remove, Key}}. Stability: beta.

Subscribe the caller to reminder deliveries. Receives {barrel_p2p_reminder, Key, Payload, Fence} on the node that owns the key when it fires. Subscribe on every node where the handler may run. Stability: beta.

Subscribe the caller to ownership events. Receives {barrel_p2p_shard, {acquired, Partition}} when this node gains a partition and {barrel_p2p_shard, {released, Partition}} when it loses one. Stability: beta.

Functions

active_view()

-spec active_view() -> [node()].

cancel_reminder(Key)

-spec cancel_reminder(term()) -> ok.

Cancel a pending reminder cluster-wide. Stability: beta.

delete_map(Name)

-spec delete_map(atom()) -> ok.

Stop a map on this node (node-local; not a cluster-wide erase). Stability: beta.

fence(Name)

-spec fence(term()) -> {ok, non_neg_integer()} | {error, not_leader}.

This node's fencing token for Name, valid only while it leads. Stability: beta.

get_proxy(Name)

-spec get_proxy(atom() | binary()) -> {ok, pid()} | not_found.

global_register(Name)

-spec global_register(atom() | binary()) -> {ok, pid()} | {error, term()}.

is_leader(Name)

-spec is_leader(term()) -> boolean().

Whether this node currently holds leadership for Name. Stability: beta.

is_owner(Key)

-spec is_owner(term()) -> boolean().

Whether this node currently owns Key. Stability: beta.

join(ContactNode)

-spec join(node()) -> ok | {error, term()}.

lead(Name)

-spec lead(term()) -> {ok, {leader, non_neg_integer()}} | {ok, follower} | {error, term()}.

Campaign for leadership of the singleton Name. The calling process becomes a candidate and is monitored; if it dies it stops being a candidate. The return value is the caller's initial role: - {ok, {leader, Fence}} if it holds leadership now - {ok, follower} otherwise On every later transition the caller is sent one of: - {barrel_p2p_leader, Name, {elected, Fence}} - {barrel_p2p_leader, Name, revoked}

Fence is a non_neg_integer() fencing token, strictly increasing across leadership terms within a connected partition. Stamp it on writes to a shared resource and have the resource reject any operation whose token is not strictly greater than the highest it has accepted; that is what makes "exactly one" safe when an old leader is paused or partitioned.

Partition caveat: under a network partition each side may elect its own leader, and token monotonicity is only guaranteed within a connected component. Safety then rests on the resource's fence check.

Stability: beta. The message and return shapes may change across a 0.x minor bump.

lead(Name, Opts)

-spec lead(term(), map()) -> {ok, {leader, non_neg_integer()}} | {ok, follower} | {error, term()}.

As lead/1 with options. #{priority => integer()} (default 0) biases the election: higher priority wins, ties fall back to the lowest node atom. Stability: beta.

leader(Name)

-spec leader(term()) -> {ok, node(), pid()} | {error, no_leader}.

The current leader for Name cluster-wide, if any. Stability: beta.

leave()

-spec leave() -> ok.

list_services()

-spec list_services() -> [atom() | binary()].

lookup(Name)

-spec lookup(atom() | binary()) -> {ok, [tuple()]} | {error, not_found}.

lookup_local(Name)

-spec lookup_local(atom() | binary()) -> {ok, pid()} | {error, not_found}.

map_get(Name, Key)

-spec map_get(atom(), term()) -> {ok, term()} | not_found.

map_keys(Name)

-spec map_keys(atom()) -> [term()].

map_put(Name, Key, Value)

-spec map_put(atom(), term(), term()) -> ok | {error, invalid_value | no_such_map}.

map_remove(Name, Key)

-spec map_remove(atom(), term()) -> ok | {error, no_such_map}.

map_to_list(Name)

-spec map_to_list(atom()) -> [{term(), term()}].

members()

-spec members() -> [node()].

The current live member set (sorted). Stability: beta.

migrate_peer(Node)

-spec migrate_peer(node()) -> ok | {error, term()}.

Trigger RFC 9000 §9 path migration on the QUIC connection backing the dist channel to Node. The connection rebinds to a new local 4-tuple via PATH_CHALLENGE/PATH_RESPONSE; keys, streams, and any open circuits ride through transparently. Useful when the local network changes (NIC/IP swap, tethering, multi-link policy).

Returns ok on successful path validation. Common errors: - {error, not_connected} — no current dist channel to Node - {error, no_conn} — controller alive but underlying conn gone - {error, peer_disable_migration} — peer set the transport-param flag forbidding migration; treat as terminal for this connection - {error, timeout} — path validation didn't complete in time

Stability: beta. The opts map may grow keys; existing keys stay.

migrate_peer(Node, Opts)

-spec migrate_peer(node(), #{timeout => pos_integer()}) -> ok | {error, term()}.

new_map(Name)

-spec new_map(atom()) -> {ok, pid()} | {error, term()}.

Start a replicated map named Name on this node. A map is node-local: to be cluster-wide it must run on every participating node (declare it in the replicated_maps env, or call this on each node). Idempotent. See barrel_p2p_map for the full API and caveats. Stability: beta.

new_map(Name, Opts)

-spec new_map(atom(), barrel_p2p_map:opts()) -> {ok, pid()} | {error, term()}.

owners(Key, N)

-spec owners(term(), pos_integer()) -> [node()].

The top-N distinct owner nodes for Key (best first), for replicated placement. Stability: beta.

partition(Key)

-spec partition(term()) -> non_neg_integer().

The ring partition Key falls in (0..ring_size-1). Use it to map keys to partitions when reacting to ownership events. Stability: beta.

passive_view()

-spec passive_view() -> [node()].

place(Key)

-spec place(term()) -> node() | undefined.

The node that should own Key cluster-wide. Agreement is eventual: under churn nodes can briefly disagree until the member set converges. Stability: beta.

register_name(Name, Pid)

-spec register_name(Name :: term(), Pid :: pid()) -> yes | no.

register_service(Name)

-spec register_service(atom() | binary()) -> ok | {error, term()}.

register_service(Name, Meta)

-spec register_service(atom() | binary(), map()) -> ok | {error, term()}.

register_service(Name, Pid, Meta)

-spec register_service(atom() | binary(), pid(), map()) -> ok | {error, term()}.

remind(Key, FireAtMs, Payload)

-spec remind(term(), integer(), term()) -> ok.

Set a reminder for Key to fire at absolute wall-clock FireAtMs (erlang:system_time(millisecond) scale), delivering Payload to subscribers on the owning node. Re-setting a Key replaces it. Fires exactly once in steady state; best-effort under churn or a crash at the fire instant. Stability: beta.

remind_after(Key, DelayMs, Payload)

-spec remind_after(term(), non_neg_integer(), term()) -> ok.

Like remind/3 but DelayMs from now, converted to an absolute target so all nodes agree. Stability: beta.

resign(Name)

-spec resign(term()) -> ok.

Stop campaigning for Name and yield leadership if held. No revoked message is sent (the caller asked to step down). Stability: beta.

send(Name, Msg)

-spec send(Name :: term(), Msg :: term()) -> pid().

start_service_holder(ServiceName)

-spec start_service_holder(atom() | binary()) -> {ok, pid()} | {error, term()}.

stop_service_holder(Pid)

-spec stop_service_holder(pid()) -> ok.

subscribe()

-spec subscribe() -> ok.

subscribe(Pid)

-spec subscribe(pid()) -> ok.

subscribe_map(Name)

-spec subscribe_map(atom()) -> ok | {error, no_such_map}.

Subscribe the caller to a map's change events: {barrel_p2p_map, Name, {put, Key, Value} | {remove, Key}}. Stability: beta.

subscribe_map(Name, Pid)

-spec subscribe_map(atom(), pid()) -> ok | {error, no_such_map}.

subscribe_reminders()

-spec subscribe_reminders() -> ok.

Subscribe the caller to reminder deliveries. Receives {barrel_p2p_reminder, Key, Payload, Fence} on the node that owns the key when it fires. Subscribe on every node where the handler may run. Stability: beta.

subscribe_reminders(Pid)

-spec subscribe_reminders(pid()) -> ok.

subscribe_services()

-spec subscribe_services() -> ok.

subscribe_services(Pid)

-spec subscribe_services(pid()) -> ok.

subscribe_shard()

-spec subscribe_shard() -> ok.

Subscribe the caller to ownership events. Receives {barrel_p2p_shard, {acquired, Partition}} when this node gains a partition and {barrel_p2p_shard, {released, Partition}} when it loses one. Stability: beta.

subscribe_shard(Pid)

-spec subscribe_shard(pid()) -> ok.

unregister_name(Name)

-spec unregister_name(Name :: term()) -> ok.

unregister_service(Name)

-spec unregister_service(atom() | binary()) -> ok.

unsubscribe(Pid)

-spec unsubscribe(pid()) -> ok.

unsubscribe_map(Name)

-spec unsubscribe_map(atom()) -> ok | {error, no_such_map}.

unsubscribe_map(Name, Pid)

-spec unsubscribe_map(atom(), pid()) -> ok | {error, no_such_map}.

unsubscribe_reminders(Pid)

-spec unsubscribe_reminders(pid()) -> ok.

unsubscribe_services(Pid)

-spec unsubscribe_services(pid()) -> ok.

whereis_name(Name)

-spec whereis_name(Name :: term()) -> pid() | undefined.

whereis_service(Name)

-spec whereis_service(atom() | binary()) -> {ok, pid()} | {ok, node(), pid()} | {error, not_found}.

whereis_service(Name, Opts)

-spec whereis_service(atom() | binary(), map()) ->
                         {ok, pid()} | {ok, node(), pid()} | {error, not_found}.