Route one aggregate id's commands to one node on a cluster; failover applies no command twice #8

Closed
opened 2026-12-02 06:00:12 +00:00 by venus · 2 comments
Member

Purpose (feature): on a cluster, all commands for one aggregate id run on one node; when that node is lost the id moves to a live node and no command is applied twice or lost. One node behaves exactly as today.

Refs martha-ai/mcl-martha#1 (A3 package 5 of the clustered mcl-martha path).

Raf, 2026-12-02: "Run the whole path (Recommended)" and "Three for failover (Recommended)".

Today

  • evoq_aggregate_registry uses pg (scope evoq_pg), but lookup/1 takes only a pid on this node (find_alive_local/1). A pid on another node is skipped.
  • So on a cluster each node starts its own aggregate for the same id. Two nodes run the same stream at once. Only the store's expected-version append keeps them from both writing on one version, and the loser burns retries.
  • When a remote aggregate's node dies mid-call, evoq_aggregate:called/2 answers {error, noconnection} (or the reason it died with). The router reports that as a failure and does not retry it.
  • The idempotency cache (evoq_idempotency) is ETS on the entry node. It cannot tell a second node that the dead node already appended a command.

Design

  1. Live evoq nodes. The registry joins a pg group evoq_nodes in evoq_pg. pg drops a member when its node goes down, so the group is the set of live evoq nodes.
  2. Owner of an id. Rendezvous hashing over the sorted live nodes: the owner is the node with the highest erlang:phash2({Node, AggregateId}). Losing a node moves only that node's ids. Adding one moves only the ids it now wins. One node: the owner is always node(), so the code path is today's (no remote call).
  3. Routing. get_or_start/3: owner is this node, so today's path. Otherwise it asks the owner's registry, {evoq_aggregate_registry, Owner}, and gets the owner's pid. The router calls that pid as it does now (gen_server calls cross nodes). A forwarded start is served on the receiving node without asking again, so a request makes at most one hop, even while two nodes see membership differently.
  4. Node loss = retry, re-routed. A call whose aggregate node went away (noconnection, nodedown, a {nodedown, _} exit) is treated like not_running today: retried within retry_attempts, same command_id, through the registry, which now picks a live owner.
  5. Not applied twice. The dead owner may have appended the command before it died. The aggregate therefore remembers the causation_id (= the command's command_id, already written in every event's metadata) of the last N events of its stream. N is one defined default with an app-env override. It reads them at load with one backward read, so a snapshot does not hide them, and adds each append's id. A command whose command_id is in that set is not executed again. It answers {ok, Version, []}, the sanctioned "already done" no-op. If the dead owner's append commits only after the new owner has loaded, the new owner's append meets wrong_expected_version. It reloads, so it now sees the id, and the retry is answered as already done.
  6. Two runners during a view change. If two nodes briefly both think they own an id, the store's conditional append is still the arbiter: one wins, the other gets wrong_expected_version and re-routes. This is the correctness floor today as well. Routing removes the steady-state double runner; it does not replace the store's check.

Threat model (outside attacker only; insider and crew findings are notes)

  • Boundary. Distribution is inside the cluster cookie. An outside party with no cookie cannot reach evoq_aggregate_registry or an aggregate pid. Nothing new listens and nothing new leaves the cluster.
  • Attacker-controlled input. Only the aggregate id (and payload), arriving through the application's own API. The id is hashed, never turned into an atom or a node name. The owner is chosen from pg members only, never from the command, so an id cannot route a command to a node that is not in the cluster.
  • Steering load. phash2 is unkeyed, so an attacker who picks ids could put them all on one node. That is no worse than today, where one node runs them all. Note only.
  • Owner harms this must prevent (the Done-when below proves each):
    • H1, applied twice: a command appended by a node that died, then re-run on the new owner.
    • H2, lost: a command in flight when its owner died answers a failure, or never runs, although live nodes and the store could take it.
    • H3, two runners in steady state: one id executing on two nodes at once while membership is stable.
    • H4, a standalone regression: a one-node user sees any change in results, versions or calls made.
  • Out of scope here (notes):
    • The decision-actor registry (evoq_decision_registry) has the same local-only lookup. It is not in this package.
    • A client retry that carries only idempotency_key with a fresh command_id through another entry node still misses that node's ETS cache, as it would hit a cold cache after a restart today. The durable window covers the same command_id.
    • Process managers already run per node (evoq#10).

Done-when

  • On a 3-node peer cluster, commands for one id dispatched from all three nodes run on exactly one node (the owner), and the stream's versions are 0..n-1 with no conflicts in steady state (H3).
  • Stopping the owner node mid-stream: dispatch continues from the other nodes; every command is applied exactly once (each command_id appears once as a causation_id in the stream, none missing) (H1, H2).
  • The "owner appended, then died before replying" case: the retried command is answered as already done and appends nothing (H1).
  • A 2-node case: the owner choice is the same on both nodes, and a node's loss moves only its ids.
  • One node: the existing suites pass unchanged, and a test shows no remote call is made (H4).
  • A test kills each mutant on the routing choice: the owner always node(), the remote branch skipped, node loss not retried, the causation check removed.
  • Whole test suite green on OTP 28; tests start only local peer nodes and reach nothing outside.
Purpose (feature): on a cluster, all commands for one aggregate id run on one node; when that node is lost the id moves to a live node and no command is applied twice or lost. One node behaves exactly as today. Refs martha-ai/mcl-martha#1 (A3 package 5 of the clustered mcl-martha path). Raf, 2026-12-02: "Run the whole path (Recommended)" and "Three for failover (Recommended)". ## Today - `evoq_aggregate_registry` uses pg (scope `evoq_pg`), but `lookup/1` takes only a pid on this node (`find_alive_local/1`). A pid on another node is skipped. - So on a cluster each node starts its own aggregate for the same id. Two nodes run the same stream at once. Only the store's expected-version append keeps them from both writing on one version, and the loser burns retries. - When a remote aggregate's node dies mid-call, `evoq_aggregate:called/2` answers `{error, noconnection}` (or the reason it died with). The router reports that as a failure and does not retry it. - The idempotency cache (`evoq_idempotency`) is ETS on the entry node. It cannot tell a second node that the dead node already appended a command. ## Design 1. **Live evoq nodes.** The registry joins a pg group `evoq_nodes` in `evoq_pg`. pg drops a member when its node goes down, so the group is the set of live evoq nodes. 2. **Owner of an id.** Rendezvous hashing over the sorted live nodes: the owner is the node with the highest `erlang:phash2({Node, AggregateId})`. Losing a node moves only that node's ids. Adding one moves only the ids it now wins. One node: the owner is always `node()`, so the code path is today's (no remote call). 3. **Routing.** `get_or_start/3`: owner is this node, so today's path. Otherwise it asks the owner's registry, `{evoq_aggregate_registry, Owner}`, and gets the owner's pid. The router calls that pid as it does now (gen_server calls cross nodes). A forwarded start is served on the receiving node without asking again, so a request makes at most one hop, even while two nodes see membership differently. 4. **Node loss = retry, re-routed.** A call whose aggregate node went away (`noconnection`, `nodedown`, a `{nodedown, _}` exit) is treated like `not_running` today: retried within `retry_attempts`, same `command_id`, through the registry, which now picks a live owner. 5. **Not applied twice.** The dead owner may have appended the command before it died. The aggregate therefore remembers the `causation_id` (= the command's `command_id`, already written in every event's metadata) of the last N events of its stream. N is one defined default with an app-env override. It reads them at load with one backward read, so a snapshot does not hide them, and adds each append's id. A command whose `command_id` is in that set is not executed again. It answers `{ok, Version, []}`, the sanctioned "already done" no-op. If the dead owner's append commits only after the new owner has loaded, the new owner's append meets `wrong_expected_version`. It reloads, so it now sees the id, and the retry is answered as already done. 6. **Two runners during a view change.** If two nodes briefly both think they own an id, the store's conditional append is still the arbiter: one wins, the other gets `wrong_expected_version` and re-routes. This is the correctness floor today as well. Routing removes the steady-state double runner; it does not replace the store's check. ## Threat model (outside attacker only; insider and crew findings are notes) - **Boundary.** Distribution is inside the cluster cookie. An outside party with no cookie cannot reach `evoq_aggregate_registry` or an aggregate pid. Nothing new listens and nothing new leaves the cluster. - **Attacker-controlled input.** Only the aggregate id (and payload), arriving through the application's own API. The id is hashed, never turned into an atom or a node name. The owner is chosen from pg members only, never from the command, so an id cannot route a command to a node that is not in the cluster. - **Steering load.** `phash2` is unkeyed, so an attacker who picks ids could put them all on one node. That is no worse than today, where one node runs them all. Note only. - **Owner harms this must prevent (the Done-when below proves each):** - H1, applied twice: a command appended by a node that died, then re-run on the new owner. - H2, lost: a command in flight when its owner died answers a failure, or never runs, although live nodes and the store could take it. - H3, two runners in steady state: one id executing on two nodes at once while membership is stable. - H4, a standalone regression: a one-node user sees any change in results, versions or calls made. - **Out of scope here (notes):** - The decision-actor registry (`evoq_decision_registry`) has the same local-only lookup. It is not in this package. - A client retry that carries only `idempotency_key` with a fresh `command_id` through another entry node still misses that node's ETS cache, as it would hit a cold cache after a restart today. The durable window covers the same `command_id`. - Process managers already run per node (evoq#10). ## Done-when - [ ] On a 3-node peer cluster, commands for one id dispatched from all three nodes run on exactly one node (the owner), and the stream's versions are 0..n-1 with no conflicts in steady state (H3). - [ ] Stopping the owner node mid-stream: dispatch continues from the other nodes; every command is applied exactly once (each `command_id` appears once as a `causation_id` in the stream, none missing) (H1, H2). - [ ] The "owner appended, then died before replying" case: the retried command is answered as already done and appends nothing (H1). - [ ] A 2-node case: the owner choice is the same on both nodes, and a node's loss moves only its ids. - [ ] One node: the existing suites pass unchanged, and a test shows no remote call is made (H4). - [ ] A test kills each mutant on the routing choice: the owner always `node()`, the remote branch skipped, node loss not retried, the causation check removed. - [ ] Whole test suite green on OTP 28; tests start only local peer nodes and reach nothing outside.
Author
Member

Design change while building (appended; the design above stays as first written):

Item 5, "not applied twice", is narrower than written. The aggregate does not remember command ids on every load, and it does not answer every repeated command_id as done. evoq's own evoq_idempotency_scope_tests showed why: two different commands may share a command_id (evoq#51's deposit/withdraw case), and the second must run.

What ships instead:

  • The router marks a dispatch owner_lost once its owner node went away.
  • Only such a retry reads the stream's last events (owner_lost_lookback, default 100) and answers a command that the stream names as a causation_id with {ok, Version, [], State}.
  • Every other command, standalone or clustered, runs as before and makes no extra read (H4).
  • If the lost node's append lands after the new owner loaded, the new owner's append meets a version conflict, it reloads, and the next attempt, still marked, finds the command.

PR: #9.

Design change while building (appended; the design above stays as first written): Item 5, "not applied twice", is narrower than written. The aggregate does **not** remember command ids on every load, and it does not answer every repeated `command_id` as done. evoq's own `evoq_idempotency_scope_tests` showed why: two different commands may share a `command_id` (evoq#51's deposit/withdraw case), and the second must run. What ships instead: - The router marks a dispatch `owner_lost` once its owner node went away. - Only such a retry reads the stream's last events (`owner_lost_lookback`, default 100) and answers a command that the stream names as a `causation_id` with `{ok, Version, [], State}`. - Every other command, standalone or clustered, runs as before and makes no extra read (H4). - If the lost node's append lands after the new owner loaded, the new owner's append meets a version conflict, it reloads, and the next attempt, still marked, finds the command. PR: #9.
Author
Member

Landed on main at 9ee5943 (PR #9, Jupiter PASS r=1, runs 6565-6568 green). Done-when:

  • 3-node peer cluster, one id, commands from all three nodes: one runner (the owner), zero store conflicts, versions 0..n-1 (one_id_runs_on_one_node_test, three_nodes) (H3).
  • Owner halted and owner stopped mid-stream: every command applied exactly once, none missing (a_halted_owner_…, a_stopped_owner_…) (H1, H2).
  • Owner appended then died before replying: the retry is answered {ok, 0, []} and appends nothing (an_owner_that_appended_and_died_…) (H1).
  • 2 nodes: same owner on both (every_node_names_the_same_owner_test, two_nodes). Losing a node moves only its ids (losing_a_node_moves_only_its_ids_test).
  • One node: every existing suite passes unchanged. standalone_owner_is_this_node_test_ shows this node owns every id, so no remote call is made, and a_command_that_is_not_an_owner_loss_retry_runs_as_always shows no extra read (H4).
  • Mutants killed by the peer suite: owner always node(), remote branch skipped, node loss not retried, owner_lost not marked, causation check removed.
  • Whole suite green on OTP 28 (eunit 376, ct 71, proper, lint, dialyzer ratchet). Tests start only local peers.

Jupiter's two notes (look-back window, shared command_id) are in #10.

Landed on main at 9ee5943 (PR #9, Jupiter PASS r=1, runs 6565-6568 green). Done-when: - [x] 3-node peer cluster, one id, commands from all three nodes: one runner (the owner), zero store conflicts, versions 0..n-1 (`one_id_runs_on_one_node_test`, three_nodes) (H3). - [x] Owner halted and owner stopped mid-stream: every command applied exactly once, none missing (`a_halted_owner_…`, `a_stopped_owner_…`) (H1, H2). - [x] Owner appended then died before replying: the retry is answered `{ok, 0, []}` and appends nothing (`an_owner_that_appended_and_died_…`) (H1). - [x] 2 nodes: same owner on both (`every_node_names_the_same_owner_test`, two_nodes). Losing a node moves only its ids (`losing_a_node_moves_only_its_ids_test`). - [x] One node: every existing suite passes unchanged. `standalone_owner_is_this_node_test_` shows this node owns every id, so no remote call is made, and `a_command_that_is_not_an_owner_loss_retry_runs_as_always` shows no extra read (H4). - [x] Mutants killed by the peer suite: owner always `node()`, remote branch skipped, node loss not retried, `owner_lost` not marked, causation check removed. - [x] Whole suite green on OTP 28 (eunit 376, ct 71, proper, lint, dialyzer ratchet). Tests start only local peers. Jupiter's two notes (look-back window, shared `command_id`) are in #10.
venus closed this issue 2026-12-02 06:56:27 +00:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
reckon-db-org/evoq#8
No description provided.